From 191e2f45fa254b7f634397ee62bc247fda18315a Mon Sep 17 00:00:00 2001 From: Aleksei Sviridkin Date: Wed, 18 Nov 2026 14:55:07 +0300 Subject: [PATCH] fix(capi-providers-core): stage unfinished kubeadm installations A kubeadm installation can finish at its old requested version while core moves to the intermediate release. Ignoring its initial empty installedVersion leaves the final bootstrap chart outside its supported upgrade window. Fixes: 4d251f41221c ("fix(capi-providers-core): let a never-installed provider take the fresh path") Assisted-by: LLM Signed-off-by: Aleksei Sviridkin --- hack/capi-upgrade_test.bats | 60 ++++++++++++++++++- hack/testdata/capi-upgrade/kubectl | 2 + .../files/upgrade-providers.sh | 34 +++++++---- 3 files changed, 82 insertions(+), 14 deletions(-) diff --git a/hack/capi-upgrade_test.bats b/hack/capi-upgrade_test.bats index 9172f904a..9e6c59568 100644 --- a/hack/capi-upgrade_test.bats +++ b/hack/capi-upgrade_test.bats @@ -200,12 +200,68 @@ accepted_patches() { [ "$(accepted_patches)" = '[]' ] } -@test "a kubeadm provider that never finished installing is left to the final manifests" { +@test "a kubeadm installation completing during the core upgrade is staged to the bridge" { + fixture '.bootstrapAfterCore = .providers.kubeadm | del(.providers.kubeadm.status.installedVersion)' + run bash "$UPGRADE_SCRIPT" + [ "$status" -eq 0 ] + [ "$(accepted_patches)" = '[{"name":"cluster-api","version":"v1.13.8-core"},{"name":"kubeadm","version":"v1.13.8-bootstrap"}]' ] + [ "$(jq --raw-output '.providers.kubeadm.status.installedVersion' "$CAPI_TEST_STATE")" = v1.13.8-bootstrap ] +} + +@test "an unfinished kubeadm installation at an old requested version is staged to the bridge" { fixture 'del(.providers.kubeadm.status.installedVersion)' run bash "$UPGRADE_SCRIPT" [ "$status" -eq 0 ] + [ "$(accepted_patches)" = '[{"name":"cluster-api","version":"v1.13.8-core"},{"name":"kubeadm","version":"v1.13.8-bootstrap"}]' ] + [ "$(jq --raw-output '.providers.kubeadm.status.installedVersion' "$CAPI_TEST_STATE")" = v1.13.8-bootstrap ] +} + +@test "an unfinished kubeadm target request does not wait for the final bootstrap chart" { + fixture 'del(.providers.kubeadm.status.installedVersion) | .providers.kubeadm.spec.version = "v1.14.4-bootstrap" | .providers.kubeadm.spec.fetchConfig.selector.matchLabels = {"provider.cluster.x-k8s.io/version": "v1.14.4-bootstrap"}' + run bash "$UPGRADE_SCRIPT" + [ "$status" -eq 0 ] + [ "$(accepted_patches)" = '[{"name":"cluster-api","version":"v1.13.8-core"}]' ] + jq --exit-status '.providers.kubeadm | .spec.version == "v1.14.4-bootstrap" and (.status | has("installedVersion") | not)' "$CAPI_TEST_STATE" +} + +@test "an unfinished kubeadm request within the upgrade window is left to the final manifests" { + fixture 'del(.providers.kubeadm.status.installedVersion) | .providers.kubeadm.spec.version = "v1.13.7-bootstrap"' + run bash "$UPGRADE_SCRIPT" + [ "$status" -eq 0 ] + [ "$(accepted_patches)" = '[{"name":"cluster-api","version":"v1.13.8-core"}]' ] + jq --exit-status '.providers.kubeadm | .spec.version == "v1.13.7-bootstrap" and (.status | has("installedVersion") | not)' "$CAPI_TEST_STATE" +} + +@test "an unfinished kubeadm installation with a malformed request is rejected" { + fixture 'del(.providers.kubeadm.status.installedVersion) | .providers.kubeadm.spec.version = "invalid"' + run bash "$UPGRADE_SCRIPT" + [ "$status" -ne 0 ] + [[ "$output" == *"invalid requested version: invalid"* ]] + [ "$(accepted_patches)" = '[]' ] +} + +@test "an unfinished kubeadm installation with no requested version is rejected" { + fixture 'del(.providers.kubeadm.status.installedVersion, .providers.kubeadm.spec.version)' + run bash "$UPGRADE_SCRIPT" + [ "$status" -ne 0 ] + [[ "$output" == *"invalid requested version:"* ]] + [ "$(accepted_patches)" = '[]' ] +} + +@test "a kubeadm replacement during the core upgrade is not adopted" { + fixture '.bootstrapAfterCore = (.providers.kubeadm | .metadata.uid = "replacement") | del(.providers.kubeadm.status.installedVersion)' + run bash "$UPGRADE_SCRIPT" + [ "$status" -ne 0 ] + [[ "$output" == *"kubeadm was replaced during the upgrade"* ]] + [ "$(accepted_patches)" = '[{"name":"cluster-api","version":"v1.13.8-core"}]' ] +} + +@test "a kubeadm version installed during the core upgrade is revalidated" { + fixture '.bootstrapAfterCore = (.providers.kubeadm | .status.installedVersion = "v1.14.5-bootstrap") | del(.providers.kubeadm.status.installedVersion)' + run bash "$UPGRADE_SCRIPT" + [ "$status" -ne 0 ] + [[ "$output" == *"bootstrap provider downgrades are not supported"* ]] [ "$(accepted_patches)" = '[{"name":"cluster-api","version":"v1.13.8-core"}]' ] - [ "$(jq --raw-output '.providers.kubeadm.spec.version' "$CAPI_TEST_STATE")" = v1.10.1-bootstrap ] } @test "an installed kubeadm without an installed core provider is refused" { diff --git a/hack/testdata/capi-upgrade/kubectl b/hack/testdata/capi-upgrade/kubectl index ba8c3682f..d1b217849 100644 --- a/hack/testdata/capi-upgrade/kubectl +++ b/hack/testdata/capi-upgrade/kubectl @@ -92,6 +92,8 @@ if verb == "get": obj["status"]["readyReplicas"] = 1 obj["status"]["availableReplicas"] = 1 del state["rolloutReads"][name] + if name == "capi-controller-manager" and "bootstrapAfterCore" in state and obj["status"]["readyReplicas"] == 1: + state["providers"]["kubeadm"] = state.pop("bootstrapAfterCore") save() print(json.dumps(obj)) elif verb == "patch": diff --git a/packages/system/capi-providers-core/files/upgrade-providers.sh b/packages/system/capi-providers-core/files/upgrade-providers.sh index b4464e92d..d5d56709e 100644 --- a/packages/system/capi-providers-core/files/upgrade-providers.sh +++ b/packages/system/capi-providers-core/files/upgrade-providers.sh @@ -39,31 +39,31 @@ target_minor="$(minor_version "$TARGET_VERSION" target)" core="$(kube get coreproviders.operator.cluster.x-k8s.io cluster-api --ignore-not-found --output json)" bootstrap="$(kube get bootstrapproviders.operator.cluster.x-k8s.io kubeadm --ignore-not-found --output json)" -# cluster-api-operator installs a provider with no installed version from -# scratch at whatever version it requests, so it needs no intermediate step. installed_version() { jq --raw-output '.status.installedVersion // empty' <<< "$1" } -if [[ -n "$bootstrap" && -z "$(installed_version "$bootstrap")" ]]; then - bootstrap="" -fi if [[ -z "$core" || -z "$(installed_version "$core")" ]]; then - [[ -z "$bootstrap" ]] || fail "kubeadm is installed without an installed core provider" + [[ -z "$bootstrap" || -z "$(installed_version "$bootstrap")" ]] || fail "kubeadm is installed without an installed core provider" printf 'No installed providers need an intermediate upgrade.\n' exit 0 fi +upgrade_version() { + # An unfinished install can complete at its old requested version while core upgrades. + jq --raw-output '(.status.installedVersion | select(. != null and . != "")) // .spec.version // empty' <<< "$1" +} + validate_provider() { local object="$1" suffix="$2" installed requested uid minor requested_minor newer - installed="$(installed_version "$object")" + requested="$(jq --raw-output '.spec.version // empty' <<< "$object")" + requested_minor="$(minor_version "$requested" requested)" + installed="$(upgrade_version "$object")" minor="$(minor_version "$installed" installed)" newer="$(jq --null-input --arg installed "$installed" --arg target "$TARGET_VERSION" ' def base: ltrimstr("v") | split("-")[0] | split("+")[0] | split(".") | map(tonumber); ($installed | base) > ($target | base)')" [[ "$newer" == false ]] || fail "$suffix provider downgrades are not supported" ((bridge_minor - minor <= 3)) || fail "$suffix is outside the intermediate upgrade window" - requested="$(jq --raw-output '.spec.version // empty' <<< "$object")" - requested_minor="$(minor_version "$requested" requested)" uid="$(jq --raw-output '.metadata.uid // empty' <<< "$object")" [[ -n "$uid" ]] || fail "$suffix provider UID is missing" if [[ "$requested" != "$installed" && "$requested" != "$BRIDGE_VERSION-$suffix" && "$requested" != "$TARGET_VERSION-$suffix" ]]; then @@ -78,7 +78,7 @@ core_minor="$(minor_version "$(jq --raw-output '.status.installedVersion' <<< "$ bootstrap_minor="$target_minor" if [[ -n "$bootstrap" ]]; then validate_provider "$bootstrap" bootstrap - bootstrap_minor="$(minor_version "$(jq --raw-output '.status.installedVersion' <<< "$bootstrap")" installed)" + bootstrap_minor="$(minor_version "$(upgrade_version "$bootstrap")" installed)" fi bridge_selector_needs_repair() { jq --exit-status --arg version "$BRIDGE_VERSION-$2" ' @@ -129,7 +129,7 @@ wait_provider() { stage_provider() { local resource="$1" name="$2" deployment="$3" suffix="$4" object="$5" local installed requested uid minor requested_minor expected patch - installed="$(jq --raw-output '.status.installedVersion' <<< "$object")" + installed="$(upgrade_version "$object")" requested="$(jq --raw-output '.spec.version' <<< "$object")" uid="$(jq --raw-output '.metadata.uid' <<< "$object")" minor="$(minor_version "$installed" installed)" @@ -165,7 +165,17 @@ stage_provider() { } stage_provider coreproviders.operator.cluster.x-k8s.io cluster-api capi-controller-manager core "$core" +bootstrap_uid="$(jq --raw-output '.metadata.uid // empty' <<< "$bootstrap")" +bootstrap="$(kube get bootstrapproviders.operator.cluster.x-k8s.io kubeadm --ignore-not-found --output json)" +if [[ -n "$bootstrap_uid" ]]; then + [[ "$(jq --raw-output '.metadata.uid // empty' <<< "$bootstrap")" == "$bootstrap_uid" ]] || fail "kubeadm was replaced during the upgrade" +fi if [[ -n "$bootstrap" ]]; then - stage_provider bootstrapproviders.operator.cluster.x-k8s.io kubeadm capi-kubeadm-bootstrap-controller-manager bootstrap "$bootstrap" + validate_provider "$bootstrap" bootstrap + bootstrap_minor="$(minor_version "$(upgrade_version "$bootstrap")" requested)" + # Unfinished supported requests may need repositories from the dependent bootstrap chart. + if [[ -n "$(installed_version "$bootstrap")" ]] || ((target_minor - bootstrap_minor > 3)); then + stage_provider bootstrapproviders.operator.cluster.x-k8s.io kubeadm capi-kubeadm-bootstrap-controller-manager bootstrap "$bootstrap" + fi fi printf 'Intermediate provider upgrade completed.\n'