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