|
| 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 |
0 commit comments