Skip to content

Commit 67ebc33

Browse files
Refactoring code for label-based filtering for Ansible Operators (#5086)
Signed-off-by: Venkat Ramaraju <[email protected]>
1 parent a05f966 commit 67ebc33

File tree

4 files changed

+64
-92
lines changed

4 files changed

+64
-92
lines changed

internal/ansible/controller/controller.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package controller
1717
import (
1818
"fmt"
1919
"os"
20+
"reflect"
2021
"strings"
2122
"time"
2223

@@ -33,7 +34,6 @@ import (
3334

3435
"github.com/operator-framework/operator-sdk/internal/ansible/events"
3536
"github.com/operator-framework/operator-sdk/internal/ansible/handler"
36-
"github.com/operator-framework/operator-sdk/internal/ansible/predicate"
3737
"github.com/operator-framework/operator-sdk/internal/ansible/runner"
3838
)
3939

@@ -103,12 +103,17 @@ func Add(mgr manager.Manager, options Options) *controller.Controller {
103103
predicates := []ctrlpredicate.Predicate{
104104
ctrlpredicate.Or(ctrlpredicate.GenerationChangedPredicate{}, libpredicate.NoGenerationPredicate{}),
105105
}
106-
filterPredicate, err := predicate.NewResourceFilterPredicate(options.Selector)
106+
107+
p, err := parsePredicateSelector(options.Selector)
108+
107109
if err != nil {
108-
log.Error(err, "Error creating resource filter predicate")
110+
log.Error(err, "")
109111
os.Exit(1)
110112
}
111-
predicates = append(predicates, filterPredicate)
113+
114+
if p != nil {
115+
predicates = append(predicates, p)
116+
}
112117

113118
u := &unstructured.Unstructured{}
114119
u.SetGroupVersionKind(options.GVK)
@@ -120,3 +125,17 @@ func Add(mgr manager.Manager, options Options) *controller.Controller {
120125

121126
return &c
122127
}
128+
129+
// parsePredicateSelector parses the selector in the WatchOptions and creates a predicate
130+
// that is used to filter resources based on the specified selector
131+
func parsePredicateSelector(selector metav1.LabelSelector) (ctrlpredicate.Predicate, error) {
132+
// If a selector has been specified in watches.yaml, add it to the watch's predicates.
133+
if !reflect.ValueOf(selector).IsZero() {
134+
p, err := ctrlpredicate.LabelSelectorPredicate(selector)
135+
if err != nil {
136+
return nil, fmt.Errorf("error constructing predicate from watches selector: %v", err)
137+
}
138+
return p, nil
139+
}
140+
return nil, nil
141+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2021 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package controller
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/assert"
21+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
)
23+
24+
func TestFilterPredicate(t *testing.T) {
25+
matchLabelPass := make(map[string]string)
26+
matchLabelPass["testKey"] = "testValue"
27+
selectorPass := metav1.LabelSelector{
28+
MatchLabels: matchLabelPass,
29+
}
30+
noSelector := metav1.LabelSelector{}
31+
32+
passPredicate, err := parsePredicateSelector(selectorPass)
33+
assert.Equal(t, nil, err, "Verify that no error is thrown on a valid populated selector")
34+
assert.NotEqual(t, nil, passPredicate, "Verify that a predicate is constructed using a valid selector")
35+
36+
nilPredicate, err := parsePredicateSelector(noSelector)
37+
assert.Equal(t, nil, err, "Verify that no error is thrown on a valid unpopulated selector")
38+
assert.Equal(t, nil, nilPredicate, "Verify correct parsing of an unpopulated selector")
39+
}

internal/ansible/predicate/predicate.go

Lines changed: 0 additions & 56 deletions
This file was deleted.

internal/ansible/watches/watches.go

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -85,36 +85,6 @@ var (
8585
ansibleVerbosityDefault = 2
8686
)
8787

88-
// Creates, populates, and returns a LabelSelector object. Used in Unmarshal().
89-
func parseLabelSelector(dls tempLabelSelector) metav1.LabelSelector {
90-
obj := metav1.LabelSelector{}
91-
obj.MatchLabels = dls.MatchLabels
92-
93-
for _, v := range dls.MatchExpressions {
94-
requirement := metav1.LabelSelectorRequirement{
95-
Key: v.Key,
96-
Operator: v.Operator,
97-
Values: v.Values,
98-
}
99-
100-
obj.MatchExpressions = append(obj.MatchExpressions, requirement)
101-
}
102-
103-
return obj
104-
}
105-
106-
// Temporary structs created to store yaml parsing
107-
type tempLabelSelector struct {
108-
MatchLabels map[string]string `yaml:"matchLabels,omitempty"`
109-
MatchExpressions []tempRequirement `json:"matchExpressions,omitempty"`
110-
}
111-
112-
type tempRequirement struct {
113-
Key string `json:"key"`
114-
Operator metav1.LabelSelectorOperator `json:"operator"`
115-
Values []string `json:"values,omitempty"`
116-
}
117-
11888
// Use an alias struct to handle complex types
11989
type alias struct {
12090
Group string `yaml:"group"`
@@ -132,7 +102,7 @@ type alias struct {
132102
MarkUnsafe *bool `yaml:"markUnsafe"`
133103
Blacklist []schema.GroupVersionKind `yaml:"blacklist,omitempty"`
134104
Finalizer *Finalizer `yaml:"finalizer"`
135-
Selector tempLabelSelector `yaml:"selector"`
105+
Selector metav1.LabelSelector `yaml:"selector"`
136106
}
137107

138108
// buildWatch will build Watch based on the values parsed from alias
@@ -201,7 +171,7 @@ func (w *Watch) setValuesFromAlias(tmp alias) error {
201171
return err
202172
}
203173
w.addRolePlaybookPaths(wd)
204-
w.Selector = parseLabelSelector(tmp.Selector)
174+
w.Selector = tmp.Selector
205175

206176
return nil
207177
}

0 commit comments

Comments
 (0)