fix(cluster-api): name the kubeconfig the upgrade hook runs with

The provider upgrade hook reaches the API through the kubeconfig its
Job mounts at /scripts/kubeconfig and exports as KUBECONFIG, never
through the in-cluster config. The script only named KUBE_CONTEXT,
so hack/check-hook-request-timeout.bats, which since it started
reading files/ exempts a script only when it names KUBECONFIG, took
its "kubectl --context ... --request-timeout=10s" for an in-cluster
call and failed "no in-cluster kubectl call carries a non-zero
--request-timeout" on main.

Require KUBECONFIG alongside the other inputs instead of loosening the
check or dropping the timeout: the per-call timeout is safe because
the explicit kubeconfig is what serves it, and the script now refuses
to fall back to the in-cluster config, where the timeout would hit the
bug the check guards against.

Tested with bats on hack/check-hook-request-timeout.bats,
hack/capi-upgrade_test.bats and hack/capi-upgrade-chart_test.bats; with
the script change reverted, the check and the new missing-KUBECONFIG
test both fail.

Fixes: 9680ed64df ("feat(cluster-api)!: update Cluster API to v1.14.4 (#4902)")
Assisted-by: LLM
Signed-off-by: Mattia Eleuteri <mattia@hidora.io>
This commit is contained in:
Mattia Eleuteri committed 2026-11-23 13:24:41 +01:00
1 parent 2af1e21299
commit 5e6772ec3f
3 files changed
+11 -1

No files matched your search

+2
View File
@@ -83,6 +83,8 @@ setup() {
[ "$(yq --no-doc 'select(.kind == "Job") | .spec.template.spec.volumes[] | select(.name == "scripts") | .configMap.name' "$RENDERED")" = capi-providers-core-upgrade ]
[ "$(yq --no-doc 'select(.kind == "Job") | .spec.template.spec.containers[0].volumeMounts[] | select(.name == "scripts") | .mountPath' "$RENDERED")" = /scripts ]
[ "$(yq --no-doc 'select(.kind == "Job") | .spec.template.spec.containers[0].env[] | select(.name == "KUBE_CONTEXT") | .value' "$RENDERED")" = capi-upgrade ]
[ "$(yq --no-doc 'select(.kind == "Job") | .spec.template.spec.containers[0].env[] | select(.name == "KUBECONFIG") | .value' "$RENDERED")" = /scripts/kubeconfig ]
[ "$(yq --no-doc 'select(.kind == "ConfigMap" and .metadata.name == "capi-providers-core-upgrade") | .data.kubeconfig | from_yaml | .current-context' "$RENDERED")" = capi-upgrade ]
image="$(yq --no-doc 'select(.kind == "Job") | .spec.template.spec.containers[0].image' "$RENDERED")"
[[ "$image" =~ @sha256:[0-9a-f]{64}$ ]]
}
+8
View File
@@ -53,6 +53,14 @@ accepted_patches() {
jq --slurp --compact-output '[.[] | select(.accepted == true) | {name, version: (.patch[] | select(.path == "/spec/version" and .op == "replace") | .value)}]' "$CAPI_TEST_LOG"
}
@test "the script refuses to reach the API without the hook's kubeconfig" {
unset KUBECONFIG
run bash "$UPGRADE_SCRIPT"
[ "$status" -ne 0 ]
[[ "$output" == *KUBECONFIG* ]]
[ ! -s "$CAPI_TEST_LOG" ]
}
@test "fresh installations do not create or patch providers" {
fixture '.providers = {}'
run bash "$UPGRADE_SCRIPT"
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
set -euo pipefail
: "${KUBE_CONTEXT:?}" "${NAMESPACE:?}" "${BRIDGE_VERSION:?}" "${TARGET_VERSION:?}"
: "${KUBECONFIG:?}" "${KUBE_CONTEXT:?}" "${NAMESPACE:?}" "${BRIDGE_VERSION:?}" "${TARGET_VERSION:?}"
fail() {
printf 'CAPI upgrade: %s\n' "$*" >&2