mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-01 20:56:02 +09:00
Migration 64 converts the existing Postgres releases. It first applies the CredentialTemplate CRD baked into its image, owned by the cozystack-controller release so that release's upgrade adopts it: on a live upgrade the tenant releases switched to the converted chart about 17 minutes before cozystack-controller upgraded, and every render in between failed on the unknown kind. It then runs credentials-migrate seed, which writes the account Secrets from <release>-credentials so clients keep logging in at every point of the upgrade. A release that grants createRole is left out and keeps its previous chart. The finish half runs from a Job the migration creates, not from the platform chart: a chart-rendered Job has a fixed name and an image that changes every release, so the next platform upgrade cannot patch its immutable pod template, and helm-controller would wait on a Job that runs up to 30 minutes and exits non-zero while any release is left out. The Job runs the image the hook runs as, read from its own pod, under a least-privilege ClusterRole rather than cluster-admin, and its ServiceAccount is owned by the Job so the garbage collector removes it. The identity carries the migration number like the Job, so a later conversion migration cannot delete the ServiceAccount a still-running earlier Job uses. The CredentialsMigrationIncomplete alert keys on the incomplete label of the state ConfigMap through kube-state-metrics, which allowlists only that one ConfigMap label, so a release whose plaintext was never retired stays visible after the Job's TTL. The bats suite pins the CRD apply, seed, image read, Job apply, ownership and stamp steps as fail-closed; its fake kubectl counts the Job's name inside the Job document only. Co-authored-by: IvanHunters <xorokhotnikov@gmail.com> Co-authored-by: Andrei Kvapil <kvapss@gmail.com> Assisted-by: LLM Signed-off-by: Timofei Larkin <lllamnyp@gmail.com> Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
128 lines
4.7 KiB
Bash
Executable File
128 lines
4.7 KiB
Bash
Executable File
#!/bin/sh
|
|
# Fake kubectl for hack/migration-64-postgres-credentials.bats, modelled on
|
|
# hack/testdata/migration-58-http-cache/kubectl. It serves only the calls
|
|
# migration 64 and lib/cozystack-version.sh make, and logs each one to
|
|
# $FAKE_CMDLOG.
|
|
#
|
|
# Migration 64 reads its own pod's image, applies the finish Job (with its
|
|
# ServiceAccount and ClusterRoleBinding), and stamps the version. The Job
|
|
# manifest and the version stamp both arrive as `apply --filename -`, so this
|
|
# fake reads stdin and branches on the manifest's kind.
|
|
#
|
|
# FAKE_CMDLOG file to append a one-line record of each call to
|
|
# FAKE_IMAGE image the `get pod` call returns (default a test ref)
|
|
# FAKE_IMAGE_EMPTY non-empty => `get pod` returns an empty image
|
|
# FAKE_GETPOD_FAIL non-empty => `get pod` exits 1 with this stderr
|
|
# FAKE_APPLY_FAIL non-empty => applying the Job exits 1 with this stderr
|
|
set -u
|
|
|
|
log() { [ -n "${FAKE_CMDLOG:-}" ] && printf '%s\n' "$*" >> "$FAKE_CMDLOG"; }
|
|
log "KUBECTL $*"
|
|
|
|
args="$*"
|
|
IMAGE="${FAKE_IMAGE:-registry.example.org/cozystack/cozystack:v1.7.0}"
|
|
|
|
case "$args" in
|
|
*"apply --server-side --force-conflicts --filename "*credentialtemplates*)
|
|
log "CRD apply"
|
|
[ -n "${FAKE_CRD_FAIL-}" ] && exit 1
|
|
exit 0
|
|
;;
|
|
*"label crd credentialtemplates.cozystack.io app.kubernetes.io/managed-by=Helm"*)
|
|
log "CRD label"
|
|
exit 0
|
|
;;
|
|
*"annotate crd credentialtemplates.cozystack.io"*"meta.helm.sh/release-name=cozystack-controller"*)
|
|
log "CRD annotate"
|
|
exit 0
|
|
;;
|
|
*"wait --for=condition=established crd/credentialtemplates.cozystack.io"*)
|
|
log "CRD established"
|
|
exit 0
|
|
;;
|
|
*"get pod"*jsonpath*)
|
|
if [ -n "${FAKE_GETPOD_FAIL:-}" ]; then
|
|
printf '%s\n' "$FAKE_GETPOD_FAIL" >&2
|
|
exit 1
|
|
fi
|
|
[ -n "${FAKE_IMAGE_EMPTY:-}" ] || printf '%s' "$IMAGE"
|
|
exit 0
|
|
;;
|
|
|
|
*"delete job cozystack-credentials-migration-64"*)
|
|
# The hook deletes a Job left by an earlier run before recreating it.
|
|
log "DELETE-JOB"
|
|
exit 0
|
|
;;
|
|
|
|
*"delete serviceaccount cozystack-credentials-migration-64"*)
|
|
# The hook deletes a ServiceAccount left owned by the Job it just deleted,
|
|
# so the apply recreates a fresh, owner-less one.
|
|
log "DELETE-SA"
|
|
exit 0
|
|
;;
|
|
|
|
*"get job cozystack-credentials-migration-64"*jsonpath*)
|
|
# The hook reads the Job's uid to own its ServiceAccount.
|
|
if [ -n "${FAKE_GETJOB_FAIL:-}" ]; then
|
|
printf '%s\n' "$FAKE_GETJOB_FAIL" >&2
|
|
exit 1
|
|
fi
|
|
[ -n "${FAKE_JOBUID_EMPTY:-}" ] || printf '%s' "11111111-2222-3333-4444-555555555555"
|
|
exit 0
|
|
;;
|
|
|
|
*"patch serviceaccount cozystack-credentials-migration-64"*)
|
|
# The ownerReference tying the ServiceAccount to the Job.
|
|
if [ -n "${FAKE_PATCH_FAIL:-}" ]; then
|
|
printf '%s\n' "$FAKE_PATCH_FAIL" >&2
|
|
exit 1
|
|
fi
|
|
owner=$(printf '%s' "$args" | grep -c 'cozystack-credentials-migration-64')
|
|
log "PATCH-SA-OWNER owner=$owner"
|
|
exit 0
|
|
;;
|
|
|
|
*"apply --filename -"*)
|
|
manifest=$(cat)
|
|
case "$manifest" in
|
|
*"kind: ConfigMap"*)
|
|
v=$(printf '%s\n' "$manifest" | sed -n 's/^ version: "\{0,1\}\([0-9]\{1,\}\)"\{0,1\}$/\1/p' | head -1)
|
|
log "STAMP ${v:-unknown}"
|
|
exit 0
|
|
;;
|
|
*"kind: Job"*)
|
|
if [ -n "${FAKE_APPLY_FAIL:-}" ]; then
|
|
printf '%s\n' "$FAKE_APPLY_FAIL" >&2
|
|
exit 1
|
|
fi
|
|
# Record the fields the Job must carry, so a copy that drops the
|
|
# ServiceAccount, names the wrong migration, or renders the wrong image
|
|
# is caught.
|
|
role=$(printf '%s\n' "$manifest" | grep -c '^kind: ClusterRole$')
|
|
sa=$(printf '%s\n' "$manifest" | grep -c '^kind: ServiceAccount$')
|
|
crb=$(printf '%s\n' "$manifest" | grep -c '^kind: ClusterRoleBinding$')
|
|
# The binding must grant the least-privilege role, never cluster-admin.
|
|
admin=$(printf '%s\n' "$manifest" | grep -c 'cluster-admin')
|
|
# Count the name only inside the Job document: the ClusterRole, the
|
|
# ServiceAccount and the binding share it.
|
|
jobname=$(printf '%s\n' "$manifest" | sed -n '/^kind: Job$/,/^spec:$/p' | grep -c '^ name: cozystack-credentials-migration-64$')
|
|
mig=$(printf '%s\n' "$manifest" | grep -c -- '--migration=64')
|
|
img=$(printf '%s\n' "$manifest" | grep -c "^ image: ${IMAGE}$")
|
|
sacc=$(printf '%s\n' "$manifest" | grep -c '^ serviceAccountName: cozystack-credentials-migration-64$')
|
|
log "APPLY-FINISH role=$role sa=$sa crb=$crb admin=$admin jobname=$jobname migration=$mig image=$img serviceaccount=$sacc"
|
|
exit 0
|
|
;;
|
|
*)
|
|
log "APPLY-UNKNOWN"
|
|
printf 'fake kubectl: unmodelled apply\n' >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
;;
|
|
esac
|
|
|
|
log "UNHANDLED $args"
|
|
printf 'fake kubectl: unmodelled call: %s\n' "$args" >&2
|
|
exit 1
|