Skip to content

Commit

Permalink
ROX- 8130: Add WithExtraWatch option. (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
porridge authored and SimonBaeumer committed May 5, 2022
1 parent dd01e3d commit a8d57a5
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions pkg/reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/predicate"
ctrlpredicate "sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/source"

Expand Down Expand Up @@ -79,6 +80,7 @@ type Reconciler struct {
selectorPredicate predicate.Predicate
overrideValues map[string]string
skipDependentWatches bool
extraWatches []watchDescription
maxConcurrentReconciles int
reconcilePeriod time.Duration
markFailedAfter time.Duration
Expand All @@ -94,6 +96,12 @@ type Reconciler struct {
uninstallAnnotations map[string]annotation.Uninstall
}

type watchDescription struct {
src source.Source
predicates []predicate.Predicate
handler handler.EventHandler
}

// New creates a new Reconciler that reconciles custom resources that define a
// Helm release. New takes variadic Option arguments that are used to configure
// the Reconciler.
Expand Down Expand Up @@ -525,6 +533,22 @@ func WithValueMapper(m values.Mapper) Option {
}
}

// WithExtraWatch is an Option that adds an extra event watch.
// Use this if you want your controller to respond to events other than coming from the primary custom resource,
// the helm release secret, or resources created by your helm chart.
// The meaning of the arguments is the same as for sigs.k8s.io/controller-runtime/pkg/controller.Controller Watch
// function.
func WithExtraWatch(src source.Source, handler handler.EventHandler, predicates ...predicate.Predicate) Option {
return func(r *Reconciler) error {
r.extraWatches = append(r.extraWatches, watchDescription{
src: src,
predicates: predicates,
handler: handler,
})
return nil
}
}

// WithSelector is an Option that configures the reconciler to creates a
// predicate that is used to filter resources based on the specified selector
func WithSelector(s metav1.LabelSelector) Option {
Expand Down Expand Up @@ -1033,6 +1057,12 @@ func (r *Reconciler) setupWatches(mgr ctrl.Manager, c controller.Controller) err
return err
}

for _, w := range r.extraWatches {
if err := c.Watch(w.src, w.handler, w.predicates...); err != nil {
return err
}
}

if !r.skipDependentWatches {
r.postHooks = append([]hook.PostHook{internalhook.NewDependentResourceWatcher(c, mgr.GetRESTMapper())}, r.postHooks...)
}
Expand Down

0 comments on commit a8d57a5

Please sign in to comment.