optimization the process of getting PropagationPolicy with higher priority
Signed-off-by: xinjie xu <1912747539@qq.com>
This commit is contained in:
parent
3b52b0df91
commit
c5539fd153
|
@ -0,0 +1,27 @@
|
||||||
|
package detector
|
||||||
|
|
||||||
|
import policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
|
||||||
|
|
||||||
|
// GetHigherPriorityPropagationPolicy compare two PropagationPolicies with some priority comparison logic
|
||||||
|
func GetHigherPriorityPropagationPolicy(a, b *policyv1alpha1.PropagationPolicy) *policyv1alpha1.PropagationPolicy {
|
||||||
|
if a == nil {
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
// logic of priority comparison
|
||||||
|
if a.Name < b.Name {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetHigherPriorityClusterPropagationPolicy compare two ClusterPropagationPolicies with some priority comparison logic
|
||||||
|
func GetHigherPriorityClusterPropagationPolicy(a, b *policyv1alpha1.ClusterPropagationPolicy) *policyv1alpha1.ClusterPropagationPolicy {
|
||||||
|
if a == nil {
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
// logic of priority comparison
|
||||||
|
if a.Name < b.Name {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
|
@ -0,0 +1,171 @@
|
||||||
|
package detector
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
|
||||||
|
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_GetHigherPriorityPropagationPolicy(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
a *policyv1alpha1.PropagationPolicy
|
||||||
|
b *policyv1alpha1.PropagationPolicy
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want *policyv1alpha1.PropagationPolicy
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Test 1",
|
||||||
|
args: args{
|
||||||
|
a: &policyv1alpha1.PropagationPolicy{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "A",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
b: &policyv1alpha1.PropagationPolicy{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "B",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: &policyv1alpha1.PropagationPolicy{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "A",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Test 2",
|
||||||
|
args: args{
|
||||||
|
a: &policyv1alpha1.PropagationPolicy{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "abc",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
b: &policyv1alpha1.PropagationPolicy{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "ab",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: &policyv1alpha1.PropagationPolicy{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "ab",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Test 3",
|
||||||
|
args: args{
|
||||||
|
a: &policyv1alpha1.PropagationPolicy{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
b: &policyv1alpha1.PropagationPolicy{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "ab",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: &policyv1alpha1.PropagationPolicy{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result := GetHigherPriorityPropagationPolicy(tt.args.a, tt.args.b)
|
||||||
|
if result.Name != tt.want.Name {
|
||||||
|
t.Errorf("divideRemainingReplicas() got = %v, want %v", result.Name, tt.want.Name)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_GetHigherPriorityClusterPropagationPolicy(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
a *policyv1alpha1.ClusterPropagationPolicy
|
||||||
|
b *policyv1alpha1.ClusterPropagationPolicy
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want *policyv1alpha1.ClusterPropagationPolicy
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Test 1",
|
||||||
|
args: args{
|
||||||
|
a: &policyv1alpha1.ClusterPropagationPolicy{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "A",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
b: &policyv1alpha1.ClusterPropagationPolicy{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "B",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: &policyv1alpha1.ClusterPropagationPolicy{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "A",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Test 2",
|
||||||
|
args: args{
|
||||||
|
a: &policyv1alpha1.ClusterPropagationPolicy{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "abc",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
b: &policyv1alpha1.ClusterPropagationPolicy{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "ab",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: &policyv1alpha1.ClusterPropagationPolicy{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "ab",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Test 3",
|
||||||
|
args: args{
|
||||||
|
a: &policyv1alpha1.ClusterPropagationPolicy{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
b: &policyv1alpha1.ClusterPropagationPolicy{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "ab",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: &policyv1alpha1.ClusterPropagationPolicy{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result := GetHigherPriorityClusterPropagationPolicy(tt.args.a, tt.args.b)
|
||||||
|
if result.Name != tt.want.Name {
|
||||||
|
t.Errorf("divideRemainingReplicas() got = %v, want %v", result.Name, tt.want.Name)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,7 +3,6 @@ package detector
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
@ -357,23 +356,19 @@ func (d *ResourceDetector) LookForMatchedPolicy(object *unstructured.Unstructure
|
||||||
policyList = append(policyList, policy)
|
policyList = append(policyList, policy)
|
||||||
}
|
}
|
||||||
|
|
||||||
matchedPolicies := make([]*policyv1alpha1.PropagationPolicy, 0)
|
var matchedPolicy *policyv1alpha1.PropagationPolicy
|
||||||
for _, policy := range policyList {
|
for _, policy := range policyList {
|
||||||
if util.ResourceMatchSelectors(object, policy.Spec.ResourceSelectors...) {
|
if util.ResourceMatchSelectors(object, policy.Spec.ResourceSelectors...) {
|
||||||
matchedPolicies = append(matchedPolicies, policy)
|
matchedPolicy = GetHigherPriorityPropagationPolicy(matchedPolicy, policy)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Slice(matchedPolicies, func(i, j int) bool {
|
if matchedPolicy == nil {
|
||||||
return matchedPolicies[i].Name < matchedPolicies[j].Name
|
|
||||||
})
|
|
||||||
|
|
||||||
if len(matchedPolicies) == 0 {
|
|
||||||
klog.V(2).Infof("no propagationpolicy match for resource(%s)", objectKey)
|
klog.V(2).Infof("no propagationpolicy match for resource(%s)", objectKey)
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
klog.V(2).Infof("Matched policy(%s/%s) for resource(%s)", matchedPolicies[0].Namespace, matchedPolicies[0].Name, objectKey)
|
klog.V(2).Infof("Matched policy(%s/%s) for resource(%s)", matchedPolicy.Namespace, matchedPolicy.Name, objectKey)
|
||||||
return matchedPolicies[0], nil
|
return matchedPolicy, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// LookForMatchedClusterPolicy tries to find a ClusterPropagationPolicy for object referenced by object key.
|
// LookForMatchedClusterPolicy tries to find a ClusterPropagationPolicy for object referenced by object key.
|
||||||
|
@ -399,23 +394,19 @@ func (d *ResourceDetector) LookForMatchedClusterPolicy(object *unstructured.Unst
|
||||||
policyList = append(policyList, policy)
|
policyList = append(policyList, policy)
|
||||||
}
|
}
|
||||||
|
|
||||||
matchedClusterPolicies := make([]*policyv1alpha1.ClusterPropagationPolicy, 0)
|
var matchedClusterPolicy *policyv1alpha1.ClusterPropagationPolicy
|
||||||
for _, policy := range policyList {
|
for _, policy := range policyList {
|
||||||
if util.ResourceMatchSelectors(object, policy.Spec.ResourceSelectors...) {
|
if util.ResourceMatchSelectors(object, policy.Spec.ResourceSelectors...) {
|
||||||
matchedClusterPolicies = append(matchedClusterPolicies, policy)
|
matchedClusterPolicy = GetHigherPriorityClusterPropagationPolicy(matchedClusterPolicy, policy)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Slice(matchedClusterPolicies, func(i, j int) bool {
|
if matchedClusterPolicy == nil {
|
||||||
return matchedClusterPolicies[i].Name < matchedClusterPolicies[j].Name
|
|
||||||
})
|
|
||||||
|
|
||||||
if len(matchedClusterPolicies) == 0 {
|
|
||||||
klog.V(2).Infof("no propagationpolicy match for resource(%s)", objectKey)
|
klog.V(2).Infof("no propagationpolicy match for resource(%s)", objectKey)
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
klog.V(2).Infof("Matched cluster policy(%s) for resource(%s)", matchedClusterPolicies[0].Name, objectKey)
|
klog.V(2).Infof("Matched cluster policy(%s) for resource(%s)", matchedClusterPolicy.Name, objectKey)
|
||||||
return matchedClusterPolicies[0], nil
|
return matchedClusterPolicy, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApplyPolicy starts propagate the object referenced by object key according to PropagationPolicy.
|
// ApplyPolicy starts propagate the object referenced by object key according to PropagationPolicy.
|
||||||
|
|
Loading…
Reference in New Issue