From fb1419c3cc58daf4ce7d38fc8f67c86e5eb2f8e6 Mon Sep 17 00:00:00 2001 From: "Dr. Stefan Schimanski" Date: Wed, 6 Sep 2023 16:52:49 +0200 Subject: [PATCH] controller: add TriggeredBy custom watches to engine Signed-off-by: Dr. Stefan Schimanski --- pkg/controller/engine.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/pkg/controller/engine.go b/pkg/controller/engine.go index ac470e776..109dc0866 100644 --- a/pkg/controller/engine.go +++ b/pkg/controller/engine.go @@ -149,9 +149,10 @@ func (e *Engine) done(name string, err error) { // Watch an object. type Watch struct { - kind client.Object - handler handler.EventHandler - predicates []predicate.Predicate + kind client.Object + handler handler.EventHandler + predicates []predicate.Predicate + customCache cache.Cache } // For returns a Watch for the supplied kind of object. Events will be handled @@ -160,6 +161,13 @@ func For(kind client.Object, h handler.EventHandler, p ...predicate.Predicate) W return Watch{kind: kind, handler: h, predicates: p} } +// TriggeredBy returns a custom watch for secondary resources triggering the +// controller. Events will be handled by the supplied EventHandler, and may be +// filtered by the supplied predicates. +func TriggeredBy(cache cache.Cache, kind client.Object, h handler.EventHandler, p ...predicate.Predicate) Watch { + return Watch{kind: kind, handler: h, predicates: p, customCache: cache} +} + // Start the named controller. Each controller is started with its own cache // whose lifecycle is coupled to the controller. The controller is started with // the supplied options, and configured with the supplied watches. Start does @@ -191,6 +199,10 @@ func (e *Engine) Start(name string, o controller.Options, w ...Watch) error { } for _, wt := range w { + ca := ca + if wt.customCache != nil { + ca = wt.customCache + } if err := ctrl.Watch(source.Kind(ca, wt.kind), wt.handler, wt.predicates...); err != nil { return errors.Wrap(err, errWatch) }