Skip to content

Commit 50f78ce

Browse files
IvanHuntersmyasnikovdaniil
authored andcommitted
fix(platform): backfill tenant ancestor labels (backport #2912)
Backfill tenant.cozystack.io/<ancestor> labels on existing tenant namespaces so clusters heal on upgrade without waiting on each tenant HelmRelease to re-reconcile (#2810/#2171). Backport of migration 49 from #2912, adapted for release-1.5: - migration file 49 -> 45 (release-1.5 is at targetVersion 45; next free slot) - targetVersion 45 -> 46, not main's 50, so the missing-file guard does not fail on the absent 46/47/48/49 - stamp via the inline labeled-ConfigMap apply used by release-1.5 migrations, since lib/cozystack-version.sh does not exist on this branch Best-effort and idempotent: kubectl label --overwrite adds only the listed labels, and per-namespace failures are tolerated so a transient apiserver error cannot abort the platform upgrade. Signed-off-by: Myasnikov Daniil <[email protected]>
1 parent 6ce0907 commit 50f78ce

2 files changed

Lines changed: 83 additions & 1 deletion

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/bin/sh
2+
# Migration 45 --> 46
3+
# Backfill tenant.cozystack.io/<ancestor> labels on existing tenant namespaces.
4+
#
5+
# Tenant chart versions before this release derived the ancestor label chain by
6+
# splitting .Release.Namespace, which only ever reached one level up and dropped
7+
# the transitive ancestors (notably tenant.cozystack.io/tenant-root) for tenants
8+
# at depth >= 2. That broke the <ancestor>-egress CiliumClusterwideNetworkPolicy
9+
# (packages/apps/tenant/templates/networkpolicy.yaml): it grants an ancestor
10+
# reachability to a descendant namespace only when the descendant carries
11+
# tenant.cozystack.io/<ancestor>, so a missing tenant-root label left the root
12+
# ingress controller unable to reach nested tenants (upstream timeouts, #2810 /
13+
# #2171).
14+
#
15+
# The chart fix makes every tenant namespace self-compute its full ancestor
16+
# chain from its own name on the next reconcile. This migration backfills the
17+
# labels immediately so clusters heal without waiting on each tenant
18+
# HelmRelease to re-reconcile (e.g. suspended or failed releases).
19+
#
20+
# Best-effort by design: the worst case if a relabel is skipped is the
21+
# pre-existing missing-label bug this PR set out to fix, which is strictly no
22+
# worse than the status quo. A transient apiserver hiccup must therefore NOT
23+
# abort the whole platform upgrade, so the namespace list and per-namespace
24+
# relabel tolerate failure (|| true) rather than exiting non-zero. Idempotent:
25+
# kubectl label --overwrite only adds/refreshes the listed labels and never
26+
# removes others.
27+
28+
set -euo pipefail
29+
30+
# Every tenant lives in a namespace named tenant-<...>. tenant-root is the
31+
# implicit chain root and carries no tenant.cozystack.io/* label (the chart
32+
# omits the label block for it), so it is skipped. The `|| true` keeps a
33+
# transient apiserver error from aborting the upgrade.
34+
tenant_ns=$(kubectl get namespaces \
35+
-o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' \
36+
| grep '^tenant-' || true)
37+
38+
for ns in $tenant_ns; do
39+
[ "$ns" = "tenant-root" ] && continue
40+
41+
# Build the ancestor label list from the namespace name: every tenant
42+
# descends from tenant-root, then each progressive dash-prefix of the name
43+
# (skipping the bare "tenant" segment) is a real ancestor namespace, the last
44+
# being the tenant itself. e.g. tenant-ktj-htdev -> tenant-root, tenant-ktj,
45+
# tenant-ktj-htdev.
46+
labels="tenant.cozystack.io/tenant-root="
47+
acc=""
48+
IFS='-'
49+
for seg in $ns; do
50+
if [ -z "$acc" ]; then
51+
acc="$seg"
52+
else
53+
acc="$acc-$seg"
54+
fi
55+
[ "$acc" = "tenant" ] && continue
56+
labels="$labels tenant.cozystack.io/$acc="
57+
done
58+
unset IFS
59+
60+
echo "Backfilling ancestor labels on namespace $ns: $labels"
61+
# shellcheck disable=SC2086 # word splitting of $labels into flags is intended
62+
kubectl label namespace "$ns" $labels --overwrite || true
63+
done
64+
65+
# Stamp version.
66+
# Mirror migration 43's labeled manifest. A label-less apply by the same field
67+
# manager would strip the platform.cozystack.io/no-delete label, dropping
68+
# cozystack-version out of the cozystack-no-delete-guardrail
69+
# ValidatingAdmissionPolicy. templates/cozystack-version.yaml only re-renders
70+
# the labeled ConfigMap on first install, so the label must be carried inline
71+
# here.
72+
kubectl apply --filename - <<EOF
73+
apiVersion: v1
74+
kind: ConfigMap
75+
metadata:
76+
name: cozystack-version
77+
namespace: cozy-system
78+
labels:
79+
platform.cozystack.io/no-delete: "true"
80+
data:
81+
version: "46"
82+
EOF

‎packages/core/platform/values.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ sourceRef:
66
migrations:
77
enabled: false
88
image: ghcr.io/cozystack/cozystack/platform-migrations:v1.5.2@sha256:5d601b0709c578945f2e04d655b77d29008c94666a4350e67960c9f6bf9d2bde
9-
targetVersion: 45
9+
targetVersion: 46
1010
# Bundle deployment configuration
1111
bundles:
1212
system:

0 commit comments

Comments
 (0)