diff --git a/pkg/controller/predicates.go b/pkg/controller/predicates.go index 3bb0f2f..feadd11 100644 --- a/pkg/controller/predicates.go +++ b/pkg/controller/predicates.go @@ -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) + }) +} diff --git a/pkg/controller/predicates_test.go b/pkg/controller/predicates_test.go index 010f93e..4e15a36 100644 --- a/pkg/controller/predicates_test.go +++ b/pkg/controller/predicates_test.go @@ -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 {