Skip to content

Add generation-change predicates to controller watches #4635

@ChrisJBurns

Description

@ChrisJBurns

Summary

Zero predicate usage exists across all controller watch configurations. Every metadata-only change (label additions, annotation updates, resource version bumps) triggers a full reconciliation cycle. This is particularly wasteful for MCPServer reconciliation which performs expensive operations like image validation and StatefulSet comparison.

Severity: SHOULD FIX
Area: Controller Efficiency
Breaking: No

Location

  • All controller SetupWithManager methods across cmd/thv-operator/controllers/

Problem

Example from a typical controller:

func (r *MCPServerReconciler) SetupWithManager(mgr ctrl.Manager) error {
    return ctrl.NewControllerManagedBy(mgr).
        For(&v1alpha1.MCPServer{}).
        Owns(&appsv1.StatefulSet{}).
        Complete(r)
}

No builder.WithPredicates(...) is applied to any watch.

Impact

  • Metadata-only changes (e.g., adding a label) trigger full reconciliation
  • MCPServer reconciliation is particularly expensive due to image validation, StatefulSet comparison, and Service reconciliation
  • Higher API server load from unnecessary reconciliation cycles
  • Increased log noise from no-op reconciliations
  • In clusters with frequent metadata updates (e.g., from GitOps tools adding annotations), this causes continuous unnecessary work

Recommended Fix

Add generation-change predicates on primary resource watches:

import "sigs.k8s.io/controller-runtime/pkg/predicate"

func (r *MCPServerReconciler) SetupWithManager(mgr ctrl.Manager) error {
    return ctrl.NewControllerManagedBy(mgr).
        For(&v1alpha1.MCPServer{},
            builder.WithPredicates(predicate.GenerationChangedPredicate{})).
        Owns(&appsv1.StatefulSet{}).
        Complete(r)
}

Apply to all controller primary resource watches. Do NOT apply to Owns() watches since owned resource status changes need to trigger reconciliation.

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestgoPull requests that update go codekubernetesItems related to Kubernetesoperator

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions