Skip to content
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

Merged

Conversation

VenkatRamaraju
Copy link
Contributor

@VenkatRamaraju VenkatRamaraju commented Aug 7, 2020

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.

@k8s-ci-robot
Copy link
Contributor

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.

@k8s-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Aug 7, 2020
@k8s-ci-robot
Copy link
Contributor

Welcome @VenkatRamaraju!

It looks like this is your first PR to kubernetes-sigs/controller-runtime 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/controller-runtime has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot
Copy link
Contributor

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 /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

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.

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Aug 7, 2020
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Aug 7, 2020
}

// NewLabelSelectorPredicate instantiates a new LabelSelectorPredicate with the LabelSelector specified through parameters.
func NewLabelSelectorPredicate(s metav1.LabelSelector) (Predicate, error) {
Copy link
Member

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())
}

Copy link
Contributor Author

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?

Copy link
Member

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())
    }
}

// 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
Copy link
Member

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?

Copy link
Contributor Author

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.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 11, 2020
@VenkatRamaraju VenkatRamaraju changed the title ✨ Added the Label Selector predicate for filtering events ✨ Added the EventReconcileFilter function for filtering events Aug 11, 2020
// 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) {
Copy link
Member

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.

Copy link
Contributor Author

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!

Copy link
Member

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

@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 11, 2020
@VenkatRamaraju VenkatRamaraju changed the title ✨ Added the EventReconcileFilter function for filtering events ✨ Added the LabelSelectorPredicate function for filtering events Aug 11, 2020
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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.

// 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) {
Copy link
Member

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.

@@ -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"}}))
Copy link
Member

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?

It("should return false", func() {
failMatch := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"bar": "foo"},
Copy link
Member

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

Copy link
Member

@alvaroaleman alvaroaleman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 11, 2020
@k8s-ci-robot
Copy link
Contributor

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 11, 2020
@alvaroaleman
Copy link
Member

Thanks @VenkatRamaraju !

@alvaroaleman
Copy link
Member

/retest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants