From b0faab56a08fb432076be30711fc111c591d0a73 Mon Sep 17 00:00:00 2001 From: Aleksei Sviridkin Date: Mon, 16 Nov 2026 13:19:28 +0300 Subject: [PATCH] fix(capi-operator): keep legacy selector labels off the bridge bundles On a v1.10 installation the kubeadm BootstrapProvider still selects its repository by bootstrap-components and bootstrap-provider alone. The bridge bootstrap ConfigMap carried both labels, so that selector matched two ConfigMaps as soon as the operator chart was upgraded. When the hook then moved core to the bridge, cluster-api-operator resolved kubeadm's repository for the upgrade, refused the ambiguous selector, and core never left v1.10; every retry hit the same state. The bridge ConfigMaps now carry only the version label, and the hook selects a bridge by that label alone, narrowing a selector that still keeps legacy labels. Assisted-by: LLM Signed-off-by: Aleksei Sviridkin --- hack/capi-upgrade-chart_test.bats | 9 +++++++- hack/capi-upgrade_test.bats | 21 ++++++++++++------- .../templates/upgrade-bundles.yaml | 7 +++---- .../files/upgrade-providers.sh | 12 +++++------ 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/hack/capi-upgrade-chart_test.bats b/hack/capi-upgrade-chart_test.bats index 03e76ba98..ac7b0c5c5 100644 --- a/hack/capi-upgrade-chart_test.bats +++ b/hack/capi-upgrade-chart_test.bats @@ -35,6 +35,13 @@ setup() { done } +@test "intermediate bundles carry no label a legacy version-less selector matches" { + local rendered="$BATS_TEST_TMPDIR/operator.yaml" + helm template capi-operator "$REPO_ROOT/packages/system/capi-operator" --namespace test-capi > "$rendered" + yq --output-format=json --no-doc 'select(.kind == "ConfigMap" and .metadata.annotations."helm.sh/resource-policy" == "keep")' "$rendered" | + jq --slurp --exit-status 'length == 2 and all(.[]; .metadata.labels == {"provider.cluster.x-k8s.io/version": .metadata.name})' +} + @test "the hook has no cluster-wide or secret permissions" { local roles roles="$(yq --output-format=json --no-doc 'select(.kind == "Role") | .rules' "$RENDERED")" @@ -125,7 +132,7 @@ repository_matches_request() { bridge="$(< "$CORE/files/bridge-version")" jq --arg bridge "$bridge" 'map(if .kind == "CoreProvider" or (.kind == "BootstrapProvider" and .metadata.name == "kubeadm") then .spec.version = ($bridge + (if .kind == "CoreProvider" then "-core" else "-bootstrap" end)) | - .spec.fetchConfig.selector.matchLabels["provider.cluster.x-k8s.io/version"] = .spec.version + .spec.fetchConfig.selector.matchLabels = {"provider.cluster.x-k8s.io/version": .spec.version} else . end)' "$BATS_TEST_TMPDIR/repositories.json" > "$BATS_TEST_TMPDIR/bridge.json" mv "$BATS_TEST_TMPDIR/bridge.json" "$BATS_TEST_TMPDIR/repositories.json" repository_matches_request diff --git a/hack/capi-upgrade_test.bats b/hack/capi-upgrade_test.bats index 9876d6b58..1af6d0556 100644 --- a/hack/capi-upgrade_test.bats +++ b/hack/capi-upgrade_test.bats @@ -21,7 +21,7 @@ setup() { jq --null-input ' def provider($name; $suffix): { metadata: {name: $name, uid: $name, generation: 1}, - spec: {version: ("v1.10.1-" + $suffix), fetchConfig: {selector: {matchLabels: {($suffix + "-components"): "cozy"}}}}, + spec: {version: ("v1.10.1-" + $suffix), fetchConfig: {selector: {matchLabels: ({($suffix + "-components"): "cozy"} + if $suffix == "bootstrap" then {"bootstrap-provider": "kubeadm"} else {} end)}}}, status: {installedVersion: ("v1.10.1-" + $suffix), observedGeneration: 1, conditions: [{type: "Ready", status: "True"}]} }; def deployment: { @@ -73,7 +73,7 @@ accepted_patches() { } @test "an interrupted transition resumes without downgrading core" { - fixture '.providers["cluster-api"].spec.version = "v1.13.8-core" | .providers["cluster-api"].spec.fetchConfig.selector.matchLabels["provider.cluster.x-k8s.io/version"] = "v1.13.8-core" | .providers["cluster-api"].status.installedVersion = "v1.13.8-core"' + fixture '.providers["cluster-api"].spec.version = "v1.13.8-core" | .providers["cluster-api"].spec.fetchConfig.selector.matchLabels = {"provider.cluster.x-k8s.io/version": "v1.13.8-core"} | .providers["cluster-api"].status.installedVersion = "v1.13.8-core"' run bash "$UPGRADE_SCRIPT" [ "$status" -eq 0 ] [ "$(accepted_patches)" = '[{"name":"kubeadm","version":"v1.13.8-bootstrap"}]' ] @@ -209,27 +209,34 @@ accepted_patches() { any(.patch[]; .op == "test" and .path == "/spec/version") and any(.patch[]; .op == "test" and .path == "/spec/fetchConfig") and any(.patch[]; .path == "/spec/fetchConfig" and .op == "replace" and - .value.selector.matchLabels["provider.cluster.x-k8s.io/version"] == - (if $call.name == "cluster-api" then "v1.13.8-core" else "v1.13.8-bootstrap" end)))' "$CAPI_TEST_LOG" + .value.selector.matchLabels == {"provider.cluster.x-k8s.io/version": + (if $call.name == "cluster-api" then "v1.13.8-core" else "v1.13.8-bootstrap" end)}))' "$CAPI_TEST_LOG" } @test "already selected bridges repair stale selectors inside the supported window" { fixture '.providers |= with_entries(.value.spec.version = ("v1.13.8-" + (if .key == "cluster-api" then "core" else "bootstrap" end)) | .value.status.installedVersion = .value.spec.version)' run bash "$UPGRADE_SCRIPT" [ "$status" -eq 0 ] - jq --exit-status 'all(.providers[]; .spec.fetchConfig.selector.matchLabels["provider.cluster.x-k8s.io/version"] == .spec.version)' "$CAPI_TEST_STATE" + jq --exit-status 'all(.providers[]; .spec.fetchConfig.selector.matchLabels == {"provider.cluster.x-k8s.io/version": .spec.version})' "$CAPI_TEST_STATE" +} + +@test "a bridge selector that keeps legacy labels is narrowed to the version label" { + fixture '.providers |= with_entries(.value.spec.version = ("v1.13.8-" + (if .key == "cluster-api" then "core" else "bootstrap" end)) | .value.status.installedVersion = .value.spec.version | .value.spec.fetchConfig.selector.matchLabels["provider.cluster.x-k8s.io/version"] = .value.spec.version)' + run bash "$UPGRADE_SCRIPT" + [ "$status" -eq 0 ] + jq --exit-status 'all(.providers[]; .spec.fetchConfig.selector.matchLabels == {"provider.cluster.x-k8s.io/version": .spec.version})' "$CAPI_TEST_STATE" } @test "already selected bridges repair missing fetchConfig without changing other spec fields" { fixture '.providers |= with_entries(.value.spec.version = ("v1.13.8-" + (if .key == "cluster-api" then "core" else "bootstrap" end)) | .value.status.installedVersion = .value.spec.version | .value.spec.deployment = {replicas: 2} | del(.value.spec.fetchConfig))' run bash "$UPGRADE_SCRIPT" [ "$status" -eq 0 ] - jq --exit-status 'all(.providers[]; .spec.deployment.replicas == 2 and .spec.fetchConfig.selector.matchLabels["provider.cluster.x-k8s.io/version"] == .spec.version)' "$CAPI_TEST_STATE" + jq --exit-status 'all(.providers[]; .spec.deployment.replicas == 2 and .spec.fetchConfig.selector.matchLabels == {"provider.cluster.x-k8s.io/version": .spec.version})' "$CAPI_TEST_STATE" jq --slurp --exit-status 'all(.[] | select(.accepted); any(.patch[]; .op == "test" and .path == "/spec") and any(.patch[]; .op == "add" and .path == "/spec/fetchConfig"))' "$CAPI_TEST_LOG" } @test "a matching bridge selector is an idempotent no-op" { - fixture '.providers |= with_entries(.value.spec.version = ("v1.13.8-" + (if .key == "cluster-api" then "core" else "bootstrap" end)) | .value.status.installedVersion = .value.spec.version | .value.spec.fetchConfig.selector.matchLabels["provider.cluster.x-k8s.io/version"] = .value.spec.version)' + fixture '.providers |= with_entries(.value.spec.version = ("v1.13.8-" + (if .key == "cluster-api" then "core" else "bootstrap" end)) | .value.status.installedVersion = .value.spec.version | .value.spec.fetchConfig.selector.matchLabels = {"provider.cluster.x-k8s.io/version": .value.spec.version})' run bash "$UPGRADE_SCRIPT" [ "$status" -eq 0 ] [ "$(accepted_patches)" = '[]' ] diff --git a/packages/system/capi-operator/templates/upgrade-bundles.yaml b/packages/system/capi-operator/templates/upgrade-bundles.yaml index 4ad5e49c0..1d871e1fb 100644 --- a/packages/system/capi-operator/templates/upgrade-bundles.yaml +++ b/packages/system/capi-operator/templates/upgrade-bundles.yaml @@ -1,12 +1,13 @@ {{- $bridge := required "intermediate provider version is missing" (.Files.Get "files/bridge-version" | trim) -}} -{{/* Keep repositories available for providers left at the intermediate version. */}} +{{/* Keep repositories available for providers left at the intermediate version. +Only the version label: a live v1.10 selector has none, and the operator fails every +provider upgrade while any installed provider's selector matches two ConfigMaps. */}} apiVersion: v1 kind: ConfigMap metadata: name: {{ $bridge }}-core labels: provider.cluster.x-k8s.io/version: {{ $bridge }}-core - core-components: cozy annotations: provider.cluster.x-k8s.io/compressed: "true" helm.sh/resource-policy: keep @@ -21,8 +22,6 @@ metadata: name: {{ $bridge }}-bootstrap labels: provider.cluster.x-k8s.io/version: {{ $bridge }}-bootstrap - bootstrap-components: cozy - bootstrap-provider: kubeadm annotations: provider.cluster.x-k8s.io/compressed: "true" helm.sh/resource-policy: keep diff --git a/packages/system/capi-providers-core/files/upgrade-providers.sh b/packages/system/capi-providers-core/files/upgrade-providers.sh index 392df4775..c4f851b7c 100644 --- a/packages/system/capi-providers-core/files/upgrade-providers.sh +++ b/packages/system/capi-providers-core/files/upgrade-providers.sh @@ -76,7 +76,7 @@ fi bridge_selector_needs_repair() { jq --exit-status --arg version "$BRIDGE_VERSION-$2" ' .spec.version == $version and - .spec.fetchConfig.selector.matchLabels["provider.cluster.x-k8s.io/version"] != $version' <<< "$1" >/dev/null + .spec.fetchConfig.selector.matchLabels != {"provider.cluster.x-k8s.io/version": $version}' <<< "$1" >/dev/null } if ((target_minor - core_minor <= 3 && target_minor - bootstrap_minor <= 3)) && @@ -136,13 +136,11 @@ stage_provider() { expected="$BRIDGE_VERSION-$suffix" fi if [[ "$requested" != "$expected" ]] || bridge_selector_needs_repair "$object" "$suffix"; then - patch="$(jq --compact-output --arg uid "$uid" --arg old "$requested" --arg next "$expected" --arg suffix "$suffix" ' + # The bridge bundles carry only the version label, so a selector keeping a + # legacy label would match nothing. + patch="$(jq --compact-output --arg uid "$uid" --arg old "$requested" --arg next "$expected" ' .spec as $spec | - (if $spec.fetchConfig == null then - {selector: {matchLabels: {($suffix + "-components"): "cozy"}}} | - if $suffix == "bootstrap" then .selector.matchLabels["bootstrap-provider"] = "kubeadm" else . end - else $spec.fetchConfig end | - .selector.matchLabels["provider.cluster.x-k8s.io/version"] = $next) as $fetch | + ($spec.fetchConfig // {} | .selector = {matchLabels: {"provider.cluster.x-k8s.io/version": $next}}) as $fetch | [ {op: "test", path: "/metadata/uid", value: $uid}, {op: "test", path: "/spec/version", value: $old},