Skip to content

Commit

Permalink
support namespaceselectors for cluster propagation policies
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Jan 4, 2025
1 parent 72b6bd7 commit 11368d5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
11 changes: 11 additions & 0 deletions pkg/apis/policy/v1alpha1/propagation_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ type PropagationSpec struct {
// +kubebuilder:validation:MinItems=1
ResourceSelectors []ResourceSelector `json:"resourceSelectors"`

// NamespaceSelectors used to select resources.
// +optional
NamespaceSelectors []NamespaceSelector `json:"namespaceSelectors"`

// Association tells if relevant resources should be selected automatically.
// e.g. a ConfigMap referred by a Deployment.
// default false.
Expand Down Expand Up @@ -228,6 +232,13 @@ type ResourceSelector struct {
LabelSelector *metav1.LabelSelector `json:"labelSelector,omitempty"`
}

// NamespaceSelector the resource namespace will be selected.
type NamespaceSelector struct {
// A label query over a set of namespaces.
// +required
LabelSelector *metav1.LabelSelector `json:"labelSelector,omitempty"`
}

// FieldSelector is a field filter.
type FieldSelector struct {
// A list of field selector requirements.
Expand Down
16 changes: 15 additions & 1 deletion pkg/detector/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,26 @@ func getHighestPriorityPropagationPolicy(policies []*policyv1alpha1.PropagationP
var matchedPolicy *policyv1alpha1.PropagationPolicy

for _, policy := range policies {
// any namespace selector matches ?
if len(policy.Spec.NamespaceSelectors) != 0 {
matched := false
for _, ns := range policy.Spec.NamespaceSelectors {
if !util.MatchesSelector(GetNamespace(resource.GetNamespace()), ns.LabelSelector) {

Check failure on line 40 in pkg/detector/compare.go

View workflow job for this annotation

GitHub Actions / Test on Kubernetes (v1.29.0)

undefined: GetNamespace

Check failure on line 40 in pkg/detector/compare.go

View workflow job for this annotation

GitHub Actions / Test on Kubernetes (v1.29.0)

undefined: GetNamespace

Check failure on line 40 in pkg/detector/compare.go

View workflow job for this annotation

GitHub Actions / Test on Kubernetes (v1.29.0)

undefined: GetNamespace

Check failure on line 40 in pkg/detector/compare.go

View workflow job for this annotation

GitHub Actions / Test on Kubernetes (v1.30.0)

undefined: GetNamespace

Check failure on line 40 in pkg/detector/compare.go

View workflow job for this annotation

GitHub Actions / lint

undefined: GetNamespace) (typecheck)

Check failure on line 40 in pkg/detector/compare.go

View workflow job for this annotation

GitHub Actions / lint

undefined: GetNamespace) (typecheck)

Check failure on line 40 in pkg/detector/compare.go

View workflow job for this annotation

GitHub Actions / lint

undefined: GetNamespace (typecheck)

Check failure on line 40 in pkg/detector/compare.go

View workflow job for this annotation

GitHub Actions / Test on Kubernetes (v1.30.0)

undefined: GetNamespace

Check failure on line 40 in pkg/detector/compare.go

View workflow job for this annotation

GitHub Actions / init with config file (v1.29.0)

undefined: GetNamespace

Check failure on line 40 in pkg/detector/compare.go

View workflow job for this annotation

GitHub Actions / Test on Kubernetes (v1.31.0)

undefined: GetNamespace

Check failure on line 40 in pkg/detector/compare.go

View workflow job for this annotation

GitHub Actions / Test on Kubernetes (v1.30.0)

undefined: GetNamespace

Check failure on line 40 in pkg/detector/compare.go

View workflow job for this annotation

GitHub Actions / Test on Kubernetes (v1.31.0)

undefined: GetNamespace

Check failure on line 40 in pkg/detector/compare.go

View workflow job for this annotation

GitHub Actions / init with config file (v1.30.0)

undefined: GetNamespace

Check failure on line 40 in pkg/detector/compare.go

View workflow job for this annotation

GitHub Actions / Test on Kubernetes (v1.31.0)

undefined: GetNamespace

Check failure on line 40 in pkg/detector/compare.go

View workflow job for this annotation

GitHub Actions / init with config file (v1.31.0)

undefined: GetNamespace
matched = true
}
}
if !matched {
continue
}
}

// any resource selector matches ?
implicitPriority := util.ResourceMatchSelectorsPriority(resource, policy.Spec.ResourceSelectors...)
if implicitPriority <= util.PriorityMisMatch {
continue
}
explicitPriority := policy.ExplicitPriority()

explicitPriority := policy.ExplicitPriority()
if matchedPolicyExplicitPriority < explicitPriority {
matchedPolicyImplicitPriority = implicitPriority
matchedPolicyExplicitPriority = explicitPriority
Expand Down
19 changes: 12 additions & 7 deletions pkg/util/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,22 @@ func ResourceSelectorPriority(resource *unstructured.Unstructured, rs policyv1al
}

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

func MatchesSelector(resource *unstructured.Unstructured, ls *metav1.LabelSelector) bool {
s, err := metav1.LabelSelectorAsSelector(ls)
if err != nil {
// should not happen because all resource selector should be fully validated by webhook.
return false
}
if s.Matches(labels.Set(resource.GetLabels())) {
return PriorityMatchLabelSelector
return true
}
return PriorityMisMatch
return false
}

// ClusterMatches tells if specific cluster matches the affinity.
Expand Down

0 comments on commit 11368d5

Please sign in to comment.