feat(platform): report the Kubernetes clusters left on a shared etcd

Existing tenant clusters keep the shared tenant etcd they run on, and
moving one is a per-cluster operator decision that the platform never
takes on an upgrade. Retiring Tenant.spec.etcd has to wait until no
cluster uses it, so the upgrade lists the clusters that still do. It
writes nothing beyond the version stamp.

Assisted-by: LLM
Signed-off-by: Timofei Larkin <lllamnyp@gmail.com>
This commit is contained in:
Timofei Larkin committed 2026-11-24 09:04:49 +01:00
1 parent 16da53637e
commit e5f8a13d5d
7 files changed
+244 -1

No files matched your search

+117
View File
@@ -0,0 +1,117 @@
#!/usr/bin/env bats
# -----------------------------------------------------------------------------
# Unit tests for platform migration 63 --> 64 (report the tenant Kubernetes
# clusters that stay on a shared tenant etcd). See
# packages/core/platform/images/migrations/migrations/63 for the mechanism.
#
# The harness (docker, jq baked onto the migrations base image, the explicit
# `return` in run_migration) is the one hack/migration-55-fluxcd-orphan.bats
# describes; see there for why each piece is load-bearing.
# -----------------------------------------------------------------------------
load test_helper
FIXTURES="$PWD/hack/testdata/migration-63-shared-etcd"
MIG_DIR="$PWD/packages/core/platform/images/migrations/migrations"
ALPINE=$(sed --quiet 's/^FROM \(alpine:[^ ]*\).*$/\1/p' \
"$PWD/packages/core/platform/images/migrations/Dockerfile" | head -1)
TESTIMG="cozystack-migration63-test:$(printf '%s' "$ALPINE" | sed 's/[^a-zA-Z0-9]/-/g')"
WORKROOT="${TMPDIR:-/tmp}/cozy-migration-63-$$"
cozy_cleanup() {
rm --recursive --force "$WORKROOT"
return 0
}
run_migration() {
_run_migration_rc=0
docker run --rm --network none \
--user "$(id --user):$(id --group)" \
--volume "$MIG_DIR:/migrations:ro" \
--volume "$FIXTURES:/fakebin:ro" \
--volume "$WORK:/work" \
--env PATH=/fakebin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
--env FAKE_CMDLOG=/work/cmdlog \
--env FAKE_STATE=/work/state \
--env NAMESPACE="${NAMESPACE-}" \
--env FAKE_NO_KAMAJI="${FAKE_NO_KAMAJI-}" \
--env FAKE_LIST_FAIL="${FAKE_LIST_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 migration 63 inside a jq-enabled" >&2
echo "build of $ALPINE, the migrations image's base." >&2
return 1
}
docker build --quiet --tag "$TESTIMG" - >/dev/null <<DOCKERFILE
FROM $ALPINE
RUN apk add --no-cache coreutils jq
DOCKERFILE
mkdir --parents "$WORKROOT"
WORK=$(mktemp --directory "$WORKROOT/XXXXXX")
mkdir --parents "$WORK/state"
cp "$FIXTURES/kcps.json" "$FIXTURES/datastores.json" "$WORK/state/"
FAKE_CMDLOG="$WORK/cmdlog"
: > "$FAKE_CMDLOG"
export NAMESPACE=cozy-system
unset FAKE_NO_KAMAJI FAKE_LIST_FAIL || true
return 0
}
@test "lists every cluster whose DataStore its own release did not render, and changes nothing" {
prep
rc=0
run_migration 63 >"$WORK/out" 2>&1 || rc=$?
cat "$WORK/out"; cat "$FAKE_CMDLOG"
[ "$rc" -eq 0 ]
grep --quiet --line-regexp --fixed-strings "3 tenant Kubernetes cluster(s) stay on a shared tenant etcd." "$WORK/out"
grep --quiet --line-regexp --fixed-strings " shared-etcd: tenant-a/kubernetes-legacy DataStore=tenant-root endpoints=etcd.tenant-root.svc:2379" "$WORK/out"
grep --quiet --line-regexp --fixed-strings " shared-etcd: tenant-b/kubernetes-nearer DataStore=tenant-b endpoints=etcd.tenant-b.svc:2379" "$WORK/out"
# A DataStore another cluster's release renders is not this cluster's own.
grep --quiet --line-regexp --fixed-strings " shared-etcd: tenant-b/kubernetes-borrowed DataStore=tenant-a.kubernetes-own endpoints=kubernetes-own-etcd.tenant-a.svc:2379" "$WORK/out"
if grep --quiet --fixed-strings "tenant-a/kubernetes-own " "$WORK/out"; then echo "FAIL: a cluster on its own etcd was reported" >&2; false; fi
# Report only: the sole write is the version stamp.
if grep --extended-regexp "KUBECTL (patch|delete|annotate|label|create|replace|edit)" "$FAKE_CMDLOG"; then echo "FAIL: the migration wrote something" >&2; false; fi
grep --quiet --line-regexp --fixed-strings "STAMP 64" "$FAKE_CMDLOG"
rm --recursive --force "$WORK"
}
@test "says so when no cluster remains on a shared etcd" {
prep
cp "$FIXTURES/none-kcps.json" "$WORK/state/kcps.json"
rc=0
run_migration 63 >"$WORK/out" 2>&1 || rc=$?
cat "$WORK/out"; cat "$FAKE_CMDLOG"
[ "$rc" -eq 0 ]
grep --quiet --fixed-strings "none remains on a shared DataStore" "$WORK/out"
grep --quiet --line-regexp --fixed-strings "STAMP 64" "$FAKE_CMDLOG"
rm --recursive --force "$WORK"
}
@test "a cluster without Kamaji only stamps the version" {
prep
export FAKE_NO_KAMAJI=1
rc=0
run_migration 63 >"$WORK/out" 2>&1 || rc=$?
cat "$WORK/out"; cat "$FAKE_CMDLOG"
[ "$rc" -eq 0 ]
if grep --quiet --extended-regexp "get (kamajicontrolplanes|datastores)" "$FAKE_CMDLOG"; then echo "FAIL: listed control planes without the CRD" >&2; false; fi
grep --quiet --line-regexp --fixed-strings "STAMP 64" "$FAKE_CMDLOG"
rm --recursive --force "$WORK"
}
@test "a failing control-plane list aborts before stamping" {
prep
export FAKE_LIST_FAIL="Error from server (InternalError): etcdserver: request timed out"
rc=0
run_migration 63 >"$WORK/out" 2>&1 || rc=$?
cat "$WORK/out"; cat "$FAKE_CMDLOG"
[ "$rc" -ne 0 ]
if grep --quiet --fixed-strings "STAMP" "$FAKE_CMDLOG"; then echo "FAIL: stamped after a failed list" >&2; false; fi
rm --recursive --force "$WORK"
}
@@ -0,0 +1,8 @@
{"items": [
{"metadata": {"name": "tenant-root", "annotations": {"meta.helm.sh/release-name": "etcd", "meta.helm.sh/release-namespace": "tenant-root"}},
"spec": {"endpoints": ["etcd.tenant-root.svc:2379"]}},
{"metadata": {"name": "tenant-b", "annotations": {"meta.helm.sh/release-name": "etcd", "meta.helm.sh/release-namespace": "tenant-b"}},
"spec": {"endpoints": ["etcd.tenant-b.svc:2379"]}},
{"metadata": {"name": "tenant-a.kubernetes-own", "annotations": {"meta.helm.sh/release-name": "kubernetes-own", "meta.helm.sh/release-namespace": "tenant-a"}},
"spec": {"endpoints": ["kubernetes-own-etcd.tenant-a.svc:2379"]}}
]}
+6
View File
@@ -0,0 +1,6 @@
{"items": [
{"metadata": {"namespace": "tenant-a", "name": "kubernetes-legacy"}, "spec": {"dataStoreName": "tenant-root"}},
{"metadata": {"namespace": "tenant-a", "name": "kubernetes-own"}, "spec": {"dataStoreName": "tenant-a.kubernetes-own"}},
{"metadata": {"namespace": "tenant-b", "name": "kubernetes-nearer"}, "spec": {"dataStoreName": "tenant-b"}},
{"metadata": {"namespace": "tenant-b", "name": "kubernetes-borrowed"}, "spec": {"dataStoreName": "tenant-a.kubernetes-own"}}
]}
+43
View File
@@ -0,0 +1,43 @@
#!/bin/sh
# Fake kubectl for hack/migration-63-shared-etcd-report.bats. It serves only
# the calls migration 63 and lib/cozystack-version.sh make, and logs each one
# to $FAKE_CMDLOG.
#
# FAKE_CMDLOG file to append a one-line record of each call to
# FAKE_STATE directory holding kcps.json and datastores.json
# FAKE_NO_KAMAJI non-empty => the KamajiControlPlane CRD is absent
# FAKE_LIST_FAIL non-empty => listing KamajiControlPlanes exits 1 with this stderr
set -u
log() { [ -n "${FAKE_CMDLOG:-}" ] && printf '%s\n' "$*" >> "$FAKE_CMDLOG"; }
log "KUBECTL $*"
case "$*" in
*"apply --filename -"*)
manifest=$(cat)
case "$manifest" in
*"kind: ConfigMap"*)
v=$(printf '%s\n' "$manifest" | sed --quiet 's/^ version: "\{0,1\}\([0-9]\{1,\}\)"\{0,1\}$/\1/p' | head --lines=1)
log "STAMP ${v:-unknown}"
exit 0
;;
esac
printf 'fake kubectl: unmodelled apply\n' >&2
exit 1
;;
*"get crd kamajicontrolplanes.controlplane.cluster.x-k8s.io"*)
[ -n "${FAKE_NO_KAMAJI:-}" ] || echo "customresourcedefinition.apiextensions.k8s.io/kamajicontrolplanes.controlplane.cluster.x-k8s.io"
exit 0
;;
*"get kamajicontrolplanes.controlplane.cluster.x-k8s.io"*)
if [ -n "${FAKE_LIST_FAIL:-}" ]; then printf '%s\n' "$FAKE_LIST_FAIL" >&2; exit 1; fi
cat "$FAKE_STATE/kcps.json"
exit 0
;;
*"get datastores.kamaji.clastix.io"*)
cat "$FAKE_STATE/datastores.json"
exit 0
;;
esac
printf 'fake kubectl: unmodelled call: %s\n' "$*" >&2
exit 1
+3
View File
@@ -0,0 +1,3 @@
{"items": [
{"metadata": {"namespace": "tenant-a", "name": "kubernetes-own"}, "spec": {"dataStoreName": "tenant-a.kubernetes-own"}}
]}
+66
View File
@@ -0,0 +1,66 @@
#!/bin/sh
# Migration 63 --> 64
# Report the tenant Kubernetes clusters that stay on a shared tenant etcd.
#
# From this release a new Kubernetes cluster renders its own EtcdCluster and
# Kamaji DataStore, while an existing one keeps the DataStore its control plane
# already names. Moving a cluster off it is a Kamaji datastore migration, which
# freezes the tenant's writes while it copies and leaves every worker kubelet
# needing a restart, so it is an operator's decision per cluster and never an
# upgrade step. This migration changes nothing: it lists the clusters that
# remain on a shared DataStore, which is what retiring Tenant.spec.etcd waits
# on.
#
# A DataStore belongs to a cluster when the cluster's own release renders it,
# which Helm records in the meta.helm.sh/release-name and release-namespace
# annotations. Reading those rather than recomputing the chart's name for the
# DataStore keeps this from drifting from the chart.
#
# Fail-closed like migration 59: migrations never re-run, so a list it could
# not read is an error rather than an empty report.
set -euo pipefail
# shellcheck source=lib/cozystack-version.sh
. "$(dirname "$0")/lib/cozystack-version.sh"
# --ignore-not-found separates a cluster without Kamaji (no output, rc 0) from
# an API error, which aborts under set -e.
crd=$(kubectl get crd kamajicontrolplanes.controlplane.cluster.x-k8s.io --ignore-not-found --output name)
if [ -z "$crd" ]; then
echo "KamajiControlPlane is not installed; no tenant Kubernetes cluster to report"
stamp_cozystack_version 64
exit 0
fi
work=$(mktemp --directory)
trap 'rm --recursive --force "$work"' EXIT
kubectl get kamajicontrolplanes.controlplane.cluster.x-k8s.io --all-namespaces --output json > "$work/kcps.json"
kubectl get datastores.kamaji.clastix.io --output json > "$work/datastores.json"
jq --raw-output --slurpfile ds "$work/datastores.json" '
($ds[0].items | map({key: .metadata.name, value: .}) | from_entries) as $byname
| .items[]
| .metadata.namespace as $ns
| .metadata.name as $name
| (.spec.dataStoreName // "") as $store
| ($byname[$store] // {}) as $ds
| ($ds.metadata.annotations // {}) as $a
| select($a["meta.helm.sh/release-name"] != $name or $a["meta.helm.sh/release-namespace"] != $ns)
| "\($ns)/\($name) DataStore=\(if $store == "" then "<none>" else $store end) endpoints=\(($ds.spec.endpoints // []) | join(","))"
' "$work/kcps.json" > "$work/shared"
if [ ! -s "$work/shared" ]; then
echo "Every tenant Kubernetes cluster runs on its own etcd; none remains on a shared DataStore"
stamp_cozystack_version 64
exit 0
fi
echo "$(wc --lines < "$work/shared" | tr --delete ' ') tenant Kubernetes cluster(s) stay on a shared tenant etcd."
echo "They keep running as they are; only clusters created from now on get an etcd of their own."
while read -r line; do
echo " shared-etcd: $line"
done < "$work/shared"
stamp_cozystack_version 64
+1 -1
View File
@@ -14,7 +14,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: 63
targetVersion: 64
# 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