Skip to content

Commit

Permalink
Implementation + Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VenkatRamaraju committed Aug 11, 2020
1 parent 1095de6 commit 3f0bf57
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
15 changes: 15 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,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
// 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) {
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
}
35 changes: 35 additions & 0 deletions pkg/predicate/predicate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}}))
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{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"bar": "foo"},
},
}
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())
})
})
})
})

0 comments on commit 3f0bf57

Please sign in to comment.