Skip to content

Commit 7214efc

Browse files
authored
refactor(platform): factor the cozystack-version stamp into a shared migration helper (cozystack#2980)
## What this PR does Every migration that stamps the `cozystack-version` ConfigMap must carry the `platform.cozystack.io/no-delete=true` label so the `cozystack-no-delete-guardrail` ValidatingAdmissionPolicy guards it. The chart template renders the labeled ConfigMap only on first install, so each upgrade-time stamp has to re-emit the label by hand — migration 43 had to inline-copy migration 42's heredoc to avoid a label-less apply by the same field manager stripping it. The bug class was patched per-migration, not eliminated. This extracts the stamp into a shared, sourced helper (`render_cozystack_version_manifest` + `stamp_cozystack_version`) under `images/migrations/migrations/lib/`, and routes migrations 42, 43, 44 and the `run-migrations.sh` bootstrap path through it. The labeled manifest now has a single definition, so future migrations cannot drift back to a label-less apply. The upgrade path is behavior-preserving: the rendered manifest is byte-identical to the previous heredocs (pinned by a golden unit test). The bootstrap path in `run-migrations.sh` previously created the ConfigMap without the label — it now carries it like every migration-driven stamp. Adds `hack/cozystack-version-stamp.bats` (8 tests) covering the helper: label presence, ConfigMap name, quoted-string version type, namespace default and override, required-argument guard, and the byte-exact golden manifest. Closes cozystack#2781 ### Release note ```release-note fix(platform): always stamp the platform.cozystack.io/no-delete label on the cozystack-version ConfigMap, including the migration bootstrap path, so deletion protection cannot be silently dropped ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes * **Tests** * Expanded validation to confirm the version-stamp manifest is parseable YAML, includes the required no-delete label, targets the correct `cozystack-version` ConfigMap, and quotes the version value correctly. * Added coverage for default and overridden namespaces, argument validation failures, and a golden “byte-for-byte” render check. * Added a guardrail test to prevent bypassing the shared stamping logic by direct `kubectl` ConfigMap stamping. * **Refactor** * Centralized `cozystack-version` rendering and stamping into a shared helper and updated migrations and the migrations runner to use it consistently. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents 5be9bd5 + de44ecf commit 7214efc

6 files changed

Lines changed: 222 additions & 56 deletions

File tree

‎hack/cozystack-version-stamp.bats‎

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#!/usr/bin/env bats
2+
# -----------------------------------------------------------------------------
3+
# Unit tests for
4+
# packages/core/platform/images/migrations/migrations/lib/cozystack-version.sh
5+
#
6+
# render_cozystack_version_manifest <version> emits the cozystack-version
7+
# ConfigMap manifest on stdout, always carrying the
8+
# platform.cozystack.io/no-delete=true label that the
9+
# cozystack-no-delete-guardrail ValidatingAdmissionPolicy keys on. Migrations
10+
# apply it under the default kubectl field manager; a label-less apply by that
11+
# same manager would strip the label, so the manifest is centralized here and
12+
# every migration sources it instead of copy-pasting a heredoc.
13+
#
14+
# cozytest.sh's awk parser recognizes only @test blocks and a bare `}` on its
15+
# own line; there is no bats `run` or `$status`. Assertions are expressed as
16+
# direct shell tests that exit non-zero on failure. Each test runs in its own
17+
# subshell, so NAMESPACE set/unset in one test does not leak into another.
18+
#
19+
# Run with: hack/cozytest.sh hack/cozystack-version-stamp.bats
20+
# -----------------------------------------------------------------------------
21+
22+
@test "renders parseable YAML for a version" {
23+
. packages/core/platform/images/migrations/migrations/lib/cozystack-version.sh
24+
render_cozystack_version_manifest 45 | yq . >/dev/null
25+
}
26+
27+
@test "carries the no-delete label set to true" {
28+
. packages/core/platform/images/migrations/migrations/lib/cozystack-version.sh
29+
val=$(render_cozystack_version_manifest 45 \
30+
| yq -r '.metadata.labels."platform.cozystack.io/no-delete"')
31+
[ "$val" = "true" ]
32+
}
33+
34+
@test "names the ConfigMap cozystack-version" {
35+
. packages/core/platform/images/migrations/migrations/lib/cozystack-version.sh
36+
name=$(render_cozystack_version_manifest 45 | yq -r '.metadata.name')
37+
[ "$name" = "cozystack-version" ]
38+
}
39+
40+
@test "stamps the requested version as a quoted string" {
41+
# The version MUST render as a quoted YAML string; an unquoted numeric
42+
# scalar (version: 45) would change the ConfigMap data type the rest of
43+
# the platform reads as a string.
44+
. packages/core/platform/images/migrations/migrations/lib/cozystack-version.sh
45+
out=$(render_cozystack_version_manifest 45)
46+
printf '%s\n' "$out" | grep -qx ' version: "45"'
47+
v=$(printf '%s\n' "$out" | yq -r '.data.version')
48+
[ "$v" = "45" ]
49+
}
50+
51+
@test "defaults namespace to cozy-system when NAMESPACE is unset" {
52+
unset NAMESPACE
53+
. packages/core/platform/images/migrations/migrations/lib/cozystack-version.sh
54+
ns=$(render_cozystack_version_manifest 45 | yq -r '.metadata.namespace')
55+
[ "$ns" = "cozy-system" ]
56+
}
57+
58+
@test "honors a NAMESPACE override" {
59+
. packages/core/platform/images/migrations/migrations/lib/cozystack-version.sh
60+
out=$(export NAMESPACE=cozy-other; render_cozystack_version_manifest 45)
61+
ns=$(printf '%s\n' "$out" | yq -r '.metadata.namespace')
62+
[ "$ns" = "cozy-other" ]
63+
}
64+
65+
@test "requires a version argument" {
66+
. packages/core/platform/images/migrations/migrations/lib/cozystack-version.sh
67+
if ( render_cozystack_version_manifest ) 2>/dev/null; then
68+
echo "expected non-zero exit when version arg is missing" >&2
69+
exit 1
70+
fi
71+
}
72+
73+
@test "stamp_cozystack_version requires a version argument" {
74+
# The guard fires before the kubectl pipe, so this needs no cluster: a
75+
# missing version must abort rather than apply empty input to kubectl.
76+
. packages/core/platform/images/migrations/migrations/lib/cozystack-version.sh
77+
if ( stamp_cozystack_version ) 2>/dev/null; then
78+
echo "expected non-zero exit when version arg is missing" >&2
79+
exit 1
80+
fi
81+
}
82+
83+
@test "render output matches the canonical labeled manifest" {
84+
# Golden test: pins the exact bytes migrations 42/43/44 apply today, so the
85+
# refactor that routes them through this helper is provably manifest-stable.
86+
unset NAMESPACE
87+
. packages/core/platform/images/migrations/migrations/lib/cozystack-version.sh
88+
expected=$(cat <<'EOF'
89+
apiVersion: v1
90+
kind: ConfigMap
91+
metadata:
92+
name: cozystack-version
93+
namespace: cozy-system
94+
labels:
95+
platform.cozystack.io/no-delete: "true"
96+
data:
97+
version: "45"
98+
EOF
99+
)
100+
actual=$(render_cozystack_version_manifest 45)
101+
if [ "$actual" != "$expected" ]; then
102+
printf 'expected:\n%s\n---\nactual:\n%s\n' "$expected" "$actual" >&2
103+
exit 1
104+
fi
105+
}
106+
107+
@test "no go-forward stamp bypasses the helper (migrations >= 42 and run-migrations.sh)" {
108+
# The helper exists so go-forward stamps cannot drift back to a label-less
109+
# apply that drops the no-delete label. Migrations 1-41 are frozen and
110+
# backfilled by 42, so they are exempt; migration 42 onward and the
111+
# bootstrap path must each source the helper, call its API, and never stamp
112+
# cozystack-version directly with kubectl. Blocking only the historical
113+
# 'kubectl create configmap' shape is too weak — an inline 'kubectl apply'
114+
# of a hand-rolled label-less manifest would slip past it.
115+
mig_dir=packages/core/platform/images/migrations/migrations
116+
files=""
117+
for f in "$mig_dir"/*; do
118+
n=$(basename "$f")
119+
case "$n" in *[!0-9]*) continue ;; esac # skip lib/ and other non-numeric
120+
[ "$n" -ge 42 ] || continue
121+
files="$files $f"
122+
done
123+
files="$files packages/core/platform/images/migrations/run-migrations.sh"
124+
125+
for f in $files; do
126+
if ! grep -Eq 'cozystack-version\.sh' "$f"; then
127+
echo "$f does not source the cozystack-version helper" >&2
128+
exit 1
129+
fi
130+
if ! grep -Eq '(render_cozystack_version_manifest|stamp_cozystack_version)' "$f"; then
131+
echo "$f does not call the cozystack-version helper API" >&2
132+
exit 1
133+
fi
134+
# Strip full-comment lines first: a comment that explains why a stamp
135+
# routes through the helper must not read as a direct stamp itself. A
136+
# real bypass command never starts with '#', so the guard keeps its
137+
# strength against actual direct stamping.
138+
if grep -v '^[[:space:]]*#' "$f" | grep -Eq 'kubectl[[:space:]].*(create[[:space:]]+configmap|apply).*cozystack-version'; then
139+
echo "$f stamps cozystack-version directly instead of via the helper" >&2
140+
exit 1
141+
fi
142+
done
143+
}

‎packages/core/platform/images/migrations/migrations/42‎

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,20 @@
1414
# the next chart-driven apply sees no diff.
1515
#
1616
# 3-way-merge note: this apply records the no-delete label in the kubectl
17-
# last-applied-configuration annotation under the default field manager.
18-
# Any future migration that re-stamps cozystack-version via the same field
19-
# manager MUST include the label inline (matching the chart template at
20-
# templates/cozystack-version.yaml) — a label-less apply by the same field
21-
# manager would strip it.
17+
# last-applied-configuration annotation under the default field manager. A
18+
# label-less apply by the same field manager would strip it, so the manifest
19+
# is rendered by the shared helper (lib/cozystack-version.sh) that every
20+
# migration sources rather than a hand-copied heredoc.
2221

2322
set -euo pipefail
2423

24+
# shellcheck source=lib/cozystack-version.sh
25+
. "$(dirname "$0")/lib/cozystack-version.sh"
26+
2527
NAMESPACE="${NAMESPACE:-cozy-system}"
2628
DRY_RUN="${MIGRATION_DRY_RUN:-0}"
2729

28-
MANIFEST=$(cat <<EOF
29-
apiVersion: v1
30-
kind: ConfigMap
31-
metadata:
32-
name: cozystack-version
33-
namespace: ${NAMESPACE}
34-
labels:
35-
platform.cozystack.io/no-delete: "true"
36-
data:
37-
version: "43"
38-
EOF
39-
)
30+
MANIFEST=$(render_cozystack_version_manifest 43)
4031

4132
if [ "$DRY_RUN" = "1" ]; then
4233
echo "[dry-run] would apply:"

‎packages/core/platform/images/migrations/migrations/43‎

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
set -euo pipefail
1414

15+
# shellcheck source=lib/cozystack-version.sh
16+
. "$(dirname "$0")/lib/cozystack-version.sh"
17+
1518
# Iterate every namespace that has a Cluster named "seaweedfs-db".
1619
for ns in $(kubectl get cluster.postgresql.cnpg.io -A \
1720
-o jsonpath='{range .items[?(@.metadata.name=="seaweedfs-db")]}{.metadata.namespace}{"\n"}{end}'); do
@@ -27,21 +30,6 @@ for ns in $(kubectl get cluster.postgresql.cnpg.io -A \
2730
fi
2831
done
2932

30-
# Stamp version.
31-
# Mirror migration 42's labeled manifest. A label-less apply by the same field
32-
# manager (kubectl create | kubectl apply) would strip the
33-
# platform.cozystack.io/no-delete label migration 42 added, dropping
34-
# cozystack-version out of the cozystack-no-delete-guardrail
35-
# ValidatingAdmissionPolicy. templates/cozystack-version.yaml only re-renders the
36-
# labeled ConfigMap on first install, so the label must be carried inline here.
37-
kubectl apply --filename - <<EOF
38-
apiVersion: v1
39-
kind: ConfigMap
40-
metadata:
41-
name: cozystack-version
42-
namespace: cozy-system
43-
labels:
44-
platform.cozystack.io/no-delete: "true"
45-
data:
46-
version: "44"
47-
EOF
33+
# Stamp version. The labeled manifest lives in the shared helper so it cannot
34+
# drift back to a label-less apply that strips platform.cozystack.io/no-delete.
35+
stamp_cozystack_version 44

‎packages/core/platform/images/migrations/migrations/44‎

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
set -euo pipefail
1212

13+
# shellcheck source=lib/cozystack-version.sh
14+
. "$(dirname "$0")/lib/cozystack-version.sh"
15+
1316
if kubectl get deployment flux-tenants -n cozy-fluxcd >/dev/null 2>&1; then
1417
# No early-close (head) in the pipe: under pipefail a large name list would
1518
# SIGPIPE kubectl and abort the migration.
@@ -23,21 +26,6 @@ if kubectl get deployment flux-tenants -n cozy-fluxcd >/dev/null 2>&1; then
2326
fi
2427
fi
2528

26-
# Stamp version.
27-
# Mirror migration 43's labeled manifest. A label-less apply by the same field
28-
# manager would strip the platform.cozystack.io/no-delete label, dropping
29-
# cozystack-version out of the cozystack-no-delete-guardrail
30-
# ValidatingAdmissionPolicy. templates/cozystack-version.yaml only re-renders
31-
# the labeled ConfigMap on first install, so the label must be carried inline
32-
# here.
33-
kubectl apply --filename - <<EOF
34-
apiVersion: v1
35-
kind: ConfigMap
36-
metadata:
37-
name: cozystack-version
38-
namespace: cozy-system
39-
labels:
40-
platform.cozystack.io/no-delete: "true"
41-
data:
42-
version: "45"
43-
EOF
29+
# Stamp version. The labeled manifest lives in the shared helper so it cannot
30+
# drift back to a label-less apply that strips platform.cozystack.io/no-delete.
31+
stamp_cozystack_version 45
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# shellcheck shell=sh
2+
# Shared helper for stamping the cozystack-version ConfigMap during migrations.
3+
#
4+
# The ConfigMap MUST carry the platform.cozystack.io/no-delete=true label so the
5+
# cozystack-no-delete-guardrail ValidatingAdmissionPolicy guards it against
6+
# deletion. Migrations apply it under the default kubectl field manager; a
7+
# label-less apply by that same manager would strip the label (the failure mode
8+
# migration 42 first fixed). templates/cozystack-version.yaml renders the
9+
# labeled ConfigMap only on first install (lookup-if-not), so every upgrade-time
10+
# stamp must re-emit the label by hand.
11+
#
12+
# Centralizing the manifest here means migration 45+ cannot drift back to a
13+
# label-less apply: every go-forward stamp (migration 42 onward and the
14+
# bootstrap path in run-migrations.sh) sources this file and calls the helper
15+
# instead of copy-pasting a heredoc. Migrations 1-41 predate the no-delete
16+
# label and stamp label-less on purpose; migration 42 backfills the label on
17+
# any upgrade path through them, so they are left frozen. The
18+
# cozystack-version-stamp.bats suite pins this — no migration numbered >= 42
19+
# (nor run-migrations.sh) may stamp cozystack-version without the helper.
20+
#
21+
# Sourced, not executed. Each migrations/<N> script (42+) does:
22+
# . "$(dirname "$0")/lib/cozystack-version.sh"
23+
# and run-migrations.sh sources /migrations/lib/cozystack-version.sh.
24+
25+
# render_cozystack_version_manifest <version>
26+
# Emit the labeled cozystack-version ConfigMap manifest on stdout. Pure (no
27+
# kubectl), so it is unit-testable in isolation. The namespace defaults to
28+
# cozy-system and is overridable via $NAMESPACE.
29+
render_cozystack_version_manifest() {
30+
: "${1:?render_cozystack_version_manifest: version argument required}"
31+
cat <<EOF
32+
apiVersion: v1
33+
kind: ConfigMap
34+
metadata:
35+
name: cozystack-version
36+
namespace: ${NAMESPACE:-cozy-system}
37+
labels:
38+
platform.cozystack.io/no-delete: "true"
39+
data:
40+
version: "$1"
41+
EOF
42+
}
43+
44+
# stamp_cozystack_version <version>
45+
# Render and apply the labeled manifest under the default kubectl field manager.
46+
stamp_cozystack_version() {
47+
# Guard the version here too, not only in render: without pipefail a render
48+
# failure would not abort the pipeline and kubectl would apply empty input.
49+
: "${1:?stamp_cozystack_version: version argument required}"
50+
render_cozystack_version_manifest "$1" | kubectl apply --filename -
51+
}

‎packages/core/platform/images/migrations/run-migrations.sh‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/bin/sh
22
set -euo pipefail
33

4+
# shellcheck source=migrations/lib/cozystack-version.sh
5+
. /migrations/lib/cozystack-version.sh
6+
47
NAMESPACE="${NAMESPACE:-cozy-system}"
58
CURRENT_VERSION="${CURRENT_VERSION:-0}"
69
TARGET_VERSION="${TARGET_VERSION:-0}"
@@ -10,9 +13,11 @@ echo "Starting migrations from version $CURRENT_VERSION to $TARGET_VERSION"
1013
# Check if ConfigMap exists
1114
if ! kubectl get configmap --namespace "$NAMESPACE" cozystack-version >/dev/null 2>&1; then
1215
echo "ConfigMap cozystack-version does not exist, creating it with version $TARGET_VERSION"
13-
kubectl create configmap --namespace "$NAMESPACE" cozystack-version \
14-
--from-literal=version="$TARGET_VERSION" \
15-
--dry-run=client --output yaml | kubectl apply --filename -
16+
# Stamp via the shared helper so the bootstrap ConfigMap carries the
17+
# platform.cozystack.io/no-delete label, matching every go-forward stamp
18+
# (migration 42 onward; migrations 1-41 stamp label-less and are backfilled
19+
# by migration 42, so they are intentionally left as-is).
20+
stamp_cozystack_version "$TARGET_VERSION"
1621
echo "ConfigMap created with version $TARGET_VERSION"
1722
exit 0
1823
fi

0 commit comments

Comments
 (0)