Skip to content

Commit 020b64e

Browse files
authored
feat: add exact name predicate (#149)
* feat: add exact name predicate * allow namespace wildcard for exact name predicate
1 parent 911636c commit 020b64e

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

pkg/controller/predicates.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,18 @@ func (p StatusChangedPredicate) Update(e event.UpdateEvent) bool {
217217
}
218218
return !reflect.DeepEqual(oldStatus, newStatus)
219219
}
220+
221+
////////////////////////////////////
222+
/// IDENTITY MATCHING PREDICATES ///
223+
////////////////////////////////////
224+
225+
// ExactNamePredicate returns true if the object's name and namespace exactly match the specified values.
226+
// The namespace can be set to '*' to match any namespace.
227+
func ExactNamePredicate(name, namespace string) predicate.Predicate {
228+
return predicate.NewPredicateFuncs(func(obj client.Object) bool {
229+
if obj == nil {
230+
return false
231+
}
232+
return obj.GetName() == name && (namespace == "*" || obj.GetNamespace() == namespace)
233+
})
234+
}

pkg/controller/predicates_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,25 @@ var _ = Describe("Predicates", func() {
234234

235235
})
236236

237+
Context("Identity", func() {
238+
239+
It("should match resources with the specified identity", func() {
240+
p := ctrlutils.ExactNamePredicate("foo", "bar")
241+
e := event.CreateEvent{Object: base}
242+
Expect(p.Create(e)).To(BeTrue())
243+
244+
p2 := ctrlutils.ExactNamePredicate("foo", "")
245+
Expect(p2.Create(e)).To(BeFalse())
246+
247+
p3 := ctrlutils.ExactNamePredicate("foo", "baz")
248+
Expect(p3.Create(e)).To(BeFalse())
249+
250+
p4 := ctrlutils.ExactNamePredicate("foo", "*")
251+
Expect(p4.Create(e)).To(BeTrue())
252+
})
253+
254+
})
255+
237256
})
238257

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

0 commit comments

Comments
 (0)