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
Merged
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
14 changes: 14 additions & 0 deletions pkg/predicate/predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package predicate

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
Expand Down Expand Up @@ -268,3 +270,15 @@ func (o or) Generic(e event.GenericEvent) bool {
}
return false
}

// LabelSelectorPredicate constructs a Predicate from a LabelSelector.
// Only objects matching the LabelSelector will be admitted.
func LabelSelectorPredicate(s metav1.LabelSelector) (Predicate, error) {
selector, err := metav1.LabelSelectorAsSelector(&s)
if err != nil {
return Funcs{}, err
}
return NewPredicateFuncs(func(o controllerutil.Object) bool {
return selector.Matches(labels.Set(o.GetLabels()))
}), nil
}
31 changes: 31 additions & 0 deletions pkg/predicate/predicate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,35 @@ var _ = Describe("Predicate", func() {
})
})
})

Describe("When checking a LabelSelectorPredicate", func() {
instance, err := predicate.LabelSelectorPredicate(metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}})
if err != nil {
Fail("Improper Label Selector passed during predicate instantiation.")
}

Context("When the Selector does not match the event labels", func() {
It("should return false", func() {
failMatch := &corev1.Pod{}
Expect(instance.Create(event.CreateEvent{Object: failMatch})).To(BeFalse())
Expect(instance.Delete(event.DeleteEvent{Object: failMatch})).To(BeFalse())
Expect(instance.Generic(event.GenericEvent{Object: failMatch})).To(BeFalse())
Expect(instance.Update(event.UpdateEvent{ObjectNew: failMatch})).To(BeFalse())
})
})

Context("When the Selector matches the event labels", func() {
It("should return true", func() {
successMatch := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"foo": "bar"},
},
}
Expect(instance.Create(event.CreateEvent{Object: successMatch})).To(BeTrue())
Expect(instance.Delete(event.DeleteEvent{Object: successMatch})).To(BeTrue())
Expect(instance.Generic(event.GenericEvent{Object: successMatch})).To(BeTrue())
Expect(instance.Update(event.UpdateEvent{ObjectNew: successMatch})).To(BeTrue())
})
})
})
})