Skip to content

Commit 83bf32b

Browse files
porridgemisberner
authored andcommitted
ROX- 8130: Add WithExtraWatch option. (#22)
1 parent be5747a commit 83bf32b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

pkg/reconciler/reconciler.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
534558
func WithSelector(s metav1.LabelSelector) Option {
@@ -1063,6 +1087,12 @@ func (r *Reconciler) setupWatches(mgr ctrl.Manager, c controller.Controller) err
10631087
return err
10641088
}
10651089

1090+
for _, w := range r.extraWatches {
1091+
if err := c.Watch(w.src, w.handler, w.predicates...); err != nil {
1092+
return err
1093+
}
1094+
}
1095+
10661096
if !r.skipDependentWatches {
10671097
r.postHooks = append([]hook.PostHook{internalhook.NewDependentResourceWatcher(c, mgr.GetRESTMapper())}, r.postHooks...)
10681098
}

0 commit comments

Comments
 (0)