Skip to content

Commit

Permalink
predicate for helm
Browse files Browse the repository at this point in the history
Signed-off-by: Venkat Ramaraju <venky2063@gmail.com>
  • Loading branch information
VenkatRamaraju committed Jun 18, 2021
1 parent 0c3d84d commit c05f541
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions internal/cmd/helm-operator/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func run(cmd *cobra.Command, f *flags.Flags) {
WatchDependentResources: *w.WatchDependentResources,
OverrideValues: w.OverrideValues,
MaxConcurrentReconciles: f.MaxConcurrentReconciles,
Selector: w.Selector,
})
if err != nil {
log.Error(err, "Failed to add manager factory to controller.")
Expand Down
12 changes: 11 additions & 1 deletion internal/helm/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
crthandler "sigs.k8s.io/controller-runtime/pkg/handler"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
filterPredicate "sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/source"
"sigs.k8s.io/yaml"

Expand All @@ -51,6 +52,7 @@ type WatchOptions struct {
WatchDependentResources bool
OverrideValues map[string]string
MaxConcurrentReconciles int
Selector metav1.LabelSelector
}

// Add creates a new helm operator controller and adds it to the manager
Expand Down Expand Up @@ -78,9 +80,17 @@ func Add(mgr manager.Manager, options WatchOptions) error {
return err
}

// Predicate that will filter events based on labels
filterPredicate, err := filterPredicate.LabelSelectorPredicate(options.Selector)

if err != nil {
log.Error(err, "Unable to create predicate from selector.")
return err
}

o := &unstructured.Unstructured{}
o.SetGroupVersionKind(options.GVK)
if err := c.Watch(&source.Kind{Type: o}, &libhandler.InstrumentedEnqueueRequestForObject{}); err != nil {
if err := c.Watch(&source.Kind{Type: o}, &libhandler.InstrumentedEnqueueRequestForObject{}, filterPredicate); err != nil {
return err
}

Expand Down
8 changes: 5 additions & 3 deletions internal/helm/watches/watches.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"

"helm.sh/helm/v3/pkg/chartutil"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/yaml"
)
Expand All @@ -32,9 +33,10 @@ const WatchesFile = "watches.yaml"
// custom resource.
type Watch struct {
schema.GroupVersionKind `json:",inline"`
ChartDir string `json:"chart"`
WatchDependentResources *bool `json:"watchDependentResources,omitempty"`
OverrideValues map[string]string `json:"overrideValues,omitempty"`
ChartDir string `json:"chart"`
WatchDependentResources *bool `json:"watchDependentResources,omitempty"`
OverrideValues map[string]string `json:"overrideValues,omitempty"`
Selector metav1.LabelSelector `json:"selector"`
}

// UnmarshalYAML unmarshals an individual watch from the Helm watches.yaml file
Expand Down

0 comments on commit c05f541

Please sign in to comment.