diff --git a/pkg/config/config.go b/pkg/config/config.go index edca53f..0defa70 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -98,6 +98,7 @@ type Config struct { ResourceTags []string WatchNamespace string WatchSelectors string + WatchSelectorsParsed labels.Selector EnableWebhookServer bool WebhookServerAddr string DeletionPolicy ackv1alpha1.DeletionPolicy @@ -489,7 +490,8 @@ func (cfg *Config) ParseWatchSelectors() (labels.Selector, error) { // If the WatchSelectors isn't set, return nil. controller-runtime will be in charge // of defaulting to watching all objects. if cfg.WatchSelectors == "" { - return nil, nil + cfg.WatchSelectorsParsed = labels.Everything() + return labels.Everything(), nil } labelSelector, err := labels.Parse(cfg.WatchSelectors) @@ -497,6 +499,8 @@ func (cfg *Config) ParseWatchSelectors() (labels.Selector, error) { return nil, fmt.Errorf("invalid value for flag '%s': %v", flagWatchSelectors, err) } + // Set the parsed label selector in the Config struct for later use + cfg.WatchSelectorsParsed = labelSelector return labelSelector, nil } diff --git a/pkg/runtime/reconciler.go b/pkg/runtime/reconciler.go index ddc94f4..4a71810 100644 --- a/pkg/runtime/reconciler.go +++ b/pkg/runtime/reconciler.go @@ -26,6 +26,7 @@ import ( "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" ctrlrt "sigs.k8s.io/controller-runtime" @@ -214,6 +215,18 @@ func (r *resourceReconciler) Reconcile(ctx context.Context, req ctrlrt.Request) return ctrlrt.Result{}, err } + // Check if the resource still matches the watchSelectors. + if !r.cfg.WatchSelectorsParsed.Matches(labels.Set(desired.MetaObject().GetLabels())) { + r.log.V(1).Info( + "Skipping reconcile for resource that does not match watch selectors", + "resource", desired.MetaObject().GetName(), + "namespace", desired.MetaObject().GetNamespace(), + "kind", r.rd.GroupVersionKind().Kind, + "watchSelectors", r.cfg.WatchSelectors, + ) + return ctrlrt.Result{}, nil + } + rlog := ackrtlog.NewResourceLogger( r.log, desired, // All the fields for a resource that do not change during reconciliation