Skip to content

fix: skip reconciliation when label selector does not match #431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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: 6 additions & 0 deletions pkg/reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/source"
Expand Down Expand Up @@ -571,6 +572,11 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
return ctrl.Result{}, err
}

if r.selectorPredicate != nil && !r.selectorPredicate.Generic(event.GenericEvent{Object: obj}) {
log.V(1).Info("Label selector does not match, skipping reconcile")
return ctrl.Result{}, nil
}

// The finalizer must be present on the CR before we can do anything. Otherwise, if the reconciliation fails,
// there might be resources created by the chart that will not be garbage-collected
// (cluster-scoped resources or resources in other namespaces, which are not bound by an owner reference).
Expand Down
39 changes: 39 additions & 0 deletions pkg/reconciler/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ var _ = Describe("Reconciler", func() {
Expect(mgr.GetCache().WaitForCacheSync(ctx)).To(BeTrue())

obj = testutil.BuildTestCR(gvk)
obj.SetLabels(map[string]string{"foo": "bar"})
objKey = types.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()}
req = reconcile.Request{NamespacedName: objKey}
})
Expand Down Expand Up @@ -518,6 +519,8 @@ var _ = Describe("Reconciler", func() {
cancel()
})

selector := metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}}

// After migration to Ginkgo v2 this can be rewritten using e.g. DescribeTable.
parameterizedReconcilerTests := func(opts reconcilerTestSuiteOpts) {
BeforeEach(func() {
Expand All @@ -535,6 +538,7 @@ var _ = Describe("Reconciler", func() {
WithInstallAnnotations(annotation.InstallDescription{}),
WithUpgradeAnnotations(annotation.UpgradeDescription{}),
WithUninstallAnnotations(annotation.UninstallDescription{}),
WithSelector(selector),
WithOverrideValues(map[string]string{
"image.repository": "custom-nginx",
}),
Expand All @@ -549,6 +553,7 @@ var _ = Describe("Reconciler", func() {
WithInstallAnnotations(annotation.InstallDescription{}),
WithUpgradeAnnotations(annotation.UpgradeDescription{}),
WithUninstallAnnotations(annotation.UninstallDescription{}),
WithSelector(selector),
WithOverrideValues(map[string]string{
"image.repository": "custom-nginx",
}),
Expand Down Expand Up @@ -1425,6 +1430,40 @@ var _ = Describe("Reconciler", func() {
})
})
})
When("label selector succeeds", func() {
It("reconciles only matching label", func() {
By("setting an invalid action client getter to assert different reconcile results", func() {
r.actionClientGetter = helmclient.ActionClientGetterFunc(func(context.Context, client.Object) (helmclient.ActionInterface, error) {
fakeClient := helmfake.NewActionClient()
return &fakeClient, nil
})
})

By("setting not matching label to the CR", func() {
Expect(mgr.GetClient().Get(ctx, objKey, obj)).To(Succeed())
obj.SetLabels(map[string]string{"foo": "baz"})
Expect(mgr.GetClient().Update(ctx, obj)).To(Succeed())
})

By("reconciling is skipped, action client was not called and no error returned", func() {
res, err := r.Reconcile(ctx, req)
Expect(res).To(Equal(reconcile.Result{}))
Expect(err).ToNot(HaveOccurred())
})

By("setting matching label to the CR", func() {
Expect(mgr.GetClient().Get(ctx, objKey, obj)).To(Succeed())
obj.SetLabels(map[string]string{"foo": "bar"})
Expect(mgr.GetClient().Update(ctx, obj)).To(Succeed())
})

By("reconciling is not skipped and error returned because of broken action client", func() {
res, err := r.Reconcile(ctx, req)
Expect(res).To(Equal(reconcile.Result{}))
Expect(err).To(MatchError("get not implemented"))
})
})
})
})
})
})
Expand Down
Loading