Merge pull request #2267 from Garrybest/pr_pp
add implicit priority for PP
This commit is contained in:
commit
448e8bc80a
|
@ -373,9 +373,16 @@ func (d *ResourceDetector) LookForMatchedPolicy(object *unstructured.Unstructure
|
||||||
policyList = append(policyList, policy)
|
policyList = append(policyList, policy)
|
||||||
}
|
}
|
||||||
|
|
||||||
var matchedPolicy *policyv1alpha1.PropagationPolicy
|
var (
|
||||||
|
matchedPolicy *policyv1alpha1.PropagationPolicy
|
||||||
|
matchedPolicyPriority = util.PriorityMisMatch
|
||||||
|
)
|
||||||
|
|
||||||
for _, policy := range policyList {
|
for _, policy := range policyList {
|
||||||
if util.ResourceMatchSelectors(object, policy.Spec.ResourceSelectors...) {
|
if p := util.ResourceMatchSelectorsPriority(object, policy.Spec.ResourceSelectors...); p > matchedPolicyPriority {
|
||||||
|
matchedPolicy = policy
|
||||||
|
matchedPolicyPriority = p
|
||||||
|
} else if p > util.PriorityMisMatch && p == matchedPolicyPriority {
|
||||||
matchedPolicy = GetHigherPriorityPropagationPolicy(matchedPolicy, policy)
|
matchedPolicy = GetHigherPriorityPropagationPolicy(matchedPolicy, policy)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -411,9 +418,16 @@ func (d *ResourceDetector) LookForMatchedClusterPolicy(object *unstructured.Unst
|
||||||
policyList = append(policyList, policy)
|
policyList = append(policyList, policy)
|
||||||
}
|
}
|
||||||
|
|
||||||
var matchedClusterPolicy *policyv1alpha1.ClusterPropagationPolicy
|
var (
|
||||||
|
matchedClusterPolicy *policyv1alpha1.ClusterPropagationPolicy
|
||||||
|
matchedClusterPolicyPriority = util.PriorityMisMatch
|
||||||
|
)
|
||||||
|
|
||||||
for _, policy := range policyList {
|
for _, policy := range policyList {
|
||||||
if util.ResourceMatchSelectors(object, policy.Spec.ResourceSelectors...) {
|
if p := util.ResourceMatchSelectorsPriority(object, policy.Spec.ResourceSelectors...); p > matchedClusterPolicyPriority {
|
||||||
|
matchedClusterPolicy = policy
|
||||||
|
matchedClusterPolicyPriority = p
|
||||||
|
} else if p > util.PriorityMisMatch && p == matchedClusterPolicyPriority {
|
||||||
matchedClusterPolicy = GetHigherPriorityClusterPropagationPolicy(matchedClusterPolicy, policy)
|
matchedClusterPolicy = GetHigherPriorityClusterPropagationPolicy(matchedClusterPolicy, policy)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,12 +10,33 @@ import (
|
||||||
"github.com/karmada-io/karmada/pkg/util/lifted"
|
"github.com/karmada-io/karmada/pkg/util/lifted"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ImplicitPriority describes the extent to which a ResourceSelector or a set of
|
||||||
|
// ResourceSelectors match resources.
|
||||||
|
type ImplicitPriority int
|
||||||
|
|
||||||
|
const (
|
||||||
|
// PriorityMisMatch means the ResourceSelector does not match the resource.
|
||||||
|
PriorityMisMatch ImplicitPriority = iota
|
||||||
|
// PriorityMatchAll means the ResourceSelector whose Name and LabelSelector is empty
|
||||||
|
// matches the resource.
|
||||||
|
PriorityMatchAll
|
||||||
|
// PriorityMatchLabelSelector means the LabelSelector of ResourceSelector matches the resource.
|
||||||
|
PriorityMatchLabelSelector
|
||||||
|
// PriorityMatchName means the Name of ResourceSelector matches the resource.
|
||||||
|
PriorityMatchName
|
||||||
|
)
|
||||||
|
|
||||||
// ResourceMatches tells if the specific resource matches the selector.
|
// ResourceMatches tells if the specific resource matches the selector.
|
||||||
func ResourceMatches(resource *unstructured.Unstructured, rs policyv1alpha1.ResourceSelector) bool {
|
func ResourceMatches(resource *unstructured.Unstructured, rs policyv1alpha1.ResourceSelector) bool {
|
||||||
|
return ResourceSelectorPriority(resource, rs) > PriorityMisMatch
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResourceSelectorPriority tells the priority between the specific resource and the selector.
|
||||||
|
func ResourceSelectorPriority(resource *unstructured.Unstructured, rs policyv1alpha1.ResourceSelector) ImplicitPriority {
|
||||||
if resource.GetAPIVersion() != rs.APIVersion ||
|
if resource.GetAPIVersion() != rs.APIVersion ||
|
||||||
resource.GetKind() != rs.Kind ||
|
resource.GetKind() != rs.Kind ||
|
||||||
(len(rs.Namespace) > 0 && resource.GetNamespace() != rs.Namespace) {
|
(len(rs.Namespace) > 0 && resource.GetNamespace() != rs.Namespace) {
|
||||||
return false
|
return PriorityMisMatch
|
||||||
}
|
}
|
||||||
|
|
||||||
// match rules:
|
// match rules:
|
||||||
|
@ -27,12 +48,15 @@ func ResourceMatches(resource *unstructured.Unstructured, rs policyv1alpha1.Reso
|
||||||
|
|
||||||
// case 1, 2: name not empty, don't need to consult selector.
|
// case 1, 2: name not empty, don't need to consult selector.
|
||||||
if len(rs.Name) > 0 {
|
if len(rs.Name) > 0 {
|
||||||
return rs.Name == resource.GetName()
|
if rs.Name == resource.GetName() {
|
||||||
|
return PriorityMatchName
|
||||||
|
}
|
||||||
|
return PriorityMisMatch
|
||||||
}
|
}
|
||||||
|
|
||||||
// case 4: short path, both name and selector empty, matches all
|
// case 4: short path, both name and selector empty, matches all
|
||||||
if rs.LabelSelector == nil {
|
if rs.LabelSelector == nil {
|
||||||
return true
|
return PriorityMatchAll
|
||||||
}
|
}
|
||||||
|
|
||||||
// case 3: matches with selector
|
// case 3: matches with selector
|
||||||
|
@ -40,10 +64,13 @@ func ResourceMatches(resource *unstructured.Unstructured, rs policyv1alpha1.Reso
|
||||||
var err error
|
var err error
|
||||||
if s, err = metav1.LabelSelectorAsSelector(rs.LabelSelector); err != nil {
|
if s, err = metav1.LabelSelectorAsSelector(rs.LabelSelector); err != nil {
|
||||||
// should not happen because all resource selector should be fully validated by webhook.
|
// should not happen because all resource selector should be fully validated by webhook.
|
||||||
return false
|
return PriorityMisMatch
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.Matches(labels.Set(resource.GetLabels()))
|
if s.Matches(labels.Set(resource.GetLabels())) {
|
||||||
|
return PriorityMatchLabelSelector
|
||||||
|
}
|
||||||
|
return PriorityMisMatch
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClusterMatches tells if specific cluster matches the affinity.
|
// ClusterMatches tells if specific cluster matches the affinity.
|
||||||
|
@ -116,6 +143,17 @@ func ResourceMatchSelectors(resource *unstructured.Unstructured, selectors ...po
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ResourceMatchSelectorsPriority returns the highest priority between specific resource and the selectors.
|
||||||
|
func ResourceMatchSelectorsPriority(resource *unstructured.Unstructured, selectors ...policyv1alpha1.ResourceSelector) ImplicitPriority {
|
||||||
|
var priority ImplicitPriority
|
||||||
|
for _, rs := range selectors {
|
||||||
|
if p := ResourceSelectorPriority(resource, rs); p > priority {
|
||||||
|
priority = p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return priority
|
||||||
|
}
|
||||||
|
|
||||||
func extractClusterFields(cluster *clusterv1alpha1.Cluster) labels.Set {
|
func extractClusterFields(cluster *clusterv1alpha1.Cluster) labels.Set {
|
||||||
clusterFieldsMap := make(labels.Set)
|
clusterFieldsMap := make(labels.Set)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue