Skip to content

Commit 3c9e47e

Browse files
porridgevladbologa
authored andcommitted
ROX- 8130: Add WithExtraWatch option. (#22)
1 parent f538f73 commit 3c9e47e

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.
@@ -526,6 +534,22 @@ func WithValueMapper(m values.Mapper) Option {
526534
}
527535
}
528536

537+
// WithExtraWatch is an Option that adds an extra event watch.
538+
// Use this if you want your controller to respond to events other than coming from the primary custom resource,
539+
// the helm release secret, or resources created by your helm chart.
540+
// The meaning of the arguments is the same as for sigs.k8s.io/controller-runtime/pkg/controller.Controller Watch
541+
// function.
542+
func WithExtraWatch(src source.Source, handler handler.EventHandler, predicates ...predicate.Predicate) Option {
543+
return func(r *Reconciler) error {
544+
r.extraWatches = append(r.extraWatches, watchDescription{
545+
src: src,
546+
predicates: predicates,
547+
handler: handler,
548+
})
549+
return nil
550+
}
551+
}
552+
529553
// WithSelector is an Option that configures the reconciler to creates a
530554
// predicate that is used to filter resources based on the specified selector
531555
func WithSelector(s metav1.LabelSelector) Option {
@@ -1056,6 +1080,12 @@ func (r *Reconciler) setupWatches(mgr ctrl.Manager, c controller.Controller) err
10561080
return err
10571081
}
10581082

1083+
for _, w := range r.extraWatches {
1084+
if err := c.Watch(w.src, w.handler, w.predicates...); err != nil {
1085+
return err
1086+
}
1087+
}
1088+
10591089
if !r.skipDependentWatches {
10601090
r.postHooks = append([]hook.PostHook{internalhook.NewDependentResourceWatcher(c, mgr.GetRESTMapper())}, r.postHooks...)
10611091
}

0 commit comments

Comments
 (0)