From 2a80ad76014cd1cbd1cb401af6a601a66dcacfe9 Mon Sep 17 00:00:00 2001 From: Marcin Owsiany Date: Mon, 6 Dec 2021 12:55:55 +0100 Subject: [PATCH 1/3] Added WithExtraWatch option. --- pkg/reconciler/reconciler.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pkg/reconciler/reconciler.go b/pkg/reconciler/reconciler.go index 19f6deb..c65c75a 100644 --- a/pkg/reconciler/reconciler.go +++ b/pkg/reconciler/reconciler.go @@ -42,6 +42,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/handler" + "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/source" "github.com/joelanford/helm-operator/pkg/annotation" @@ -76,6 +77,7 @@ type Reconciler struct { chrt *chart.Chart overrideValues map[string]string skipDependentWatches bool + extraWatches []watchDescription maxConcurrentReconciles int reconcilePeriod time.Duration markFailedAfter time.Duration @@ -90,6 +92,12 @@ type Reconciler struct { uninstallAnnotations map[string]annotation.Uninstall } +type watchDescription struct { + src source.Source + predicates []predicate.Predicate + handler handler.EventHandler +} + // New creates a new Reconciler that reconciles custom resources that define a // Helm release. New takes variadic Option arguments that are used to configure // the Reconciler. @@ -463,6 +471,22 @@ func WithValueMapper(m values.Mapper) Option { } } +// WithExtraWatch is an Option that adds an extra event watch. +// Use this is you want your controller to respond to events other than coming from the primary custom resource, +// the helm release secret, or resources created by your helm chart. +// The meaning of the arguments is the same as for sigs.k8s.io/controller-runtime/pkg/controller.Controller Watch +// function. +func WithExtraWatch(src source.Source, handler handler.EventHandler, predicates... predicate.Predicate) Option { + return func(r *Reconciler) error { + r.extraWatches = append(r.extraWatches, watchDescription{ + src: src, + predicates: predicates, + handler: handler, + }) + return nil + } +} + // Reconcile reconciles a CR that defines a Helm v3 release. // // - If a release does not exist for this CR, a new release is installed. @@ -953,6 +977,12 @@ func (r *Reconciler) setupWatches(mgr ctrl.Manager, c controller.Controller) err return err } + for _, w := range r.extraWatches { + if err := c.Watch(w.src, w.handler, w.predicates...); err != nil { + return err + } + } + if !r.skipDependentWatches { r.postHooks = append([]hook.PostHook{internalhook.NewDependentResourceWatcher(c, mgr.GetRESTMapper())}, r.postHooks...) } From ec6d97017b6b3ca56f703495abb61019d722026e Mon Sep 17 00:00:00 2001 From: Marcin Owsiany Date: Fri, 10 Dec 2021 12:30:45 +0100 Subject: [PATCH 2/3] lint --- pkg/reconciler/reconciler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/reconciler/reconciler.go b/pkg/reconciler/reconciler.go index c65c75a..5116bfb 100644 --- a/pkg/reconciler/reconciler.go +++ b/pkg/reconciler/reconciler.go @@ -476,7 +476,7 @@ func WithValueMapper(m values.Mapper) Option { // the helm release secret, or resources created by your helm chart. // The meaning of the arguments is the same as for sigs.k8s.io/controller-runtime/pkg/controller.Controller Watch // function. -func WithExtraWatch(src source.Source, handler handler.EventHandler, predicates... predicate.Predicate) Option { +func WithExtraWatch(src source.Source, handler handler.EventHandler, predicates ...predicate.Predicate) Option { return func(r *Reconciler) error { r.extraWatches = append(r.extraWatches, watchDescription{ src: src, From c6cdb0e6d46eb93ddb7175ed0df9ee5944776125 Mon Sep 17 00:00:00 2001 From: Marcin Owsiany Date: Tue, 14 Dec 2021 06:54:37 +0100 Subject: [PATCH 3/3] Fix typo. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Simon Bäumer --- pkg/reconciler/reconciler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/reconciler/reconciler.go b/pkg/reconciler/reconciler.go index 5116bfb..2b9e33c 100644 --- a/pkg/reconciler/reconciler.go +++ b/pkg/reconciler/reconciler.go @@ -472,7 +472,7 @@ func WithValueMapper(m values.Mapper) Option { } // WithExtraWatch is an Option that adds an extra event watch. -// Use this is you want your controller to respond to events other than coming from the primary custom resource, +// Use this if you want your controller to respond to events other than coming from the primary custom resource, // the helm release secret, or resources created by your helm chart. // The meaning of the arguments is the same as for sigs.k8s.io/controller-runtime/pkg/controller.Controller Watch // function.