feat(platform): migrate Redis and Valkey passwords to minted credentials

The converted charts stop rendering <release>-auth and point the
operator at the account Secret of the default credential. Without a
migration that Secret would be seeded with a new random password, and
every existing client, and the running servers, would lose the one they
have.

Migrations 69 (Redis) and 70 (Valkey) seed that Secret from the password
in <release>-auth before the charts switch, so the operator finds the
same password under the new name; then a finish Job per kind deletes
whatever is left of the old Secret. Each Job's ClusterRole,
ServiceAccount and binding are named for its migration, so the two
conversions in one upgrade share none.

Both hold every release of their kind, suspended, until the
redis-operator Deployment runs its new revision, and the finish Jobs,
which may read Deployments, resume them. The new chart changes the pod
template, and the old operator rolls such a change by deleting the
master, which failed client writes for up to 78 s per instance on a test
cluster before the new operator, which moves the master off first and
applies the password without a restart, had started.

Assisted-by: LLM
Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
This commit is contained in:
Andrei Kvapil committed 2026-12-02 20:38:26 +01:00
1 parent 5f9d6b9a87
commit 94807d93f3
6 files changed
+672 -1

No files matched your search

@@ -0,0 +1,180 @@
#!/usr/bin/env bats
# -----------------------------------------------------------------------------
# Unit tests for platform migrations 69 --> 70 (Redis) and 70 --> 71 (Valkey):
# convert the default password to platform-minted credentials, first part.
#
# Each migration seeds the account Secret of the default user of every release
# of its kind from <release>-auth, then creates its finish Job directly, as
# migration 64 does. These pin what the two add over 64: the seed and the Job
# take the fixed account, its source key and the enabled flag, and the Job's
# ClusterRole, ServiceAccount and binding are named for the migration, so the
# two conversions running in one upgrade share none of them.
#
# FAIL CLOSED is pinned for each path: a failed seed, a pod whose image cannot
# be read, a failed apply of the Job, and a Job that cannot be owned must stop
# the migration before it stamps the version, because migrations never re-run.
#
# The harness (docker, the migrations image base, fake kubectl and
# credentials-migrate on PATH) is the one hack/migration-58-http-cache-freeze.bats
# describes; see there for why each piece is load-bearing.
#
# Run with: hack/cozytest.sh hack/migration-69-70-redis-valkey-credentials.bats
# -----------------------------------------------------------------------------
load test_helper
FAKEBIN="$PWD/hack/testdata/migration-69-70-redis-valkey-credentials"
MIG_DIR="$PWD/packages/core/platform/images/migrations/migrations"
ALPINE=$(sed -n 's/^FROM \(alpine:[^ ]*\).*$/\1/p' \
"$PWD/packages/core/platform/images/migrations/Dockerfile" | head -1)
TESTIMG="cozystack-migration69-test:$(printf '%s' "$ALPINE" | sed 's/[^a-zA-Z0-9]/-/g')"
WORKROOT="${TMPDIR:-/tmp}/cozy-migration-69-$$"
IMAGE=registry.example.org/cozystack/cozystack:v1.7.0
cozy_cleanup() {
rm -rf "$WORKROOT"
return 0
}
cmdlog_line() {
grep -nE -- "$1" "$FAKE_CMDLOG" | head -1 | cut -d: -f1
}
run_migration() {
_run_migration_rc=0
docker run --rm --network none \
--user "$(id -u):$(id -g)" \
-v "$MIG_DIR:/migrations:ro" \
-v "$FAKEBIN:/fakebin:ro" \
-v "$WORK:/work" \
-e PATH=/fakebin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
-e FAKE_CMDLOG=/work/cmdlog \
-e FAKE_MIGRATION="$1" \
-e NAMESPACE="${NAMESPACE-}" \
-e FAKE_IMAGE="${FAKE_IMAGE-}" \
-e FAKE_IMAGE_EMPTY="${FAKE_IMAGE_EMPTY-}" \
-e FAKE_GETPOD_FAIL="${FAKE_GETPOD_FAIL-}" \
-e FAKE_APPLY_FAIL="${FAKE_APPLY_FAIL-}" \
-e FAKE_SEED_FAIL="${FAKE_SEED_FAIL-}" \
-e FAKE_GETJOB_FAIL="${FAKE_GETJOB_FAIL-}" \
-e FAKE_JOBUID_EMPTY="${FAKE_JOBUID_EMPTY-}" \
-e FAKE_PATCH_FAIL="${FAKE_PATCH_FAIL-}" \
"$TESTIMG" "/migrations/$1" || _run_migration_rc=$?
return "$_run_migration_rc"
}
prep() {
docker info >/dev/null 2>&1 || {
echo "docker is required: these tests run migrations 69 and 70 inside a" >&2
echo "build of $ALPINE, the migrations image's base." >&2
return 1
}
docker build -q -t "$TESTIMG" - >/dev/null <<DOCKERFILE
FROM $ALPINE
DOCKERFILE
chmod +x "$FAKEBIN/kubectl" "$FAKEBIN/credentials-migrate"
mkdir -p "$WORKROOT"
WORK=$(mktemp -d "$WORKROOT/XXXXXX")
export FAKE_CMDLOG="$WORK/cmdlog"
: > "$FAKE_CMDLOG"
export NAMESPACE=cozy-system
export FAKE_IMAGE="$IMAGE"
unset FAKE_IMAGE_EMPTY FAKE_GETPOD_FAIL FAKE_APPLY_FAIL FAKE_SEED_FAIL FAKE_GETJOB_FAIL FAKE_JOBUID_EMPTY FAKE_PATCH_FAIL || true
return 0
}
# check_converts <migration> <kind>
check_converts() {
prep
rc=0
run_migration "$1" >"$WORK/out" 2>&1 || rc=$?
cat "$WORK/out"; cat "$FAKE_CMDLOG"
[ "$rc" -eq 0 ]
# The seed takes the one default user, its password from the password key
# of <release>-auth, and authEnabled as the switch.
# It holds the releases until the new redis-operator runs, which the
# finish Job reads the Deployment for: the old operator rolls the converted
# pod template by deleting the master.
grep -qxF "CREDMIGRATE seed --migration $1 --kind $2 --format plaintext --fixed-account default --source-key password --enabled-flag authEnabled --source-suffix=-auth --hold-until-rollout cozy-redis-operator/redis-operator" "$FAKE_CMDLOG"
# The finish Job carries its least-privilege ClusterRole (never
# cluster-admin), ServiceAccount and binding, all named for the migration
# (five names: the three objects, the roleRef and the Job), the same account
# flags as the seed, and the image this hook runs as.
grep -qxE "APPLY-FINISH role=1 sa=1 crb=1 admin=0 migration=1 image=1 serviceaccount=1 fixed=5 actor=1 named=5 deployments=1" "$FAKE_CMDLOG"
grep -qxE "PATCH-SA-OWNER owner=1" "$FAKE_CMDLOG"
# Stale Job deleted before the apply, owned after it, stamped last.
_del=$(cmdlog_line "^DELETE-JOB$")
_apply=$(cmdlog_line "^APPLY-FINISH ")
_own=$(cmdlog_line "^PATCH-SA-OWNER ")
_stamp=$(cmdlog_line "^STAMP $(($1 + 1))$")
[ -n "$_del" ] && [ "$_del" -lt "$_apply" ]
[ -n "$_own" ] && [ "$_apply" -lt "$_own" ]
[ -n "$_stamp" ] && [ "$_own" -lt "$_stamp" ]
rm -rf "$WORK"
}
# check_fails_closed <migration> <fake variable> <value>
check_fails_closed() {
prep
export "$2=$3"
rc=0
run_migration "$1" >"$WORK/out" 2>&1 || rc=$?
cat "$WORK/out"; cat "$FAKE_CMDLOG"
[ "$rc" -ne 0 ]
if grep -qF "STAMP" "$FAKE_CMDLOG"; then echo "migration $1 stamped after $2" >&2; return 1; fi
rm -rf "$WORK"
}
@test "69 converts Redis: seeds, creates its own finish Job, and stamps 70" {
check_converts 69 Redis
}
@test "70 converts Valkey: seeds, creates its own finish Job, and stamps 71" {
check_converts 70 Valkey
}
@test "a failed seed stops before the Job and the stamp" {
for n in 69 70; do
check_fails_closed "$n" FAKE_SEED_FAIL "apiserver is unavailable"
done
}
@test "a pod whose image cannot be read stops before the Job and the stamp" {
for n in 69 70; do
check_fails_closed "$n" FAKE_GETPOD_FAIL "the server could not find the requested resource"
check_fails_closed "$n" FAKE_IMAGE_EMPTY 1
done
}
@test "a seed that fails to hold a release stops before the Job and the stamp" {
# A release suspended with no finish Job to resume it would stay frozen.
for n in 69 70; do
prep
export FAKE_SEED_FAIL="tenant-a/redis-x: hold until the new components run: the object has been modified"
rc=0
run_migration "$n" >"$WORK/out" 2>&1 || rc=$?
cat "$WORK/out"; cat "$FAKE_CMDLOG"
[ "$rc" -ne 0 ]
grep -qF "hold until the new components run" "$WORK/out"
if grep -qE "^APPLY-FINISH" "$FAKE_CMDLOG"; then echo "migration $n created the Job after a failed hold" >&2; return 1; fi
if grep -qF "STAMP" "$FAKE_CMDLOG"; then echo "migration $n stamped after a failed hold" >&2; return 1; fi
rm -rf "$WORK"
done
}
@test "a failed apply of the Job stops before stamping" {
for n in 69 70; do
check_fails_closed "$n" FAKE_APPLY_FAIL "admission webhook denied the request"
done
}
@test "a Job that cannot be owned stops before stamping" {
for n in 69 70; do
check_fails_closed "$n" FAKE_GETJOB_FAIL "the server was unable to return a response"
check_fails_closed "$n" FAKE_JOBUID_EMPTY 1
check_fails_closed "$n" FAKE_PATCH_FAIL "serviceaccounts not found"
done
}
@@ -0,0 +1,22 @@
#!/bin/sh
# Fake credentials-migrate for hack/migration-69-70-redis-valkey-credentials.bats.
# Migrations 69 and 70 run `credentials-migrate seed` in the hook; the real binary is
# exercised by cmd/credentials-migrate's Go tests, so this fake only records the
# call and fails on demand to pin the hook's fail-closed behaviour.
#
# FAKE_CMDLOG file to append a one-line record of each call to
# FAKE_SEED_FAIL non-empty => `seed` exits 1 with this stderr
set -u
[ -n "${FAKE_CMDLOG:-}" ] && printf 'CREDMIGRATE %s\n' "$*" >> "$FAKE_CMDLOG"
case "${1:-}" in
seed)
if [ -n "${FAKE_SEED_FAIL:-}" ]; then
printf '%s\n' "$FAKE_SEED_FAIL" >&2
exit 1
fi
exit 0
;;
esac
exit 0
+119
View File
@@ -0,0 +1,119 @@
#!/bin/sh
# Fake kubectl for hack/migration-69-70-redis-valkey-credentials.bats, the
# one hack/testdata/migration-64-postgres-credentials/kubectl is with the
# migration number taken from $FAKE_MIGRATION. It serves only the calls
# migrations 69 and 70 and lib/cozystack-version.sh make, and logs each one
# to $FAKE_CMDLOG.
#
# Each migration 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_MIGRATION number of the migration under test
# 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}"
N="${FAKE_MIGRATION:?FAKE_MIGRATION is required}"
NAME="cozystack-credentials-migration-$N"
case "$args" in
*"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 $NAME "*)
# The hook deletes a Job left by an earlier run before recreating it.
log "DELETE-JOB"
exit 0
;;
*"delete serviceaccount $NAME "*)
# 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 $NAME "*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 $NAME "*)
# 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 "\"name\":\"$NAME\"")
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')
mig=$(printf '%s\n' "$manifest" | grep -c -- "--migration=$N\$")
# The Job finishes the kind with the same accounts the hook seeded.
fixed=$(printf '%s\n' "$manifest" | grep -c -e '- --fixed-account=default$' -e '- --source-key=password$' -e '- --enabled-flag=authEnabled$' -e '- --source-suffix=-auth$' -e '- --format=plaintext$')
actor=$(printf '%s\n' "$manifest" | grep -c "value: system:serviceaccount:.*:$NAME\$")
img=$(printf '%s\n' "$manifest" | grep -c "^ image: ${IMAGE}$")
sacc=$(printf '%s\n' "$manifest" | grep -c "^ serviceAccountName: $NAME\$")
# ClusterRole, ServiceAccount, ClusterRoleBinding and Job, each named for
# the migration, so two conversions in one upgrade do not share them.
named=$(printf '%s\n' "$manifest" | grep -c "^ name: $NAME\$")
# It reads Deployments to resume the held releases after the rollout.
deploy=$(printf '%s\n' "$manifest" | grep -c '^ resources: \["deployments"\]$')
log "APPLY-FINISH role=$role sa=$sa crb=$crb admin=$admin migration=$mig image=$img serviceaccount=$sacc fixed=$fixed actor=$actor named=$named deployments=$deploy"
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
+176
View File
@@ -0,0 +1,176 @@
#!/bin/sh
# Migration 69 --> 70
# Convert the Redis password to platform-minted credentials, first part.
#
# The converted redis chart renders one CredentialTemplate, for the default
# user, while authEnabled is on, and points the RedisFailover's
# auth.secretPath at its account Secret, which cozystack-controller seeds
# with a random password only on a release marked for seeding. This hook runs
# before any component chart changes version and writes that Secret, unowned,
# for every existing Redis release from the password in <release>-auth, so
# the clients keep the password they have at every point of the upgrade.
# (Replication, the Sentinels, the probes and the exporter move to the
# operator's own internal account when the operator is upgraded,
# independently of this migration.) The controller adopts the Secret once
# the template exists. A release whose Secret was written, or that has
# authentication off, is marked for seeding.
# The Redis backup strategy reads the same account Secret, so the next
# backup authenticates with the migrated password.
#
# The hook also suspends every Redis release until the redis-operator
# Deployment runs its new revision. Every application chart switches the
# moment the platform chart moves the packages source, while the operator
# rolls out later, and the converted chart changes the pod template: the
# old operator rolls such a change by deleting the master, failing writes
# for about a minute, where the new one moves the master off first and
# applies the password with no restart. The finish Job resumes the
# releases once the new operator runs, and may read Deployments for that.
#
# The second part is the cozystack-credentials-migration-69 Job, which waits
# per release for its chart to switch, then deletes whatever is left of
# <release>-auth, which the switched chart no longer renders. The hook creates
# the Job directly, with a least-privilege ServiceAccount the Job owns, for the
# reasons migration 64 gives: a chart-rendered Job would hold up the platform
# release and fault on its immutable pod template at the next upgrade. To
# rerun it by hand, delete the Job and re-apply the manifest below.
#
# Fail-closed: migrations never re-run, so an error here stops the upgrade
# rather than stamping the version past a release that was not recorded.
set -euo pipefail
# shellcheck source=lib/cozystack-version.sh
. "$(dirname "$0")/lib/cozystack-version.sh"
NAMESPACE="${NAMESPACE:-cozy-system}"
credentials-migrate seed \
--migration 69 \
--kind Redis \
--format plaintext \
--fixed-account default \
--source-key password \
--enabled-flag authEnabled \
--source-suffix=-auth \
--hold-until-rollout cozy-redis-operator/redis-operator
# The finish Job runs the same image as this hook, so a release and its
# conversion share the credentials-migrate build. Read it from this pod's own
# container by name rather than render it from values, which the hook does not
# carry, and rather than by position, which a webhook-injected container shifts.
image=$(kubectl get pod "$(hostname)" \
--namespace "$NAMESPACE" \
--output 'jsonpath={.spec.containers[?(@.name=="migration")].image}')
if [ -z "$image" ]; then
echo "cannot read this pod's image to run the finish Job" >&2
exit 1
fi
# Delete any Job left by an earlier run of this hook: its pod template is
# immutable, so a rerun on a newer image would fault on apply. finish recovers
# its progress from the state ConfigMap, so recreating the Job loses nothing.
# Delete the ServiceAccount with it: an earlier run may have left it owned by
# the Job just deleted, and the apply below does not touch ownerReferences, so
# without this the garbage collector could remove the re-applied ServiceAccount
# out from under the ownerReference patch. A fresh, owner-less ServiceAccount is
# never collected before the patch names the new Job.
kubectl delete job cozystack-credentials-migration-69 \
--namespace "$NAMESPACE" \
--ignore-not-found
kubectl delete serviceaccount cozystack-credentials-migration-69 \
--namespace "$NAMESPACE" \
--ignore-not-found
kubectl apply --filename - <<YAML
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: cozystack-credentials-migration-69
rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get", "create", "patch", "delete"]
- apiGroups: [""]
resources: ["configmaps"]
verbs: ["get", "create", "update"]
- apiGroups: [""]
resources: ["events"]
verbs: ["create"]
- apiGroups: ["helm.toolkit.fluxcd.io"]
resources: ["helmreleases"]
verbs: ["get", "list", "patch"]
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["get"]
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: cozystack-credentials-migration-69
namespace: ${NAMESPACE}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: cozystack-credentials-migration-69
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cozystack-credentials-migration-69
subjects:
- kind: ServiceAccount
name: cozystack-credentials-migration-69
namespace: ${NAMESPACE}
---
apiVersion: batch/v1
kind: Job
metadata:
name: cozystack-credentials-migration-69
namespace: ${NAMESPACE}
spec:
backoffLimit: 6
ttlSecondsAfterFinished: 604800
template:
metadata:
labels:
policy.cozystack.io/allow-to-apiserver: "true"
spec:
serviceAccountName: cozystack-credentials-migration-69
restartPolicy: OnFailure
containers:
- name: migration
image: ${image}
command:
- credentials-migrate
- finish
- --migration=69
- --kind=Redis
- --format=plaintext
- --fixed-account=default
- --source-key=password
- --enabled-flag=authEnabled
- --source-suffix=-auth
- --timeout=30m
env:
- name: CREDENTIALS_MIGRATE_ACTOR
value: system:serviceaccount:${NAMESPACE}:cozystack-credentials-migration-69
YAML
# Tie the ServiceAccount's lifetime to the Job: when the Job is deleted (its
# ttlSecondsAfterFinished, a rerun's delete above, or an operator by hand) the
# garbage collector removes the ServiceAccount, on the success and the failure
# path alike, so the least-privilege identity does not stand indefinitely after
# a release the conversion left out.
job_uid=$(kubectl get job cozystack-credentials-migration-69 \
--namespace "$NAMESPACE" \
--output 'jsonpath={.metadata.uid}')
if [ -z "$job_uid" ]; then
echo "cannot read the finish Job's uid to own its ServiceAccount" >&2
exit 1
fi
kubectl patch serviceaccount cozystack-credentials-migration-69 \
--namespace "$NAMESPACE" \
--type merge \
--patch "{\"metadata\":{\"ownerReferences\":[{\"apiVersion\":\"batch/v1\",\"kind\":\"Job\",\"name\":\"cozystack-credentials-migration-69\",\"uid\":\"${job_uid}\"}]}}"
stamp_cozystack_version 70
+174
View File
@@ -0,0 +1,174 @@
#!/bin/sh
# Migration 70 --> 71
# Convert the Valkey password to platform-minted credentials, first part.
#
# The converted valkey chart renders one CredentialTemplate, for the default
# user, while authEnabled is on, and points the RedisFailover's
# auth.secretPath at its account Secret, which cozystack-controller seeds
# with a random password only on a release marked for seeding. This hook runs
# before any component chart changes version and writes that Secret, unowned,
# for every existing Valkey release from the password in <release>-auth, so
# the clients keep the password they have at every point of the upgrade.
# (Replication, the Sentinels, the probes and the exporter move to the
# operator's own internal account when the operator is upgraded,
# independently of this migration.) The controller adopts the Secret once
# the template exists. A release whose Secret was written, or that has
# authentication off, is marked for seeding.
#
# The hook also suspends every Valkey release until the redis-operator
# Deployment runs its new revision. Every application chart switches the
# moment the platform chart moves the packages source, while the operator
# rolls out later, and the converted chart changes the pod template: the
# old operator rolls such a change by deleting the master, failing writes
# for about a minute, where the new one moves the master off first and
# applies the password with no restart. The finish Job resumes the
# releases once the new operator runs, and may read Deployments for that.
#
# The second part is the cozystack-credentials-migration-70 Job, which waits
# per release for its chart to switch, then deletes whatever is left of
# <release>-auth, which the switched chart no longer renders. The hook creates
# the Job directly, with a least-privilege ServiceAccount the Job owns, for the
# reasons migration 64 gives: a chart-rendered Job would hold up the platform
# release and fault on its immutable pod template at the next upgrade. To
# rerun it by hand, delete the Job and re-apply the manifest below.
#
# Fail-closed: migrations never re-run, so an error here stops the upgrade
# rather than stamping the version past a release that was not recorded.
set -euo pipefail
# shellcheck source=lib/cozystack-version.sh
. "$(dirname "$0")/lib/cozystack-version.sh"
NAMESPACE="${NAMESPACE:-cozy-system}"
credentials-migrate seed \
--migration 70 \
--kind Valkey \
--format plaintext \
--fixed-account default \
--source-key password \
--enabled-flag authEnabled \
--source-suffix=-auth \
--hold-until-rollout cozy-redis-operator/redis-operator
# The finish Job runs the same image as this hook, so a release and its
# conversion share the credentials-migrate build. Read it from this pod's own
# container by name rather than render it from values, which the hook does not
# carry, and rather than by position, which a webhook-injected container shifts.
image=$(kubectl get pod "$(hostname)" \
--namespace "$NAMESPACE" \
--output 'jsonpath={.spec.containers[?(@.name=="migration")].image}')
if [ -z "$image" ]; then
echo "cannot read this pod's image to run the finish Job" >&2
exit 1
fi
# Delete any Job left by an earlier run of this hook: its pod template is
# immutable, so a rerun on a newer image would fault on apply. finish recovers
# its progress from the state ConfigMap, so recreating the Job loses nothing.
# Delete the ServiceAccount with it: an earlier run may have left it owned by
# the Job just deleted, and the apply below does not touch ownerReferences, so
# without this the garbage collector could remove the re-applied ServiceAccount
# out from under the ownerReference patch. A fresh, owner-less ServiceAccount is
# never collected before the patch names the new Job.
kubectl delete job cozystack-credentials-migration-70 \
--namespace "$NAMESPACE" \
--ignore-not-found
kubectl delete serviceaccount cozystack-credentials-migration-70 \
--namespace "$NAMESPACE" \
--ignore-not-found
kubectl apply --filename - <<YAML
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: cozystack-credentials-migration-70
rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get", "create", "patch", "delete"]
- apiGroups: [""]
resources: ["configmaps"]
verbs: ["get", "create", "update"]
- apiGroups: [""]
resources: ["events"]
verbs: ["create"]
- apiGroups: ["helm.toolkit.fluxcd.io"]
resources: ["helmreleases"]
verbs: ["get", "list", "patch"]
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["get"]
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: cozystack-credentials-migration-70
namespace: ${NAMESPACE}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: cozystack-credentials-migration-70
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cozystack-credentials-migration-70
subjects:
- kind: ServiceAccount
name: cozystack-credentials-migration-70
namespace: ${NAMESPACE}
---
apiVersion: batch/v1
kind: Job
metadata:
name: cozystack-credentials-migration-70
namespace: ${NAMESPACE}
spec:
backoffLimit: 6
ttlSecondsAfterFinished: 604800
template:
metadata:
labels:
policy.cozystack.io/allow-to-apiserver: "true"
spec:
serviceAccountName: cozystack-credentials-migration-70
restartPolicy: OnFailure
containers:
- name: migration
image: ${image}
command:
- credentials-migrate
- finish
- --migration=70
- --kind=Valkey
- --format=plaintext
- --fixed-account=default
- --source-key=password
- --enabled-flag=authEnabled
- --source-suffix=-auth
- --timeout=30m
env:
- name: CREDENTIALS_MIGRATE_ACTOR
value: system:serviceaccount:${NAMESPACE}:cozystack-credentials-migration-70
YAML
# Tie the ServiceAccount's lifetime to the Job: when the Job is deleted (its
# ttlSecondsAfterFinished, a rerun's delete above, or an operator by hand) the
# garbage collector removes the ServiceAccount, on the success and the failure
# path alike, so the least-privilege identity does not stand indefinitely after
# a release the conversion left out.
job_uid=$(kubectl get job cozystack-credentials-migration-70 \
--namespace "$NAMESPACE" \
--output 'jsonpath={.metadata.uid}')
if [ -z "$job_uid" ]; then
echo "cannot read the finish Job's uid to own its ServiceAccount" >&2
exit 1
fi
kubectl patch serviceaccount cozystack-credentials-migration-70 \
--namespace "$NAMESPACE" \
--type merge \
--patch "{\"metadata\":{\"ownerReferences\":[{\"apiVersion\":\"batch/v1\",\"kind\":\"Job\",\"name\":\"cozystack-credentials-migration-70\",\"uid\":\"${job_uid}\"}]}}"
stamp_cozystack_version 71
+1 -1
View File
@@ -89,7 +89,7 @@ migrations:
# refuses to advance past a migration file missing from the image (exit 1), so
# a stale pin fails loudly rather than silently skipping the etcd adoption.
image: ghcr.io/cozystack/cozystack/platform-migrations:v1.6.0@sha256:6777042e3e9c6bad76e7d96fb64baa115061975f39061cb3ede84e21d0e2213f
targetVersion: 69
targetVersion: 71
# Adopt legacy etcd clusters onto the v1alpha2 operator WITHOUT the mandatory
# pre-adoption safety snapshot (migration 50). Leave false unless migration 50
# has actually refused to proceed and you accept the risk: adoption rewires