Repository navigation
Description
Summary
The VirtualMCPServer controller enters a hot reconcile loop — updating the managed Deployment roughly twice every 30 seconds — whenever spec.imagePullSecrets is empty and the live Deployment carries a stale toolhive.stacklok.io/imagepullsecrets-hash annotation from a previous configuration. Each update increments metadata.generation, the Deployment watch re-enqueues the reconcile, and the cycle repeats indefinitely. On a kind cluster this saturates etcd write bandwidth and can starve leader-election lease renewals, causing the operator to restart.
Symptom
- Deployment
metadata.generationincrements continuously at ~2/minute with no spec changes on the VirtualMCPServer - Continuous stream of
"Updating Deployment"lines in operator logs - Continuous
DeploymentUpdatedevents on the VirtualMCPServer object - On resource-constrained clusters (kind, single-node): leader election failures and operator restarts
Root cause
Three functions interact:
1. imagePullSecretsHash short-circuits for empty input (virtualmcpserver_deployment.go):
if len(secrets) == 0 {
return "", nil
}2. buildDeploymentMetadataForVmcp skips writing the annotation when hash is empty (virtualmcpserver_deployment.go):
if hash, err := imagePullSecretsHash(...); err == nil && hash != "" {
deploymentAnnotations[imagePullRefsHashAnnotation] = hash
}When imagePullSecrets is empty, the annotation is absent from newDeployment.Annotations.
3. The update write path uses MergeAnnotations, which preserves stale keys (virtualmcpserver_controller.go):
deployment.Annotations = ctrlutil.MergeAnnotations(newDeployment.Annotations, deployment.Annotations)A stale imagePullRefsHashAnnotation on the live Deployment survives every write.
4. imagePullSecretsNeedsUpdate returns true every reconcile (virtualmcpserver_controller.go):
_, present := deployment.Annotations[imagePullRefsHashAnnotation]
if expectedHash == "" {
return present // always true because MergeAnnotations kept the stale annotation
}Trigger condition
- Create a VirtualMCPServer with
spec.imagePullSecretsset (or with chart-level imagePullSecrets configured) - Apply a change that removes all imagePullSecrets
- The operator wrote the annotation in step 1;
MergeAnnotationsnever removes it in step 2; the loop starts
Also triggered on any upgrade that leaves the annotation on an existing Deployment while the current CR has the field unset.
Proposed fix
Part 1 — make imagePullSecretsNeedsUpdate symmetric:
storedHash := deployment.Annotations[imagePullRefsHashAnnotation]
return storedHash != expectedHashThis treats absent annotation + empty expected hash as steady state, and triggers exactly one update to clean up a stale annotation.
Part 2 — explicitly remove the annotation from the merged result when the desired hash is empty, so MergeAnnotations doesn't re-inject it on the next reconcile:
merged := ctrlutil.MergeAnnotations(newDeployment.Annotations, deployment.Annotations)
if _, want := newDeployment.Annotations[imagePullRefsHashAnnotation]; !want {
delete(merged, imagePullRefsHashAnnotation)
}
deployment.Annotations = mergedRelated
Same MergeAnnotations-preserves-stale-key class of bug fixed for Services in #5731. That PR did not extend the fix to the Deployment write path.
See also: companion bugs for podTemplateSpecHashAnnotation and podTemplateMetadataNeedsUpdate (filed separately).
Activity
Stop VirtualMCPServer hot-reconcile on cleared imagePullSecrets (stac…
Stop VirtualMCPServer hot-reconcile on cleared podTemplateSpec (#5846)