Skip to content

Commit af75988

Browse files
porridgeludydoo
authored andcommitted
ROX- 8130: Add WithExtraWatch option. (#22)
1 parent b06755e commit af75988

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
@@ -44,6 +44,7 @@ import (
4444
"sigs.k8s.io/controller-runtime/pkg/controller"
4545
"sigs.k8s.io/controller-runtime/pkg/handler"
4646
"sigs.k8s.io/controller-runtime/pkg/predicate"
47+
"sigs.k8s.io/controller-runtime/pkg/predicate"
4748
ctrlpredicate "sigs.k8s.io/controller-runtime/pkg/predicate"
4849
"sigs.k8s.io/controller-runtime/pkg/source"
4950

@@ -81,6 +82,7 @@ type Reconciler struct {
8182
selectorPredicate predicate.Predicate
8283
overrideValues map[string]string
8384
skipDependentWatches bool
85+
extraWatches []watchDescription
8486
maxConcurrentReconciles int
8587
reconcilePeriod time.Duration
8688
markFailedAfter time.Duration
@@ -97,6 +99,12 @@ type Reconciler struct {
9799
uninstallAnnotations map[string]annotation.Uninstall
98100
}
99101

102+
type watchDescription struct {
103+
src source.Source
104+
predicates []predicate.Predicate
105+
handler handler.EventHandler
106+
}
107+
100108
// New creates a new Reconciler that reconciles custom resources that define a
101109
// Helm release. New takes variadic Option arguments that are used to configure
102110
// the Reconciler.
@@ -538,6 +546,22 @@ func WithValueMapper(m values.Mapper) Option {
538546
}
539547
}
540548

549+
// WithExtraWatch is an Option that adds an extra event watch.
550+
// Use this if you want your controller to respond to events other than coming from the primary custom resource,
551+
// the helm release secret, or resources created by your helm chart.
552+
// The meaning of the arguments is the same as for sigs.k8s.io/controller-runtime/pkg/controller.Controller Watch
553+
// function.
554+
func WithExtraWatch(src source.Source, handler handler.EventHandler, predicates ...predicate.Predicate) Option {
555+
return func(r *Reconciler) error {
556+
r.extraWatches = append(r.extraWatches, watchDescription{
557+
src: src,
558+
predicates: predicates,
559+
handler: handler,
560+
})
561+
return nil
562+
}
563+
}
564+
541565
// WithSelector is an Option that configures the reconciler to creates a
542566
// predicate that is used to filter resources based on the specified selector
543567
func WithSelector(s metav1.LabelSelector) Option {
@@ -1104,6 +1128,12 @@ func (r *Reconciler) setupWatches(mgr ctrl.Manager, c controller.Controller) err
11041128
return err
11051129
}
11061130

1131+
for _, w := range r.extraWatches {
1132+
if err := c.Watch(w.src, w.handler, w.predicates...); err != nil {
1133+
return err
1134+
}
1135+
}
1136+
11071137
if !r.skipDependentWatches {
11081138
r.postHooks = append([]hook.PostHook{internalhook.NewDependentResourceWatcher(c, mgr.GetRESTMapper(), mgr.GetCache(), mgr.GetScheme())}, r.postHooks...)
11091139
}

0 commit comments

Comments
 (0)