diff --git a/hack/capi-upgrade_test.bats b/hack/capi-upgrade_test.bats index 394d7fa7b..d4f19adca 100644 --- a/hack/capi-upgrade_test.bats +++ b/hack/capi-upgrade_test.bats @@ -193,11 +193,26 @@ accepted_patches() { [ "$(accepted_patches)" = '[]' ] } -@test "an uninitialised provider is not mistaken for a fresh installation" { +@test "providers that never finished installing take the fresh installation path" { + fixture 'del(.providers[].status.installedVersion)' + run bash "$UPGRADE_SCRIPT" + [ "$status" -eq 0 ] + [ "$(accepted_patches)" = '[]' ] +} + +@test "a kubeadm provider that never finished installing is left to the final manifests" { + fixture 'del(.providers.kubeadm.status.installedVersion)' + run bash "$UPGRADE_SCRIPT" + [ "$status" -eq 0 ] + [ "$(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" { fixture 'del(.providers["cluster-api"].status.installedVersion)' run bash "$UPGRADE_SCRIPT" [ "$status" -ne 0 ] - [[ "$output" == *"installed version is not recorded"* ]] + [[ "$output" == *"kubeadm is installed without an installed core provider"* ]] [ "$(accepted_patches)" = '[]' ] } diff --git a/packages/system/capi-providers-bootstrap/templates/_helpers.tpl b/packages/system/capi-providers-bootstrap/templates/_helpers.tpl index 3bca41c1e..36e9565ad 100644 --- a/packages/system/capi-providers-bootstrap/templates/_helpers.tpl +++ b/packages/system/capi-providers-bootstrap/templates/_helpers.tpl @@ -1,12 +1,8 @@ {{- define "capi-providers-bootstrap.assertUpgradeWindow" -}} {{- if .Capabilities.APIVersions.Has "operator.cluster.x-k8s.io/v1alpha2" -}} {{- $provider := lookup "operator.cluster.x-k8s.io/v1alpha2" "BootstrapProvider" .Release.Namespace "kubeadm" -}} -{{- if $provider -}} -{{- $status := get $provider "status" | default dict -}} -{{- $installed := get $status "installedVersion" -}} -{{- if or (not (kindIs "string" $installed)) (eq $installed "") -}} -{{- fail "kubeadm provider installed version is not recorded; wait for the existing installation before upgrading" -}} -{{- end -}} +{{- $installed := dig "status" "installedVersion" "" ($provider | default dict) -}} +{{- if and (kindIs "string" $installed) (ne $installed "") -}} {{- $current := semver $installed -}} {{- $target := semver .Chart.AppVersion -}} {{- if or (gt $current.Major $target.Major) (and (eq $current.Major $target.Major) (gt $current.Minor $target.Minor)) (and (eq $current.Major $target.Major) (eq $current.Minor $target.Minor) (gt $current.Patch $target.Patch)) -}} diff --git a/packages/system/capi-providers-bootstrap/tests/upgrade_guard_test.yaml b/packages/system/capi-providers-bootstrap/tests/upgrade_guard_test.yaml index 5d1617485..cccc667d9 100644 --- a/packages/system/capi-providers-bootstrap/tests/upgrade_guard_test.yaml +++ b/packages/system/capi-providers-bootstrap/tests/upgrade_guard_test.yaml @@ -94,15 +94,18 @@ tests: - isKind: of: BootstrapProvider - - it: refuses an existing provider without an installed version + - it: treats a provider that never finished installing as a fresh installation kubernetesProvider: scheme: *scheme objects: - <<: *legacy status: {} + documentSelector: + path: metadata.name + value: kubeadm asserts: - - failedTemplate: - errorPattern: "installed version is not recorded" + - isKind: + of: BootstrapProvider - it: refuses a downgrade kubernetesProvider: diff --git a/packages/system/capi-providers-core/files/upgrade-providers.sh b/packages/system/capi-providers-core/files/upgrade-providers.sh index c4f851b7c..39676b9d4 100644 --- a/packages/system/capi-providers-core/files/upgrade-providers.sh +++ b/packages/system/capi-providers-core/files/upgrade-providers.sh @@ -39,16 +39,23 @@ target_minor="$(minor_version "$TARGET_VERSION")" 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)" -if [[ -z "$core" ]]; then - [[ -z "$bootstrap" ]] || fail "kubeadm exists without a core provider" +# 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" printf 'No installed providers need an intermediate upgrade.\n' exit 0 fi validate_provider() { local object="$1" suffix="$2" installed requested uid minor requested_minor newer - installed="$(jq --raw-output '.status.installedVersion // empty' <<< "$object")" - [[ -n "$installed" ]] || fail "$suffix installed version is not recorded" + installed="$(installed_version "$object")" minor="$(minor_version "$installed")" newer="$(jq --null-input --arg installed "$installed" --arg target "$TARGET_VERSION" ' def base: ltrimstr("v") | split("-")[0] | split("+")[0] | split(".") | map(tonumber);