From f2eeba8fd4a88329a1baebfca024f689576fff46 Mon Sep 17 00:00:00 2001 From: Iulian Taiatu Date: Mon, 30 Jun 2025 14:15:39 +0300 Subject: [PATCH 1/2] Skip reconcile on resources that do not match watchSelectors Signed-off-by: Iulian Taiatu --- pkg/config/config.go | 6 +++++- pkg/runtime/reconciler.go | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) 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..614d0fb 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 matched the 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 From 854f30be982386710df17275c56c7c4d985b986c Mon Sep 17 00:00:00 2001 From: Iulian Taiatu Date: Mon, 30 Jun 2025 14:16:44 +0300 Subject: [PATCH 2/2] Fix typo Signed-off-by: Iulian Taiatu --- pkg/runtime/reconciler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/runtime/reconciler.go b/pkg/runtime/reconciler.go index 614d0fb..4a71810 100644 --- a/pkg/runtime/reconciler.go +++ b/pkg/runtime/reconciler.go @@ -215,7 +215,7 @@ func (r *resourceReconciler) Reconcile(ctx context.Context, req ctrlrt.Request) return ctrlrt.Result{}, err } - // Check if the resource still matched the the watchSelectors. + // 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",