Skip to content

Commit

Permalink
controller: add TriggeredBy custom watches to engine
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Stefan Schimanski <stefan.schimanski@upbound.io>
  • Loading branch information
sttts committed Sep 6, 2023
1 parent a2674ee commit fb1419c
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions pkg/controller/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit fb1419c

Please sign in to comment.