Merge pull request #2267 from Garrybest/pr_pp

add implicit priority for PP
This commit is contained in:
karmada-bot 2022-09-30 15:44:59 +08:00 committed by GitHub
commit 448e8bc80a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 9 deletions

View File

@ -373,9 +373,16 @@ func (d *ResourceDetector) LookForMatchedPolicy(object *unstructured.Unstructure
policyList = append(policyList, policy)
}
var matchedPolicy *policyv1alpha1.PropagationPolicy
var (
matchedPolicy *policyv1alpha1.PropagationPolicy
matchedPolicyPriority = util.PriorityMisMatch
)
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)
}
}
@ -411,9 +418,16 @@ func (d *ResourceDetector) LookForMatchedClusterPolicy(object *unstructured.Unst
policyList = append(policyList, policy)
}
var matchedClusterPolicy *policyv1alpha1.ClusterPropagationPolicy
var (
matchedClusterPolicy *policyv1alpha1.ClusterPropagationPolicy
matchedClusterPolicyPriority = util.PriorityMisMatch
)
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)
}
}

View File

@ -10,12 +10,33 @@ import (
"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.
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 ||
resource.GetKind() != rs.Kind ||
(len(rs.Namespace) > 0 && resource.GetNamespace() != rs.Namespace) {
return false
return PriorityMisMatch
}
// 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.
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
if rs.LabelSelector == nil {
return true
return PriorityMatchAll
}
// case 3: matches with selector
@ -40,10 +64,13 @@ func ResourceMatches(resource *unstructured.Unstructured, rs policyv1alpha1.Reso
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 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.
@ -116,6 +143,17 @@ func ResourceMatchSelectors(resource *unstructured.Unstructured, selectors ...po
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 {
clusterFieldsMap := make(labels.Set)