Skip to content

Hot reconcile loop when imagePullSecrets is cleared on VirtualMCPServer #5817

Closed

Description

@jhrozek

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.generation increments continuously at ~2/minute with no spec changes on the VirtualMCPServer
  • Continuous stream of "Updating Deployment" lines in operator logs
  • Continuous DeploymentUpdated events 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

  1. Create a VirtualMCPServer with spec.imagePullSecrets set (or with chart-level imagePullSecrets configured)
  2. Apply a change that removes all imagePullSecrets
  3. The operator wrote the annotation in step 1; MergeAnnotations never 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 != expectedHash

This 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 = merged

Related

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

  1. added
    bug
    go
    on Jul 15, 2026
  2. added
    needs-triage
    on Jul 15, 2026
  3. added a commit that references this issue on Jul 16, 2026
    2894cac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggoneeds-triageoperator

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions