Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support namespaceselectors for cluster propagation policies #6016

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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
Loading