WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 05ab225

Browse files
committed
Use LabelSelector directly in namespaceSelector
- Replace `matchLabels`/`matchExpressions` with `LabelSelector` for simplicity - Clean up variable declarations throughout Signed-off-by: Dale Haiducek <[email protected]>
1 parent 9d6e388 commit 05ab225

8 files changed

+109
-140
lines changed

api/v1/configurationpolicy_types.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,35 +70,24 @@ type Severity string
7070
type PruneObjectBehavior string
7171

7272
type Target struct {
73+
*metav1.LabelSelector `json:",inline"`
74+
7375
// Include is an array of filepath expressions to include objects by name.
7476
Include []NonEmptyString `json:"include,omitempty"`
7577

7678
// Exclude is an array of filepath expressions to exclude objects by name.
7779
Exclude []NonEmptyString `json:"exclude,omitempty"`
78-
79-
// MatchLabels is a map of {key,value} pairs matching objects by label.
80-
MatchLabels *map[string]string `json:"matchLabels,omitempty"`
81-
82-
// MatchExpressions is an array of label selector requirements matching objects by label.
83-
MatchExpressions *[]metav1.LabelSelectorRequirement `json:"matchExpressions,omitempty"`
8480
}
8581

8682
// Define String() so that the LabelSelector is dereferenced in the logs
8783
func (t Target) String() string {
8884
fmtSelectorStr := "{include:%s,exclude:%s,matchLabels:%+v,matchExpressions:%+v}"
89-
if t.MatchLabels == nil && t.MatchExpressions == nil {
90-
return fmt.Sprintf(fmtSelectorStr, t.Include, t.Exclude, nil, nil)
91-
}
92-
93-
if t.MatchLabels == nil {
94-
return fmt.Sprintf(fmtSelectorStr, t.Include, t.Exclude, nil, *t.MatchExpressions)
95-
}
9685

97-
if t.MatchExpressions == nil {
98-
return fmt.Sprintf(fmtSelectorStr, t.Include, t.Exclude, *t.MatchLabels, nil)
86+
if t.LabelSelector == nil {
87+
return fmt.Sprintf(fmtSelectorStr, t.Include, t.Exclude, nil, nil)
9988
}
10089

101-
return fmt.Sprintf(fmtSelectorStr, t.Include, t.Exclude, *t.MatchLabels, *t.MatchExpressions)
90+
return fmt.Sprintf(fmtSelectorStr, t.Include, t.Exclude, t.MatchLabels, t.MatchExpressions)
10291
}
10392

10493
// EvaluationInterval configures the minimum elapsed time before a configuration policy is
@@ -292,7 +281,8 @@ type ConfigurationPolicySpec struct {
292281
// `spec["object-templates"]`. All selector rules are combined. If 'include' is not provided but
293282
// `matchLabels` and/or `matchExpressions` are, `include` will behave as if `['*']` were given. If
294283
// `matchExpressions` and `matchLabels` are both not provided, `include` must be provided to
295-
// retrieve namespaces.
284+
// retrieve namespaces. If there is a namespace defined in the `objectDefinition`, the
285+
// `namespaceSelector` is ignored.
296286
NamespaceSelector Target `json:"namespaceSelector,omitempty"`
297287

298288
// The `object-templates` is an array of object configurations for the configuration policy to

api/v1/zz_generated.deepcopy.go

Lines changed: 5 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

controllers/configurationpolicy_controller.go

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,11 @@ import (
5555
)
5656

5757
const (
58-
ControllerName string = "configuration-policy-controller"
59-
CRDName string = "configurationpolicies.policy.open-cluster-management.io"
60-
pruneObjectFinalizer string = "policy.open-cluster-management.io/delete-related-objects"
61-
disableTemplatesAnnotation string = "policy.open-cluster-management.io/disable-templates"
62-
)
63-
64-
var log = ctrl.Log.WithName(ControllerName)
65-
66-
// PlcChan a channel used to pass policies ready for update
67-
var PlcChan chan *policyv1.ConfigurationPolicy
68-
69-
var (
70-
eventNormal = "Normal"
71-
eventWarning = "Warning"
72-
eventFmtStr = "policy: %s/%s"
73-
)
58+
ControllerName = "configuration-policy-controller"
59+
CRDName = "configurationpolicies.policy.open-cluster-management.io"
60+
pruneObjectFinalizer = "policy.open-cluster-management.io/delete-related-objects"
61+
disableTemplatesAnnotation = "policy.open-cluster-management.io/disable-templates"
7462

75-
const (
7663
reasonWantFoundExists = "Resource found as expected"
7764
reasonWantFoundCreated = "K8s creation success"
7865
reasonUpdateSuccess = "K8s update success"
@@ -85,11 +72,19 @@ const (
8572
reasonFoundNotApplicable = "Resource found but will not be handled in mustnothave mode"
8673
)
8774

88-
var ErrPolicyInvalid = errors.New("the Policy is invalid")
75+
var (
76+
log = ctrl.Log.WithName(ControllerName)
77+
78+
eventNormal = "Normal"
79+
eventWarning = "Warning"
80+
eventFmtStr = "policy: %s/%s"
8981

90-
// commonSprigFuncMap includes only the sprig functions that are available in the
91-
// stolostron/go-template-utils library.
92-
var commonSprigFuncMap template.FuncMap
82+
ErrPolicyInvalid = errors.New("the Policy is invalid")
83+
84+
// commonSprigFuncMap includes only the sprig functions that are available in the
85+
// stolostron/go-template-utils library.
86+
commonSprigFuncMap template.FuncMap
87+
)
9388

9489
func init() {
9590
commonSprigFuncMap = template.FuncMap{}
@@ -444,8 +439,7 @@ func (r *ConfigurationPolicyReconciler) shouldEvaluatePolicy(
444439
return true, 0
445440
}
446441

447-
usesSelector := policy.Spec.NamespaceSelector.MatchLabels != nil ||
448-
policy.Spec.NamespaceSelector.MatchExpressions != nil ||
442+
usesSelector := policy.Spec.NamespaceSelector.LabelSelector != nil ||
449443
len(policy.Spec.NamespaceSelector.Include) != 0
450444

451445
if usesSelector && r.SelectorReconciler.HasUpdate(policy.Namespace, policy.Name) {
@@ -1269,8 +1263,8 @@ func (r *ConfigurationPolicyReconciler) determineDesiredObject(
12691263
}
12701264
}
12711265

1272-
// strings.TrimSpace() is needed here because a multi-line value will have '\n' in it. This is kept for
1273-
// backwards compatibility.
1266+
// strings.TrimSpace() is needed here because a multi-line value will have
1267+
// '\n' in it. This is kept for backwards compatibility.
12741268
desiredObj.SetName(strings.TrimSpace(desiredObj.GetName()))
12751269
desiredObj.SetNamespace(strings.TrimSpace(desiredObj.GetNamespace()))
12761270
desiredObj.SetKind(strings.TrimSpace(desiredObj.GetKind()))
@@ -1289,11 +1283,16 @@ func (r *ConfigurationPolicyReconciler) determineDesiredObject(
12891283
}
12901284
}
12911285

1292-
if scopedGVR.Namespaced && desiredObj.GetNamespace() == "" {
1293-
selectedNamespaces, err := r.SelectorReconciler.Get(plc.Namespace, plc.Name, plc.Spec.NamespaceSelector)
1286+
// Fetch and filter namespaces using provided namespaceSelector
1287+
desiredNs := desiredObj.GetNamespace()
1288+
1289+
if scopedGVR.Namespaced && desiredNs == "" {
1290+
nsSelector := plc.Spec.NamespaceSelector
1291+
1292+
selectedNamespaces, err := r.SelectorReconciler.Get(plc.Namespace, plc.Name, nsSelector)
12941293
if err != nil {
12951294
log.Error(err, "Failed to select the namespaces",
1296-
"namespaceSelector", fmt.Sprintf("%+v", plc.Spec.NamespaceSelector))
1295+
"namespaceSelector", nsSelector.String())
12971296

12981297
msg := fmt.Sprintf("Error filtering namespaces with provided namespaceSelector: %v", err)
12991298

@@ -1308,12 +1307,12 @@ func (r *ConfigurationPolicyReconciler) determineDesiredObject(
13081307
}
13091308

13101309
if len(selectedNamespaces) == 0 {
1311-
relevantNamespaces = []string{desiredObj.GetNamespace()}
1310+
relevantNamespaces = []string{desiredNs}
13121311
} else {
13131312
relevantNamespaces = selectedNamespaces
13141313
}
13151314
} else {
1316-
relevantNamespaces = []string{desiredObj.GetNamespace()}
1315+
relevantNamespaces = []string{desiredNs}
13171316
}
13181317

13191318
return desiredObj, scopedGVR, relevantNamespaces, errEvent, mappingErr

deploy/crds/kustomize_configurationpolicy/policy.open-cluster-management.io_configurationpolicies.yaml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ spec:
9999
`spec["object-templates"]`. All selector rules are combined. If 'include' is not provided but
100100
`matchLabels` and/or `matchExpressions` are, `include` will behave as if `['*']` were given. If
101101
`matchExpressions` and `matchLabels` are both not provided, `include` must be provided to
102-
retrieve namespaces.
102+
retrieve namespaces. If there is a namespace defined in the `objectDefinition`, the
103+
`namespaceSelector` is ignored.
103104
properties:
104105
exclude:
105106
description: Exclude is an array of filepath expressions to exclude
@@ -116,8 +117,8 @@ spec:
116117
type: string
117118
type: array
118119
matchExpressions:
119-
description: MatchExpressions is an array of label selector requirements
120-
matching objects by label.
120+
description: matchExpressions is a list of label selector requirements.
121+
The requirements are ANDed.
121122
items:
122123
description: |-
123124
A label selector requirement is a selector that contains values, a key, and an operator that
@@ -147,13 +148,17 @@ spec:
147148
- operator
148149
type: object
149150
type: array
151+
x-kubernetes-list-type: atomic
150152
matchLabels:
151153
additionalProperties:
152154
type: string
153-
description: MatchLabels is a map of {key,value} pairs matching
154-
objects by label.
155+
description: |-
156+
matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels
157+
map is equivalent to an element of matchExpressions, whose key field is "key", the
158+
operator is "In", and the values array contains only "value". The requirements are ANDed.
155159
type: object
156160
type: object
161+
x-kubernetes-map-type: atomic
157162
object-templates:
158163
description: |-
159164
The `object-templates` is an array of object configurations for the configuration policy to

deploy/crds/policy.open-cluster-management.io_configurationpolicies.yaml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ spec:
106106
`spec["object-templates"]`. All selector rules are combined. If 'include' is not provided but
107107
`matchLabels` and/or `matchExpressions` are, `include` will behave as if `['*']` were given. If
108108
`matchExpressions` and `matchLabels` are both not provided, `include` must be provided to
109-
retrieve namespaces.
109+
retrieve namespaces. If there is a namespace defined in the `objectDefinition`, the
110+
`namespaceSelector` is ignored.
110111
properties:
111112
exclude:
112113
description: Exclude is an array of filepath expressions to exclude
@@ -123,8 +124,8 @@ spec:
123124
type: string
124125
type: array
125126
matchExpressions:
126-
description: MatchExpressions is an array of label selector requirements
127-
matching objects by label.
127+
description: matchExpressions is a list of label selector requirements.
128+
The requirements are ANDed.
128129
items:
129130
description: |-
130131
A label selector requirement is a selector that contains values, a key, and an operator that
@@ -154,13 +155,17 @@ spec:
154155
- operator
155156
type: object
156157
type: array
158+
x-kubernetes-list-type: atomic
157159
matchLabels:
158160
additionalProperties:
159161
type: string
160-
description: MatchLabels is a map of {key,value} pairs matching
161-
objects by label.
162+
description: |-
163+
matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels
164+
map is equivalent to an element of matchExpressions, whose key field is "key", the
165+
operator is "In", and the values array contains only "value". The requirements are ANDed.
162166
type: object
163167
type: object
168+
x-kubernetes-map-type: atomic
164169
object-templates:
165170
description: |-
166171
The `object-templates` is an array of object configurations for the configuration policy to

main.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,6 @@ var (
7070
// Policies applied by users are deployed here. Used only in non-hosted mode.
7171
const ocmPolicyNs = "open-cluster-management-policies"
7272

73-
func printVersion() {
74-
log.Info("Using", "OperatorVersion", version.Version, "GoVersion", runtime.Version(),
75-
"GOOS", runtime.GOOS, "GOARCH", runtime.GOARCH)
76-
}
77-
7873
func init() {
7974
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
8075
//+kubebuilder:scaffold:scheme
@@ -163,7 +158,8 @@ func main() {
163158
panic("The --evaluation-concurrency option cannot be less than 1")
164159
}
165160

166-
printVersion()
161+
log.Info("Using", "OperatorVersion", version.Version, "GoVersion", runtime.Version(),
162+
"GOOS", runtime.GOOS, "GOARCH", runtime.GOARCH)
167163

168164
// Get a config to talk to the apiserver
169165
cfg, err := config.GetConfig()

0 commit comments

Comments
 (0)