Skip to content

Commit 23f102a

Browse files
ChrisJBurnsclaude
authored andcommitted
Handle stale ReferencingWorkloads in telemetry watch handler (stacklok#4508)
Handle stale ReferencingWorkloads in telemetry watch The MCPTelemetryConfig watch handler only enqueued the currently-referenced config when an MCPServer changed. If a server removed its telemetryConfigRef, the previously-referenced config was never reconciled to clean up its stale ReferencingWorkloads entry. Update the watch handler to also enqueue any MCPTelemetryConfig that still lists the changed server in ReferencingWorkloads, matching the pattern already used by MCPOIDCConfig. Closes stacklok#4491 Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 865b730 commit 23f102a

1 file changed

Lines changed: 40 additions & 12 deletions

File tree

cmd/thv-operator/controllers/mcptelemetryconfig_controller.go

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,26 +140,54 @@ func (r *MCPTelemetryConfigReconciler) Reconcile(ctx context.Context, req ctrl.R
140140
}
141141

142142
// SetupWithManager sets up the controller with the Manager.
143+
// Watches MCPServer changes to maintain accurate ReferencingWorkloads status.
143144
func (r *MCPTelemetryConfigReconciler) SetupWithManager(mgr ctrl.Manager) error {
144-
// Watch MCPServer changes to update ReferencingWorkloads status
145+
// Watch MCPServer changes to update ReferencingWorkloads on referenced MCPTelemetryConfigs.
146+
// This handler enqueues both the currently-referenced MCPTelemetryConfig AND any
147+
// MCPTelemetryConfig that still lists this server in ReferencingWorkloads (covers the
148+
// case where a server removes its telemetryConfigRef — the previously-referenced
149+
// config needs to reconcile and clean up the stale entry).
145150
mcpServerHandler := handler.EnqueueRequestsFromMapFunc(
146-
func(_ context.Context, obj client.Object) []reconcile.Request {
147-
mcpServer, ok := obj.(*mcpv1alpha1.MCPServer)
151+
func(ctx context.Context, obj client.Object) []reconcile.Request {
152+
server, ok := obj.(*mcpv1alpha1.MCPServer)
148153
if !ok {
149154
return nil
150155
}
151156

152-
// If this MCPServer references a MCPTelemetryConfig, enqueue it for reconciliation
153-
if mcpServer.Spec.TelemetryConfigRef == nil {
154-
return nil
157+
seen := make(map[types.NamespacedName]struct{})
158+
var requests []reconcile.Request
159+
160+
// Enqueue the currently-referenced MCPTelemetryConfig (if any)
161+
if server.Spec.TelemetryConfigRef != nil {
162+
nn := types.NamespacedName{
163+
Name: server.Spec.TelemetryConfigRef.Name,
164+
Namespace: server.Namespace,
165+
}
166+
seen[nn] = struct{}{}
167+
requests = append(requests, reconcile.Request{NamespacedName: nn})
168+
}
169+
170+
// Also enqueue any MCPTelemetryConfig that still lists this server in
171+
// ReferencingWorkloads — handles ref-removal and server-deletion cases.
172+
telemetryConfigList := &mcpv1alpha1.MCPTelemetryConfigList{}
173+
if err := r.List(ctx, telemetryConfigList, client.InNamespace(server.Namespace)); err != nil {
174+
log.FromContext(ctx).Error(err, "Failed to list MCPTelemetryConfigs for MCPServer watch")
175+
return requests
176+
}
177+
for _, cfg := range telemetryConfigList.Items {
178+
nn := types.NamespacedName{Name: cfg.Name, Namespace: cfg.Namespace}
179+
if _, already := seen[nn]; already {
180+
continue
181+
}
182+
for _, ref := range cfg.Status.ReferencingWorkloads {
183+
if ref.Kind == "MCPServer" && ref.Name == server.Name {
184+
requests = append(requests, reconcile.Request{NamespacedName: nn})
185+
break
186+
}
187+
}
155188
}
156189

157-
return []reconcile.Request{{
158-
NamespacedName: types.NamespacedName{
159-
Name: mcpServer.Spec.TelemetryConfigRef.Name,
160-
Namespace: mcpServer.Namespace,
161-
},
162-
}}
190+
return requests
163191
},
164192
)
165193

0 commit comments

Comments
 (0)