Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type Config struct {
ResourceTags []string
WatchNamespace string
WatchSelectors string
WatchSelectorsParsed labels.Selector
EnableWebhookServer bool
WebhookServerAddr string
DeletionPolicy ackv1alpha1.DeletionPolicy
Expand Down Expand Up @@ -489,14 +490,17 @@ 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)
if err != nil {
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
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/runtime/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down