From 7b57097d71e71c0b567cb956acf613d0950dee8f Mon Sep 17 00:00:00 2001 From: Marian Koreniuk Date: Mon, 19 Oct 2026 23:14:09 +0200 Subject: [PATCH 1/3] fix(e2e): read the KubernetesNodes pool release in the HelmRelease guard cozy_guard_all_helmreleases reads the parent and the kubernetes-- addons. The worker pool is kubernetes-nodes--md0, an Application HelmRelease the apiserver stamps with RetryOnFailure like the parent, on the node-join path through the talos-reconcile Job it renders. Its name does not start with the addon prefix, so a failed action on it was never read, on the passing path or in the failure-path report. Read it on both paths. The pool name is derived from the parent, the way the addon prefix is, so the two cannot drift. The failure-path report checks that the release exists first: a run that failed between the parent and the pool apply has none, and that is a fact about where the run stopped rather than a failed read. Closes #4737 Signed-off-by: Marian Koreniuk Assisted-by: LLM (cherry picked from commit cac152048efeb0c5e2c37140f40e36a4458c00a6) Signed-off-by: cozystack-ci[bot] <274107086+cozystack-ci[bot]@users.noreply.github.com> --- hack/e2e-chainsaw/_lib/run-kubernetes.sh | 41 +++++- hack/run-kubernetes-remediation_test.bats | 163 +++++++++++++++++++++- 2 files changed, 195 insertions(+), 9 deletions(-) diff --git a/hack/e2e-chainsaw/_lib/run-kubernetes.sh b/hack/e2e-chainsaw/_lib/run-kubernetes.sh index e75990010..bd953d2db 100644 --- a/hack/e2e-chainsaw/_lib/run-kubernetes.sh +++ b/hack/e2e-chainsaw/_lib/run-kubernetes.sh @@ -5230,9 +5230,10 @@ _cozy_guard_kubectl() { cozy_guard_helmrelease() { local _ns="$1" local _hr="$2" - # Non-empty on a release whose install strategy the cluster is known to set, - # which is the parent Application HelmRelease and nothing else here. See the - # branch that reads it for why the check cannot be applied to the addons. + # Non-empty on a release whose install strategy the cluster is known to set: + # the parent Application HelmRelease and the worker pool release, which is an + # Application HelmRelease too. See the branch that reads it for why the check + # cannot be applied to the addons. local _strategy_required="${3:-}" local _probe _strategy _history _ready _read_rc _cozy_guard_read_bounds @@ -5437,16 +5438,32 @@ cozy_guard_addon_helmreleases() { # keeps the parent's own verdict, which an early return past the addons would # drop. # +# The worker pool release of parent kubernetes-. The pool is a +# KubernetesNodes named -md0 (see the apply in the run), and the +# aggregated apiserver names its HelmRelease kubernetes-nodes--md0. That +# name does not start with the addon prefix kubernetes--, so the addon +# listing never selects it, and it is read on its own. Derived from the parent +# rather than passed in, for the same reason as the addon prefix below. +cozy_pool_helmrelease_name() { + local _parent="$1" + printf 'kubernetes-nodes-%s-md0\n' "${_parent#kubernetes-}" +} + # The addon prefix is derived here rather than passed in, so the parent name and # the prefix that selects its children cannot drift apart. cozy_guard_all_helmreleases() { local _ns="$1" local _parent="$2" local _failed=0 - # The parent is asked to prove its install strategy read back non-empty; the - # addons are not, because most of them legitimately set none. See the branch - # in cozy_guard_helmrelease that reads this argument. + # The parent and the pool release are asked to prove their install strategy + # read back non-empty: both are Application HelmReleases, which the apiserver + # stamps. The addons are not, because most of them legitimately set none. See + # the branch in cozy_guard_helmrelease that reads this argument. cozy_guard_helmrelease "$_ns" "${_parent}" required || _failed=1 + # The pool release sits on the node-join path, through the talos-reconcile + # Job it renders, so a failed action on it is as much a finding as one on the + # parent. By the time this runs the pool has scaled, so the release exists. + cozy_guard_helmrelease "$_ns" "$(cozy_pool_helmrelease_name "${_parent}")" required || _failed=1 cozy_guard_addon_helmreleases "$_ns" "${_parent}-" || _failed=1 # These releases are now classified, and the same flag the failure-path report # uses records it. The two are not alternatives: this call runs before the @@ -5515,7 +5532,7 @@ cozy_report_helmrelease_remediation() { local _ns="$1" local _parent="$2" local _label="${3:-}" - local _addon_hrs _hr _deadline _budget + local _addon_hrs _hr _pool _deadline _budget [ "${_COZY_GUARD_REPORT_DONE}" -eq 0 ] || return 0 # An empty parent name means the run failed before the release was created, so # there is no history anywhere to read yet. @@ -5541,6 +5558,16 @@ cozy_report_helmrelease_remediation() { cozy_diag_phase_has_time "HelmRelease remediation footprint of ${_parent}" || return 0 echo "=== ${_label}tenant HelmRelease remediation footprint (management cluster, ns ${_ns}) — classification only, this does not decide the run ===" cozy_guard_helmrelease "$_ns" "${_parent}" required || true + # The pool release is created by a later apply than the parent, so a run that + # failed between the two has none to read. Its absence is then a fact about + # where the run stopped, not a failed read, and is reported as such. + _pool=$(cozy_pool_helmrelease_name "${_parent}") + cozy_diag_phase_has_time "HelmRelease remediation footprint of ${_pool}" || return 0 + if _cozy_guard_kubectl get hr -n "$_ns" "${_pool}" -o name >/dev/null 2>&1; then + cozy_guard_helmrelease "$_ns" "${_pool}" required || true + else + echo "» ${_pool} does not exist - the run ended before the worker pool was applied, or its HelmRelease was never created; nothing to read" + fi cozy_diag_phase_has_time "the addon HelmRelease listing of ${_parent}" || return 0 if [ "$(date +%s)" -ge "${_deadline}" ]; then echo "=== HelmRelease remediation footprint: stopped after ${_budget}s; the addon releases of ${_parent} were not listed, so nothing was observed about any of them ===" >&2 diff --git a/hack/run-kubernetes-remediation_test.bats b/hack/run-kubernetes-remediation_test.bats index 5f0b18e95..8464db61a 100644 --- a/hack/run-kubernetes-remediation_test.bats +++ b/hack/run-kubernetes-remediation_test.bats @@ -1,6 +1,6 @@ #!/usr/bin/env bats # ----------------------------------------------------------------------------- -# Unit tests for cozy_guard_addon_helmreleases in +# Unit tests for cozy_guard_all_helmreleases and cozy_guard_addon_helmreleases in # hack/e2e-chainsaw/_lib/run-kubernetes.sh # # cozy_guard_helmrelease judges one release; cozy_guard_addon_helmreleases runs @@ -32,7 +32,8 @@ load test_helper # created. The stub answers the shapes the guard issues: the namespace listing # (from ALL_HR), one release's install strategy and Ready condition together # (.strategy and .ready, joined the way the guard's jsonpath joins -# them), and its history statuses (.history). A missing fixture file +# them), its history statuses (.history), and whether it exists at all +# (`-o name`, which an ABSENT marker turns into NotFound). A missing fixture file # stands for a field the object does not carry, which is what kubectl prints for # an absent jsonpath - nothing. # @@ -96,6 +97,7 @@ for a in "$@"; do describe) mode=describe ;; *'status.history'*) mode=history ;; *'install.strategy.name'*) mode=probe ;; + name) mode=exists ;; kubernetes-*) name=$a ;; esac done @@ -145,6 +147,15 @@ case "$mode" in "$(cat "$FIXTURES/$name.strategy" 2>/dev/null || true)" \ "$(cat "$FIXTURES/$name.ready" 2>/dev/null || true)" ;; + exists) + # `get hr -o name`: NotFound is a non-zero exit with the message on + # stderr, which an ABSENT marker reproduces; anything else exists. + if [ -f "$FIXTURES/$name.ABSENT" ]; then + echo "Error from server (NotFound): helmreleases.helm.toolkit.fluxcd.io \"$name\" not found" >&2 + exit 1 + fi + echo "helmrelease.helm.toolkit.fluxcd.io/$name" + ;; describe) echo "stub describe of $name" ;; esac exit 0 @@ -252,6 +263,154 @@ STUB rm -rf "$tmp" } +@test "the pool release is read by the full guard, and its footprint fails the run" { + # The worker pool is kubernetes-nodes--md0, an Application HelmRelease + # the apiserver stamps like the parent, on the node-join path through the + # talos-reconcile Job it renders. Its name does not start with the addon + # prefix kubernetes--, so the addon listing never selects it, and + # before this read a failed action on it was invisible to the guard on every + # path. Parent and addon are clean here; the pool alone carries the finding. + . hack/e2e-chainsaw/_lib/run-kubernetes.sh + tmp=$(mktemp -d) + cozy_make_kubectl_stub "$tmp" + export FIXTURES="$tmp/fixtures" + printf 'kubernetes-test-latest-version-coredns\n' > "$FIXTURES/ALL_HR" + printf 'RetryOnFailure' > "$FIXTURES/kubernetes-test-latest-version.strategy" + printf 'deployed\n' > "$FIXTURES/kubernetes-test-latest-version.history" + printf 'deployed\n' > "$FIXTURES/kubernetes-test-latest-version-coredns.history" + printf 'RetryOnFailure' > "$FIXTURES/kubernetes-nodes-test-latest-version-md0.strategy" + printf 'failed\ndeployed\n' > "$FIXTURES/kubernetes-nodes-test-latest-version-md0.history" + + rc=0 + out=$(PATH="$tmp/bin:$PATH" cozy_guard_all_helmreleases tenant-test kubernetes-test-latest-version 2>&1) || rc=$? + if [ "$rc" -eq 0 ]; then + echo "expected the pool release footprint to fail the run, got: $out" >&2 + exit 1 + fi + if ! printf '%s\n' "$out" | grep -q -F 'HelmRelease kubernetes-nodes-test-latest-version-md0 carries a failed or uninstalled Snapshot, and its install strategy is RetryOnFailure'; then + echo "expected the pool release to be named in the verdict, got: $out" >&2 + exit 1 + fi + if ! grep -q -F 'kubernetes-nodes-test-latest-version-md0' "$FIXTURES/CALLS"; then + echo "expected a read of the pool release, calls were: $(cat "$FIXTURES/CALLS")" >&2 + exit 1 + fi + + rm -rf "$tmp" +} + +@test "a clean pool release leaves the full guard green" { + # The pin for the passing path: with every release clean the composite + # returns zero, so the pool read above is a read and not an unconditional + # verdict. The pool name is derived from the parent, the way the addon + # prefix is, which this fixture spells out. + . hack/e2e-chainsaw/_lib/run-kubernetes.sh + tmp=$(mktemp -d) + cozy_make_kubectl_stub "$tmp" + export FIXTURES="$tmp/fixtures" + printf 'kubernetes-test-latest-version-coredns\n' > "$FIXTURES/ALL_HR" + printf 'RetryOnFailure' > "$FIXTURES/kubernetes-test-latest-version.strategy" + printf 'deployed\n' > "$FIXTURES/kubernetes-test-latest-version.history" + printf 'deployed\n' > "$FIXTURES/kubernetes-test-latest-version-coredns.history" + printf 'RetryOnFailure' > "$FIXTURES/kubernetes-nodes-test-latest-version-md0.strategy" + printf 'deployed\n' > "$FIXTURES/kubernetes-nodes-test-latest-version-md0.history" + + PATH="$tmp/bin:$PATH" cozy_guard_all_helmreleases tenant-test kubernetes-test-latest-version + [ "$(cozy_pool_helmrelease_name kubernetes-test-latest-version)" = kubernetes-nodes-test-latest-version-md0 ] + + rm -rf "$tmp" +} + +@test "the pool release is held to a stamped install strategy like the parent" { + # The pool is an Application HelmRelease, so an empty strategy there is the + # read and never the object, the same as on the parent. Without this the + # jsonpath rename the parent check exists for would leave the pool's + # footprint on the lenient branch while the parent's check still passed. + . hack/e2e-chainsaw/_lib/run-kubernetes.sh + tmp=$(mktemp -d) + cozy_make_kubectl_stub "$tmp" + export FIXTURES="$tmp/fixtures" + printf 'kubernetes-test-latest-version-coredns\n' > "$FIXTURES/ALL_HR" + printf 'RetryOnFailure' > "$FIXTURES/kubernetes-test-latest-version.strategy" + printf 'deployed\n' > "$FIXTURES/kubernetes-test-latest-version.history" + printf 'deployed\n' > "$FIXTURES/kubernetes-test-latest-version-coredns.history" + printf 'True' > "$FIXTURES/kubernetes-nodes-test-latest-version-md0.ready" + printf 'deployed\n' > "$FIXTURES/kubernetes-nodes-test-latest-version-md0.history" + + rc=0 + out=$(PATH="$tmp/bin:$PATH" cozy_guard_all_helmreleases tenant-test kubernetes-test-latest-version 2>&1) || rc=$? + if [ "$rc" -eq 0 ]; then + echo "expected an empty install strategy on the pool release to fail the run, got: $out" >&2 + exit 1 + fi + if ! printf '%s\n' "$out" | grep -q -F 'spec.install.strategy.name of kubernetes-nodes-test-latest-version-md0 came back empty'; then + echo "expected the failure to name the pool release, got: $out" >&2 + exit 1 + fi + + rm -rf "$tmp" +} + +@test "the failure-path report classifies the pool release too" { + # Same reading as the passing path, from the exit handler: a pool release + # that failed an action is named, and the report still returns zero. + . hack/e2e-chainsaw/_lib/run-kubernetes.sh + tmp=$(mktemp -d) + cozy_make_kubectl_stub "$tmp" + export FIXTURES="$tmp/fixtures" + printf 'kubernetes-test-latest-version-coredns\n' > "$FIXTURES/ALL_HR" + printf 'RetryOnFailure' > "$FIXTURES/kubernetes-test-latest-version.strategy" + printf 'deployed\n' > "$FIXTURES/kubernetes-test-latest-version.history" + printf 'deployed\n' > "$FIXTURES/kubernetes-test-latest-version-coredns.history" + printf 'RetryOnFailure' > "$FIXTURES/kubernetes-nodes-test-latest-version-md0.strategy" + printf 'failed\ndeployed\n' > "$FIXTURES/kubernetes-nodes-test-latest-version-md0.history" + + rc=0 + out=$(PATH="$tmp/bin:$PATH" cozy_report_helmrelease_remediation tenant-test kubernetes-test-latest-version 2>&1) || rc=$? + if [ "$rc" -ne 0 ]; then + echo "expected the report to return zero, got rc=$rc: $out" >&2 + exit 1 + fi + if ! printf '%s\n' "$out" | grep -q -F 'HelmRelease kubernetes-nodes-test-latest-version-md0 carries a failed or uninstalled Snapshot'; then + echo "expected the pool release footprint to be classified, got: $out" >&2 + exit 1 + fi + + rm -rf "$tmp" +} + +@test "the failure-path report says when the pool release was never created" { + # The pool is applied after the parent, so a run that failed between the two + # has no pool release. That is a fact about where the run stopped, and the + # report says so instead of reporting a failed read of a missing object. + . hack/e2e-chainsaw/_lib/run-kubernetes.sh + tmp=$(mktemp -d) + cozy_make_kubectl_stub "$tmp" + export FIXTURES="$tmp/fixtures" + printf 'kubernetes-test-latest-version-coredns\n' > "$FIXTURES/ALL_HR" + printf 'RetryOnFailure' > "$FIXTURES/kubernetes-test-latest-version.strategy" + printf 'deployed\n' > "$FIXTURES/kubernetes-test-latest-version.history" + printf 'deployed\n' > "$FIXTURES/kubernetes-test-latest-version-coredns.history" + : > "$FIXTURES/kubernetes-nodes-test-latest-version-md0.ABSENT" + + rc=0 + out=$(PATH="$tmp/bin:$PATH" cozy_report_helmrelease_remediation tenant-test kubernetes-test-latest-version 2>&1) || rc=$? + if [ "$rc" -ne 0 ]; then + echo "expected the report to return zero, got rc=$rc: $out" >&2 + exit 1 + fi + if ! printf '%s\n' "$out" | grep -q -F 'kubernetes-nodes-test-latest-version-md0 does not exist'; then + echo "expected the report to say the pool release does not exist, got: $out" >&2 + exit 1 + fi + if printf '%s\n' "$out" | grep -q -F 'Reading .spec.install.strategy.name and the Ready condition of kubernetes-nodes-test-latest-version-md0 failed'; then + echo "expected no failed read of the absent pool release, got: $out" >&2 + exit 1 + fi + + rm -rf "$tmp" +} + @test "single-release guard fails a RetryOnFailure release that failed and recovered" { # The entry point the parent kubernetes- release goes through, and the # shape the rule exists for. The apiserver stamps RetryOnFailure on every From 7bed15d579f94afde7c67d4006353ff28b429983 Mon Sep 17 00:00:00 2001 From: Marian Koreniuk Date: Wed, 28 Oct 2026 18:45:23 +0100 Subject: [PATCH 2/3] fix(e2e): tell a failed pool existence read from an absent pool release The failure-path report checked the pool release with `get hr -o name` and discarded the exit status and stderr, so a timeout, a kill, a Forbidden or a refused connection all printed that the run ended before the worker pool was applied. That is the conflation the guard wrapper exists to refuse, on the path where a slow apiserver is most likely. Read it with --ignore-not-found instead: exit 0 with empty output is an absent release, exit 0 with a name is one to classify, and any non-zero exit is a failed read named through _cozy_guard_read_failure. The pool reads also skipped the report's own deadline and checked only the phase clock, which says yes on every path that opens no phase. Check the deadline before the existence read, like the listing and the addons do, and name what was not read. The stub's ABSENT marker now answers the flag the way kubectl does. New cases pin the failed read and the deadline; the two budget cases are re-timed for the extra clock reading. Signed-off-by: Marian Koreniuk Assisted-by: LLM (cherry picked from commit 4d7d336efcb2a70dbfff4678995b92b2fd820e82) Signed-off-by: cozystack-ci[bot] <274107086+cozystack-ci[bot]@users.noreply.github.com> --- hack/e2e-chainsaw/_lib/run-kubernetes.sh | 24 +++-- hack/run-kubernetes-remediation_test.bats | 124 +++++++++++++++++++--- 2 files changed, 129 insertions(+), 19 deletions(-) diff --git a/hack/e2e-chainsaw/_lib/run-kubernetes.sh b/hack/e2e-chainsaw/_lib/run-kubernetes.sh index bd953d2db..d0ee48795 100644 --- a/hack/e2e-chainsaw/_lib/run-kubernetes.sh +++ b/hack/e2e-chainsaw/_lib/run-kubernetes.sh @@ -5512,7 +5512,8 @@ _COZY_GUARD_REPORT_DONE=0 # failing path would replace the caller's own exit status, and the tenant # crust-gather snapshot hangs off that exit. # -# Bounded on its own clock, checked before the listing and before each release. +# Bounded on its own clock, checked before the pool release, before the listing +# and before each addon release. # What comes after this on the exit path is the tenant snapshot, which needs the # rest of the op, so a namespace whose apiserver answers slowly must cost a # bounded number of seconds here rather than however many releases happen to be @@ -5532,7 +5533,7 @@ cozy_report_helmrelease_remediation() { local _ns="$1" local _parent="$2" local _label="${3:-}" - local _addon_hrs _hr _pool _deadline _budget + local _addon_hrs _hr _pool _pool_found _read_rc _deadline _budget [ "${_COZY_GUARD_REPORT_DONE}" -eq 0 ] || return 0 # An empty parent name means the run failed before the release was created, so # there is no history anywhere to read yet. @@ -5560,13 +5561,24 @@ cozy_report_helmrelease_remediation() { cozy_guard_helmrelease "$_ns" "${_parent}" required || true # The pool release is created by a later apply than the parent, so a run that # failed between the two has none to read. Its absence is then a fact about - # where the run stopped, not a failed read, and is reported as such. + # where the run stopped, not a failed read, and is reported as such. Only an + # exit 0 with empty output means absent: --ignore-not-found makes NotFound + # that, so any non-zero exit is a read that failed and is named as one. _pool=$(cozy_pool_helmrelease_name "${_parent}") cozy_diag_phase_has_time "HelmRelease remediation footprint of ${_pool}" || return 0 - if _cozy_guard_kubectl get hr -n "$_ns" "${_pool}" -o name >/dev/null 2>&1; then - cozy_guard_helmrelease "$_ns" "${_pool}" required || true - else + if [ "$(date +%s)" -ge "${_deadline}" ]; then + echo "=== HelmRelease remediation footprint: stopped after ${_budget}s; ${_pool} and the addon releases of ${_parent} were not read, so nothing was observed about any of them ===" >&2 + return 0 + fi + _cozy_guard_read_bounds + _read_rc=0 + _pool_found=$(_cozy_guard_kubectl get hr -n "$_ns" "${_pool}" --ignore-not-found -o name) || _read_rc=$? + if [ "${_read_rc}" -ne 0 ]; then + echo "Checking whether ${_pool} exists failed - $(_cozy_guard_read_failure "${_read_rc}"); nothing was observed about it either way." >&2 + elif [ -z "${_pool_found}" ]; then echo "» ${_pool} does not exist - the run ended before the worker pool was applied, or its HelmRelease was never created; nothing to read" + else + cozy_guard_helmrelease "$_ns" "${_pool}" required || true fi cozy_diag_phase_has_time "the addon HelmRelease listing of ${_parent}" || return 0 if [ "$(date +%s)" -ge "${_deadline}" ]; then diff --git a/hack/run-kubernetes-remediation_test.bats b/hack/run-kubernetes-remediation_test.bats index 8464db61a..fc06e2ab1 100644 --- a/hack/run-kubernetes-remediation_test.bats +++ b/hack/run-kubernetes-remediation_test.bats @@ -33,9 +33,10 @@ load test_helper # (from ALL_HR), one release's install strategy and Ready condition together # (.strategy and .ready, joined the way the guard's jsonpath joins # them), its history statuses (.history), and whether it exists at all -# (`-o name`, which an ABSENT marker turns into NotFound). A missing fixture file -# stands for a field the object does not carry, which is what kubectl prints for -# an absent jsonpath - nothing. +# (`--ignore-not-found -o name`, which a .ABSENT marker turns into empty +# output, and whose read the markers below take as .exists). A missing +# fixture file stands for a field the object does not carry, which is what +# kubectl prints for an absent jsonpath - nothing. # # A FAILS marker file makes the matching read exit non-zero with a message on # stderr, which is how the API answers a timeout or a denied request. That @@ -105,6 +106,7 @@ case "$mode" in list) markers=LIST ;; history) markers="$name.history" ;; probe) markers="$name.strategy $name.ready" ;; + exists) markers="$name.exists" ;; *) markers= ;; esac for m in $markers; do @@ -148,9 +150,14 @@ case "$mode" in "$(cat "$FIXTURES/$name.ready" 2>/dev/null || true)" ;; exists) - # `get hr -o name`: NotFound is a non-zero exit with the message on - # stderr, which an ABSENT marker reproduces; anything else exists. + # `get hr --ignore-not-found -o name`: an absent release prints + # nothing and exits 0, which an ABSENT marker reproduces; anything else + # exists. Without the flag kubectl answers NotFound with a non-zero exit, + # so a read that drops it is told apart here the way the cluster would. if [ -f "$FIXTURES/$name.ABSENT" ]; then + case " $* " in + *' --ignore-not-found '*) exit 0 ;; + esac echo "Error from server (NotFound): helmreleases.helm.toolkit.fluxcd.io \"$name\" not found" >&2 exit 1 fi @@ -411,6 +418,96 @@ STUB rm -rf "$tmp" } +@test "a failed existence read of the pool release is not reported as absence" { + # The report runs on the failure path, where a slow apiserver is most + # likely. A cut-off existence read said "does not exist" there, which sends + # the reader to the wrong stage of the run; it has to be named as the failed + # read it is, the way the parent's and the listing's are, and the walk goes + # on to the addons. + . hack/e2e-chainsaw/_lib/run-kubernetes.sh + tmp=$(mktemp -d) + cozy_make_kubectl_stub "$tmp" + export FIXTURES="$tmp/fixtures" + printf 'kubernetes-test-latest-version-coredns\n' > "$FIXTURES/ALL_HR" + printf 'RetryOnFailure' > "$FIXTURES/kubernetes-test-latest-version.strategy" + printf 'deployed\n' > "$FIXTURES/kubernetes-test-latest-version.history" + printf 'deployed\n' > "$FIXTURES/kubernetes-test-latest-version-coredns.history" + : > "$FIXTURES/kubernetes-nodes-test-latest-version-md0.exists.CUTOFF" + + rc=0 + out=$(PATH="$tmp/bin:$PATH" cozy_report_helmrelease_remediation tenant-test kubernetes-test-latest-version 2>&1) || rc=$? + if [ "$rc" -ne 0 ]; then + echo "expected the report to return zero, got rc=$rc: $out" >&2 + exit 1 + fi + if ! printf '%s\n' "$out" | grep -q -F 'Checking whether kubernetes-nodes-test-latest-version-md0 exists failed - the read did not finish'; then + echo "expected the cut-off existence read to be named as a failed read, got: $out" >&2 + exit 1 + fi + if printf '%s\n' "$out" | grep -q -F 'kubernetes-nodes-test-latest-version-md0 does not exist'; then + echo "expected a failed read not to be reported as absence, got: $out" >&2 + exit 1 + fi + if grep -F 'kubernetes-nodes-test-latest-version-md0' "$FIXTURES/CALLS" | grep -q -v -F -e '-o name'; then + echo "expected no classification read of a release whose existence is unknown, calls were: $(cat "$FIXTURES/CALLS")" >&2 + exit 1 + fi + if ! printf '%s\n' "$out" | grep -q 'HelmRelease kubernetes-test-latest-version-coredns '; then + echo "expected the addons to be read after the failed pool read, got: $out" >&2 + exit 1 + fi + + rm -rf "$tmp" +} + +@test "the failure-path report does not read the pool release past its budget" { + # The parent's reads are issued before the first clock check, so on a path + # that opens no phase they can spend the whole budget on their own. The pool + # release is three more bounded reads, and starting them past the deadline + # takes them out of the tenant snapshot's time. + . hack/e2e-chainsaw/_lib/run-kubernetes.sh + tmp=$(mktemp -d) + cozy_make_kubectl_stub "$tmp" + export FIXTURES="$tmp/fixtures" + printf 'kubernetes-test-latest-version-coredns\n' > "$FIXTURES/ALL_HR" + printf 'RetryOnFailure' > "$FIXTURES/kubernetes-test-latest-version.strategy" + printf 'deployed\n' > "$FIXTURES/kubernetes-test-latest-version.history" + printf 'RetryOnFailure' > "$FIXTURES/kubernetes-nodes-test-latest-version-md0.strategy" + printf 'failed\ndeployed\n' > "$FIXTURES/kubernetes-nodes-test-latest-version-md0.history" + # The deadline is stamped at 1000+90 and the pool release is checked one + # reading later, at 1100. + date_ticks="$tmp/ticks" + printf '0' > "$date_ticks" + date_now=1000 + date_step=100 + COZY_GUARD_REPORT_BUDGET=90 + + rc=0 + out=$(PATH="$tmp/bin:$PATH" cozy_report_helmrelease_remediation tenant-test kubernetes-test-latest-version 2>&1) || rc=$? + date_now= + date_step=0 + date_ticks= + unset COZY_GUARD_REPORT_BUDGET + if [ "$rc" -ne 0 ]; then + echo "expected the report to return zero, got rc=$rc: $out" >&2 + exit 1 + fi + if ! printf '%s\n' "$out" | grep -q 'HelmRelease kubernetes-test-latest-version (install strategy'; then + echo "expected the parent to have been classified before the budget ran out, got: $out" >&2 + exit 1 + fi + if grep -q -F 'kubernetes-nodes-test-latest-version-md0' "$FIXTURES/CALLS"; then + echo "expected no read of the pool release once the budget was spent, calls were: $(cat "$FIXTURES/CALLS")" >&2 + exit 1 + fi + if ! printf '%s\n' "$out" | grep -q -F 'stopped after 90s; kubernetes-nodes-test-latest-version-md0 and the addon releases of kubernetes-test-latest-version were not read'; then + echo "expected the skipped pool release to be named, got: $out" >&2 + exit 1 + fi + + rm -rf "$tmp" +} + @test "single-release guard fails a RetryOnFailure release that failed and recovered" { # The entry point the parent kubernetes- release goes through, and the # shape the rule exists for. The apiserver stamps RetryOnFailure on every @@ -1269,14 +1366,15 @@ STUB printf 'deployed\n' > "$FIXTURES/kubernetes-test-latest-version.history" printf 'deployed\n' > "$FIXTURES/kubernetes-test-latest-version-cilium.history" printf 'deployed\n' > "$FIXTURES/kubernetes-test-latest-version-coredns.history" - # 30s per reading against a 90s budget: the report stamps its deadline at - # 1000+90, the listing is checked at 1030, the first addon at 1060 and the - # second at 1090, which is the first reading the deadline refuses. + # 30s per reading against a 120s budget: the report stamps its deadline at + # 1000+120, the pool release is checked at 1030, the listing at 1060, the + # first addon at 1090 and the second at 1120, which is the first reading + # the deadline refuses. date_ticks="$tmp/ticks" printf '0' > "$date_ticks" date_now=1000 date_step=30 - COZY_GUARD_REPORT_BUDGET=90 + COZY_GUARD_REPORT_BUDGET=120 rc=0 out=$(PATH="$tmp/bin:$PATH" cozy_report_helmrelease_remediation tenant-test kubernetes-test-latest-version 2>&1) || rc=$? @@ -1296,7 +1394,7 @@ STUB echo "expected the second addon to fall outside the budget, got: $out" >&2 exit 1 fi - if ! printf '%s\n' "$out" | grep -q -F 'stopped after 90s; kubernetes-test-latest-version-coredns and every release after it were not read'; then + if ! printf '%s\n' "$out" | grep -q -F 'stopped after 120s; kubernetes-test-latest-version-coredns and every release after it were not read'; then echo "expected the cut-off release to be named, got: $out" >&2 exit 1 fi @@ -1479,13 +1577,13 @@ $out" >&2 printf 'RetryOnFailure' > "$FIXTURES/kubernetes-test-latest-version.strategy" printf 'deployed\n' > "$FIXTURES/kubernetes-test-latest-version.history" printf 'uninstalled\ndeployed\n' > "$FIXTURES/kubernetes-test-latest-version-cilium.history" - # The deadline is stamped at 1000+90 and the listing is checked one reading - # later, at 1100. + # The deadline is stamped at 1000+150, the pool release is admitted at 1100 + # and the listing is checked one reading later, at 1200. date_ticks="$tmp/ticks" printf '0' > "$date_ticks" date_now=1000 date_step=100 - COZY_GUARD_REPORT_BUDGET=90 + COZY_GUARD_REPORT_BUDGET=150 rc=0 out=$(PATH="$tmp/bin:$PATH" cozy_report_helmrelease_remediation tenant-test kubernetes-test-latest-version 2>&1) || rc=$? From 6b17a76ef6cbd27cca6c1035040ce66e6365b90a Mon Sep 17 00:00:00 2001 From: Marian Koreniuk Date: Thu, 29 Oct 2026 12:34:22 +0100 Subject: [PATCH 3/3] fix(e2e): arm the tenant failure handler before the pool apply The exit handler was armed after the KubernetesNodes apply, so a failing pool apply exited under errexit with no handler installed: the parent went unclassified, and the report's "pool release does not exist" line described a run that could never reach it. Arm it right after the parent Kubernetes apply instead. The snapshot half still no-ops until the tenant kubeconfig is set, so the only change is that a failed pool apply now gets the parent classified and the absent pool reported. The ordering pin now also requires the handler to be armed before the pool apply. Assisted-by: LLM Signed-off-by: Marian Koreniuk (cherry picked from commit 75bb70e58dd695c286f157103ccbbac9c2cbd4ab) Signed-off-by: cozystack-ci[bot] <274107086+cozystack-ci[bot]@users.noreply.github.com> --- hack/e2e-chainsaw/_lib/run-kubernetes.sh | 50 +++++++++++++---------- hack/run-kubernetes-remediation_test.bats | 20 +++++++-- 2 files changed, 46 insertions(+), 24 deletions(-) diff --git a/hack/e2e-chainsaw/_lib/run-kubernetes.sh b/hack/e2e-chainsaw/_lib/run-kubernetes.sh index d0ee48795..84b5da33b 100644 --- a/hack/e2e-chainsaw/_lib/run-kubernetes.sh +++ b/hack/e2e-chainsaw/_lib/run-kubernetes.sh @@ -5559,11 +5559,13 @@ cozy_report_helmrelease_remediation() { cozy_diag_phase_has_time "HelmRelease remediation footprint of ${_parent}" || return 0 echo "=== ${_label}tenant HelmRelease remediation footprint (management cluster, ns ${_ns}) — classification only, this does not decide the run ===" cozy_guard_helmrelease "$_ns" "${_parent}" required || true - # The pool release is created by a later apply than the parent, so a run that - # failed between the two has none to read. Its absence is then a fact about - # where the run stopped, not a failed read, and is reported as such. Only an - # exit 0 with empty output means absent: --ignore-not-found makes NotFound - # that, so any non-zero exit is a read that failed and is named as one. + # The pool release is created by a later apply than the parent, and + # run_kubernetes_test arms the exit handler between the two, so a run whose + # pool apply failed reaches this with none to read. Its absence is then a fact + # about where the run stopped, not a failed read, and is reported as such. + # Only an exit 0 with empty output means absent: --ignore-not-found makes + # NotFound that, so any non-zero exit is a read that failed and is named as + # one. _pool=$(cozy_pool_helmrelease_name "${_parent}") cozy_diag_phase_has_time "HelmRelease remediation footprint of ${_pool}" || return 0 if [ "$(date +%s)" -ge "${_deadline}" ]; then @@ -5762,6 +5764,28 @@ ${ouroboros_addon} version: "${k8s_version}" EOF + # Armed here, at the parent apply, rather than once the tenant answers. The + # parent HelmRelease exists from this point on -- the aggregated apiserver + # creates it before it answers the apply above -- and everything between here + # and the tenant LB is a management-cluster wait on that release or on what it + # installs: the KamajiControlPlane, the control-plane Deployments, the + # MachineDeployment, the admin-kubeconfig Secret. That window is the one the + # guard was written for. An install action that times out while + # admin-kubeconfig is still being provisioned leaves the failed Snapshot this + # reads, and every one of those waits exits under errexit with no reading at + # all if the handler is armed after them. + # + # Ahead of the pool apply below as well, not after it: a pool apply that fails + # exits under errexit too, and armed after it that run would leave the parent + # unclassified. Armed here, it is the run the report's "pool release does not + # exist" line describes. + # + # Arming this early costs the snapshot nothing: _tenant_snapshot_on_fail + # returns on an unset CURRENT_TENANT_KC, which is assigned where the + # kubeconfig becomes usable, so it no-ops until then. + CURRENT_TENANT_PARENT_HR="kubernetes-${test_name}" + trap '_tenant_failure_on_exit' EXIT + # Worker node pool is a separate KubernetesNodes resource since the Phase 2 # split. Named "-md0" so it produces MachineDeployment # kubernetes-${test_name}-md0 — the same object the pre-split @@ -5820,22 +5844,6 @@ ${talos_block} storageClass: "${storage_class}" version: "${k8s_version}" EOF - # Armed here, at the apply, rather than once the tenant answers. The parent - # HelmRelease exists from this point on -- the aggregated apiserver renders it - # from the CR above -- and everything between here and the tenant LB is a - # management-cluster wait on that release or on what it installs: the - # KamajiControlPlane, the control-plane Deployments, the MachineDeployment, - # the admin-kubeconfig Secret. That window is the one the guard was written - # for. An install action that times out while admin-kubeconfig is still being - # provisioned leaves the failed Snapshot this reads, and every one of those - # waits exits under errexit with no reading at all if the handler is armed - # after them. - # - # Arming this early costs the snapshot nothing: _tenant_snapshot_on_fail - # returns on an unset CURRENT_TENANT_KC, which is assigned where the - # kubeconfig becomes usable, so it no-ops until then. - CURRENT_TENANT_PARENT_HR="kubernetes-${test_name}" - trap '_tenant_failure_on_exit' EXIT # Wait for the tenant-test namespace to be active kubectl wait namespace tenant-test --timeout=20s --for=jsonpath='{.status.phase}'=Active diff --git a/hack/run-kubernetes-remediation_test.bats b/hack/run-kubernetes-remediation_test.bats index fc06e2ab1..6b71fddc2 100644 --- a/hack/run-kubernetes-remediation_test.bats +++ b/hack/run-kubernetes-remediation_test.bats @@ -387,9 +387,10 @@ STUB } @test "the failure-path report says when the pool release was never created" { - # The pool is applied after the parent, so a run that failed between the two - # has no pool release. That is a fact about where the run stopped, and the - # report says so instead of reporting a failed read of a missing object. + # The pool is applied after the parent, and the exit handler is armed between + # the two, so a run whose pool apply failed reaches the report with no pool + # release. That is a fact about where the run stopped, and the report says so + # instead of reporting a failed read of a missing object. . hack/e2e-chainsaw/_lib/run-kubernetes.sh tmp=$(mktemp -d) cozy_make_kubectl_stub "$tmp" @@ -1484,6 +1485,19 @@ $out" >&2 echo "expected the handler armed (line $arm) before the control-plane wait (line $cp_wait)" >&2 exit 1 fi + # And before the pool apply, which follows the parent's. A failing pool + # apply exits under errexit, so armed after it that run leaves the parent + # unclassified, and the report's absent-pool line describes a run that never + # reaches it. + pool_apply=$(grep -n '^kind: KubernetesNodes$' "$lib" | head -n 1 | cut -d: -f1) + if [ -z "$pool_apply" ]; then + echo "expected to find the KubernetesNodes apply in $lib" >&2 + exit 1 + fi + if [ "$arm" -ge "$pool_apply" ]; then + echo "expected the handler armed (line $arm) before the pool apply (line $pool_apply)" >&2 + exit 1 + fi # The parent name is part of this one, because it is what tells the # node-join call apart from the handler's. What follows it is not: a # section label is presentation and may change without the wiring changing.