Files
infra-k8/scripts/gate1-check.sh
Noel Sabia b98a1bab42
infra-ci / manifests (push) Failing after 4s
infra-ci / manifests (pull_request) Failing after 3s
feat: prepare v1.1 deploy and presenter load test
- build and push v1.1 images from the service main worktrees
- switch manifests to :v1.1; add web control Secret refs
- configure API BOOTSTRAP_REFRESH=60s and remove unused DATA_DIR
- add k6 presenter scenario with a mid-run control POST
- require control credentials in make load; update runbook and drills
2026-11-14 08:00:58 +01:00

239 lines
7.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# Read-only Gate 1 readiness check.
#
# It NEVER applies, creates, patches, deletes, scales, or restarts anything.
# Allowed calls: kubectl config/get/auth can-i/apply --dry-run=client, curl.
#
# Usage (normally through make):
# export KUBECONFIG=/path/to/fch26-onemanarmy-sa-kubeconfig.yaml
# make gate1-check
# make gate1-check CHECK_PUBLIC=1 # also curl the public URLs
# make gate1-check ALLOW_PENDING=1 # exit 0 while human secrets are pending
# make gate1-check STATIC_ONLY=1 # CI: no kubeconfig/cluster required
#
# Exit codes: 0 ready, 1 hard failure (permissions/manifests/cluster), 2 only
# human prerequisites are missing (Secrets, Deployments, public URLs).
set -uo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
NS="${NS:-fch26-onemanarmy}"
CONTEXT="${CONTEXT:-fch26-onemanarmy-sa-context}"
REGISTRY="${REGISTRY:-git.noelsabia.dev}"
OWNER="${OWNER:-noel}"
TAG="${TAG:-gate1}"
CHECK_PUBLIC="${CHECK_PUBLIC:-0}"
ALLOW_PENDING="${ALLOW_PENDING:-0}"
STATIC_ONLY="${STATIC_ONLY:-0}"
WEB_URL="${WEB_URL:-https://pulse.noelsabia.dev}"
API_URL="${API_URL:-https://api-pulse.noelsabia.dev}"
CURL_MAX_TIME="${CURL_MAX_TIME:-15}"
hard=0
pending=0
warnings=0
ok() { printf '[ok] %s\n' "$*"; }
bad() { printf '[FAIL] %s\n' "$*"; hard=$((hard + 1)); }
missing() { printf '[pending] %s\n' "$*"; pending=$((pending + 1)); }
warn() { printf '[warn] %s\n' "$*"; warnings=$((warnings + 1)); }
section() { printf '\n== %s\n' "$*"; }
run_static_checks() {
local manifest_out
if manifest_out="$(python3 \
"$ROOT/scripts/check_manifests.py" \
"$ROOT/deploy/k8s" "$NS" "$TAG" "$REGISTRY" "$OWNER" 2>&1)"; then
ok "static guardrails passed"
else
bad "static guardrails failed"
fi
printf '%s\n' "$manifest_out" | sed 's/^/ /'
}
finish() {
printf '\n== Gate 1 readiness summary\n'
printf 'hard failures: %d\n' "$hard"
printf 'pending prerequisites: %d\n' "$pending"
printf 'warnings: %d\n' "$warnings"
if ((hard > 0)); then
printf 'RESULT: NOT READY (fix the failures above)\n'
exit 1
fi
if ((pending > 0)); then
if [[ "$ALLOW_PENDING" == "1" ]]; then
printf 'RESULT: READY except for pending prerequisites (ALLOW_PENDING=1)\n'
exit 0
fi
printf 'RESULT: NOT READY (prerequisites above are still pending)\n'
exit 2
fi
printf 'RESULT: READY for Gate 1\n'
exit 0
}
if [[ "$STATIC_ONLY" == "1" ]]; then
printf 'Gate 1 static checks (cluster-free; STATIC_ONLY=1)\n'
printf 'namespace=%s tag=%s\n' "$NS" "$TAG"
section "manifest guardrails"
run_static_checks
printf '\n== Gate 1 static summary\n'
printf 'hard failures: %d\n' "$hard"
if ((hard > 0)); then
printf 'RESULT: STATIC CHECKS FAILED\n'
exit 1
fi
printf 'RESULT: STATIC CHECKS PASSED (cluster checks skipped)\n'
exit 0
fi
trap finish EXIT
printf 'Gate 1 readiness (read-only; no cluster mutation)\n'
printf 'namespace=%s context=%s tag=%s\n' "$NS" "$CONTEXT" "$TAG"
section "kubeconfig and cluster"
if [[ -z "${KUBECONFIG:-}" ]]; then
bad "KUBECONFIG is not set"
printf ' export KUBECONFIG=/path/to/fch26-onemanarmy-sa-kubeconfig.yaml\n'
else
if [[ ! -f "$KUBECONFIG" ]]; then
bad "KUBECONFIG does not point to a file: $KUBECONFIG"
else
mode="$(stat -c '%a' "$KUBECONFIG" 2>/dev/null || echo unknown)"
if [[ "$mode" != "600" && "$mode" != "400" ]]; then
warn "kubeconfig mode is $mode; run: chmod 600 \"$KUBECONFIG\""
else
ok "kubeconfig mode $mode"
fi
fi
fi
if ! command -v kubectl >/dev/null 2>&1; then
bad "kubectl not found"
exit 1
fi
if ! kubectl config get-contexts -o name 2>/dev/null | grep -Fxq "$CONTEXT"; then
bad "context '$CONTEXT' not found in KUBECONFIG"
exit 1
fi
ok "context '$CONTEXT' exists"
KC=(kubectl --context "$CONTEXT" --namespace "$NS")
if "${KC[@]}" get --raw=/readyz --request-timeout=5s >/dev/null 2>&1; then
ok "API server reachable"
else
bad "API server is not reachable with context '$CONTEXT'"
exit 1
fi
if "${KC[@]}" get deployments >/dev/null 2>&1; then
ok "namespace '$NS' is readable"
else
bad "cannot read namespace '$NS'"
exit 1
fi
section "namespace permissions (expected values)"
check_can_i() {
local expected="$1" verb="$2" resource="$3" got
got="$("${KC[@]}" auth can-i "$verb" "$resource" 2>/dev/null)"
if [[ "$got" == "$expected" ]]; then
ok "$verb $resource -> $got"
else
bad "$verb $resource -> ${got:-<error>} (expected $expected)"
fi
}
check_can_i yes create deployments
check_can_i yes create services
check_can_i yes create configmaps
check_can_i yes create secrets
check_can_i yes create pods
check_can_i no create ingresses
check_can_i no create jobs
check_can_i no create cronjobs
check_can_i no create networkpolicies
check_can_i no create roles
check_can_i no create rolebindings
section "manifest guardrails"
run_static_checks
section "kubectl client dry-run"
if dry_run_out="$("${KC[@]}" apply --dry-run=client -f "$ROOT/deploy/k8s" 2>&1)"; then
count="$(printf '%s\n' "$dry_run_out" | grep -c '(dry run)')"
ok "kubectl apply --dry-run=client passed ($count resources)"
else
bad "kubectl apply --dry-run=client failed"
printf '%s\n' "$dry_run_out" | sed 's/^/ /'
fi
section "human prerequisites: Secrets"
check_secret_keys() {
local secret="$1"
shift
if ! "${KC[@]}" get secret "$secret" >/dev/null 2>&1; then
missing "Secret/$secret is missing (create it with docs/deploy.md; never commit it)"
return
fi
ok "Secret/$secret exists"
local have key
have="$("${KC[@]}" get secret "$secret" -o go-template='{{range $k,$v := .data}}{{$k}}{{"\n"}}{{end}}' 2>/dev/null || true)"
for key in "$@"; do
if printf '%s\n' "$have" | grep -qx "$key"; then
ok "Secret/$secret has key $key"
else
missing "Secret/$secret is missing key $key"
fi
done
}
check_secret_keys gitea-regcred .dockerconfigjson
if "${KC[@]}" get secret gitea-regcred >/dev/null 2>&1; then
secret_type="$("${KC[@]}" get secret gitea-regcred -o jsonpath='{.type}' 2>/dev/null || true)"
if [[ "$secret_type" == "kubernetes.io/dockerconfigjson" ]]; then
ok "Secret/gitea-regcred type is kubernetes.io/dockerconfigjson"
else
bad "Secret/gitea-regcred type is ${secret_type:-<unknown>}, expected kubernetes.io/dockerconfigjson"
fi
fi
check_secret_keys cloudflared TUNNEL_TOKEN
check_secret_keys control-auth CONTROL_USER CONTROL_PASS CONTROL_TOKEN
section "deployment status"
for deployment in web api ml cloudflared; do
if ! "${KC[@]}" get deployment "$deployment" >/dev/null 2>&1; then
missing "Deployment/$deployment is not deployed yet"
continue
fi
ready="$("${KC[@]}" get deployment "$deployment" -o jsonpath='{.status.availableReplicas}' 2>/dev/null || true)"
desired="$("${KC[@]}" get deployment "$deployment" -o jsonpath='{.spec.replicas}' 2>/dev/null || true)"
image="$("${KC[@]}" get deployment "$deployment" -o jsonpath='{.spec.template.spec.containers[0].image}' 2>/dev/null || true)"
if [[ -n "$ready" && -n "$desired" ]] && ((ready >= desired)); then
ok "Deployment/$deployment ready ($ready/$desired, $image)"
else
bad "Deployment/$deployment not ready (${ready:-0}/${desired:-?}, $image)"
fi
if [[ "$deployment" != "cloudflared" && "$image" != *":$TAG" ]]; then
bad "Deployment/$deployment image is ${image:-<unknown>}, expected :$TAG"
fi
done
if [[ "$CHECK_PUBLIC" == "1" ]]; then
section "public HTTPS"
for target in "web|$WEB_URL/" "api|$API_URL/api/healthz"; do
name="${target%%|*}"
url="${target#*|}"
code="$(curl -sS -o /dev/null -w '%{http_code}' --max-time "$CURL_MAX_TIME" "$url" 2>/dev/null || echo 000)"
if [[ "$code" == "200" ]]; then
ok "$name public URL -> HTTP 200 ($url)"
else
missing "$name public URL -> HTTP ${code:-000} ($url)"
fi
done
else
section "public HTTPS"
printf '[skip] CHECK_PUBLIC=0; run make smoke (or gate1-check CHECK_PUBLIC=1) after deploy\n'
fi