Repository navigation
fix: watch Sandbox resources in ClientRegistration controller - #338
Conversation
The ClientRegistrationReconciler only watched Deployment and StatefulSet, so Sandbox workloads never got Keycloak OAuth clients registered. This broke AuthBridge authentication for sandbox-deployed agents. Add Sandbox as a third watched resource type using the same unstructured pattern established by AgentRuntimeReconciler. The controller now: - Conditionally watches Sandbox CRs (guarded by SandboxCRDExists) - Extracts pod template labels/SA from spec.podTemplate - Registers Keycloak client and patches the annotation Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: cwiklik <[email protected]>
mrsabath
left a comment
There was a problem hiding this comment.
Clean fix that extends ClientRegistration to watch Sandbox resources, correctly mirroring the unstructured pattern from AgentRuntimeReconciler. Reuses sandboxGVK and SandboxCRDExists, conditionally registers the watch, and follows the cascading Deployment→StatefulSet→Sandbox structure. Annotation patching for unstructured Sandbox handles the read-check-write cycle correctly.
Areas reviewed: Go (controller logic, error handling, RBAC markers, predicate, manager setup)
CI: All 16 checks pass (Build, Unit, Integration, E2E, Lint)
| annotations = map[string]string{} | ||
| } | ||
| annotations[AnnotationKeycloakClientSecretName] = secretName | ||
| _ = unstructured.SetNestedStringMap(fresh.Object, annotations, "spec", "podTemplate", "metadata", "annotations") |
There was a problem hiding this comment.
nit: SetNestedStringMap error is discarded. Unlikely to fail for map[string]string but a one-line log at debug level would help if someone hits an edge case:
| _ = unstructured.SetNestedStringMap(fresh.Object, annotations, "spec", "podTemplate", "metadata", "annotations") | |
| if err := unstructured.SetNestedStringMap(fresh.Object, annotations, "spec", "podTemplate", "metadata", "annotations"); err != nil { | |
| return err | |
| } |
Non-blocking — up to you.
huang195
left a comment
There was a problem hiding this comment.
Watch plumbing and predicate are fine. Ale reconcile path added by this PR is unreachable — an err-scoping bug silently returns before we ever hit the Sandbox branch. See inline on line 137.
Same pattern bug at line 117 is pre-existing on main (StatefulSet path), but this PR propagates it to Sandbox — the one workload you're adding support for. Worth fixing both in the same PR.
Test coverage gap. The existing deployment and statefulset not found case in clientregistration_controller_test.go only asserts err == nil + zero Result, which the buggy reconcile also satisfies. That's why 6/6 pass without exercising the added path. Add a happy-path Sandbox test: set up a Sandbox unstructured with kagenti.io/type: agent, run Reconcile, assert kagenti.io/keycloak-client-credentials-secret-name landed on spec.podTemplate.metadata.annotations. That covers the unchecked item in your test plan and would have caught the scope bug before merge.
Areas reviewed: Go (clientregistration_controller.go)
Commits: 1, signed-off: yes
CI: green, but the coverage gap above means CI can't catch this class of bug today.
| }) | ||
| }) | ||
| } | ||
| if !apierrors.IsNotFound(err) { |
There was a problem hiding this comment.
must-fix. err here is the outer one from readClusterFeatureGates on line 91 — the err declared at line 122 (if err := r.Get…) went out of scope at the closing } on line 136. Outer err is guaranteed nil by the early return on lines 92-95, so !apierrors.IsNotFound(nil) is always true and we return (ctrl.Result{}, nil) right here. The Sandbox branch below (141-169) never runs.
Same bug at line 117 is pre-existing on main (StatefulSet path), but this PR propagates it to Sandbox.
Fix: drop := and assign to the outer err (err = r.Get(...)), or go back to the pre-PR StatefulSet shape:
if err := r.Get(ctx, req.NamespacedName, sts); err != nil {
if !apierrors.IsNotFound(err) { return ctrl.Result{}, err }
// fall through to Sandbox
} else {
return r.reconcileOne(...)
}Fix line 117 in the same PR — same bug, just the older victim.
| return r.reconcileOne(ctx, sts, injectTools, sts.Name, &sts.Spec.Template, | ||
| podLabels, _, _ := unstructured.NestedStringMap(sbx.Object, "spec", "podTemplate", "metadata", "labels") | ||
| saName, _, _ := unstructured.NestedString(sbx.Object, "spec", "podTemplate", "spec", "serviceAccountName") | ||
| syntheticTemplate := &corev1.PodTemplateSpec{ |
There was a problem hiding this comment.
Synthetic template omits existing annotations from the real Sandbox. reconcileOne's short-circuit at line 293 (template.Annotations != nil && ... == secretName) therefore never fires for this path, so every reconcile falls through to the Update callback. Callback is idempotent (checks fresh before Update), so not a correctness bug, ale wasteful on steady state.
Pull spec.podTemplate.metadata.annotations into the synthetic template next to labels and SA.
pdettori
left a comment
There was a problem hiding this comment.
Clean extension of the existing pattern. Single file, mirrors AgentRuntimeReconciler's unstructured Sandbox handling exactly — same field paths, same conditional CRD watch, same retry-on-conflict flow. RBAC marker added. All 16 CI checks pass including E2E.
One minor suggestion inline (non-blocking).
| return nil | ||
| } | ||
| return r.Update(ctx, s) | ||
| if annotations == nil { |
There was a problem hiding this comment.
suggestion: SetNestedStringMap error is discarded here. Unlike the read helpers (where a missing field is normal), a write failure means the annotation won't persist and the retry loop will spin silently until the conflict limit. Consider returning the error:
if err := unstructured.SetNestedStringMap(fresh.Object, annotations, "spec", "podTemplate", "metadata", "annotations"); err != nil {
return err
}Non-blocking since AgentRuntimeReconciler uses the same _ = pattern — just flagging for awareness.
The Deployment and StatefulSet Get() calls used `err :=` (block-scoped), so the subsequent `if !apierrors.IsNotFound(err)` checks referenced the outer `err` from readClusterFeatureGates (always nil after passing the gate check). This prevented the StatefulSet and Sandbox branches from ever executing. Fix: reuse the outer `err` via `err = r.Get(...)` with if/else-if to properly fall through when the resource is NotFound. Also addresses review feedback: - Include podTemplate annotations in the Sandbox synthetic template so reconcileOne's short-circuit check fires correctly - Return error from SetNestedStringMap instead of discarding it Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: cwiklik <[email protected]>
|
Thanks for the thorough review! All feedback addressed in 1e9b1ba: @huang195 (MUST-FIX) — @huang195 (suggestion) — synthetic template missing annotations: @mrsabath / @pdettori (nit) — discarded |
huang195
left a comment
There was a problem hiding this comment.
Dobra robota — must-fix from prior review is resolved correctly.
1e9b1ba switches both Deployment and StatefulSet Get calls to err = r.Get(...) with if err == nil { ... } else if !apierrors.IsNotFound(err) { return err }. Outer err now carries state across blocks and the Sandbox branch is reachable.
Both prior suggestions also addressed:
- Synthetic template now pulls podTemplate annotations, so
reconcileOne's short-circuit fires on steady state (no more wasted Update calls). SetNestedStringMaperror is wrapped withfmt.Errorf(... %w)instead of discarded.
Remaining non-blocking gap: no unit test covers the Sandbox reconcile happy path — the coverage that would have caught the original scope bug. Worth adding in a follow-up if not this PR (set up a Sandbox unstructured with kagenti.io/type: agent, run Reconcile, assert kagenti.io/keycloak-client-credentials-secret-name landed on spec.podTemplate.metadata.annotations).
Commits: 2, both signed-off
CI: 15/15 green
Summary
agents.x-k8s.io/v1alpha1) as a watched resource type inClientRegistrationReconcilerAgentRuntimeReconcilerpattern)kagenti.io/keycloak-client-credentials-secret-nameannotation on the Sandbox podTemplateWhy: The controller only watched Deployment and StatefulSet, so Sandbox workloads deployed via the Sandbox epic (#1155) never got OAuth credentials registered — breaking AuthBridge authentication.
Test plan
go build ./...— compiles cleango vet ./...— no issuesTestClientRegistrationReconciler_Reconciletests pass (6/6)kagenti.io/type: agentlabel → verify operator watches Sandboxkagenti.io/keycloak-client-credentials-secret-nameannotation appears on Sandbox podTemplate (requires workload without legacy sidecar label)Verification (Kind cluster, v0.6.0-rc.1)
Operator built from this branch (rebased on v0.2.0-alpha.34) and deployed to Kind cluster.
Controller now watches Sandbox resources (3 event sources instead of 2):
Sandbox workload
weather-service-newrunning 4/4 in team1:This workload uses the legacy sidecar path (
kagenti.io/client-registration-inject: "true"), so the operator-managed path correctly skips it. The fix enables the operator-managed path for Sandbox workloads that opt out of the legacy sidecar — matching Deployment/StatefulSet behavior.Workaround for local testing
The Helm chart passes
--enable-operator-client-registration=truewhich requires building from the v0.2.0-alpha.34 base (not latestmain). To test locally on Kind:Important: Use a unique tag each time (e.g.
sandbox-fix-v2) because Kind nodes cache images by tag andimagePullPolicy: IfNotPresentwon't repull a changed digest under the same tag.Assisted-By: Claude Code