Skip to content

Commit 11368d5

Browse files
committed
support namespaceselectors for cluster propagation policies
1 parent 72b6bd7 commit 11368d5

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

pkg/apis/policy/v1alpha1/propagation_types.go

+11
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ type PropagationSpec struct {
6868
// +kubebuilder:validation:MinItems=1
6969
ResourceSelectors []ResourceSelector `json:"resourceSelectors"`
7070

71+
// NamespaceSelectors used to select resources.
72+
// +optional
73+
NamespaceSelectors []NamespaceSelector `json:"namespaceSelectors"`
74+
7175
// Association tells if relevant resources should be selected automatically.
7276
// e.g. a ConfigMap referred by a Deployment.
7377
// default false.
@@ -228,6 +232,13 @@ type ResourceSelector struct {
228232
LabelSelector *metav1.LabelSelector `json:"labelSelector,omitempty"`
229233
}
230234

235+
// NamespaceSelector the resource namespace will be selected.
236+
type NamespaceSelector struct {
237+
// A label query over a set of namespaces.
238+
// +required
239+
LabelSelector *metav1.LabelSelector `json:"labelSelector,omitempty"`
240+
}
241+
231242
// FieldSelector is a field filter.
232243
type FieldSelector struct {
233244
// A list of field selector requirements.

pkg/detector/compare.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,26 @@ func getHighestPriorityPropagationPolicy(policies []*policyv1alpha1.PropagationP
3333
var matchedPolicy *policyv1alpha1.PropagationPolicy
3434

3535
for _, policy := range policies {
36+
// any namespace selector matches ?
37+
if len(policy.Spec.NamespaceSelectors) != 0 {
38+
matched := false
39+
for _, ns := range policy.Spec.NamespaceSelectors {
40+
if !util.MatchesSelector(GetNamespace(resource.GetNamespace()), ns.LabelSelector) {
41+
matched = true
42+
}
43+
}
44+
if !matched {
45+
continue
46+
}
47+
}
48+
49+
// any resource selector matches ?
3650
implicitPriority := util.ResourceMatchSelectorsPriority(resource, policy.Spec.ResourceSelectors...)
3751
if implicitPriority <= util.PriorityMisMatch {
3852
continue
3953
}
40-
explicitPriority := policy.ExplicitPriority()
4154

55+
explicitPriority := policy.ExplicitPriority()
4256
if matchedPolicyExplicitPriority < explicitPriority {
4357
matchedPolicyImplicitPriority = implicitPriority
4458
matchedPolicyExplicitPriority = explicitPriority

pkg/util/selector.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,22 @@ func ResourceSelectorPriority(resource *unstructured.Unstructured, rs policyv1al
7979
}
8080

8181
// case 3: matches with selector
82-
var s labels.Selector
83-
var err error
84-
if s, err = metav1.LabelSelectorAsSelector(rs.LabelSelector); err != nil {
85-
// should not happen because all resource selector should be fully validated by webhook.
86-
return PriorityMisMatch
82+
if MatchesSelector(resource, rs.LabelSelector) {
83+
return PriorityMatchLabelSelector
8784
}
85+
return PriorityMisMatch
86+
}
8887

88+
func MatchesSelector(resource *unstructured.Unstructured, ls *metav1.LabelSelector) bool {
89+
s, err := metav1.LabelSelectorAsSelector(ls)
90+
if err != nil {
91+
// should not happen because all resource selector should be fully validated by webhook.
92+
return false
93+
}
8994
if s.Matches(labels.Set(resource.GetLabels())) {
90-
return PriorityMatchLabelSelector
95+
return true
9196
}
92-
return PriorityMisMatch
97+
return false
9398
}
9499

95100
// ClusterMatches tells if specific cluster matches the affinity.

0 commit comments

Comments
 (0)