Skip to content

feat(gcp): deep observation — Config Connector managed-fields reuse (#1087) - #1182

Merged
lex00 merged 1 commit into
mainfrom
feat/1087-gcp-deep-observation
Jul 28, 2026
Merged

lex00 merged 1 commit into
mainfrom
feat/1087-gcp-deep-observation

Conversation

@lex00

@lex00 lex00 commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Closes #1087. The GCP row of #1014, per #1173's per-row checklist and #1181's k8s row (#1076), which this reuses.

What I was asked to do, restated

Implement observeResourcesDeep() for GCP by reusing the k8s row rather than writing a second implementation. A Config Connector custom resource is a Kubernetes object: kubectl get <kind>.<group> -o json against a Config Connector-enabled cluster returns metadata.managedFields, status, and the rest of the standard object envelope for the same reason a Deployment carries them — the API server on the other end is the same kind of API server. The epic's own table lists metadata.managedFields under GCP's "field noise" to prune; it's the opposite of noise, and it's available here for exactly the reason #1076 reads it.

Three things had to come out of this:

  1. Prune Config Connector's own field noise the same way feat(k8s): deep observation via SSA managed-fields (#1014 k8s row) #1076 prunes Kubernetes controller noise — by ownership, not a static per-type table.
  2. Prune the CNRM-specific noise the acceptance criteria name: its own observed-state annotations, status, generation churn.
  3. Share the k8s row's actual logic rather than duplicating it, and never take a dependency on @intentius/chant-k8s-client (gcp shells kubectl; chant feat(k8s): typed API client — kubectl shell-outs retired (#1074) #1177/feat(k8s): owner-referenced children are runtime, not orphans (#1077) #1180 kept it that way on purpose).

What's genuinely shared, and where it lives now

The managed-fields ownership walk (buildOwnershipSets/walkOwnership, resolving one object's fieldsV1 against its live and declared trees) and the generic Kubernetes object envelope/list-map-key conventions have nothing k8s-lexicon-specific about them — they're Kubernetes API machinery, true of any Kubernetes-shaped object regardless of which lexicon reads it. Rather than have gcp depend on the k8s lexicon package to reach them, I moved them to packages/core/src/managed-fields.ts — the same reason kubectl-context.ts's resolveClusterTarget already lives in core rather than in the k8s lexicon ("GCP's Config Connector observation needs it too, without taking a dependency on the k8s lexicon").

  • K8S_OBJECT_ENVELOPE_PRUNE_PATTERNS — status, metadata.{uid,resourceVersion,generation,creationTimestamp,managedFields,selfLink}. Was k8s-lexicon-private (K8S_UNCONDITIONAL_PRUNE_PATTERNS); now core, exported.
  • k8sListMapOrderKey — Kubernetes' own list-map-key conventions (containers/env/volumes by name, ports by containerPort/port+protocol). Needed on the gcp side too: some CNRM kinds embed genuinely k8s-shaped substructures (Cloud Run's RunService wraps a Knative pod spec).
  • buildOwnershipSets, OwnershipSets, pruneByOwnership — the three-question rule itself (chant-owned → always diffable; foreign+undeclared → pruned; foreign+declared → contested, kept). Moved out of lexicons/k8s/src/deep-observe.ts verbatim.

lexicons/k8s/src/deep-observe.ts and deep-observe-hooks.ts now delegate to core for that shared half and re-export buildOwnershipSets/OwnershipSets so nothing changes for an existing importer of that file (its own test suite from #1181 runs unmodified — same 21 tests, same assertions). What stays k8s-lexicon-specific is only K8S_SERVICE_DEFAULTS, keyed by chant's own k8s entityType catalog.

What's genuinely gcp-specific:

  • No typed operation surface — operationFor is a k8s-lexicon/typed-client concept generated from cluster discovery this reader never touches. deriveGVK (already in describe-resources.ts) stands in.
  • Config Connector's own observed-state annotations (cnrm.cloud.google.com/observed-secret-versions, confirmed from Config Connector's own docs) — narrow and evidence-based, since several cnrm.cloud.google.com/* annotations (state-into-spec, deletion-policy, management-conflict-prevention-policy) are themselves user-authored configuration, not observed state, and blindly pruning that whole family would hide genuine drift on them.
  • The kubectl transport itself: describe-resources.ts gained two exported helpers (resolveGcpKubectlContext, execConfigConnectorGet) so the thin and deep readers share the exact cluster-binding resolution and kubectl get -o json mechanics rather than each restating them.

Same split k8s uses for the same reason: lexicons/gcp/src/deep-observe-hooks.ts (no transport dependency, imported statically from plugin.ts because core normalizes the declared tree with it whether or not a cluster read ever happens) vs. lexicons/gcp/src/deep-observe.ts (imports ./describe-resources.ts, which pulls in node:child_process; reached only via plugin.ts's dynamic import(), matching how describeResources/exportResources are already wired).

The manager-identity finding

Chant #1076's rule treats a field as always-diffable when any chant field manager (chant, chant:<stack> — chant #1075's SSA identity) owns it. That identity is only ever set by the k8s lexicon's typed client applying with an explicit --field-manager. GCP has no equivalent apply activity — every gcp example's own deploy script (e.g. examples/cockroachdb-multi-region-gke/scripts/deploy.sh) runs plain kubectl apply -f dist/*.yaml: classic client-side apply, no --server-side, no --field-manager. kubectl's own hardcoded default field manager for that command is kubectl-client-side-apply — identical to what a human's kubectl apply -f their-own-file.yaml would record.

There is no field-manager name on GCP's real deploy path that distinguishes "chant applied this" from "a person applied this." (Config Connector's own controller is distinguishable — it records itself as cnrm-controller-manager, per the k8s-config-connector source's own ControllerManagedFieldManager constant, confirmed via its docs/source rather than assumed.)

Working out whether that breaks the contract: it doesn't, because question 3 of the three-question rule (is the path declared?) already covers it. Chant's own kubectl apply always applies the exact built manifest, so any field chant's own most recent apply set is, by construction, also in declaredRoot on the very next read.

  • A field only chant's own applies have touched: not recognized as chantOwned (manager is kubectl-client-side-apply, not chant), but foreign-owned and declared → contested → kept diffable anyway. Same outcome as chant-owned would have been.
  • A field Config Connector's controller sets and chant never declares: foreign-owned, undeclared → pruned. Correct regardless of whether "chant" is recognized.
  • A field a person sets by hand (kubectl edit, or their own kubectl apply -f) that chant does declare: foreign-owned and declared → contested → surfaces as drift. This is the issue's acceptance criterion 1.

isGcpChantFieldManager still checks the chant/chant:<stack> family (restated locally rather than imported from @intentius/chant-k8s-client — a two-branch string comparison, not the kind of logic the "never duplicate" instruction is protecting), so a future gcp apply path that does route through server-side apply with an explicit chant manager is recognized with no change needed here. On today's plain-apply path it's provably inert (a dedicated test drives the same mutation through kubectl-client-side-apply, cnrm-controller-manager, and an arbitrary third-party operator, and gets identical results) — the contested-field rule is what actually keeps GCP's drift semantics correct without it.

Tests

lexicons/gcp/src/deep-observe.test.ts, same harness describe-resources.test.ts uses (node:child_process's exec mocked, no ambient kubectl config, no cluster contacted):

  • Wiring: gcpPlugin.observeResourcesDeep exists; deepNormalizationHooks is the shared static instance.
  • gcpDeepNormalizationHooks unit tests: envelope pruning, the CNRM observed-state-vs-user-config annotation distinction, list-map ordering reused for an embedded k8s pod spec.
  • observeResourcesDeepGcp unit tests: unsupported-kind, read-failure-as-hole, --owned filtering.
  • End-to-end (the reference shape): a bucket (hand-kubectl edit-ed label → drift; CNRM-owned undeclared field and its observed-state annotation → pruned; classic kubectl apply's own last-applied-configuration annotation → pruned by the reused ownership rule with no special case, proving that claim rather than asserting it) and a SQL instance (Config Connector's own controller currently holds a field chant declares differently → contested → still drift, sharpening "CNRM-owned doesn't surface as drift" to undeclared CNRM-owned specifically), plus unsupported-kind and read-failed entities, a baseline acceptance case, and a whole-lexicon-failure case routed through core's real observeDeep.
  • The managers-specific case: the identical mutation on a declared field is drift under three different non-chant managers (kubectl-client-side-apply, cnrm-controller-manager, an arbitrary third-party operator) and silence under the same three when undeclared, plus a check that an explicit chant:<stack> manager (were it ever used on this path) is still recognized as chant-owned rather than merely contested.

packages/core/src/managed-fields.test.ts (new): direct unit tests for the relocated K8S_OBJECT_ENVELOPE_PRUNE_PATTERNS, k8sListMapOrderKey, buildOwnershipSets (ported from the k8s lexicon's own suite, now exercised at its new home), and pruneByOwnership.

Test plan

  • just build — core tsc clean
  • npm run build -w @intentius/chant-lexicon-gcp — clean
  • npm run build -w @intentius/chant-lexicon-k8s — clean (touched its hooks module)
  • just lint — 0 errors (also ran npx eslint directly over every touched file in both lexicons and core — clean)
  • just test — 800 passed | 1 skipped (801 files); 9776 passed | 5 skipped (9781 tests)
  • just check-lexicons — 11/11 lexicons, 17/17 tier-1 checks each, no untracked failures
  • just fold-differential — 104/104, drift: 0
  • just sandbox-differential — 103/103, drift: 0
  • examples/k8s-client-boundary.test.ts — 38/38; 117 modules reachable from the k8s lexicon's entry points, 0 importing the client package; 32 corpus builds, 0 loading it (unaffected by this PR, re-verified)
  • node scripts/depcheck.mjs — no undeclared runtime dependencies
  • No cluster/cloud access anywhere. Every gcp test mocks node:child_process's exec; nothing reads ~/.kube/config/$KUBECONFIG, no socket opens.

Deliberately not in scope

status.conditions staleness as its own signal, which the issue's own "Proposed" section floats as worth deciding rather than doing. status is pruned outright here (matching k8s, and the acceptance criteria), and a CNRM projection lagging real GCP state is a genuine gap — but detecting it means reading the actual GCP API, not the CR, a different read than this contract's "normalize what Kubernetes reports" scope. Flagged in the module doc, not papered over.

Part of #1014.

…1087)

Implements #1014's deep-observe contract for the gcp lexicon by reusing the
k8s row (#1076) rather than writing a second implementation: a Config
Connector custom resource is a Kubernetes object too, so it carries the same
envelope (status, metadata.uid/resourceVersion/generation/creationTimestamp/
managedFields/selfLink) and the same SSA fieldsV1 encoding for
metadata.managedFields.

The genuinely shared piece (the managed-fields ownership walk and the
generic Kubernetes object envelope/list-map-key conventions) moves out of
the k8s lexicon into packages/core/src/managed-fields.ts, the same reason
kubectl-context.ts's resolveClusterTarget lives in core — so gcp can reuse
it without depending on the k8s lexicon package or on
@intentius/chant-k8s-client. lexicons/k8s/src/deep-observe.ts and
deep-observe-hooks.ts now delegate to core for that shared half and keep
only what's genuinely k8s-lexicon-specific (K8S_SERVICE_DEFAULTS).

gcp's own row (lexicons/gcp/src/deep-observe.ts, deep-observe-hooks.ts)
reads through the same kubectl-shelled transport describe-resources.ts
already uses (factored into resolveGcpKubectlContext/execConfigConnectorGet
so neither reader restates it), adds Config Connector's own observed-state
annotation noise, and works out the GCP-specific twist on the
contested/owned rule: chant's own kubectl-apply path has no distinct
"chant" field manager the way the k8s lexicon's typed-client SSA path does
(GCP's deploy scripts run plain `kubectl apply -f`, attributing chant's own
writes to kubectl's own default manager), so the contested-field rule
(a declared field is always diffable regardless of who owns it live) does
the work isChantFieldManager does on the k8s side.

Gate evidence:
- npx tsc --noEmit -p packages/core/tsconfig.json — clean
- npm run build -w @intentius/chant-lexicon-gcp — clean
- npm run build -w @intentius/chant-lexicon-k8s — clean
- npx eslint packages/ — clean
- just test — 800 passed | 1 skipped (801 files); 9776 passed | 5 skipped
- just check-lexicons — 11/11 lexicons, 17/17 tier-1 checks each
- just fold-differential — 104/104, drift: 0
- just sandbox-differential — 103/103, drift: 0
- examples/k8s-client-boundary.test.ts — 38/38, 0 modules importing the
  client package
- node scripts/depcheck.mjs — no undeclared runtime dependencies
@lex00
lex00 merged commit c6cf86e into main Jul 28, 2026
5 checks passed
@lex00
lex00 deleted the feat/1087-gcp-deep-observation branch August 3, 2026 19:21
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(gcp): deep observation — reuse the k8s managed-fields path (#1014 gcp row)

1 participant