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 Jul 29, 2021
1 parent 0c3d84d commit 6942467
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 5 deletions.
7 changes: 7 additions & 0 deletions changelog/fragments/helm-labelselector.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
entries:
- description: >
Added a filter predicate for labels based on selectors specified in watches.yaml.
Only the resources that contain the labels specified by selectors will be reconciled.
kind: addition
breaking: false
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
18 changes: 17 additions & 1 deletion internal/helm/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package controller

import (
"fmt"
"reflect"
"strings"
"sync"
"time"
Expand All @@ -30,6 +31,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"
ctrlpredicate "sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/source"
"sigs.k8s.io/yaml"

Expand All @@ -51,6 +53,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 @@ -80,7 +83,20 @@ func Add(mgr manager.Manager, options WatchOptions) error {

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

var preds []ctrlpredicate.Predicate

// If a selector has been specified in watches.yaml, add it to the watch's predicates.
if !reflect.ValueOf(options.Selector).IsZero() {
p, err := ctrlpredicate.LabelSelectorPredicate(options.Selector)
if err != nil {
log.Error(err, "Unable to create predicate from selector.")
return err
}
preds = append(preds, p)
}

if err := c.Watch(&source.Kind{Type: o}, &libhandler.InstrumentedEnqueueRequestForObject{}, preds...); err != nil {
return err
}

Expand Down
43 changes: 43 additions & 0 deletions internal/helm/controller/controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2018 The Operator-SDK Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package controller

import (
"testing"

"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/manager"
)

func TestFilterPredicate(t *testing.T) {
selector := make(map[string]string)
selector["testKey"] = "testValue"
options := WatchOptions{
Selector: metav1.LabelSelector{
MatchLabels: selector,
},
GVK: schema.GroupVersionKind{
Group: "test-group",
Version: "v1",
Kind: "Test-Kind",
}}
cfg, _ := config.GetConfig()
mgr, _ := manager.New(cfg, manager.Options{})

assert.Equal(t, nil, Add(mgr, options), "Assert Add function returns nil after completion")
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ The follow tables describes the fields in an entry in `watches.yaml`:
| chart | The path to the helm chart to use when reconciling this GVK. |
| watchDependentResources | Enable watching resources that are created by helm (default: `true`). |
| overrideValues | Values to be used for overriding Helm chart's defaults. For additional information see the [reference doc][override-values]. |
| selector | The conditions that a resource's labels must satsify in order to get reconciled. For additional information see [labels and selectors documentation][label-selector-doc]. |


For reference, here is an example of a simple `watches.yaml` file:
Expand All @@ -31,7 +32,11 @@ For reference, here is an example of a simple `watches.yaml` file:
chart: helm-charts/foo
overrideValues:
image.repository: quay.io/mycustomrepo
watchDependentResources: false
watchDependentResources: false
selector:
matchExpressions:
- {key: testLabel, operator: Exists, values: []}
```
[override-values]: /docs/building-operators/helm/reference/advanced_features/override_values/
[label-selector-doc]: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/

0 comments on commit 6942467

Please sign in to comment.