Skip to content
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
15 changes: 15 additions & 0 deletions pkg/controller/predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,18 @@ func (p StatusChangedPredicate) Update(e event.UpdateEvent) bool {
}
return !reflect.DeepEqual(oldStatus, newStatus)
}

////////////////////////////////////
/// IDENTITY MATCHING PREDICATES ///
////////////////////////////////////

// ExactNamePredicate returns true if the object's name and namespace exactly match the specified values.
// The namespace can be set to '*' to match any namespace.
func ExactNamePredicate(name, namespace string) predicate.Predicate {
return predicate.NewPredicateFuncs(func(obj client.Object) bool {
if obj == nil {
return false
}
return obj.GetName() == name && (namespace == "*" || obj.GetNamespace() == namespace)
})
}
19 changes: 19 additions & 0 deletions pkg/controller/predicates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,25 @@ var _ = Describe("Predicates", func() {

})

Context("Identity", func() {

It("should match resources with the specified identity", func() {
p := ctrlutils.ExactNamePredicate("foo", "bar")
e := event.CreateEvent{Object: base}
Expect(p.Create(e)).To(BeTrue())

p2 := ctrlutils.ExactNamePredicate("foo", "")
Expect(p2.Create(e)).To(BeFalse())

p3 := ctrlutils.ExactNamePredicate("foo", "baz")
Expect(p3.Create(e)).To(BeFalse())

p4 := ctrlutils.ExactNamePredicate("foo", "*")
Expect(p4.Create(e)).To(BeTrue())
})

})

})

func updateEvent(old, new client.Object) event.UpdateEvent {
Expand Down