Skip to content

ROX- 8130: Add WithExtraWatch option. #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions pkg/reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -463,6 +471,22 @@ func WithValueMapper(m values.Mapper) Option {
}
}

// WithExtraWatch is an Option that adds an extra event watch.
// 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.
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.
Expand Down Expand Up @@ -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...)
}
Expand Down