feat(gitops): add flux-diagnose for read-only Flux reconcile checks

flux-diagnose reports source, Kustomization and HelmRelease state,
answers "has this commit been applied yet?" with bounded polling (a cap
counted in 60-second kubectl wait pauses, never flux reconcile), and
walks a failure down the source -> Kustomization -> HelmRelease chain,
handing workload problems to k8s-diagnose. Read tier only; it uses the
caller's kubeconfig and never looks for credentials.

gitops 0.2.0; the package description now covers live reconcile state.

Refs: #194

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi
This commit is contained in:
Defame1297andClaude Opus 5.5 committed 2026-11-24 20:34:42 +00:00
1 parent 67e2523252
commit 2a6eea3db1
6 files changed
+362 -2

No files matched your search

@@ -0,0 +1,134 @@
---
source_keys:
- adr-0032-action-tiers
- delivery-guard-flux-allowlist
---
# Applied check flow
Answer one question: has this Kustomization applied this commit? Read tier only; every command
here is in the `SKILL.md` tier table.
## Inputs
- Kustomization name and namespace. No namespace given → `flux-system`, and say so in the output.
- Target commit: a full 40-character SHA. Given a short SHA or a branch name, return
`unverifiable: need a full commit SHA` and stop; do not resolve it yourself.
- Wait cap: the caller passes a cap in minutes. It counts 60-second pauses, not wall-clock
time, so a cap of N allows at most N pauses. No cap given → read once and
return; never invent a wait.
- HelmReleases, optional: a list of `<ns>/<name>`. Given → check only those. Not given → check
only the HelmReleases this Kustomization applies itself (step 3).
- Kubeconfig path, optional: given → prefix every command below with `KUBECONFIG=<path>`.
## 1 — Read the Kustomization
Run exactly, substituting name and namespace, behind the `KUBECONFIG=<path>` prefix when given:
```bash
kubectl get kustomization <name> -n <ns> -o jsonpath='{.spec.suspend}{"|"}{.spec.sourceRef.kind}{"|"}{.spec.sourceRef.namespace}{"|"}{.spec.sourceRef.name}{"|"}{.status.lastAppliedRevision}{"|"}{.status.lastAttemptedRevision}{"|"}{.status.conditions[?(@.type=="Ready")].status}{"|"}{.status.conditions[?(@.type=="Ready")].reason}{"\n"}'
```
An empty source namespace means the Kustomization's own namespace.
Gates, in order:
- `suspend` is `true` → verdict `suspended`. Stop; never resume it.
- Source kind is not `GitRepository` → verdict `unverifiable: source is <kind>; its revision
carries no commit SHA`. Stop.
## 2 — Extract SHAs
A Git revision reads `<branch-or-tag>@sha1:<sha>`, or `sha1:<sha>` when the source pins a commit.
Flux before 2.0 wrote `<branch>/<sha>`. Take the 40-hex SHA after `sha1:`, or after the last `/`
for the legacy form. Compare full SHAs only.
## 3 — Classify
"At or past the target" means the SHA equals the target, or the target is its ancestor. Test
ancestry in the repo checkout with `rtk git merge-base --is-ancestor <target> <sha>`; the exit
code is the answer. Exit 0 → at or past; exit 1 → not. Exit 128 (unknown
commit) → `rtk git fetch` once and retry; still 128 → `unknown: <sha> not in local history`.
First match wins:
1. Applied SHA at or past the target → `applied`, whatever Ready says now. Applied SHA past the
target → note `superseded by <applied-sha>`. Attempted SHA differs from the applied one and
Ready is `False` → note `later revision <attempted-sha> failing`; that failure belongs to the
later revision, never to this target.
2. Attempted SHA at or past the target, Ready `False` → `failed`: the target is not applied and
the attempt carrying it fails. Record the Ready reason and the failing HelmReleases (below),
then read `references/diagnose.md` and follow it from its step 3 for this Kustomization.
3. Attempted SHA at or past the target, Ready `Unknown` → `pending: reconciling`.
4. Ready reason `DependencyNotReady` → `pending: waiting on dependency`; name the `dependsOn`
objects from `flux get ks <name> -n <ns>`'s message.
5. Read the source with `flux get sources git <source-name> -n <source-ns>`. Source revision not
at or past the target and READY `False` → `failed` at the source, since the target cannot
arrive; diagnose as in rule 2. Source revision not at or past the target → `pending: source has
not fetched the commit`. Otherwise → `pending: queued for the next reconcile`.
On `applied`, check the HelmReleases: the caller's list, or else the HelmRelease entries of this
Kustomization's own inventory, read with
`kubectl get kustomization <name> -n <ns> -o jsonpath='{.status.inventory.entries[*].id}'`
(an id reads `<hr-ns>_<hr>_helm.toolkit.fluxcd.io_HelmRelease`). Never widen to `flux tree` of a
root Kustomization: it spans the whole cluster, and an unrelated failing release would block a
commit that never touched it. For each release read
`kubectl get helmrelease <hr> -n <hr-ns> -o jsonpath='{.metadata.generation}{"|"}{.status.observedGeneration}{"|"}{.status.conditions[?(@.type=="Ready")].status}{"|"}{.status.conditions[?(@.type=="Ready")].reason}{"\n"}'`.
Observed generation behind generation, or Ready `Unknown` → verdict becomes
`pending: helm upgrade in progress`. Ready `False` at the current generation → verdict becomes `failed`, naming the release.
A release failing since before this commit is still reported; attributing it is the caller's job.
On `failed` by rule 2, read the same releases and name each Ready `False` on the HelmReleases
line. A Kustomization with `wait: true` or `healthChecks` whose health check on a release fails is
believed, not confirmed, to keep the applied revision at the previous commit, so that release is
the failure's likely cause.
## 4 — Poll, only with a cap and a `pending` verdict
Each round makes exactly one pause, and every pause counts toward the cap, even one that returns
early. Pause on the object the verdict waits for.
Kustomization pending:
```bash
kubectl wait kustomization/<name> -n <ns> --for=jsonpath='{.status.lastAppliedRevision}'=<rev> --timeout=60s
```
`<rev>` is the target in the form of the current revision: `<ref>@sha1:<target>`, where `<ref>` is
the text before `@` in the attempted revision, or `sha1:<target>` for a source pinned to a commit.
HelmRelease pending, observed generation behind the `metadata.generation` read this round:
```bash
kubectl wait helmrelease/<hr> -n <hr-ns> --for=jsonpath='{.status.observedGeneration}'=<generation> --timeout=60s
```
HelmRelease pending, observed generation already equal and Ready `Unknown`:
```bash
kubectl wait helmrelease/<hr> -n <hr-ns> --for=jsonpath='{.status.conditions[?(@.type=="Ready")].status}'=True --timeout=60s
```
Waiting on the generation there would return at once and spend a pause without waiting. Never
pause with `--for=condition=Ready` while the generation is behind: a release still Ready at its
old generation satisfies it before helm-controller has seen the new spec.
A non-zero exit on timeout is the normal end of a pause, not an error. After every pause, repeat
steps 1–3.
Stop polling on any verdict other than `pending`, or once the pause count reaches the cap. At the
cap, return the last `pending` verdict as it stands. Never extend the cap, and never
`flux reconcile` to hurry it; that is an odd job for a human.
## Output
```text
Kustomization: <ns>/<name> Target: <sha> Verdict: <applied|pending: …|failed|suspended|unverifiable: …|unknown: …>
Applied: <lastAppliedRevision> Attempted: <lastAttemptedRevision> Ready: <status> <reason>
Source: <kind> <ns>/<name> at <revision>, Ready <status>
HelmReleases: <ns/name Ready reason>, … | none
Pauses: <n> of <cap> | not polled
Note: <superseded by …, later revision <sha> failing, defaulted namespace, or empty>
```
On `failed`, append the diagnosis output from `references/diagnose.md` below these lines.