feat(core): emit Kubernetes Events on Agent/ModelConfig/MCPServerTool/RemoteMCPServer reconciles#2150
Conversation
Add an optional EventRecorder to the Agent, ModelConfig, MCPServerTool and RemoteMCPServer controllers so reconcile outcomes are visible via 'kubectl describe', mirroring the pattern already used by the agentharness-substrate controller. Emitted events: - Agent / ModelConfig / RemoteMCPServer: Normal Accepted on success, Warning ReconcileFailed on error. - MCPServerTool: Normal ToolsDiscovered on success, Warning ValidationFailed for spec validation errors, Warning ReconcileFailed for transient errors. Recording is nil-guarded on both the Recorder and Client, so it is a no-op when either is unwired (e.g. in tests) and behaviour is unchanged. RBAC is already covered by the leader-election role (events: create,patch). Recorders are wired from pkg/app/app.go via mgr.GetEventRecorder. Refs: kagent-dev#2148 Signed-off-by: Diego Braga <diego.braga86@gmail.com>
There was a problem hiding this comment.
Pull request overview
Adds Kubernetes Event emission to several go/core controllers so reconcile outcomes are visible via kubectl describe / kubectl get events, wiring recorders from the controller manager.
Changes:
- Wire
Client+EventRecorderinto Agent/ModelConfig/MCPServerTool/RemoteMCPServer controllers frompkg/app/app.go. - Emit Normal/Warning Kubernetes Events on reconcile success/failure for those controllers via a nil-guarded
recordEventhelper. - Add a unit test covering MCPServerTool event emission paths.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| go/core/pkg/app/app.go | Wires manager client + event recorders into the four controllers. |
| go/core/internal/controller/agent_controller.go | Emits Accepted/ReconcileFailed Events for Agent reconciles via recordEvent. |
| go/core/internal/controller/modelconfig_controller.go | Emits Accepted/ReconcileFailed Events for ModelConfig reconciles via recordEvent. |
| go/core/internal/controller/mcp_server_tool_controller.go | Emits ToolsDiscovered/ValidationFailed/ReconcileFailed Events for MCPServer tool discovery reconciles. |
| go/core/internal/controller/remote_mcp_server_controller.go | Emits Accepted/ReconcileFailed Events for RemoteMCPServer reconciles via recordEvent. |
| go/core/internal/controller/mcp_server_tool_controller_events_test.go | Adds tests validating MCPServerTool emits expected event type/reason. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| r.recordEvent(ctx, req, "Normal", "ToolsDiscovered", "Reconcile", "MCPServer tools discovered successfully") | ||
| // Success - requeue after 60s to refresh tool server status | ||
| return ctrl.Result{ | ||
| RequeueAfter: 60 * time.Second, | ||
| }, nil |
| r.recordEvent(ctx, req, "Normal", "Accepted", "Reconcile", "RemoteMCPServer reconciled successfully") | ||
| // Success - requeue after 60s to refresh tool server status | ||
| return ctrl.Result{ | ||
| RequeueAfter: 60 * time.Second, | ||
| }, nil |
The MCPServerTool and RemoteMCPServer controllers requeue every 60s to refresh tool status, and previously recorded a Normal success Event on every reconcile — flooding kubectl events and adding control-plane writes even when nothing changed. Gate the success Event on a pending spec change (generation != observedGeneration, captured before reconcile); periodic no-op refreshes no longer emit. Warning events (ReconcileFailed/ValidationFailed) still emit every time. The Agent/ModelConfig controllers are unaffected (they don't requeue periodically). Refs: kagent-dev#2148 Signed-off-by: Diego Braga <diego.braga86@gmail.com>
|
Addressed the Copilot feedback about Event flooding: the MCPServerTool and RemoteMCPServer controllers requeue every 60s, so they now emit the Normal success Event only on a meaningful transition — a pending spec change ( |
EItanya
left a comment
There was a problem hiding this comment.
Overall the functionality makes sense. However I'm wondering if we should add this directly to the reconciler since that code already does the Get required to ensure that the object exists.
What
Adds Kubernetes Events on reconcile to the Agent, ModelConfig, MCPServerTool, and RemoteMCPServer controllers, so users can see reconcile outcomes with
kubectl describe/kubectl get events. This is the second of the three observability gaps proposed in #2148.Today these four controllers emit no Events (unlike
agentharness-substrate-controller, which already does), so a bad Agent/ModelConfig/MCPServer gives no signal inkubectl describe.How
Each controller gains an optional
Client+Recorder events.EventRecorderand a small nil-guardedrecordEventhelper that re-fetches the reconciled object and emits an Event against it, mirroring the existing substrate-controller pattern.Emitted events:
Normal AcceptedWarning ReconcileFailedNormal AcceptedWarning ReconcileFailedNormal AcceptedWarning ReconcileFailedNormal ToolsDiscoveredWarning ValidationFailed(spec validation) /Warning ReconcileFailed(transient)Recorders are wired from
pkg/app/app.goviamgr.GetEventRecorder(...).Additive / no behaviour change
recordEventis a no-op when eitherRecorderorClientis nil, so existing tests and any caller that doesn't wire them are unaffected.events: create,patch), so no RBAC changes are needed.go.modunchanged);k8s.io/client-go/tools/eventsandapierrorsare already used in-tree.Testing
mcp_server_tool_controller_events_test.go— asserts the correct event type/reason is emitted for each of the three MCPServerTool outcomes (ToolsDiscovered / ValidationFailed / ReconcileFailed) using a fake client +events.NewFakeRecorder, plus a no-Recorder no-panic case.go build ./core/...,go test -race -skip 'TestE2E.*' ./core/internal/controller/..., andgolangci-lint runall pass.Notes
Opening as draft / work-in-progress per CONTRIBUTING; happy to adjust event reasons/actions. The remaining gap from #2148 (controller log→OTLP bridge) will follow as a separate PR. Companion PR for gap 1 (token metrics): #2149.
Refs: #2148