Skip to content

Commit a8d57a5

Browse files
porridgeSimonBaeumer
authored andcommitted
ROX- 8130: Add WithExtraWatch option. (#22)
1 parent dd01e3d commit a8d57a5

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

@@ -79,6 +80,7 @@ type Reconciler struct {
7980
selectorPredicate predicate.Predicate
8081
overrideValues map[string]string
8182
skipDependentWatches bool
83+
extraWatches []watchDescription
8284
maxConcurrentReconciles int
8385
reconcilePeriod time.Duration
8486
markFailedAfter time.Duration
@@ -94,6 +96,12 @@ type Reconciler struct {
9496
uninstallAnnotations map[string]annotation.Uninstall
9597
}
9698

99+
type watchDescription struct {
100+
src source.Source
101+
predicates []predicate.Predicate
102+
handler handler.EventHandler
103+
}
104+
97105
// New creates a new Reconciler that reconciles custom resources that define a
98106
// Helm release. New takes variadic Option arguments that are used to configure
99107
// the Reconciler.
@@ -525,6 +533,22 @@ func WithValueMapper(m values.Mapper) Option {
525533
}
526534
}
527535

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

1060+
for _, w := range r.extraWatches {
1061+
if err := c.Watch(w.src, w.handler, w.predicates...); err != nil {
1062+
return err
1063+
}
1064+
}
1065+
10361066
if !r.skipDependentWatches {
10371067
r.postHooks = append([]hook.PostHook{internalhook.NewDependentResourceWatcher(c, mgr.GetRESTMapper())}, r.postHooks...)
10381068
}

0 commit comments

Comments
 (0)