diff --git a/hack/capi-provider-components_test.bats b/hack/capi-provider-components_test.bats index 272c219f0..fa0c420f0 100644 --- a/hack/capi-provider-components_test.bats +++ b/hack/capi-provider-components_test.bats @@ -317,3 +317,129 @@ check_bootstrap_version() { jq --exit-status '.httpGet.path == "/healthz" and .httpGet.port == "healthz" and .failureThreshold * .periodSeconds >= 300' <<< "$probe" done } + +# capi-operator refuses a provider when its selector matches more than one +# ConfigMap, and it does that before it reads spec.version +# (processProviderConfigMaps in v0.30). During a Helm upgrade that renames the +# ConfigMap the old and the new one coexist for the length of the release, so a +# selector that does not pin the version matches both and the reconcile fails. +# Every selector therefore carries provider.cluster.x-k8s.io/version, and every +# ConfigMap carries it with the value the operator compares against. +# +# The old and new ConfigMaps are synthesised here rather than waiting for a +# bump: adding the retained copy of the current release beside the rendered one +# is the same state the upgrade window creates. +check_selectors_pin_the_version() { + local pkg="$1" provider_kind="$2" + local dir="$REPO_ROOT/packages/system/$pkg" rendered="$BATS_TEST_TMPDIR/$pkg.yaml" + + helm template provider "$dir" --namespace cozy-cluster-api --kube-version 1.33.0 > "$rendered" \ + || { echo "$pkg: does not render" >&2; return 1; } + + # One provider object and one components bundle, both carrying the same + # non-empty version label, and the selector's version equal to that label. + # Only ConfigMaps marked compressed are bundles: capi-providers-core also + # ships its upgrade script in one, which no selector matches. + yq --output-format=json --no-doc '.' "$rendered" | jq --slurp --exit-status --arg kind "$provider_kind" --arg pkg "$pkg" ' + [.[] | select(.kind == $kind)] as $providers + | [.[] | select(.kind == "ConfigMap" + and .metadata.annotations["provider.cluster.x-k8s.io/compressed"] == "true")] as $maps + | ($providers | length) == 1 + and ($maps | length) == 1 + and ($maps[0].metadata.labels["provider.cluster.x-k8s.io/version"] // "") != "" + and $providers[0].spec.fetchConfig.selector.matchLabels["provider.cluster.x-k8s.io/version"] + == $maps[0].metadata.labels["provider.cluster.x-k8s.io/version"] + and $providers[0].spec.fetchConfig.selector.matchLabels["provider.cluster.x-k8s.io/version"] + == $providers[0].spec.version + ' > /dev/null || { echo "$pkg: selector does not pin the ConfigMap version" >&2; return 1; } +} + +@test "every provider selector pins the ConfigMap version" { + check_selectors_pin_the_version capi-providers-infraprovider InfrastructureProvider + check_selectors_pin_the_version capi-providers-infraprovider-proxmox InfrastructureProvider + check_selectors_pin_the_version capi-providers-cpprovider ControlPlaneProvider + check_selectors_pin_the_version capi-providers-ipam-in-cluster IPAMProvider + check_selectors_pin_the_version capi-providers-core CoreProvider +} + +# The upgrade window itself: with the previous release's ConfigMap still in the +# namespace, the selector must still match exactly one object. Reverting the +# version label out of both the selector and the ConfigMap reproduces the bug +# this guard is for, so the fixture below proves the assertion can fail. +check_one_match_during_upgrade() { + local pkg="$1" provider_kind="$2" drop_version="$3" + local dir="$REPO_ROOT/packages/system/$pkg" rendered="$BATS_TEST_TMPDIR/$pkg-upgrade.yaml" + local objects="$BATS_TEST_TMPDIR/$pkg-upgrade.json" + + helm template provider "$dir" --namespace cozy-cluster-api --kube-version 1.33.0 > "$rendered" \ + || { echo "$pkg: does not render" >&2; return 1; } + + yq --output-format=json --no-doc '.' "$rendered" | jq --slurp --arg kind "$provider_kind" \ + --arg drop "$drop_version" ' + # The components bundle the previous release left behind, one version lower. + . + [([.[] | select(.kind == "ConfigMap" + and .metadata.annotations["provider.cluster.x-k8s.io/compressed"] == "true")][0] + | .metadata.name = "v0.0.1-previous" + | if $drop == "true" then del(.metadata.labels["provider.cluster.x-k8s.io/version"]) + else .metadata.labels["provider.cluster.x-k8s.io/version"] = "v0.0.1-previous" end)] + | if $drop == "true" then + map(if (.kind == $kind) then del(.spec.fetchConfig.selector.matchLabels["provider.cluster.x-k8s.io/version"]) else . end) + else . end + ' > "$objects" + + jq --slurp --exit-status --arg kind "$provider_kind" --arg pkg "$pkg" --arg drop "$drop_version" ' + .[0] as $all + | [$all[] | select(.kind == $kind)][0] as $provider + | [$all[] | select(.kind == "ConfigMap" + and .metadata.annotations["provider.cluster.x-k8s.io/compressed"] == "true") + | select(.metadata.labels as $labels + | $provider.spec.fetchConfig.selector.matchLabels | to_entries | all(.[]; $labels[.key] == .value))] + | if $drop == "true" then + # The regression: without the version in the selector both ConfigMaps + # match, which is what the operator rejects. + length != 1 + else + length == 1 + end + ' "$objects" > /dev/null || { + if [ "$drop_version" = "true" ]; then + echo "$pkg: fixture no longer reproduces a double match" >&2 + else + echo "$pkg: an upgrade matches more than one ConfigMap" >&2 + fi + return 1 + } +} + +@test "an upgrade with the previous ConfigMap present still matches one bundle" { + check_one_match_during_upgrade capi-providers-infraprovider InfrastructureProvider false + check_one_match_during_upgrade capi-providers-infraprovider-proxmox InfrastructureProvider false + check_one_match_during_upgrade capi-providers-cpprovider ControlPlaneProvider false + check_one_match_during_upgrade capi-providers-ipam-in-cluster IPAMProvider false +} + +@test "the double-match fixture fails when the version label is removed" { + check_one_match_during_upgrade capi-providers-infraprovider-proxmox InfrastructureProvider true +} + +# capi-providers-bootstrap carries two providers and two ConfigMaps, so the +# single-provider helpers above do not fit it. Each pair has to pin its own +# version, and selects exactly one bundle while the other provider's ConfigMap +# is present. +@test "both bootstrap providers pin their own ConfigMap version" { + local rendered="$BATS_TEST_TMPDIR/capi-providers-bootstrap.yaml" objects="$BATS_TEST_TMPDIR/capi-providers-bootstrap.json" + helm template provider "$REPO_ROOT/packages/system/capi-providers-bootstrap" --namespace cozy-cluster-api --kube-version 1.33.0 > "$rendered" + yq --output-format=json --no-doc '.' "$rendered" | jq --slurp '.' > "$objects" + + for provider in kubeadm talos; do + jq --slurp --exit-status --arg provider "$provider" ' + .[0] as $all + | [$all[] | select(.kind == "BootstrapProvider" and .metadata.name == $provider)][0] as $p + | $p.spec.fetchConfig.selector.matchLabels["provider.cluster.x-k8s.io/version"] == $p.spec.version + and ([$all[] | select(.kind == "ConfigMap") + | select(.metadata.labels as $labels + | $p.spec.fetchConfig.selector.matchLabels | to_entries | all(.[]; $labels[.key] == .value))] | length) == 1 + ' "$objects" > /dev/null || { echo "capi-providers-bootstrap: $provider does not select exactly one bundle" >&2; return 1; } + done +} + diff --git a/packages/system/capi-providers-bootstrap/templates/_helpers.tpl b/packages/system/capi-providers-bootstrap/templates/_helpers.tpl index 36e9565ad..7788c34fe 100644 --- a/packages/system/capi-providers-bootstrap/templates/_helpers.tpl +++ b/packages/system/capi-providers-bootstrap/templates/_helpers.tpl @@ -14,3 +14,16 @@ {{- end -}} {{- end -}} {{- end -}} + +{{/* +The Talos bootstrap ConfigMap's version, defined once so the ConfigMap name, its +version label and the Talos provider's spec.version cannot drift apart. The +version label is what keeps a renamed ConfigMap from matching twice: +capi-operator lists every ConfigMap the selector matches and refuses a provider +while more than one is returned, before it reads spec.version +(processProviderConfigMaps). The kubeadm pair uses the chart appVersion instead +because it tracks the upstream Cluster API release. +*/}} +{{- define "capi-providers-bootstrap.talosVersion" -}} +v0.6.13-bootstrap-talos +{{- end -}} diff --git a/packages/system/capi-providers-bootstrap/templates/configmaps.yaml b/packages/system/capi-providers-bootstrap/templates/configmaps.yaml index 2c505ed94..c0ced93fa 100644 --- a/packages/system/capi-providers-bootstrap/templates/configmaps.yaml +++ b/packages/system/capi-providers-bootstrap/templates/configmaps.yaml @@ -16,8 +16,9 @@ data: apiVersion: v1 kind: ConfigMap metadata: - name: v0.6.13-bootstrap-talos + name: {{ include "capi-providers-bootstrap.talosVersion" . }} labels: + provider.cluster.x-k8s.io/version: {{ include "capi-providers-bootstrap.talosVersion" . }} bootstrap-components: cozy bootstrap-provider: talos annotations: diff --git a/packages/system/capi-providers-bootstrap/templates/providers.yaml b/packages/system/capi-providers-bootstrap/templates/providers.yaml index 20de4b179..8368613ce 100644 --- a/packages/system/capi-providers-bootstrap/templates/providers.yaml +++ b/packages/system/capi-providers-bootstrap/templates/providers.yaml @@ -29,9 +29,10 @@ metadata: name: talos spec: # https://github.com/siderolabs/cluster-api-bootstrap-provider-talos - version: v0.6.13-bootstrap-talos + version: {{ include "capi-providers-bootstrap.talosVersion" . }} fetchConfig: selector: matchLabels: + provider.cluster.x-k8s.io/version: {{ include "capi-providers-bootstrap.talosVersion" . }} bootstrap-components: cozy bootstrap-provider: talos diff --git a/packages/system/capi-providers-cpprovider/templates/_helpers.tpl b/packages/system/capi-providers-cpprovider/templates/_helpers.tpl new file mode 100644 index 000000000..1218a00e4 --- /dev/null +++ b/packages/system/capi-providers-cpprovider/templates/_helpers.tpl @@ -0,0 +1,12 @@ +{{/* +The version the ConfigMap, its version label and the provider's spec.version all +carry, defined once so a bump moves one line. The version label is what keeps a +renamed ConfigMap from matching twice: capi-operator lists every ConfigMap the +selector matches and refuses a provider while more than one is returned, before +it reads spec.version (processProviderConfigMaps). During a Helm upgrade that +renames the ConfigMap the old and the new one coexist for the length of the +release, so a selector without this label matches both and the reconcile fails. +*/}} +{{- define "capi-providers-cpprovider.version" -}} +v0.19.0-cozystack.0 +{{- end -}} diff --git a/packages/system/capi-providers-cpprovider/templates/configmaps.yaml b/packages/system/capi-providers-cpprovider/templates/configmaps.yaml index 77af391a3..a33f16cc5 100644 --- a/packages/system/capi-providers-cpprovider/templates/configmaps.yaml +++ b/packages/system/capi-providers-cpprovider/templates/configmaps.yaml @@ -3,8 +3,9 @@ kind: ConfigMap metadata: # Must match spec.version in templates/providers.yaml so the # ControlPlaneProvider operator selects this components bundle. - name: v0.19.0-cozystack.0 + name: {{ include "capi-providers-cpprovider.version" . }} labels: + provider.cluster.x-k8s.io/version: {{ include "capi-providers-cpprovider.version" . }} cp-components: cozy annotations: provider.cluster.x-k8s.io/compressed: "true" diff --git a/packages/system/capi-providers-cpprovider/templates/providers.yaml b/packages/system/capi-providers-cpprovider/templates/providers.yaml index a4eed70fa..2e48d2269 100644 --- a/packages/system/capi-providers-cpprovider/templates/providers.yaml +++ b/packages/system/capi-providers-cpprovider/templates/providers.yaml @@ -9,10 +9,11 @@ spec: # upstream master ships v1alpha2 only with no v1alpha1 conversion # webhook. Bump along with UPSTREAM_VERSION / COZYSTACK_REV in the # package Makefile. - version: v0.19.0-cozystack.0 + version: {{ include "capi-providers-cpprovider.version" . }} fetchConfig: selector: matchLabels: + provider.cluster.x-k8s.io/version: {{ include "capi-providers-cpprovider.version" . }} cp-components: cozy deployment: containers: diff --git a/packages/system/capi-providers-infraprovider-proxmox/templates/_helpers.tpl b/packages/system/capi-providers-infraprovider-proxmox/templates/_helpers.tpl new file mode 100644 index 000000000..cd3a00aa6 --- /dev/null +++ b/packages/system/capi-providers-infraprovider-proxmox/templates/_helpers.tpl @@ -0,0 +1,15 @@ +{{/* +The version the ConfigMap, its version label and the provider's spec.version all +carry. capi-operator lists every ConfigMap the selector matches and refuses a +provider while more than one is returned, before it reads spec.version +(processProviderConfigMaps). During a Helm upgrade that renames the ConfigMap the +old and the new one coexist for the length of the release, so a selector without +this label matches both and the reconcile fails. + +Define it once here: a bump then moves one line, and the four references below +cannot drift apart. `make components` in this package rewrites the vendored +files, not this value. +*/}} +{{- define "capi-providers-infraprovider-proxmox.version" -}} +v0.7.7-infraprovider-proxmox +{{- end -}} diff --git a/packages/system/capi-providers-infraprovider-proxmox/templates/configmaps.yaml b/packages/system/capi-providers-infraprovider-proxmox/templates/configmaps.yaml index 460e84412..87956c9c2 100644 --- a/packages/system/capi-providers-infraprovider-proxmox/templates/configmaps.yaml +++ b/packages/system/capi-providers-infraprovider-proxmox/templates/configmaps.yaml @@ -1,20 +1,23 @@ apiVersion: v1 kind: ConfigMap metadata: - name: v0.7.7-infraprovider-proxmox + name: {{ include "capi-providers-infraprovider-proxmox.version" . }} {{- /* NOT 'infraprovider-components: cozy'. That label already identifies the KubeVirt provider's ConfigMap, and a shared label would put both in the - same match. The operator would still install the right one — it keys every - matched ConfigMap by its name as the version and takes the one equal to - spec.version — but it builds that repository in a single pass and returns - an error for the whole list as soon as one match is malformed: a name that - does not parse as semver, or missing metadata. Sharing the label would let - a broken ConfigMap of one provider stop the other from being installed. - The kubeadm/talos bootstrap packages separate on a 'bootstrap-provider' - key for the same reason; this mirrors that. + same match. The kubeadm/talos bootstrap packages separate on a + 'bootstrap-provider' key for the same reason; this mirrors that. + + The version label is what keeps a renamed ConfigMap from matching twice. + capi-operator lists every ConfigMap the selector matches and refuses a + provider while more than one is returned, before it reads spec.version + (processProviderConfigMaps). During a Helm upgrade that bumps the version + the old and the new ConfigMap coexist for the length of the release, so a + selector without the version matches both and the reconcile fails until + Helm deletes the old one. With it, each upgrade sees exactly one. */}} labels: + provider.cluster.x-k8s.io/version: {{ include "capi-providers-infraprovider-proxmox.version" . }} infraprovider-provider: proxmox annotations: provider.cluster.x-k8s.io/compressed: "true" diff --git a/packages/system/capi-providers-infraprovider-proxmox/templates/providers.yaml b/packages/system/capi-providers-infraprovider-proxmox/templates/providers.yaml index 46f0be3a8..84ed6afac 100644 --- a/packages/system/capi-providers-infraprovider-proxmox/templates/providers.yaml +++ b/packages/system/capi-providers-infraprovider-proxmox/templates/providers.yaml @@ -9,13 +9,14 @@ spec: # stores v1alpha2. v1alpha1 is still served through conversion, but that has # not been tested with the kubernetes-nodes templates, and in-cluster IPAM # should move to v1.1 at the same time. Bump the two together. - version: v0.7.7-infraprovider-proxmox + version: {{ include "capi-providers-infraprovider-proxmox.version" . }} configSecret: name: {{ .Values.credentialsSecretName | quote }} namespace: {{ .Release.Namespace }} fetchConfig: selector: matchLabels: + provider.cluster.x-k8s.io/version: {{ include "capi-providers-infraprovider-proxmox.version" . }} infraprovider-provider: proxmox deployment: containers: diff --git a/packages/system/capi-providers-infraprovider/templates/_helpers.tpl b/packages/system/capi-providers-infraprovider/templates/_helpers.tpl new file mode 100644 index 000000000..202c0511f --- /dev/null +++ b/packages/system/capi-providers-infraprovider/templates/_helpers.tpl @@ -0,0 +1,12 @@ +{{/* +The version the ConfigMap, its version label and the provider's spec.version all +carry, defined once so a bump moves one line. The version label is what keeps a +renamed ConfigMap from matching twice: capi-operator lists every ConfigMap the +selector matches and refuses a provider while more than one is returned, before +it reads spec.version (processProviderConfigMaps). During a Helm upgrade that +renames the ConfigMap the old and the new one coexist for the length of the +release, so a selector without this label matches both and the reconcile fails. +*/}} +{{- define "capi-providers-infraprovider.version" -}} +v0.1.10-infraprovider +{{- end -}} diff --git a/packages/system/capi-providers-infraprovider/templates/configmaps.yaml b/packages/system/capi-providers-infraprovider/templates/configmaps.yaml index 5b7c43224..1acb7a835 100644 --- a/packages/system/capi-providers-infraprovider/templates/configmaps.yaml +++ b/packages/system/capi-providers-infraprovider/templates/configmaps.yaml @@ -1,8 +1,9 @@ apiVersion: v1 kind: ConfigMap metadata: - name: v0.1.10-infraprovider + name: {{ include "capi-providers-infraprovider.version" . }} labels: + provider.cluster.x-k8s.io/version: {{ include "capi-providers-infraprovider.version" . }} infraprovider-components: cozy annotations: provider.cluster.x-k8s.io/compressed: "true" diff --git a/packages/system/capi-providers-infraprovider/templates/providers.yaml b/packages/system/capi-providers-infraprovider/templates/providers.yaml index e00c076e1..21e573984 100644 --- a/packages/system/capi-providers-infraprovider/templates/providers.yaml +++ b/packages/system/capi-providers-infraprovider/templates/providers.yaml @@ -4,10 +4,11 @@ metadata: name: kubevirt spec: # https://github.com/kubernetes-sigs/cluster-api-provider-kubevirt - version: v0.1.10-infraprovider + version: {{ include "capi-providers-infraprovider.version" . }} fetchConfig: selector: matchLabels: + provider.cluster.x-k8s.io/version: {{ include "capi-providers-infraprovider.version" . }} infraprovider-components: cozy deployment: containers: diff --git a/packages/system/capi-providers-ipam-in-cluster/templates/_helpers.tpl b/packages/system/capi-providers-ipam-in-cluster/templates/_helpers.tpl new file mode 100644 index 000000000..01556e9c0 --- /dev/null +++ b/packages/system/capi-providers-ipam-in-cluster/templates/_helpers.tpl @@ -0,0 +1,12 @@ +{{/* +The version the ConfigMap, its version label and the provider's spec.version all +carry, defined once so a bump moves one line. The version label is what keeps a +renamed ConfigMap from matching twice: capi-operator lists every ConfigMap the +selector matches and refuses a provider while more than one is returned, before +it reads spec.version (processProviderConfigMaps). During a Helm upgrade that +renames the ConfigMap the old and the new one coexist for the length of the +release, so a selector without this label matches both and the reconcile fails. +*/}} +{{- define "capi-providers-ipam-in-cluster.version" -}} +v1.0.3-ipam-in-cluster +{{- end -}} diff --git a/packages/system/capi-providers-ipam-in-cluster/templates/configmaps.yaml b/packages/system/capi-providers-ipam-in-cluster/templates/configmaps.yaml index baf830c9b..40b76066f 100644 --- a/packages/system/capi-providers-ipam-in-cluster/templates/configmaps.yaml +++ b/packages/system/capi-providers-ipam-in-cluster/templates/configmaps.yaml @@ -1,8 +1,9 @@ apiVersion: v1 kind: ConfigMap metadata: - name: v1.0.3-ipam-in-cluster + name: {{ include "capi-providers-ipam-in-cluster.version" . }} labels: + provider.cluster.x-k8s.io/version: {{ include "capi-providers-ipam-in-cluster.version" . }} ipamprovider-provider: in-cluster annotations: provider.cluster.x-k8s.io/compressed: "true" diff --git a/packages/system/capi-providers-ipam-in-cluster/templates/providers.yaml b/packages/system/capi-providers-ipam-in-cluster/templates/providers.yaml index 511f7ee5b..4a5b22336 100644 --- a/packages/system/capi-providers-ipam-in-cluster/templates/providers.yaml +++ b/packages/system/capi-providers-ipam-in-cluster/templates/providers.yaml @@ -11,10 +11,11 @@ spec: # v1.0.x, not v1.1: both serve InClusterIPPool v1alpha2, but v1.1 moves to the # v1beta2 contract and its IPAddressClaim API, while capmox v0.7, the only # consumer here, is a v1beta1-contract provider. Move the two together. - version: v1.0.3-ipam-in-cluster + version: {{ include "capi-providers-ipam-in-cluster.version" . }} fetchConfig: selector: matchLabels: + provider.cluster.x-k8s.io/version: {{ include "capi-providers-ipam-in-cluster.version" . }} ipamprovider-provider: in-cluster deployment: containers: