@@ -43,6 +43,7 @@ import (
4343 "sigs.k8s.io/controller-runtime/pkg/controller"
4444 "sigs.k8s.io/controller-runtime/pkg/handler"
4545 "sigs.k8s.io/controller-runtime/pkg/predicate"
46+ "sigs.k8s.io/controller-runtime/pkg/predicate"
4647 ctrlpredicate "sigs.k8s.io/controller-runtime/pkg/predicate"
4748 "sigs.k8s.io/controller-runtime/pkg/source"
4849
@@ -80,6 +81,7 @@ type Reconciler struct {
8081 selectorPredicate predicate.Predicate
8182 overrideValues map [string ]string
8283 skipDependentWatches bool
84+ extraWatches []watchDescription
8385 maxConcurrentReconciles int
8486 reconcilePeriod time.Duration
8587 markFailedAfter time.Duration
@@ -95,6 +97,12 @@ type Reconciler struct {
9597 uninstallAnnotations map [string ]annotation.Uninstall
9698}
9799
100+ type watchDescription struct {
101+ src source.Source
102+ predicates []predicate.Predicate
103+ handler handler.EventHandler
104+ }
105+
98106// New creates a new Reconciler that reconciles custom resources that define a
99107// Helm release. New takes variadic Option arguments that are used to configure
100108// the Reconciler.
@@ -529,6 +537,22 @@ func WithValueMapper(m values.Mapper) Option {
529537 }
530538}
531539
540+ // WithExtraWatch is an Option that adds an extra event watch.
541+ // Use this if you want your controller to respond to events other than coming from the primary custom resource,
542+ // the helm release secret, or resources created by your helm chart.
543+ // The meaning of the arguments is the same as for sigs.k8s.io/controller-runtime/pkg/controller.Controller Watch
544+ // function.
545+ func WithExtraWatch (src source.Source , handler handler.EventHandler , predicates ... predicate.Predicate ) Option {
546+ return func (r * Reconciler ) error {
547+ r .extraWatches = append (r .extraWatches , watchDescription {
548+ src : src ,
549+ predicates : predicates ,
550+ handler : handler ,
551+ })
552+ return nil
553+ }
554+ }
555+
532556// WithSelector is an Option that configures the reconciler to creates a
533557// predicate that is used to filter resources based on the specified selector
534558func WithSelector (s metav1.LabelSelector ) Option {
@@ -1066,6 +1090,12 @@ func (r *Reconciler) setupWatches(mgr ctrl.Manager, c controller.Controller) err
10661090 return err
10671091 }
10681092
1093+ for _ , w := range r .extraWatches {
1094+ if err := c .Watch (w .src , w .handler , w .predicates ... ); err != nil {
1095+ return err
1096+ }
1097+ }
1098+
10691099 if ! r .skipDependentWatches {
10701100 r .postHooks = append ([]hook.PostHook {internalhook .NewDependentResourceWatcher (c , mgr .GetRESTMapper ())}, r .postHooks ... )
10711101 }
0 commit comments