Skip to content

Commit

Permalink
Refactoring predicate filtering for ansible
Browse files Browse the repository at this point in the history
  • Loading branch information
VenkatRamaraju committed Jul 13, 2021
1 parent 46681e3 commit 65bfa7e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 94 deletions.
15 changes: 9 additions & 6 deletions internal/ansible/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package controller
import (
"fmt"
"os"
"reflect"
"strings"
"time"

Expand All @@ -33,7 +34,6 @@ import (

"github.com/operator-framework/operator-sdk/internal/ansible/events"
"github.com/operator-framework/operator-sdk/internal/ansible/handler"
"github.com/operator-framework/operator-sdk/internal/ansible/predicate"
"github.com/operator-framework/operator-sdk/internal/ansible/runner"
)

Expand Down Expand Up @@ -103,12 +103,15 @@ func Add(mgr manager.Manager, options Options) *controller.Controller {
predicates := []ctrlpredicate.Predicate{
ctrlpredicate.Or(ctrlpredicate.GenerationChangedPredicate{}, libpredicate.NoGenerationPredicate{}),
}
filterPredicate, err := predicate.NewResourceFilterPredicate(options.Selector)
if err != nil {
log.Error(err, "Error creating resource filter predicate")
os.Exit(1)

if !reflect.ValueOf(options.Selector).IsZero() {
filterPredicate, err := ctrlpredicate.LabelSelectorPredicate(options.Selector)
if err != nil {
log.Error(err, "Unable to create predicate from selector.")
os.Exit(1)
}
predicates = append(predicates, filterPredicate)
}
predicates = append(predicates, filterPredicate)

u := &unstructured.Unstructured{}
u.SetGroupVersionKind(options.GVK)
Expand Down
56 changes: 0 additions & 56 deletions internal/ansible/predicate/predicate.go

This file was deleted.

34 changes: 2 additions & 32 deletions internal/ansible/watches/watches.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,36 +85,6 @@ var (
ansibleVerbosityDefault = 2
)

// Creates, populates, and returns a LabelSelector object. Used in Unmarshal().
func parseLabelSelector(dls tempLabelSelector) metav1.LabelSelector {
obj := metav1.LabelSelector{}
obj.MatchLabels = dls.MatchLabels

for _, v := range dls.MatchExpressions {
requirement := metav1.LabelSelectorRequirement{
Key: v.Key,
Operator: v.Operator,
Values: v.Values,
}

obj.MatchExpressions = append(obj.MatchExpressions, requirement)
}

return obj
}

// Temporary structs created to store yaml parsing
type tempLabelSelector struct {
MatchLabels map[string]string `yaml:"matchLabels,omitempty"`
MatchExpressions []tempRequirement `json:"matchExpressions,omitempty"`
}

type tempRequirement struct {
Key string `json:"key"`
Operator metav1.LabelSelectorOperator `json:"operator"`
Values []string `json:"values,omitempty"`
}

// Use an alias struct to handle complex types
type alias struct {
Group string `yaml:"group"`
Expand All @@ -132,7 +102,7 @@ type alias struct {
MarkUnsafe *bool `yaml:"markUnsafe"`
Blacklist []schema.GroupVersionKind `yaml:"blacklist,omitempty"`
Finalizer *Finalizer `yaml:"finalizer"`
Selector tempLabelSelector `yaml:"selector"`
Selector metav1.LabelSelector `yaml:"selector"`
}

// buildWatch will build Watch based on the values parsed from alias
Expand Down Expand Up @@ -201,7 +171,7 @@ func (w *Watch) setValuesFromAlias(tmp alias) error {
return err
}
w.addRolePlaybookPaths(wd)
w.Selector = parseLabelSelector(tmp.Selector)
w.Selector = tmp.Selector

return nil
}
Expand Down

0 comments on commit 65bfa7e

Please sign in to comment.