-
Notifications
You must be signed in to change notification settings - Fork 52
Pass label selector predicate to the Secret Watch and handler #443
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
Merged
perdasilva
merged 3 commits into
operator-framework:main
from
kurlov:akurlov/pass-labelselector-predicate-to-secret-watch
Jun 30, 2025
+69
−24
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,6 +53,7 @@ import ( | |
| "sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
| "sigs.k8s.io/controller-runtime/pkg/manager" | ||
| metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" | ||
| "sigs.k8s.io/controller-runtime/pkg/predicate" | ||
| "sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
| "sigs.k8s.io/controller-runtime/pkg/source" | ||
| "sigs.k8s.io/yaml" | ||
|
|
@@ -449,18 +450,21 @@ var _ = Describe("Reconciler", func() { | |
|
|
||
| selector := metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}} | ||
| Expect(WithSelector(selector)(r)).To(Succeed()) | ||
| Expect(r.selectorPredicate).NotTo(BeNil()) | ||
|
|
||
| Expect(r.selectorPredicate.Create(event.CreateEvent{Object: objLabeled})).To(BeTrue()) | ||
| Expect(r.selectorPredicate.Update(event.UpdateEvent{ObjectOld: objUnlabeled, ObjectNew: objLabeled})).To(BeTrue()) | ||
| Expect(r.selectorPredicate.Delete(event.DeleteEvent{Object: objLabeled})).To(BeTrue()) | ||
| Expect(r.selectorPredicate.Generic(event.GenericEvent{Object: objLabeled})).To(BeTrue()) | ||
|
|
||
| Expect(r.selectorPredicate.Create(event.CreateEvent{Object: objUnlabeled})).To(BeFalse()) | ||
| Expect(r.selectorPredicate.Update(event.UpdateEvent{ObjectOld: objLabeled, ObjectNew: objUnlabeled})).To(BeFalse()) | ||
| Expect(r.selectorPredicate.Update(event.UpdateEvent{ObjectOld: objUnlabeled, ObjectNew: objUnlabeled})).To(BeFalse()) | ||
| Expect(r.selectorPredicate.Delete(event.DeleteEvent{Object: objUnlabeled})).To(BeFalse()) | ||
| Expect(r.selectorPredicate.Generic(event.GenericEvent{Object: objUnlabeled})).To(BeFalse()) | ||
| Expect(r.labelSelector).NotTo(BeNil()) | ||
|
|
||
| selectorPredicate, err := predicate.LabelSelectorPredicate(r.labelSelector) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
|
|
||
| Expect(selectorPredicate.Create(event.CreateEvent{Object: objLabeled})).To(BeTrue()) | ||
| Expect(selectorPredicate.Update(event.UpdateEvent{ObjectOld: objUnlabeled, ObjectNew: objLabeled})).To(BeTrue()) | ||
| Expect(selectorPredicate.Delete(event.DeleteEvent{Object: objLabeled})).To(BeTrue()) | ||
| Expect(selectorPredicate.Generic(event.GenericEvent{Object: objLabeled})).To(BeTrue()) | ||
|
|
||
| Expect(selectorPredicate.Create(event.CreateEvent{Object: objUnlabeled})).To(BeFalse()) | ||
| Expect(selectorPredicate.Update(event.UpdateEvent{ObjectOld: objLabeled, ObjectNew: objUnlabeled})).To(BeFalse()) | ||
| Expect(selectorPredicate.Update(event.UpdateEvent{ObjectOld: objUnlabeled, ObjectNew: objUnlabeled})).To(BeFalse()) | ||
| Expect(selectorPredicate.Delete(event.DeleteEvent{Object: objUnlabeled})).To(BeFalse()) | ||
| Expect(selectorPredicate.Generic(event.GenericEvent{Object: objUnlabeled})).To(BeFalse()) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
@@ -1488,6 +1492,45 @@ var _ = Describe("Reconciler", func() { | |
| }) | ||
| }) | ||
| }) | ||
| When("label selector set", func() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. <3 |
||
| It("reconcile only matching CR", func() { | ||
| By("adding selector to the reconciler", func() { | ||
| selectorFoo := metav1.LabelSelector{MatchLabels: map[string]string{"app": "foo"}} | ||
| Expect(WithSelector(selectorFoo)(r)).To(Succeed()) | ||
| }) | ||
|
|
||
| By("adding not matching label to the CR", func() { | ||
| Expect(mgr.GetClient().Get(ctx, objKey, obj)).To(Succeed()) | ||
| obj.SetLabels(map[string]string{"app": "bar"}) | ||
| Expect(mgr.GetClient().Update(ctx, obj)).To(Succeed()) | ||
| }) | ||
|
|
||
| By("reconciling skipped and no actions for the release", func() { | ||
| res, err := r.Reconcile(ctx, req) | ||
| Expect(res).To(Equal(reconcile.Result{})) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| }) | ||
|
|
||
| By("verifying the release has not changed", func() { | ||
| rel, err := ac.Get(obj.GetName()) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| Expect(rel).NotTo(BeNil()) | ||
| Expect(*rel).To(Equal(*currentRelease)) | ||
| }) | ||
|
|
||
| By("adding matching label to the CR", func() { | ||
| Expect(mgr.GetClient().Get(ctx, objKey, obj)).To(Succeed()) | ||
| obj.SetLabels(map[string]string{"app": "foo"}) | ||
| Expect(mgr.GetClient().Update(ctx, obj)).To(Succeed()) | ||
| }) | ||
|
|
||
| By("successfully reconciling with correct labels", func() { | ||
| res, err := r.Reconcile(ctx, req) | ||
| Expect(res).To(Equal(reconcile.Result{})) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, so AFAICT
WithSelectoraccepts a selector with match expressions (and no match labels), but then this check causes these expressions to be ignored? 🤔This would be an unwelcome surprise for the user, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good catch!
I think it depends on how operator handles selectors. If operator uses built-in
ParseToLabelSelectorfrom k8s like in stackrox then this line will not be ignored and behaviour should be correct. But if some operator which uses this plugin creates selector manually with providingMatchExpressionsonly then there will be still a bug for multiple version for the same operator.I also tested fir PR locally and my setup had such selector generation and it worked as expected:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, it depends on what you pass to
ParseToLabelSelector. If you use any other operator than=or==then AFAICTMatchLabelswill be empty andMatchExpressionswill not.I think the best way would be to change the
ifto do the emptiness check differently...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#459