|
| 1 | +#!/bin/bash |
| 2 | +# Migration 43 --> 44 |
| 3 | +# Pin the implicit default "md0" node group explicitly on existing tenant |
| 4 | +# Kubernetes clusters. |
| 5 | +# |
| 6 | +# The kubernetes app chart used to ship a default nodeGroups.md0 in |
| 7 | +# values.yaml, which Helm merged into every cluster's values. The default is |
| 8 | +# now applied only when nodeGroups is empty (see the kubernetes chart helper |
| 9 | +# "kubernetes.nodeGroups"). Clusters that defined their own nodeGroups without |
| 10 | +# md0 were nonetheless getting md0 via the merge, and md0 is tracked in their |
| 11 | +# Helm release manifest; without this migration the new chart would prune the |
| 12 | +# live md0 MachineDeployment (→ CAPI MachineSet/Machine/node deletion) on the |
| 13 | +# next reconcile. We pin md0 explicitly to preserve the current node topology. |
| 14 | +# Operators may remove it afterwards on clusters that do not need it. |
| 15 | +# |
| 16 | +# Idempotent: clusters that already define md0 are left untouched. |
| 17 | +# Best-effort: a failed patch is logged and leaves the version stamp at 43 so |
| 18 | +# the migration retries on the next platform upgrade. |
| 19 | + |
| 20 | +set -euo pipefail |
| 21 | + |
| 22 | +DRY_RUN="${MIGRATION_DRY_RUN:-0}" |
| 23 | +FAILURES=0 |
| 24 | +PATCHED=0 |
| 25 | +SKIPPED=0 |
| 26 | + |
| 27 | +# Default md0 group, matching the chart's built-in default (kubernetes.nodeGroups). |
| 28 | +MD0_JSON='{"minReplicas":0,"maxReplicas":10,"instanceType":"u1.medium","diskSize":"20Gi","storageClass":"","roles":["ingress-nginx"],"resources":{},"gpus":[],"kubelet":{}}' |
| 29 | + |
| 30 | +# Add .spec.nodeGroups.md0 only when nodeGroups exists and md0 is absent. |
| 31 | +JQ_TRANSFORM=" |
| 32 | + if (.spec.nodeGroups | type) == \"object\" and (.spec.nodeGroups | has(\"md0\") | not) |
| 33 | + then .spec.nodeGroups.md0 = ${MD0_JSON} |
| 34 | + else . end |
| 35 | +" |
| 36 | + |
| 37 | +list_objects() { |
| 38 | + local kind="$1" out |
| 39 | + if ! out=$(kubectl get "$kind" --all-namespaces \ |
| 40 | + --output 'jsonpath={range .items[*]}{.metadata.namespace}{"\t"}{.metadata.name}{"\n"}{end}' \ |
| 41 | + 2>&1); then |
| 42 | + # CRD not installed (kubernetes app type never registered) → nothing to do. |
| 43 | + case "$out" in |
| 44 | + *"the server doesn't have a resource type"*|*"no matches for kind"*|*"NotFound"*) return 0 ;; |
| 45 | + esac |
| 46 | + echo "ERROR: kubectl get $kind failed: $out" >&2 |
| 47 | + return 1 |
| 48 | + fi |
| 49 | + printf '%s' "$out" |
| 50 | +} |
| 51 | + |
| 52 | +patch_md0() { |
| 53 | + local ns="$1" name="$2" obj new patch kubectl_err |
| 54 | + |
| 55 | + obj=$(kubectl get kuberneteses.apps.cozystack.io --namespace "$ns" "$name" --output json 2>/dev/null || true) |
| 56 | + if [ -z "$obj" ]; then SKIPPED=$((SKIPPED + 1)); return 0; fi |
| 57 | + |
| 58 | + new=$(printf '%s' "$obj" | jq -c "$JQ_TRANSFORM") |
| 59 | + if [ "$(printf '%s' "$obj" | jq -c .spec.nodeGroups)" = "$(printf '%s' "$new" | jq -c .spec.nodeGroups)" ]; then |
| 60 | + echo "SKIP $ns/$name: md0 already defined (or no nodeGroups)" |
| 61 | + SKIPPED=$((SKIPPED + 1)) |
| 62 | + return 0 |
| 63 | + fi |
| 64 | + |
| 65 | + patch=$(printf '%s' "$new" | jq -c '{spec: {nodeGroups: .spec.nodeGroups}}') |
| 66 | + |
| 67 | + if [ "$DRY_RUN" = "1" ]; then |
| 68 | + echo "[dry-run] would pin md0 on $ns/$name" |
| 69 | + PATCHED=$((PATCHED + 1)) |
| 70 | + return 0 |
| 71 | + fi |
| 72 | + |
| 73 | + if kubectl_err=$(printf '%s' "$patch" | kubectl patch kuberneteses.apps.cozystack.io \ |
| 74 | + --namespace "$ns" "$name" --type merge --patch-file /dev/stdin 2>&1 >/dev/null); then |
| 75 | + echo "Pinned md0 on $ns/$name" |
| 76 | + PATCHED=$((PATCHED + 1)) |
| 77 | + else |
| 78 | + echo "WARN: failed to patch $ns/$name: ${kubectl_err:-unknown error}" >&2 |
| 79 | + FAILURES=$((FAILURES + 1)) |
| 80 | + fi |
| 81 | +} |
| 82 | + |
| 83 | +echo "==> Pinning implicit md0 node group on kuberneteses.apps.cozystack.io" |
| 84 | +items=$(list_objects kuberneteses.apps.cozystack.io) || exit 1 |
| 85 | +while IFS=$'\t' read -r ns name; do |
| 86 | + [ -z "$ns" ] && continue |
| 87 | + patch_md0 "$ns" "$name" |
| 88 | +done <<<"$items" |
| 89 | + |
| 90 | +echo "==> Summary: pinned=$PATCHED skipped=$SKIPPED failures=$FAILURES" | tee /dev/stderr |
| 91 | + |
| 92 | +if [ "$DRY_RUN" = "1" ]; then |
| 93 | + echo "Dry-run complete; version stamp not advanced" |
| 94 | + exit 0 |
| 95 | +fi |
| 96 | + |
| 97 | +if [ "$FAILURES" -gt 0 ]; then |
| 98 | + echo "WARN: $FAILURES patch(es) failed; leaving CURRENT_VERSION at 43 for retry on next upgrade." |
| 99 | + exit 0 |
| 100 | +fi |
| 101 | + |
| 102 | +kubectl create configmap --namespace cozy-system cozystack-version \ |
| 103 | + --from-literal version=44 --dry-run=client --output yaml \ |
| 104 | + | kubectl apply --filename - |
0 commit comments