Skip to content

Commit e84a22b

Browse files
committed
fix(platform): carry FoundationDB cluster.version over to version
The foundationdb chart no longer reads cluster.version, so a cluster that set it to 7.1 or 7.4 would move to the v7.3 default on the first reconcile after the upgrade: a full-bounce upgrade from 7.1, or a downgrade from 7.4. Migration 60 sets version to the release line of each cluster.version it finds. It keeps cluster.version, because the old chart still renders the release between the migration and the chart upgrade and would fall back to its own default without it. A value outside the supported lines is recorded on the cozystack-version ConfigMap and does not block the upgrade. Assisted-by: LLM Signed-off-by: Andrei Kvapil <[email protected]>
1 parent 320d9b7 commit e84a22b

4 files changed

Lines changed: 287 additions & 1 deletion

File tree

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#!/usr/bin/env bats
2+
# -----------------------------------------------------------------------------
3+
# Unit tests for platform migration 60 --> 61 (carry a FoundationDB
4+
# application's cluster.version over to the version field that replaces it).
5+
#
6+
# The chart no longer reads cluster.version, so a cluster that set it to a line
7+
# other than 7.3 would move to the v7.3 default on its first reconcile after the
8+
# upgrade. These drive the real migration script against a fake kubectl
9+
# (hack/testdata/migration-60-foundationdb/) and pin that:
10+
# - each release that set cluster.version on 7.1, 7.3 or 7.4 gets version for
11+
# that line, through a JSON patch that tests the value it read and leaves
12+
# cluster.version in place for the old chart;
13+
# - releases that never set cluster.version, already have version, or are not
14+
# FoundationDB are not touched;
15+
# - an unlabelled release is still found by its chart;
16+
# - a value outside those lines is recorded and does not block the stamp;
17+
# - a failed scan or patch aborts before the stamp.
18+
#
19+
# cozytest.sh's awk parser recognizes only @test blocks and a bare `}` on its
20+
# own line; there is no bats `run`/`$status`/`setup`/`teardown`. Assertions are
21+
# direct shell tests that exit non-zero on failure.
22+
#
23+
# Run with: hack/cozytest.sh hack/migration-60-foundationdb-version.bats
24+
# -----------------------------------------------------------------------------
25+
26+
FAKEBIN="$PWD/hack/testdata/migration-60-foundationdb"
27+
MIG="$PWD/packages/core/platform/images/migrations/migrations/60"
28+
29+
prep() {
30+
WORK=$(mktemp -d)
31+
export FAKE_CMDLOG="$WORK/cmdlog"
32+
export FAKE_HR_LIST="$WORK/hrlist.json"
33+
: > "$FAKE_CMDLOG"
34+
export PATH="$FAKEBIN:$PATH"
35+
export NAMESPACE=cozy-system
36+
unset FAKE_LIST_FAIL FAKE_PATCH_FAIL MIGRATION_DRY_RUN
37+
cat > "$FAKE_HR_LIST" <<'JSON'
38+
{"items":[
39+
{"metadata":{"namespace":"tenant-a","name":"foundationdb-old71","labels":{"apps.cozystack.io/application.kind":"FoundationDB"}},
40+
"spec":{"chartRef":{"kind":"ExternalArtifact","name":"cozystack-foundationdb-application-default-foundationdb"},
41+
"values":{"cluster":{"version":"7.1.67","redundancyMode":"double"}}}},
42+
{"metadata":{"namespace":"tenant-a","name":"foundationdb-new74","labels":{"apps.cozystack.io/application.kind":"FoundationDB"}},
43+
"spec":{"values":{"cluster":{"version":"7.4.3"}}}},
44+
{"metadata":{"namespace":"tenant-b","name":"foundationdb-pinned73","labels":{"apps.cozystack.io/application.kind":"FoundationDB"}},
45+
"spec":{"values":{"cluster":{"version":"7.3.63"}}}},
46+
{"metadata":{"namespace":"tenant-b","name":"foundationdb-defaults","labels":{"apps.cozystack.io/application.kind":"FoundationDB"}},
47+
"spec":{"values":{"cluster":{"redundancyMode":"double"}}}},
48+
{"metadata":{"namespace":"tenant-b","name":"foundationdb-migrated","labels":{"apps.cozystack.io/application.kind":"FoundationDB"}},
49+
"spec":{"values":{"version":"v7.1","cluster":{"version":"7.1.67"}}}},
50+
{"metadata":{"namespace":"tenant-c","name":"foundationdb-unlabelled"},
51+
"spec":{"chartRef":{"kind":"ExternalArtifact","name":"cozystack-foundationdb-application-default-foundationdb"},
52+
"values":{"cluster":{"version":"7.1.0"}}}},
53+
{"metadata":{"namespace":"tenant-c","name":"postgres-db","labels":{"apps.cozystack.io/application.kind":"Postgres"}},
54+
"spec":{"values":{"cluster":{"version":"7.1.0"}}}},
55+
{"metadata":{"namespace":"tenant-d","name":"foundationdb-ancient","labels":{"apps.cozystack.io/application.kind":"FoundationDB"}},
56+
"spec":{"values":{"cluster":{"version":"6.3.25"}}}}
57+
]}
58+
JSON
59+
}
60+
61+
@test "carries each supported line over and leaves everything else alone" {
62+
prep
63+
rc=0
64+
bash "$MIG" >"$WORK/out" 2>&1 || rc=$?
65+
cat "$WORK/out"
66+
cat "$FAKE_CMDLOG"
67+
[ "$rc" -eq 0 ]
68+
69+
grep -qxF 'PATCH tenant-a foundationdb-old71 json [{"op":"test","path":"/spec/values/cluster/version","value":"7.1.67"},{"op":"add","path":"/spec/values/version","value":"v7.1"}]' "$FAKE_CMDLOG"
70+
grep -qxF 'PATCH tenant-a foundationdb-new74 json [{"op":"test","path":"/spec/values/cluster/version","value":"7.4.3"},{"op":"add","path":"/spec/values/version","value":"v7.4"}]' "$FAKE_CMDLOG"
71+
grep -qxF 'PATCH tenant-b foundationdb-pinned73 json [{"op":"test","path":"/spec/values/cluster/version","value":"7.3.63"},{"op":"add","path":"/spec/values/version","value":"v7.3"}]' "$FAKE_CMDLOG"
72+
grep -qxF 'PATCH tenant-c foundationdb-unlabelled json [{"op":"test","path":"/spec/values/cluster/version","value":"7.1.0"},{"op":"add","path":"/spec/values/version","value":"v7.1"}]' "$FAKE_CMDLOG"
73+
74+
[ "$(grep -c '^PATCH ' "$FAKE_CMDLOG")" -eq 4 ]
75+
if grep -q 'PATCH [^ ]* foundationdb-defaults ' "$FAKE_CMDLOG"; then echo "unexpected line matching 'PATCH [^ ]* foundationdb-defaults '"; exit 1; fi
76+
if grep -q 'PATCH [^ ]* foundationdb-migrated ' "$FAKE_CMDLOG"; then echo "unexpected line matching 'PATCH [^ ]* foundationdb-migrated '"; exit 1; fi
77+
if grep -q 'PATCH [^ ]* postgres-db ' "$FAKE_CMDLOG"; then echo "unexpected line matching 'PATCH [^ ]* postgres-db '"; exit 1; fi
78+
if grep -q 'PATCH [^ ]* foundationdb-ancient ' "$FAKE_CMDLOG"; then echo "unexpected line matching 'PATCH [^ ]* foundationdb-ancient '"; exit 1; fi
79+
80+
grep -q '^ANNOTATE .*cozystack.io/migration-58-unmapped-foundationdb=tenant-d/foundationdb-ancient=6.3.25' "$FAKE_CMDLOG"
81+
[ "$(tail -n1 "$FAKE_CMDLOG")" = "STAMP 61" ]
82+
if grep -q '^UNHANDLED' "$FAKE_CMDLOG"; then echo "unexpected line matching '^UNHANDLED'"; exit 1; fi
83+
rm -rf "$WORK"
84+
}
85+
86+
@test "a failed patch aborts before the stamp" {
87+
prep
88+
export FAKE_PATCH_FAIL=1
89+
rc=0
90+
bash "$MIG" >"$WORK/out" 2>&1 || rc=$?
91+
cat "$WORK/out"
92+
[ "$rc" -ne 0 ]
93+
if grep -q '^STAMP' "$FAKE_CMDLOG"; then echo "unexpected line matching '^STAMP'"; exit 1; fi
94+
rm -rf "$WORK"
95+
}
96+
97+
@test "a failed fleet scan aborts before the stamp" {
98+
prep
99+
export FAKE_LIST_FAIL=1
100+
rc=0
101+
bash "$MIG" >"$WORK/out" 2>&1 || rc=$?
102+
cat "$WORK/out"
103+
[ "$rc" -ne 0 ]
104+
if grep -q '^PATCH' "$FAKE_CMDLOG"; then echo "unexpected line matching '^PATCH'"; exit 1; fi
105+
if grep -q '^STAMP' "$FAKE_CMDLOG"; then echo "unexpected line matching '^STAMP'"; exit 1; fi
106+
rm -rf "$WORK"
107+
}
108+
109+
@test "dry run patches nothing and does not stamp" {
110+
prep
111+
export MIGRATION_DRY_RUN=1
112+
rc=0
113+
bash "$MIG" >"$WORK/out" 2>&1 || rc=$?
114+
cat "$WORK/out"
115+
[ "$rc" -eq 0 ]
116+
grep -q 'would set version=v7.1 on FoundationDB tenant-a/foundationdb-old71' "$WORK/out"
117+
if grep -q '^PATCH' "$FAKE_CMDLOG"; then echo "unexpected line matching '^PATCH'"; exit 1; fi
118+
if grep -q '^STAMP' "$FAKE_CMDLOG"; then echo "unexpected line matching '^STAMP'"; exit 1; fi
119+
rm -rf "$WORK"
120+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/sh
2+
# Fake kubectl for hack/migration-60-foundationdb-version.bats. It serves only
3+
# the calls migration 60 (and lib/cozystack-version.sh) make and appends one
4+
# line per call to $FAKE_CMDLOG.
5+
#
6+
# FAKE_CMDLOG file to append a record of each call to
7+
# FAKE_HR_LIST file holding the HelmRelease list JSON the fleet scan returns
8+
# FAKE_LIST_FAIL non-empty => the fleet scan fails
9+
# FAKE_PATCH_FAIL non-empty => every patch fails
10+
#
11+
# Any other call fails loudly, so drift between the script and this fake does
12+
# not pass unnoticed.
13+
set -u
14+
15+
log() { printf '%s\n' "$*" >> "$FAKE_CMDLOG"; }
16+
17+
arg_after() { k="$1"; shift; p=""; for a in "$@"; do [ "$p" = "$k" ] && { printf '%s' "$a"; return; }; p="$a"; done; }
18+
19+
args="$*"
20+
21+
case "$args" in
22+
"get helmreleases.helm.toolkit.fluxcd.io --all-namespaces --output json")
23+
if [ -n "${FAKE_LIST_FAIL:-}" ]; then
24+
echo 'error: the server was unable to return a response in the time allotted' >&2
25+
exit 1
26+
fi
27+
cat "$FAKE_HR_LIST"
28+
exit 0
29+
;;
30+
"patch helmreleases.helm.toolkit.fluxcd.io "*)
31+
if [ -n "${FAKE_PATCH_FAIL:-}" ]; then
32+
echo 'The request is invalid: the server rejected our request due to an error in our request' >&2
33+
exit 1
34+
fi
35+
ns=$(arg_after --namespace "$@")
36+
log "PATCH $ns $(arg_after "$ns" "$@") $(arg_after --type "$@") $(arg_after --patch "$@")"
37+
exit 0
38+
;;
39+
"annotate configmap "*)
40+
log "ANNOTATE $*"
41+
exit 0
42+
;;
43+
"apply --filename -")
44+
v=$(sed -n 's/^ version: "\{0,1\}\([0-9]\{1,\}\)"\{0,1\}$/\1/p' | head -1)
45+
log "STAMP ${v:-unknown}"
46+
exit 0
47+
;;
48+
esac
49+
50+
log "UNHANDLED $args"
51+
echo "fake kubectl: unmodelled call: $args" >&2
52+
exit 1
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/bin/bash
2+
# Migration 60 --> 61
3+
# Carry a FoundationDB application's cluster.version over to the version
4+
# field that replaces it.
5+
#
6+
# The foundationdb chart used to pass cluster.version, a free-form string, to
7+
# the operator as the FoundationDB version. It now takes version (v7.4, v7.3,
8+
# v7.1) and maps it to a tag, and no longer reads cluster.version at all. A
9+
# cluster that set cluster.version to anything but 7.3.x would otherwise move to
10+
# the v7.3 default on its first reconcile after the upgrade: an incompatible
11+
# upgrade from 7.1 with a full cluster bounce, or a downgrade from 7.4.
12+
#
13+
# The release line is carried over, not the patch: 7.1.x becomes v7.1, which
14+
# the chart maps to the one tag it ships for that line.
15+
#
16+
# cluster.version is left in place on purpose. This runs as a pre-upgrade hook,
17+
# so helm-controller reconciles the patched release with the OLD chart first.
18+
# That chart ignores version but still reads cluster.version; removing it would
19+
# let the old chart fall back to its own 7.3.63 default in that window, which is
20+
# the version change this migration exists to prevent. The new chart ignores the
21+
# leftover key.
22+
#
23+
# The release values are patched directly rather than through the apps API:
24+
# a read through the API fills in the schema defaults, so a cluster that never
25+
# set cluster.version would look as if it had and get pinned for no reason.
26+
#
27+
# Idempotent: a release that already has version is left alone. A patch carries
28+
# a test on the cluster.version it read, so a concurrent edit fails the patch.
29+
# Fail-closed on any read or patch error (exit 1 before the stamp) so the new
30+
# chart does not roll out over a cluster whose version was not carried over.
31+
# A cluster.version outside those three lines cannot be carried over; it is
32+
# logged and recorded on the cozystack-version ConfigMap, and does not block the
33+
# upgrade.
34+
#
35+
# If another migration 60 lands first, renumber this one and bump
36+
# migrations.targetVersion; the collision surfaces as a loud add/add conflict on
37+
# rebase.
38+
39+
set -euo pipefail
40+
41+
# shellcheck source=lib/cozystack-version.sh
42+
. "$(dirname "$0")/lib/cozystack-version.sh"
43+
44+
DRY_RUN="${MIGRATION_DRY_RUN:-0}"
45+
46+
CHART_REF_NAME="cozystack-foundationdb-application-default-foundationdb"
47+
INLINE_CHART_NAME="foundationdb"
48+
49+
# One line per FoundationDB release that needs attention:
50+
# <namespace> <name> carry <version> <cluster.version>
51+
# <namespace> <name> unmapped - <cluster.version>
52+
# Releases are matched by the application label or by the chart they install,
53+
# since the label postdates some releases (see migration 56).
54+
plan=$(kubectl get helmreleases.helm.toolkit.fluxcd.io --all-namespaces --output json | jq -r \
55+
--arg chart "$CHART_REF_NAME" --arg inlinechart "$INLINE_CHART_NAME" '
56+
.items[]
57+
| select(
58+
(.metadata.labels["apps.cozystack.io/application.kind"] == "FoundationDB")
59+
or ((.metadata.name | startswith("foundationdb-"))
60+
and ((.spec.chartRef.name == $chart) or (.spec.chart.spec.chart == $inlinechart))))
61+
| (.spec.values // {}) as $v
62+
| select((($v.version | type) == "string" and $v.version != "") | not)
63+
| select(($v.cluster | type) == "object" and ($v.cluster.version | type) == "string" and $v.cluster.version != "")
64+
| $v.cluster.version as $cv
65+
| (($cv | capture("^(?<line>7\\.(1|3|4))\\.[0-9]+$") | .line) // null) as $line
66+
| if $line then "\(.metadata.namespace) \(.metadata.name) carry v\($line) \($cv)"
67+
else "\(.metadata.namespace) \(.metadata.name) unmapped - \($cv)" end')
68+
69+
CARRIED=0
70+
UNMAPPED=""
71+
72+
while read -r ns name action version cluster_version; do
73+
[ -n "$ns" ] || continue
74+
case "$action" in
75+
carry)
76+
if [ "$DRY_RUN" = "1" ]; then
77+
echo "[dry-run] would set version=$version on FoundationDB $ns/$name (cluster.version $cluster_version)"
78+
CARRIED=$((CARRIED + 1))
79+
continue
80+
fi
81+
patch=$(jq -cn --arg cv "$cluster_version" --arg v "$version" '[
82+
{op: "test", path: "/spec/values/cluster/version", value: $cv},
83+
{op: "add", path: "/spec/values/version", value: $v}
84+
]')
85+
kubectl patch helmreleases.helm.toolkit.fluxcd.io --namespace "$ns" "$name" \
86+
--type json --patch "$patch" >/dev/null
87+
echo "Set version=$version on FoundationDB $ns/$name (cluster.version $cluster_version)"
88+
CARRIED=$((CARRIED + 1))
89+
;;
90+
unmapped)
91+
echo "WARN: FoundationDB $ns/$name has cluster.version $cluster_version, which is not on a supported line (7.1, 7.3, 7.4); it will render the v7.3 default after the upgrade" >&2
92+
UNMAPPED="${UNMAPPED}${UNMAPPED:+,}${ns}/${name}=${cluster_version}"
93+
;;
94+
esac
95+
done <<EOF
96+
$plan
97+
EOF
98+
99+
echo "==> Summary: carried=$CARRIED unmapped=${UNMAPPED:-none}"
100+
101+
if [ "$DRY_RUN" = "1" ]; then
102+
echo "Dry-run complete; version stamp not advanced"
103+
exit 0
104+
fi
105+
106+
# The record is a diagnostic aid, so failing to write it must not block the
107+
# upgrade for every tenant.
108+
if [ -n "$UNMAPPED" ]; then
109+
kubectl annotate configmap --namespace "${NAMESPACE:-cozy-system}" cozystack-version \
110+
"cozystack.io/migration-58-unmapped-foundationdb=$UNMAPPED" --overwrite >/dev/null \
111+
|| echo "WARN: failed to record the unmapped FoundationDB releases on cozystack-version" >&2
112+
fi
113+
114+
stamp_cozystack_version 61

‎packages/core/platform/values.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ migrations:
1414
# refuses to advance past a migration file missing from the image (exit 1), so
1515
# a stale pin fails loudly rather than silently skipping the etcd adoption.
1616
image: ghcr.io/cozystack/cozystack/platform-migrations:v1.6.0@sha256:6777042e3e9c6bad76e7d96fb64baa115061975f39061cb3ede84e21d0e2213f
17-
targetVersion: 60
17+
targetVersion: 61
1818
# Adopt legacy etcd clusters onto the v1alpha2 operator WITHOUT the mandatory
1919
# pre-adoption safety snapshot (migration 50). Leave false unless migration 50
2020
# has actually refused to proceed and you accept the risk: adoption rewires

0 commit comments

Comments
 (0)