--- name: k8s-diagnose description: > Use when a Kubernetes workload is unhealthy — pods not Ready, crash-looping or Pending — or `flux-diagnose` hands over a pod problem. Read-only kubectl triage. Not Flux reconcile or source status -> `flux-diagnose`. Not app code bugs -> `diagnose`. compatibility: Requires kubectl and a kubeconfig the caller supplies, exported or as a path, bound to the built-in view ClusterRole or a wider read-only role. allowed-tools: Bash Read metadata: category: k8s version: "0.1.0" source_keys: - adr-0032-action-tiers - delivery-guard-kubectl-allowlist --- ## Gotchas - `The connection to the server localhost:8080 was refused` means kubectl found no kubeconfig, not that a cluster is down. Never pick credentials yourself: no `--kubeconfig`, `--context`, `--as` or `--token`, no `kubectl config use-context`, no filesystem search. - The `view` role reads no Secrets, nodes, PersistentVolumes or StorageClasses. A Forbidden there is a gap to report, not a reason to retry another way. - `describe`, `-o yaml` and logs print inline env values and whatever the app logs. Quote only the lines that prove a finding and redact anything that looks like a credential. ## Command tiers Run only the Read row. Anything else, listed or not, goes back to the caller as a recommendation. | Tier | Commands | |---|---| | Read | `kubectl auth can-i`, `api-resources`, `explain`; `get` and `describe` on any kind but Secrets (`-o wide`, `-o jsonpath`); `logs` (`--previous`, `--tail`, `-c`, `--all-containers`); `events --for`; `top pod` when the metrics API is served | | Propose | None: the caller opens the fix PR | | Apply-by-merge | The fix itself: a manifest change in the repo, applied by Flux after an approved merge | | Odd job | None: `flux reconcile` is HITL-only | | Never | `apply`, `create`, `delete`, `edit`, `patch`, `label`, `annotate`, `scale`, `set`, `rollout restart`, `rollout undo`, `cordon`, `drain`, `taint`, `exec`, `attach`, `port-forward`, `cp`, `debug`, `proxy`, live `diff`; `get secret -o …`, `describe secret`, `get --raw` on Secrets, `config view --raw` | ## Step 1 — Check access No namespace given → stop and report `need a namespace`, so the caller can supply one; never sweep `--all-namespaces`. Run every command in the environment the caller gave. Given a kubeconfig path, prefix each command with `KUBECONFIG=`: an `export` does not carry between shell calls. Run `kubectl auth can-i list pods -n `: - `yes` → continue. - `no` → stop and report `rbac gap: cannot list pods in `. - Any connection, certificate or `Unauthorized` error, or no kubeconfig → stop and report `no cluster access: `. A later Forbidden does not stop the run; record it under Gaps. ## Step 2 — Resolve the target to pods - **Pod** → that pod. - **Workload** (Deployment, StatefulSet, DaemonSet, Job) → `kubectl describe / -n `, then `kubectl get pods -n -l '' -o wide` with `` copied verbatim from the describe output's `Selector:` line, never rebuilt from `matchLabels`, which drops `matchExpressions`. - **Namespace** → `kubectl get pods -n -o wide` and `kubectl get deploy,sts,ds,job,pvc,svc -n `. A pod is unhealthy when its phase is not `Running` or `Succeeded`, its READY count is short, or its restart count is climbing. Target not found → a finding; if the repo should create it, Flux likely never applied it: hand to `flux-diagnose`. ## Step 3 — Triage each unhealthy pod If any pod is unhealthy, read `references/symptoms.md`: run its baseline commands on each unhealthy pod, then follow its symptom row. ## Step 4 — Check storage and Services If the target's scope holds a PVC or a Service, read `references/symptoms.md` Gotchas and §Storage and Services: a PVC not `Bound` or a Service with no ready endpoints is a finding. ## Step 5 — Report ```text k8s-diagnose: in — - — evidence: `` Likely cause: Gaps: ``` Never apply a fix; name the manifest change for the caller's PR. When the cause sits in the Flux layer, end with `hand to flux-diagnose: `.