Skip to content

Commit

Permalink
refactor: parse selector in WithSelector option (#154)
Browse files Browse the repository at this point in the history
* refactor: use pointer for reconciler selector

Signed-off-by: Joe Lanford <joe.lanford@gmail.com>

* refactor: convert selector to predicate in WithSelector to catch error earlier

Signed-off-by: Joe Lanford <joe.lanford@gmail.com>
  • Loading branch information
joelanford authored Mar 2, 2022
1 parent 0d534ef commit f47bc9d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ jobs:
with:
fetch-depth: 0
- name: Run go-apidiff
uses: joelanford/go-apidiff@master
uses: joelanford/go-apidiff@main
19 changes: 9 additions & 10 deletions internal/cmd/helm-operator/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ import (
"runtime"
"strings"

"github.com/operator-framework/helm-operator-plugins/internal/flags"
"github.com/operator-framework/helm-operator-plugins/internal/metrics"
"github.com/operator-framework/helm-operator-plugins/internal/version"
"github.com/operator-framework/helm-operator-plugins/pkg/annotation"
helmmgr "github.com/operator-framework/helm-operator-plugins/pkg/manager"
"github.com/operator-framework/helm-operator-plugins/pkg/reconciler"
"github.com/operator-framework/helm-operator-plugins/pkg/watches"

"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -41,6 +33,14 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
crmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"

"github.com/operator-framework/helm-operator-plugins/internal/flags"
"github.com/operator-framework/helm-operator-plugins/internal/metrics"
"github.com/operator-framework/helm-operator-plugins/internal/version"
"github.com/operator-framework/helm-operator-plugins/pkg/annotation"
helmmgr "github.com/operator-framework/helm-operator-plugins/pkg/manager"
"github.com/operator-framework/helm-operator-plugins/pkg/reconciler"
"github.com/operator-framework/helm-operator-plugins/pkg/watches"
)

var log = logf.Log.WithName("cmd")
Expand Down Expand Up @@ -176,7 +176,6 @@ func run(cmd *cobra.Command, f *flags.Flags) {
}

for _, w := range ws {

r, err := reconciler.New(
reconciler.WithChart(*w.Chart),
reconciler.WithGroupVersionKind(w.GroupVersionKind),
Expand All @@ -190,7 +189,7 @@ func run(cmd *cobra.Command, f *flags.Flags) {
reconciler.WithUninstallAnnotations(annotation.DefaultUninstallAnnotations...),
)
if err != nil {
log.Error(err, "unable to creste helm reconciler", "controller", "Helm")
log.Error(err, "unable to create helm reconciler", "controller", "Helm")
os.Exit(1)
}

Expand Down
33 changes: 9 additions & 24 deletions pkg/reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"errors"
"fmt"
"reflect"
"strings"
"sync"
"time"
Expand All @@ -43,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"
ctrlpredicate "sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/source"

Expand Down Expand Up @@ -72,7 +72,7 @@ type Reconciler struct {
log logr.Logger
gvk *schema.GroupVersionKind
chrt *chart.Chart
selector metav1.LabelSelector
selectorPredicate predicate.Predicate
overrideValues map[string]string
skipDependentWatches bool
maxConcurrentReconciles int
Expand Down Expand Up @@ -420,7 +420,11 @@ func WithValueMapper(m values.Mapper) Option {
// predicate that is used to filter resources based on the specified selector
func WithSelector(s metav1.LabelSelector) Option {
return func(r *Reconciler) error {
r.selector = s
p, err := ctrlpredicate.LabelSelectorPredicate(s)
if err != nil {
return err
}
r.selectorPredicate = p
return nil
}
}
Expand Down Expand Up @@ -820,13 +824,8 @@ func (r *Reconciler) setupWatches(mgr ctrl.Manager, c controller.Controller) err
obj.SetGroupVersionKind(*r.gvk)

var preds []ctrlpredicate.Predicate
p, err := parsePredicateSelector(r.selector)
if err != nil {
return err
}

if p != nil {
preds = append(preds, p)
if r.selectorPredicate != nil {
preds = append(preds, r.selectorPredicate)
}

if err := c.Watch(
Expand Down Expand Up @@ -860,20 +859,6 @@ func (r *Reconciler) setupWatches(mgr ctrl.Manager, c controller.Controller) err
return nil
}

// parsePredicateSelector parses the selector in the WatchOptions and creates a predicate
// that is used to filter resources based on the specified selector
func parsePredicateSelector(selector metav1.LabelSelector) (ctrlpredicate.Predicate, error) {
// If a selector has been specified in watches.yaml, add it to the watch's predicates.
if !reflect.ValueOf(selector).IsZero() {
p, err := ctrlpredicate.LabelSelectorPredicate(selector)
if err != nil {
return nil, fmt.Errorf("error constructing predicate from watches selector: %v", err)
}
return p, nil
}
return nil, nil
}

func ensureDeployedRelease(u *updater.Updater, rel *release.Release) {
reason := conditions.ReasonInstallSuccessful
message := "release was successfully installed"
Expand Down
45 changes: 24 additions & 21 deletions pkg/reconciler/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand Down Expand Up @@ -381,6 +382,29 @@ var _ = Describe("Reconciler", func() {
Expect(r.valueTranslator.Translate(context.Background(), &unstructured.Unstructured{})).To(Equal(chartutil.Values{"translated": true}))
})
})
var _ = Describe("WithSelector", func() {
It("should set the reconciler selector", func() {
objUnlabeled := &unstructured.Unstructured{}

objLabeled := &unstructured.Unstructured{}
objLabeled.SetLabels(map[string]string{"foo": "bar"})

selector := metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}}
Expect(WithSelector(selector)(r)).To(Succeed())
Expect(r.selectorPredicate).NotTo(BeNil())

Expect(r.selectorPredicate.Create(event.CreateEvent{Object: objLabeled})).To(BeTrue())
Expect(r.selectorPredicate.Update(event.UpdateEvent{ObjectOld: objUnlabeled, ObjectNew: objLabeled})).To(BeTrue())
Expect(r.selectorPredicate.Delete(event.DeleteEvent{Object: objLabeled})).To(BeTrue())
Expect(r.selectorPredicate.Generic(event.GenericEvent{Object: objLabeled})).To(BeTrue())

Expect(r.selectorPredicate.Create(event.CreateEvent{Object: objUnlabeled})).To(BeFalse())
Expect(r.selectorPredicate.Update(event.UpdateEvent{ObjectOld: objLabeled, ObjectNew: objUnlabeled})).To(BeFalse())
Expect(r.selectorPredicate.Update(event.UpdateEvent{ObjectOld: objUnlabeled, ObjectNew: objUnlabeled})).To(BeFalse())
Expect(r.selectorPredicate.Delete(event.DeleteEvent{Object: objUnlabeled})).To(BeFalse())
Expect(r.selectorPredicate.Generic(event.GenericEvent{Object: objUnlabeled})).To(BeFalse())
})
})
})

var _ = Describe("Reconcile", func() {
Expand Down Expand Up @@ -1221,27 +1245,6 @@ var _ = Describe("Reconciler", func() {
})
})
})

var _ = Describe("Test predicate selector", func() {
It("verifying when a valid selector is passed", func() {
selectorPass := metav1.LabelSelector{
MatchLabels: map[string]string{
"testKey": "testValue",
},
}

passPredicate, err := parsePredicateSelector(selectorPass)
Expect(err).NotTo(HaveOccurred())
Expect(passPredicate).NotTo(BeNil())
})

It("verifying there is no error when no predicate is passed", func() {
noSelector := metav1.LabelSelector{}
nilPredicate, err := parsePredicateSelector(noSelector)
Expect(err).NotTo(HaveOccurred())
Expect(nilPredicate).To(BeNil())
})
})
})

func getManagerOrFail() manager.Manager {
Expand Down

0 comments on commit f47bc9d

Please sign in to comment.