|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright 2026 attune Authors |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | +# Classifier for hack/e2e-install-cert-manager.sh: first-apply OpenAPI |
| 6 | +# failures must retry; a permanently down apiserver must fail closed. |
| 7 | +# Also assert nightly and the PR-CI composite both call the helper. |
| 8 | + |
| 9 | +set -euo pipefail |
| 10 | + |
| 11 | +ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 12 | +SCRIPT="${ROOT}/hack/e2e-install-cert-manager.sh" |
| 13 | +NIGHTLY="${ROOT}/.github/workflows/e2e-nightly.yaml" |
| 14 | +COMPOSITE="${ROOT}/.github/actions/setup-e2e-cluster/action.yaml" |
| 15 | + |
| 16 | +echo "PLAN: classify e2e-install-cert-manager.sh" |
| 17 | + |
| 18 | +fail() { |
| 19 | + echo "FAIL: $*" |
| 20 | + echo "DONE: ok=false" |
| 21 | + exit 1 |
| 22 | +} |
| 23 | + |
| 24 | +[[ -f "${SCRIPT}" ]] || fail "missing ${SCRIPT}" |
| 25 | + |
| 26 | +echo "DO: nightly and setup-e2e-cluster must call the helper" |
| 27 | +grep -F -q 'hack/e2e-install-cert-manager.sh' "${NIGHTLY}" \ |
| 28 | + || fail "e2e-nightly.yaml does not invoke hack/e2e-install-cert-manager.sh" |
| 29 | +grep -F -q 'hack/e2e-install-cert-manager.sh' "${COMPOSITE}" \ |
| 30 | + || fail "setup-e2e-cluster/action.yaml does not invoke hack/e2e-install-cert-manager.sh" |
| 31 | +if grep -n 'kubectl apply -f /tmp/cert-manager.yaml' "${NIGHTLY}" "${COMPOSITE}"; then |
| 32 | + fail "bare kubectl apply of cert-manager.yaml still present; use the helper" |
| 33 | +fi |
| 34 | +echo "OK: both install paths call the helper" |
| 35 | + |
| 36 | +TMP="$(mktemp -d)" |
| 37 | +trap 'rm -rf "${TMP}"' EXIT |
| 38 | +MANIFEST="${TMP}/cert-manager.yaml" |
| 39 | +printf '%s\n' 'apiVersion: v1' 'kind: ConfigMap' >"${MANIFEST}" |
| 40 | + |
| 41 | +write_kubectl() { |
| 42 | + local apply_fails="$1" readyz_fails="$2" |
| 43 | + cat >"${TMP}/kubectl" <<EOF |
| 44 | +#!/usr/bin/env bash |
| 45 | +set -euo pipefail |
| 46 | +STATE="${TMP}/state" |
| 47 | +mkdir -p "\${STATE}" |
| 48 | +cmd="\${1:-}" |
| 49 | +shift || true |
| 50 | +case "\${cmd}" in |
| 51 | + get) |
| 52 | + if [[ "\${*}" == *readyz* ]]; then |
| 53 | + n=\$(cat "\${STATE}/readyz" 2>/dev/null || echo 0) |
| 54 | + n=\$((n + 1)) |
| 55 | + echo "\${n}" >"\${STATE}/readyz" |
| 56 | + if (( n <= ${readyz_fails} )); then |
| 57 | + echo "Error from server (ServiceUnavailable): apiserver not ready" >&2 |
| 58 | + exit 1 |
| 59 | + fi |
| 60 | + echo ok |
| 61 | + exit 0 |
| 62 | + fi |
| 63 | + echo "unexpected kubectl get \$*" >&2 |
| 64 | + exit 2 |
| 65 | + ;; |
| 66 | + apply) |
| 67 | + n=\$(cat "\${STATE}/apply" 2>/dev/null || echo 0) |
| 68 | + n=\$((n + 1)) |
| 69 | + echo "\${n}" >"\${STATE}/apply" |
| 70 | + if (( n <= ${apply_fails} )); then |
| 71 | + echo 'error: error validating "${MANIFEST}": error validating data: failed to download openapi: the server is currently unable to handle the request' >&2 |
| 72 | + exit 1 |
| 73 | + fi |
| 74 | + echo "configmap/ok created" |
| 75 | + exit 0 |
| 76 | + ;; |
| 77 | + wait) |
| 78 | + echo "deployment.apps/cert-manager condition met" |
| 79 | + exit 0 |
| 80 | + ;; |
| 81 | + *) |
| 82 | + echo "unexpected kubectl \${cmd} \$*" >&2 |
| 83 | + exit 2 |
| 84 | + ;; |
| 85 | +esac |
| 86 | +EOF |
| 87 | + chmod +x "${TMP}/kubectl" |
| 88 | +} |
| 89 | + |
| 90 | +run_helper() { |
| 91 | + KUBECTL="${TMP}/kubectl" \ |
| 92 | + READYZ_ATTEMPTS=5 \ |
| 93 | + READYZ_SLEEP=0 \ |
| 94 | + APPLY_ATTEMPTS=3 \ |
| 95 | + APPLY_RETRY_SLEEP=0 \ |
| 96 | + bash "${SCRIPT}" "${MANIFEST}" |
| 97 | +} |
| 98 | + |
| 99 | +echo "DO: first apply OpenAPI failure then success must pass" |
| 100 | +rm -rf "${TMP}/state" |
| 101 | +write_kubectl 1 0 |
| 102 | +if ! run_helper; then |
| 103 | + fail "retry after one apply failure should succeed" |
| 104 | +fi |
| 105 | +apply_n="$(cat "${TMP}/state/apply")" |
| 106 | +[[ "${apply_n}" == "2" ]] || fail "expected 2 apply calls, got ${apply_n}" |
| 107 | +echo "OK: retried apply after OpenAPI failure" |
| 108 | + |
| 109 | +echo "DO: first readyz miss then apply success must pass" |
| 110 | +rm -rf "${TMP}/state" |
| 111 | +write_kubectl 0 1 |
| 112 | +if ! run_helper; then |
| 113 | + fail "readyz miss then success should succeed" |
| 114 | +fi |
| 115 | +readyz_n="$(cat "${TMP}/state/readyz")" |
| 116 | +[[ "${readyz_n}" == "2" ]] || fail "expected 2 readyz calls, got ${readyz_n}" |
| 117 | +echo "OK: waited for readyz before apply" |
| 118 | + |
| 119 | +echo "DO: three apply failures must fail closed" |
| 120 | +rm -rf "${TMP}/state" |
| 121 | +write_kubectl 99 0 |
| 122 | +if run_helper; then |
| 123 | + fail "permanent apply failure should exit non-zero" |
| 124 | +fi |
| 125 | +apply_n="$(cat "${TMP}/state/apply")" |
| 126 | +[[ "${apply_n}" == "3" ]] || fail "expected 3 apply calls, got ${apply_n}" |
| 127 | +echo "OK: exhausted apply retries" |
| 128 | + |
| 129 | +echo "DO: never-ready apiserver must fail closed" |
| 130 | +rm -rf "${TMP}/state" |
| 131 | +write_kubectl 0 99 |
| 132 | +if run_helper; then |
| 133 | + fail "never-ready readyz should exit non-zero" |
| 134 | +fi |
| 135 | +if [[ -f "${TMP}/state/apply" ]]; then |
| 136 | + fail "apply must not run when readyz never succeeds" |
| 137 | +fi |
| 138 | +echo "OK: fail-closed when apiserver never becomes ready" |
| 139 | + |
| 140 | +echo "DO: --wait-only must poll readyz and skip apply" |
| 141 | +rm -rf "${TMP}/state" |
| 142 | +write_kubectl 0 1 |
| 143 | +if ! KUBECTL="${TMP}/kubectl" READYZ_ATTEMPTS=5 READYZ_SLEEP=0 \ |
| 144 | + bash "${SCRIPT}" --wait-only; then |
| 145 | + fail "--wait-only should succeed after one readyz miss" |
| 146 | +fi |
| 147 | +if [[ -f "${TMP}/state/apply" ]]; then |
| 148 | + fail "--wait-only must not call kubectl apply" |
| 149 | +fi |
| 150 | +readyz_n="$(cat "${TMP}/state/readyz")" |
| 151 | +[[ "${readyz_n}" == "2" ]] || fail "--wait-only expected 2 readyz calls, got ${readyz_n}" |
| 152 | +echo "OK: --wait-only" |
| 153 | + |
| 154 | +echo "DO: both paths must re-wait after Prometheus image import" |
| 155 | +grep -F -A6 'Load Prometheus and test images into cluster' "${NIGHTLY}" \ |
| 156 | + | grep -F -q 'hack/e2e-install-cert-manager.sh --wait-only' \ |
| 157 | + || fail "e2e-nightly.yaml missing --wait-only after Prometheus image import" |
| 158 | +grep -F -A6 'Load Prometheus and test images into cluster' "${COMPOSITE}" \ |
| 159 | + | grep -F -q 'hack/e2e-install-cert-manager.sh --wait-only' \ |
| 160 | + || fail "setup-e2e-cluster missing --wait-only after Prometheus image import" |
| 161 | +echo "OK: Prometheus import waits for readyz" |
| 162 | + |
| 163 | +echo "DONE: ok=true" |
| 164 | +echo "NEXT: none" |
| 165 | +exit 0 |
0 commit comments