|
| 1 | +#!/bin/bash |
| 2 | +# Migration 41 --> 42 |
| 3 | +# Rename nodeGroups[*].ephemeralStorage to nodeGroups[*].diskSize in every |
| 4 | +# kuberneteses.apps.cozystack.io Application CR. |
| 5 | +# |
| 6 | +# PR #2454 renamed the field in the kubernetes app chart with no fallback. |
| 7 | +# Without this migration, upgraded clusters either fail to reconcile (hard fail |
| 8 | +# behavior) or silently lose their disk size (silent-drop behavior). |
| 9 | +# |
| 10 | +# Idempotent: re-running on already-migrated values is a no-op |
| 11 | +# (has("ephemeralStorage") is false on the post-migration shape). |
| 12 | +# Best-effort: a failed patch is logged and leaves the version stamp at 41 so |
| 13 | +# the migration retries on the next platform upgrade. |
| 14 | + |
| 15 | +set -euo pipefail |
| 16 | + |
| 17 | +DRY_RUN="${MIGRATION_DRY_RUN:-0}" |
| 18 | +FAILURES=0 |
| 19 | +PATCHED=0 |
| 20 | +SKIPPED=0 |
| 21 | + |
| 22 | +# Emit ephemeralStorage: null rather than dropping the key, because JSON merge |
| 23 | +# patch only deletes a field when an explicit null is present in the patch |
| 24 | +# document. A patch that simply omits a key leaves the existing key intact. |
| 25 | +JQ_TRANSFORM=' |
| 26 | + walk( |
| 27 | + if type == "object" and has("nodeGroups") and (.nodeGroups | type) == "object" then |
| 28 | + .nodeGroups |= with_entries( |
| 29 | + .value |= ( |
| 30 | + if type == "object" and has("ephemeralStorage") then |
| 31 | + (if has("diskSize") and (.diskSize // "") != "" then . |
| 32 | + else .diskSize = .ephemeralStorage end) |
| 33 | + | .ephemeralStorage = null |
| 34 | + else . end |
| 35 | + ) |
| 36 | + ) |
| 37 | + else . end |
| 38 | + ) |
| 39 | +' |
| 40 | + |
| 41 | +patch_object() { |
| 42 | + local kind="$1" ns="$2" name="$3" selector="$4" |
| 43 | + local obj current new patch |
| 44 | + |
| 45 | + obj=$(kubectl get "$kind" --namespace "$ns" "$name" --output json 2>/dev/null || true) |
| 46 | + if [ -z "$obj" ]; then SKIPPED=$((SKIPPED + 1)); return 0; fi |
| 47 | + |
| 48 | + current=$(printf '%s' "$obj" | jq -c "$selector // null") |
| 49 | + if [ "$current" = "null" ]; then SKIPPED=$((SKIPPED + 1)); return 0; fi |
| 50 | + |
| 51 | + new=$(printf '%s' "$current" | jq -c "$JQ_TRANSFORM") |
| 52 | + if [ "$current" = "$new" ]; then SKIPPED=$((SKIPPED + 1)); return 0; fi |
| 53 | + |
| 54 | + patch=$(jq --null-input --argjson val "$new" --arg path "${selector#.}" ' |
| 55 | + def to_path: split("."); |
| 56 | + {} | setpath($path | to_path; $val) |
| 57 | + ') |
| 58 | + |
| 59 | + if [ "$DRY_RUN" = "1" ]; then |
| 60 | + echo "[dry-run] would patch $kind $ns/$name ($selector)" |
| 61 | + diff <(printf '%s' "$current" | jq --sort-keys .) \ |
| 62 | + <(printf '%s' "$new" | jq --sort-keys .) || true |
| 63 | + PATCHED=$((PATCHED + 1)) |
| 64 | + return 0 |
| 65 | + fi |
| 66 | + |
| 67 | + local kubectl_err |
| 68 | + if kubectl_err=$(printf '%s' "$patch" | kubectl patch "$kind" --namespace "$ns" "$name" \ |
| 69 | + --type merge --patch-file /dev/stdin 2>&1 >/dev/null); then |
| 70 | + echo "Patched $kind $ns/$name ($selector)" |
| 71 | + PATCHED=$((PATCHED + 1)) |
| 72 | + else |
| 73 | + echo "WARN: failed to patch $kind $ns/$name ($selector): ${kubectl_err:-unknown error}" >&2 |
| 74 | + FAILURES=$((FAILURES + 1)) |
| 75 | + fi |
| 76 | +} |
| 77 | + |
| 78 | +list_objects() { |
| 79 | + local kind="$1" out |
| 80 | + if ! out=$(kubectl get "$kind" --all-namespaces \ |
| 81 | + --output 'jsonpath={range .items[*]}{.metadata.namespace}{"\t"}{.metadata.name}{"\n"}{end}' \ |
| 82 | + 2>&1); then |
| 83 | + case "$out" in |
| 84 | + *"the server doesn't have a resource type"*|*"no matches for kind"*|*"NotFound"*) return 0 ;; |
| 85 | + esac |
| 86 | + echo "ERROR: kubectl get $kind failed: $out" >&2 |
| 87 | + return 1 |
| 88 | + fi |
| 89 | + printf '%s' "$out" |
| 90 | +} |
| 91 | + |
| 92 | +echo "==> Migrating kuberneteses.apps.cozystack.io" |
| 93 | +items=$(list_objects kuberneteses.apps.cozystack.io) || exit 1 |
| 94 | +while IFS=$'\t' read -r ns name; do |
| 95 | + [ -z "$ns" ] && continue |
| 96 | + patch_object kuberneteses.apps.cozystack.io "$ns" "$name" .spec |
| 97 | +done <<<"$items" |
| 98 | + |
| 99 | +echo "==> Summary: patched=$PATCHED skipped=$SKIPPED failures=$FAILURES" | tee /dev/stderr |
| 100 | + |
| 101 | +if [ "$DRY_RUN" = "1" ]; then |
| 102 | + echo "Dry-run complete; version stamp not advanced" |
| 103 | + exit 0 |
| 104 | +fi |
| 105 | + |
| 106 | +if [ "$FAILURES" -gt 0 ]; then |
| 107 | + echo "WARN: $FAILURES patch(es) failed; leaving CURRENT_VERSION at 41 for retry on next upgrade." |
| 108 | + exit 0 |
| 109 | +fi |
| 110 | + |
| 111 | +kubectl create configmap --namespace cozy-system cozystack-version \ |
| 112 | + --from-literal version=42 --dry-run=client --output yaml \ |
| 113 | + | kubectl apply --filename - |
0 commit comments