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
✨ Added the LabelSelectorPredicate function for filtering events #1121
✨ Added the LabelSelectorPredicate function for filtering events #1121
Conversation
Thanks for your pull request. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please follow instructions at https://git.k8s.io/community/CLA.md#the-contributor-license-agreement to sign the CLA. It may take a couple minutes for the CLA signature to be fully registered; after that, please reply here with a new comment and we'll verify. Thanks.
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here. |
Welcome @VenkatRamaraju! |
Hi @VenkatRamaraju. Thanks for your PR. I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
pkg/predicate/predicate.go
Outdated
} | ||
|
||
// NewLabelSelectorPredicate instantiates a new LabelSelectorPredicate with the LabelSelector specified through parameters. | ||
func NewLabelSelectorPredicate(s metav1.LabelSelector) (Predicate, error) { |
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.
I think the implementation of this can be drastically simplified by making it use NewPredicateFuncs
:
return NewPredicateFuncs(func (o controllerutil.Object) bool {
selector.Matches(labels.Set(o.GetLabels())
}
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.
NewLabelSelectorPredicate
is used to initialize the struct. If there is a way to have all types of predicate functions (Create, Delete, Update, Generic) pass through a "general" function with a "general object" as its parameter, we can simplify the implementation by doing the Matches
within that.
I'm a little confused about the implementation you suggested, could you explain a little more how that would work?
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.
func NewLabelSelectorPredicate(s metav1.LabelSelector)(Predicate, error){
selector, err := metav1.LabelSelectorAsSelector(&s)
if err != nil {return nil, err}
return NewPredicateFuncs(func (o controllerutil.Object) bool {
selector.Matches(labels.Set(o.GetLabels())
}
}
pkg/predicate/predicate.go
Outdated
// If the labels match, the event will undergo reconciliation. If the labels do not match, it will skip reconciliation for that particular event. | ||
|
||
type LabelSelectorPredicate struct { | ||
Funcs |
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.
Why the embedded Funcs
? Its unused or not? And why is the type exported?
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.
I realized that the Funcs are available by default. I will remove them from the struct and un-export the struct too.
4cfbfff
to
45354e3
Compare
pkg/predicate/predicate.go
Outdated
// EventReconcileFilter takes in a LabelSelector as a parameter and returns predicate functions. The labels of | ||
// events that are contained as parameters in these predicate functions are compared against the instantiated | ||
// Selector object and reconciliation of the event will only take place if the labels match. | ||
func EventReconcileFilter(s metav1.LabelSelector) (Funcs, error) { |
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.
Please rename to LabelSelectorPredicate
, no one will find this by its current name. Same goes for the PR title.
Also, the PR has merge conflicts and needs to be squashed so there is only one commit.
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.
Will do.
Quick question:
The conflict was caused because a lot of the event struct's fields have changed (since I forked this repo), now taking in a controllerutil object. The events require a controllerutil.Object
type (interface), which I believe can be implemented as metav1.Object
. However, the type that supports labels as metadata is the metav1.ObjectMeta
. How would I initialize a controllerutil.Object
-typed object that contains labels in it? That would help me write some tests.
Thanks!
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.
All kube objects that are not lists fullfil the controllerutil.Object
interface, for example *corev1.Pod
45354e3
to
3f0bf57
Compare
pkg/predicate/predicate.go
Outdated
@@ -268,3 +270,16 @@ func (o or) Generic(e event.GenericEvent) bool { | |||
} | |||
return false | |||
} | |||
|
|||
// LabelSelectorPredicate takes in a LabelSelector as a parameter and returns predicate functions. The labels of |
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.
// LabelSelectorPredicate takes in a LabelSelector as a parameter and returns predicate functions. The labels of | |
// LabelSelectorPredicate constructs a Predicate from a LabelSelector. Only objects matching the LabelSelector will be admitted. |
pkg/predicate/predicate.go
Outdated
// LabelSelectorPredicate takes in a LabelSelector as a parameter and returns predicate functions. The labels of | ||
// events that are contained as parameters in these predicate functions are compared against the instantiated | ||
// Selector object and reconciliation of the event will only take place if the labels match. | ||
func LabelSelectorPredicate(s metav1.LabelSelector) (Funcs, error) { |
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.
Please change this to return (Predicate, error)
. The Funcs
are an implementation detail ppl should not rely on.
pkg/predicate/predicate_test.go
Outdated
@@ -516,4 +516,39 @@ var _ = Describe("Predicate", func() { | |||
}) | |||
}) | |||
}) | |||
|
|||
Describe("When checking a LabelSelectorPredicate", func() { | |||
instance, err := predicate.LabelSelectorPredicate(*(&metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}})) |
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.
Why do you first construct a pointer just to instantly dereference it?
pkg/predicate/predicate_test.go
Outdated
It("should return false", func() { | ||
failMatch := &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: map[string]string{"bar": "foo"}, |
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.
Nit: Just using &corev1.Pod{}
would be enough, no need to give ti a different set of labels
3f0bf57
to
1ee9470
Compare
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.
/lgtm
/approve
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: alvaroaleman, VenkatRamaraju The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Thanks @VenkatRamaraju ! |
/retest |
The LabelSelectorPredicate function implements predicate functions to only reconcile events that contain certain labels.
This allows a user to pre-determine which events will undergo reconciliation via a Selector object that will compare against an event's labels, thereby "filtering" events.