Skip to content

Commit 45354e3

Browse files
Simplification
1 parent 37a7265 commit 45354e3

File tree

2 files changed

+12
-44
lines changed

2 files changed

+12
-44
lines changed

pkg/predicate/predicate.go

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -271,47 +271,15 @@ func (o or) Generic(e event.GenericEvent) bool {
271271
return false
272272
}
273273

274-
// LabelSelectorPredicate implements predicate functions and requires a selector that will filter an event by its labels.
275-
//
276-
// This predicate will skip reconciliation on all events that have labels that do not match the selector of the predicate.
277-
// A metav1.LabelSelector is passed as an input parameter while instantiating an LabelSelectorPredicate, and this is (explicitly)
278-
// converted to a labels.Selector object, and stored within the struct. When a predicate function is evoked, the event labels are checked
279-
// for a match with the predicate's selector to decide whether an event undergoes reconciliation or not.
280-
// If the labels match, the event will undergo reconciliation. If the labels do not match, it will skip reconciliation for that particular event.
281-
282-
type LabelSelectorPredicate struct {
283-
Funcs
284-
Selector labels.Selector
285-
}
286-
287-
// eventFilter skips reconciliation of events that have labels matching selectors
288-
func (r LabelSelectorPredicate) eventFilter(eventLabels map[string]string) bool {
289-
return r.Selector.Matches(labels.Set(eventLabels))
290-
}
291-
292-
// NewLabelSelectorPredicate instantiates a new LabelSelectorPredicate with the LabelSelector specified through parameters.
293-
func NewLabelSelectorPredicate(s metav1.LabelSelector) (Predicate, error) {
294-
selectorSpecs, err := metav1.LabelSelectorAsSelector(&s)
295-
requirements := LabelSelectorPredicate{Selector: selectorSpecs}
296-
return requirements, err
297-
}
298-
299-
// Update implements default UpdateEvent filter for validating event labels against the predicate selector
300-
func (r LabelSelectorPredicate) Update(e event.UpdateEvent) bool {
301-
return r.eventFilter(e.MetaNew.GetLabels())
302-
}
303-
304-
// Create implements default CreateEvent filter for validating event labels against the predicate selector
305-
func (r LabelSelectorPredicate) Create(e event.CreateEvent) bool {
306-
return r.eventFilter(e.Meta.GetLabels())
307-
}
308-
309-
// Delete implements default DeleteEvent filter for validating event labels against the predicate selector
310-
func (r LabelSelectorPredicate) Delete(e event.DeleteEvent) bool {
311-
return r.eventFilter(e.Meta.GetLabels())
312-
}
313-
314-
// Generic implements default GenericEvent filter for validating event labels against the predicate selector
315-
func (r LabelSelectorPredicate) Generic(e event.GenericEvent) bool {
316-
return r.eventFilter(e.Meta.GetLabels())
274+
// EventReconcileFilter takes in a LabelSelector as a parameter and returns predicate functions. The labels of
275+
// events that are contained as parameters in these predicate functions are compared against the instantiated
276+
// Selector object and reconciliation of the event will only take place if the labels match.
277+
func EventReconcileFilter(s metav1.LabelSelector) (Funcs, error) {
278+
selector, err := metav1.LabelSelectorAsSelector(&s)
279+
if err != nil {
280+
return Funcs{}, err
281+
}
282+
return NewPredicateFuncs(func(m metav1.Object, o runtime.Object) bool {
283+
return selector.Matches(labels.Set(m.GetLabels()))
284+
}), nil
317285
}

pkg/predicate/predicate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ var _ = Describe("Predicate", func() {
545545
})
546546

547547
Describe("When checking a LabelSelectorPredicate", func() {
548-
instance, err := predicate.NewLabelSelectorPredicate(*(&metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}}))
548+
instance, err := predicate.EventReconcileFilter(*(&metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}}))
549549
successLabels := map[string]string{"foo": "bar"}
550550
failureLabels := map[string]string{"bar": "foo"}
551551
if err != nil {

0 commit comments

Comments
 (0)