Skip to content

fix(operator): KapeAgentServer reconcile never converges (metadata clobber + API defaults) - #272

Merged
dzungtr merged 2 commits into
mainfrom
fix/agentserver-patch-loop2
Oct 22, 2026
Merged

dzungtr merged 2 commits into
mainfrom
fix/agentserver-patch-loop2

Conversation

@dzungtr

@dzungtr dzungtr commented Oct 22, 2026 •

Copy link
Copy Markdown
Owner

The KapeAgentServer reconciler never reached a steady state on a live cluster: deployment.kubernetes.io/revision was deleted and re-added in a loop, metadata.generation ran ahead of status.observedGeneration (~7/s), and the CR stayed Ready=False (Progressing: controller Deployment … is not available yet) even though the pod was 1/1 Ready with availableReplicas=1.

Two independent causes, one commit each.

1. ensure() clobbered metadata (eff029c)

patched.SetLabels(desired.GetLabels())
patched.SetAnnotations(desired.GetAnnotations())   // rendered maps are nil

A merge patch against a live object turns "nil map" into {"metadata":{"annotations":null}}, deleting annotations the platform owns. Captured by running a debug build against the cluster — the entire delta was:

{"metadata":{"annotations":null}}

The Deployment controller immediately re-adds deployment.kubernetes.io/revision, so the object was written forever.

Fix: merge the rendered keys over the live maps (rendered wins, nil overlay leaves live untouched). Test: TestAgentServerAdapterApplyKeepsForeignMetadata.

2. Rendered spec zeroed API defaults (58dabdb, supersedes #271's field list)

live.Spec = d.Spec drops every field the API server defaults, so the merge-patch diff was never empty (revisionHistoryLimit, progressDeadlineSeconds, probe thresholds, terminationMessage*, volume defaultMode, …). #271 enumerated them by hand and missed fields.

Fix: inheritUnset(desired, live) — structurally copy every unset (zero) field from the live object through structs, pointers and same-length slices; env vars excluded because their Name/Value are renderer-owned and an intentionally empty KAPE_DEFAULT_MODEL must not be back-filled. Test: TestAgentServerAdapterApplyIgnoresAPIDefaults.

Verification

  • go test ./operator/infra/k8s/ passes; go vet clean. (operator/controller/reconcile + operator/infra/qdrant failures reproduce on unmodified main — environment-dependent, unrelated.)
  • Live: operator 0.1.3 (this branch) → generation/observedGeneration frozen, kubectl get kas -n kape-system → Ready=True.

Not in scope

buildControllerRoleBinding binds kape-controller-sandbox-reader in the CR's namespace only. With the CR in kape-system and Sandbox CRs in openshell, the controller's SA has no Sandbox read access (breaks ResolveDaemonAddr → prompt_agent/exec). Tracked separately; the operator should bind in KAPE_SANDBOX_NAMESPACE.

… loop

The previous fix enumerated the API-defaulted Deployment fields by hand and
missed revisionHistoryLimit/progressDeadlineSeconds (and would miss the next
one). Generation kept churning on the live cluster at ~10/s, so the CR still
never reached Ready.

Replace the hand-written inheritance with a structural rule: walk desired vs
live and copy every unset (zero) field from live, recursively through structs,
pointers and same-length slices, and whole-value for map entries. Env vars are
excluded — their Name/Value are renderer-owned and an intentionally empty value
must not be back-filled from the live object.

Test extended with the Deployment-level defaults (strategy, revisionHistoryLimit,
progressDeadlineSeconds).
… them

ensure() assigned the rendered label and annotation maps wholesale, so the
merge patch carried {"metadata":{"annotations":null}} on every reconcile and
deleted annotations the platform maintains. On the live cluster that removed
deployment.kubernetes.io/revision, the Deployment controller re-added it
immediately, and the object was rewritten in a loop: generation 4166 -> 4172 in
20s while status.observedGeneration stayed pinned, so DeploymentStatus never
reported available and KapeAgentServer stayed Ready=False (Progressing).

Found by running a debug build of this operator against the cluster: the delta
was metadata.annotations:null and nothing else.

Merge base maps with the rendered keys winning; a nil rendered map leaves the
existing metadata untouched. New test asserts a foreign annotation and label
survive and that a re-apply does not patch.
@dzungtr dzungtr changed the title fix(operator): inherit unset Deployment fields generically (reconcile loop, follow-up to #271) fix(operator): KapeAgentServer reconcile never converges (metadata clobber + API defaults) Oct 22, 2026
@dzungtr
dzungtr merged commit b4e515b into main Oct 22, 2026
1 check failed
@dzungtr
dzungtr deleted the fix/agentserver-patch-loop2 branch October 22, 2026 05:34
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.

1 participant