fix(kubernetes-nodes): tolerate a missing KMT CRD in the pre-delete unpin hook

The unpin hook listed KubevirtMachineTemplates under set -eu with no
missing-CRD carve-out, unlike migration 54's list_kmts. During a
management-cluster CAPI teardown or an already-Terminating namespace the CRD
can be gone; the list then exits non-zero, the Job fails, and after
backoffLimit the HelmRelease finalizer wedges cluster deletion on a resource
type that no longer exists.

Distinguish a missing CRD (skip: nothing to unpin) from a real API/RBAC
failure (still exit 1) by kubectl's two deterministic RESTMapper messages,
mirroring list_kmts. stderr is folded into the captured output only because
the strict <BASE>-<6hex> grep cannot match a warning line.

Signed-off-by: IvanHunters <xorokhotnikov@gmail.com>
This commit is contained in:
IvanHunters committed 2026-08-03 23:04:09 +03:00
1 parent e969d71379
commit 6ccd1ed8c4
2 files changed
+40 -8

No files matched your search

@@ -164,14 +164,29 @@ spec:
unpin "machinedeployment.cluster.x-k8s.io/${BASE}"
unpin "machinehealthcheck.cluster.x-k8s.io/${BASE}"
unpin "workloadmonitor.cozystack.io/${BASE}"
# Discover this pool's hash-suffixed KMTs. The list is NOT wrapped in
# `2>/dev/null || true`: an API/RBAC failure here must fail the Job
# (set -e), not masquerade as an empty result and silently leak the
# keep-annotated templates on uninstall, the same "Failures are NOT
# swallowed" contract unpin() above holds. A successful-but-empty
# listing is fine: grep then matches nothing and the loop is a no-op.
kmts=$(kubectl -n "${NS}" get kubevirtmachinetemplate.infrastructure.cluster.x-k8s.io \
-o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}')
# Discover this pool's hash-suffixed KMTs. An API/RBAC failure here
# must fail the Job (set -e), not masquerade as an empty result and
# silently leak the keep-annotated templates on uninstall, the same
# "Failures are NOT swallowed" contract unpin() above holds. The one
# tolerated failure is a missing KubevirtMachineTemplate CRD: during a
# management-cluster CAPI teardown, or an already-Terminating
# namespace, the CRD can be gone, and there is then provably nothing
# to unpin -- failing would wedge the HR finalizer (backoffLimit
# exhausted) on a resource type that no longer exists. Distinguish it
# by kubectl's two deterministic RESTMapper messages (mirrors
# migration 54 list_kmts), never by a message that merely contains
# "not found". stderr is folded into the captured output only because
# the strict <BASE>-<6hex> grep below cannot match a kubectl warning
# line, so a success-path deprecation warning is harmlessly filtered.
if ! kmts=$(kubectl -n "${NS}" get kubevirtmachinetemplate.infrastructure.cluster.x-k8s.io \
-o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' 2>&1); then
case "${kmts}" in
*"the server doesn't have a resource type"*|*"no matches for kind"*)
echo "KubevirtMachineTemplate CRD absent; nothing to unpin"; kmts="" ;;
*)
echo "ERROR: listing KubevirtMachineTemplates failed: ${kmts}" >&2; exit 1 ;;
esac
fi
for kmt in $(printf '%s\n' "${kmts}" | grep -E "^${BASE}-[0-9a-f]{6}$" || true); do
unpin "kubevirtmachinetemplate.infrastructure.cluster.x-k8s.io/${kmt}"
done
@@ -46,6 +46,23 @@ tests:
path: spec.template.spec.serviceAccountName
value: kubernetes-nodes-myk8s-md0-unpin
- it: tolerates a missing KubevirtMachineTemplate CRD instead of wedging the finalizer
documentSelector:
path: kind
value: Job
asserts:
# KMT discovery must distinguish a missing CRD (skip: nothing to unpin)
# from a real API/RBAC failure (exit 1). During a management-cluster CAPI
# teardown or a Terminating namespace the CRD can be gone; failing there
# would exhaust backoffLimit and wedge the HR finalizer on a resource type
# that no longer exists.
- matchRegex:
path: spec.template.spec.containers[0].args[0]
pattern: "the server doesn't have a resource type"
- matchRegex:
path: spec.template.spec.containers[0].args[0]
pattern: "CRD absent; nothing to unpin"
- it: runs the unpin Job pod PSS-restricted
documentSelector:
path: kind