Skip to content

feat(k8s): owner-referenced children are runtime, not orphans (#1077) - #1180

Merged
lex00 merged 1 commit into
mainfrom
feat/1077-runtime-children
Jul 28, 2026
Merged

lex00 merged 1 commit into
mainfrom
feat/1077-runtime-children

Conversation

@lex00

@lex00 lex00 commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Closes #1077. Part of epic #1073.

What was asked, restated

The diff engine has exactly one bucket for "live but chant never declared it": orphan, which routes to adopt-or-delete. That's correct for the AWS case #1014/#1015 model (an out-of-band console-added SNS subscription). It's wrong on Kubernetes: a Pod a declared Deployment's controller created is live and undeclared, but it isn't drift and it isn't abandoned — it's the runtime doing its job, and deleting it just gets it recreated. ownerReferences is the mechanism that tells the two cases apart, and no other chant substrate maintains that chain.

The ask: walk a live, undeclared object's ownerReferences chain, transitively, cycle-safe, bounded. If it reaches a declared entity, classify the object as a new category — runtime — distinct from orphan and from drift, carrying which declared entity owns it. A chain that's genuinely unowned, resolves to something foreign, or can't be fully verified stays orphan, exactly as today. Thread this through whatever already renders orphans (diff --live, plan, the graph overlay), additively, the way #1168 threaded its tri-state through the same set of consumers.

Classification rules

Owner-chain outcome ownerChain.root Diff bucket Change-set action Rationale
Chain reaches a declared, chant-observed entity declared runtimeChildren runtime Expected runtime — a Pod its Deployment created
No owner reference at all unowned orphan adopt/delete (ownership-marker driven, unchanged) Genuinely standalone
Chain fully resolves to a live, undeclared root foreign orphan adopt/delete Belongs to something real, just not this build
Unreadable hop, cycle, or depth bound hit unknown orphan adopt/delete Conservative — composes with #1168: an unconfirmed chain never earns the more confident runtime verdict
Lexicon never sets ownerChain (absent) orphan adopt/delete Unaffected — today's behavior, unchanged

runtime is never escalated to delete, even if a runtime child happens to carry chant's own ownership marker (e.g. via label propagation) — that check runs before the ownership-marker branch in buildChangeSet.

Where it lives

  • packages/core/src/owner-chain.ts (new) — OwnerChainVerdict (declared | unowned | foreign | unknown) and classifyOwnerChain(), a pure, depth-bounded (default 12), cycle-safe walker over a lexicon-supplied node map. Core owns this per the issue's own division of labor ("core owns the category and the diff-engine wiring; the k8s lexicon supplies the chain").
  • packages/core/src/lexicon.ts — ResourceMetadata.ownerChain?: OwnerChainVerdict, optional, additive.
  • lexicons/k8s/src/api/owner-chain.ts (new) — resolveK8sOwnerChain(), the chain-supplying half: reads each intermediate owner through the typed client (@intentius/chant-k8s-client, feat(k8s): typed API client — kubectl shell-outs retired (#1074) #1177) — there's no server-side "who ultimately owns this" query, Kubernetes' own GC doesn't have one either — then hands the assembled chain to core's classifyOwnerChain for the verdict.
  • lexicons/k8s/src/describe-resources.ts — after declared entities resolve, scans Pods in the namespaces those entities live in and classifies each one. Scoped deliberately narrow: Pods only (the concrete case the issue and its acceptance criteria name), and only the namespaces this observation already touched — not a cluster-wide sweep. Widening to other controller-spawned kinds (ReplicaSet, Job, …) is the same walk repeated; noted as a follow-up rather than done here, matching feat(aws): deep observation surface — Cloud Control reader for undeclared-property & out-of-band-child drift #1015's own "prove it narrow, then widen" precedent.

Consumer/wire changes (additive)

GCP: does it inherit this mechanically?

No, and it shouldn't be forced. GCP's Config Connector observation (lexicons/gcp/src/describe-resources.ts) shells kubectl per declared entity — it was deliberately kept off the typed client in #1177 ("a separate lexicon and a separate decision"). Two things don't transfer:

  1. Mechanics — resolveK8sOwnerChain is built on @intentius/chant-k8s-client's list/readIfPresent; GCP has neither. A GCP equivalent would need its own kubectl-based reader, mechanical repetition of the same walk, not reuse.
  2. The underlying shape — Config Connector CRs map close to 1:1 with GCP resources; CNRM doesn't spawn child CRs the way a Deployment spawns Pods. There's no concrete "Pod owned by Deployment"-shaped case on the GCP side today motivating the work.

What does compose for free: the core classification contract (OwnerChainVerdict, ResourceMetadata.ownerChain, the diff/change-set wiring) is fully generic. If GCP ever gains a reason to populate ownerChain, every downstream consumer already understands it — no core change needed.

Tests

  • packages/core/src/owner-chain.test.ts (new, 13 tests) — the pure walker: declared chain, immediate-declared, unowned, foreign, unreadable start/intermediate hop, dangling reference, missing start id, 2-cycle, self-cycle, depth-bound-exceeded (conservative unknown), exact-at-bound (still resolves), custom maxDepth.
  • packages/core/src/lifecycle/live-diff.test.ts — new describe("runtime children vs orphans") block: declared-chain → runtimeChildren not orphan; unowned/foreign/unknown → orphan; no-ownerChain-at-all lexicon unaffected; rolling name isn't disappeared; own status churn isn't driftedSinceSnapshot.
  • packages/core/src/lifecycle/change-set.test.ts — runtime action for a declared chain (never delete/adopt), ordering guard (marker-carrying runtime child still classifies runtime), unowned/foreign/unknown stay adopt, unaffected-lexicon regression, summarize/render surface it, gitlabMrReport excludes it.
  • packages/core/src/graph-ir-live.test.ts — buildLiveGraphIr carries runtimeOwner only for a resolved declared chain; both overlay functions tag _status: "runtime" distinct from "warn".
  • lexicons/k8s/src/api/owner-chain.test.ts (new, 10 tests) — resolveK8sOwnerChain: direct declared match (no extra reads), Pod→ReplicaSet→Deployment (one extra read, past the undeclared intermediate), fully-foreign chain, 404 owner → conservative unknown, throwing owner read (RBAC) → conservative unknown, a real cycle terminates rather than hanging, custom maxDepth bounds the fetch count, missing-uid start, multiple owner refs picks the controller: true one.
  • lexicons/k8s/src/describe-resources.test.ts — new describe("runtime children (#1077)") block against the real typed client with a faked transport (fakeCluster): Pod-owned-via-ReplicaSet-by-declared-Deployment → runtime; genuinely unowned Pod → orphan; Pod owned by something never declared → orphan; --owned withholds an unrelated Pod but keeps a runtime child; a Pod that's itself declared is never duplicated; a Pod-list failure for one namespace is best-effort (declared entities still resolve); no namespaced entities → no Pod scan at all.

No cluster or kubeconfig access anywhere — every k8s test drives the real typed client against a literal kubeconfig with the transport faked (fakeCluster/fakeRequestLayer), same as every other test in this file.

Gate evidence

just build   → npx tsc --noEmit -p packages/core/tsconfig.json — clean
just lint    → npx eslint packages/ — 0 errors, 0 warnings

npm run prepack -w k8s lexicon:
  [OK] lexicon-json-exists
  [OK] types-exist
  [OK] required-names
  [OK] types-compile
  All validation checks passed. (234 resources)

just check-lexicons → 11/11 lexicons, 17/17 tier-1 checks each, no untracked failures

just test → Test Files  797 passed | 1 skipped (798)
            Tests  9726 passed | 5 skipped (9731)

fold-differential    → fold: 76  run-fallback: 25  empty: 0  drift: 0   — 104/104, matches origin/main
sandbox-differential → identical: 101  excluded: 0  drift: 0           — 103/103, matches origin/main

k8s-client-boundary (#1074) → modules reachable: 116, importing the client: 0
                               corpus builds: 32, either package loaded: 0   — unchanged from origin/main
sandbox-execution-boundary  → 141/141 passed, report shape unchanged

Test plan

  • just build
  • just lint
  • just test (9726 passed | 5 skipped)
  • npm run prepack -w @intentius/chant-lexicon-k8s
  • just check-lexicons (11/11, no regression)
  • just fold-differential (zero drift vs origin/main)
  • just sandbox-differential (zero drift vs origin/main)
  • examples/k8s-client-boundary.test.ts + examples/sandbox-execution-boundary.test.ts (both unchanged)
  • No cluster or kubeconfig access — every test mocks the transport

A Pod a declared Deployment's controller creates is live and undeclared,
the same shape the diff engine has always classified `orphan`. That makes
`lifecycle plan` propose deletes for things the controller simply
recreates. `ownerReferences` distinguishes an expected runtime child from
a genuine orphan; no other chant substrate maintains that chain.

Core owns the new classification (`OwnerChainVerdict` in
packages/core/src/owner-chain.ts, a pure bounded/cycle-safe chain
walker) and threads it through `diffLive` (new `runtimeChildren`
bucket), `buildChangeSet` (new `runtime` action, never delete/adopt),
and the graph overlays (`_status: "runtime"` instead of `warn`/foreign).
An unresolved chain (unreadable hop, cycle, depth bound) stays
conservative and routes as `orphan`, composing with #1168's tri-state.

The k8s lexicon supplies the chain: describeResources() now scans Pods
in the namespaces of entities it just resolved and walks each one's
ownerReferences (reading past undeclared intermediates like a
ReplicaSet) against the declared/observed set from the same call.
Every consumer change is additive; a lexicon that never sets
`ownerChain` is unaffected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(core): classify owner-referenced children as runtime, not orphans

1 participant