From a6d57c6aee74a5007c5416da4209679ff48a2e8e Mon Sep 17 00:00:00 2001 From: Aleksei Sviridkin Date: Mon, 16 Nov 2026 13:23:35 +0300 Subject: [PATCH] test(capi): model the operator's cross-provider repository check The kubectl fake let a provider converge as soon as its own selector named its version, so the suite could not see a failure that comes from another provider's repository. cluster-api-operator resolves the repository of every other installed provider whenever a provider's version changes, and refuses a selector that matches more than one ConfigMap; that is how a bridge bundle carrying legacy labels blocked the core step. The fake now holds the repository ConfigMaps, the legacy v1.10 ones plus the bridge ones rendered from the capi-operator chart, and applies the operator's rule before a provider converges. Assisted-by: LLM Signed-off-by: Aleksei Sviridkin --- hack/capi-upgrade_test.bats | 29 +++++++++++++++++++++++++++-- hack/testdata/capi-upgrade/kubectl | 19 ++++++++++++++++++- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/hack/capi-upgrade_test.bats b/hack/capi-upgrade_test.bats index 1af6d0556..394d7fa7b 100644 --- a/hack/capi-upgrade_test.bats +++ b/hack/capi-upgrade_test.bats @@ -18,7 +18,7 @@ setup() { export BASH_ENV="$REPO_ROOT/hack/testdata/capi-upgrade/clock.bash" export CAPI_TEST_STATE="$BATS_TEST_TMPDIR/state.json" CAPI_TEST_LOG="$BATS_TEST_TMPDIR/calls.jsonl" : > "$CAPI_TEST_LOG" - jq --null-input ' + jq --null-input --argjson bridge "$(rendered_configmaps capi-operator templates/upgrade-bundles.yaml)" ' def provider($name; $suffix): { metadata: {name: $name, uid: $name, generation: 1}, spec: {version: ("v1.10.1-" + $suffix), fetchConfig: {selector: {matchLabels: ({($suffix + "-components"): "cozy"} + if $suffix == "bootstrap" then {"bootstrap-provider": "kubeadm"} else {} end)}}}, @@ -30,10 +30,20 @@ setup() { }; { providers: {"cluster-api": provider("cluster-api"; "core"), kubeadm: provider("kubeadm"; "bootstrap")}, - deployments: {"capi-controller-manager": deployment, "capi-kubeadm-bootstrap-controller-manager": deployment} + deployments: {"capi-controller-manager": deployment, "capi-kubeadm-bootstrap-controller-manager": deployment}, + configmaps: ([ + {name: "v1.10.1-core", labels: {"core-components": "cozy"}}, + {name: "v1.10.1-bootstrap", labels: {"bootstrap-components": "cozy", "bootstrap-provider": "kubeadm"}} + ] + $bridge) }' > "$CAPI_TEST_STATE" } +rendered_configmaps() { + helm template "$1" "$REPO_ROOT/packages/system/$1" --namespace test-capi --kube-version 1.33.0 --show-only "$2" | + yq --output-format=json --no-doc 'select(.kind == "ConfigMap") | {"name": .metadata.name, "labels": .metadata.labels}' | + jq --slurp --compact-output '.' +} + fixture() { jq "$1" "$CAPI_TEST_STATE" > "$BATS_TEST_TMPDIR/next.json" mv "$BATS_TEST_TMPDIR/next.json" "$CAPI_TEST_STATE" @@ -80,6 +90,7 @@ accepted_patches() { } @test "a final core upgrade already in progress is not reset to the bridge" { + fixture ".configmaps += $(rendered_configmaps capi-providers-core templates/configmaps.yaml)" fixture '.providers["cluster-api"].spec.version = "v1.14.4-core" | .providers["cluster-api"].status.installedVersion = "v1.13.8-core" | .pending = {"cluster-api": 2} | .providers["cluster-api"].spec.fetchConfig.selector.matchLabels["provider.cluster.x-k8s.io/version"] = "v1.14.4-core"' run bash "$UPGRADE_SCRIPT" [ "$status" -eq 0 ] @@ -87,6 +98,20 @@ accepted_patches() { [ "$(jq --raw-output '.providers["cluster-api"].status.installedVersion' "$CAPI_TEST_STATE")" = v1.14.4-core ] } +@test "a core upgrade stalls while another installed provider selects two repositories" { + fixture '.configmaps += [{name: "v1.10.2-bootstrap", labels: {"bootstrap-components": "cozy", "bootstrap-provider": "kubeadm"}}]' + export CAPI_TEST_CLOCK_JUMP=61 + run bash "$UPGRADE_SCRIPT" + [ "$status" -ne 0 ] + [[ "$output" == *"cluster-api timed out"* ]] + [ "$(accepted_patches)" = '[{"name":"cluster-api","version":"v1.13.8-core"}]' ] +} + +@test "the live legacy kubeadm selector matches only its own repository during the core step" { + jq --exit-status '.providers.kubeadm.spec.fetchConfig.selector.matchLabels as $s | + [.configmaps[] | select(.labels as $l | $s | to_entries | all(.[]; $l[.key] == .value))] | map(.name) == ["v1.10.1-bootstrap"]' "$CAPI_TEST_STATE" +} + @test "an API process cannot outlive the upgrade deadline" { fixture '.readDelay = 3' export CAPI_UPGRADE_TIMEOUT_SECONDS=1 diff --git a/hack/testdata/capi-upgrade/kubectl b/hack/testdata/capi-upgrade/kubectl index 34f1201b9..ba8c3682f 100644 --- a/hack/testdata/capi-upgrade/kubectl +++ b/hack/testdata/capi-upgrade/kubectl @@ -27,6 +27,23 @@ if resource not in resources or name not in ( entry = {"verb": verb, "resource": resource, "name": name} +def selected(selector): + return [cm for cm in state.get("configmaps", []) if all(cm["labels"].get(k) == v for k, v in selector.items())] + + +def repository_ready(name, obj): + own = selected(obj["spec"].get("fetchConfig", {}).get("selector", {}).get("matchLabels", {})) + if len(own) != 1 or own[0]["labels"].get("provider.cluster.x-k8s.io/version", own[0]["name"]) != obj["spec"]["version"]: + return False + # A version change makes cluster-api-operator resolve the repository of every + # other installed provider, refusing a selector that matches more than one ConfigMap. + return obj["status"].get("installedVersion") == obj["spec"]["version"] or all( + len(selected(other["spec"].get("fetchConfig", {}).get("selector", {}).get("matchLabels", {}))) == 1 + for other_name, other in state["providers"].items() + if other_name != name and other["status"].get("installedVersion") + ) + + def save(): state_path.write_text(json.dumps(state)) @@ -53,7 +70,7 @@ if verb == "get": pending = state["pending"][name] if pending > 0: state["pending"][name] -= 1 - elif not state.get("stalled") and obj["spec"].get("fetchConfig", {}).get("selector", {}).get("matchLabels", {}).get("provider.cluster.x-k8s.io/version") == obj["spec"]["version"]: + elif not state.get("stalled") and repository_ready(name, obj): obj["status"]["installedVersion"] = obj["spec"]["version"] obj["status"]["observedGeneration"] = obj["metadata"]["generation"] obj["status"]["conditions"] = [{"type": "Ready", "status": "True"}]