From c5539fd153ae5c80e8d8166725ad49b25820664d Mon Sep 17 00:00:00 2001 From: xinjie xu <1912747539@qq.com> Date: Tue, 23 Nov 2021 20:45:18 +0800 Subject: [PATCH] optimization the process of getting PropagationPolicy with higher priority Signed-off-by: xinjie xu <1912747539@qq.com> --- pkg/detector/compare.go | 27 ++++++ pkg/detector/compare_test.go | 171 +++++++++++++++++++++++++++++++++++ pkg/detector/detector.go | 29 ++---- 3 files changed, 208 insertions(+), 19 deletions(-) create mode 100644 pkg/detector/compare.go create mode 100644 pkg/detector/compare_test.go diff --git a/pkg/detector/compare.go b/pkg/detector/compare.go new file mode 100644 index 000000000..52911463d --- /dev/null +++ b/pkg/detector/compare.go @@ -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 +} diff --git a/pkg/detector/compare_test.go b/pkg/detector/compare_test.go new file mode 100644 index 000000000..9bd2f2604 --- /dev/null +++ b/pkg/detector/compare_test.go @@ -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) + } + }) + } +} diff --git a/pkg/detector/detector.go b/pkg/detector/detector.go index e640658d9..ab6dd8089 100644 --- a/pkg/detector/detector.go +++ b/pkg/detector/detector.go @@ -3,7 +3,6 @@ package detector import ( "context" "fmt" - "sort" "strings" "sync" "time" @@ -357,23 +356,19 @@ func (d *ResourceDetector) LookForMatchedPolicy(object *unstructured.Unstructure policyList = append(policyList, policy) } - matchedPolicies := make([]*policyv1alpha1.PropagationPolicy, 0) + var matchedPolicy *policyv1alpha1.PropagationPolicy for _, policy := range policyList { if util.ResourceMatchSelectors(object, policy.Spec.ResourceSelectors...) { - matchedPolicies = append(matchedPolicies, policy) + matchedPolicy = GetHigherPriorityPropagationPolicy(matchedPolicy, policy) } } - sort.Slice(matchedPolicies, func(i, j int) bool { - return matchedPolicies[i].Name < matchedPolicies[j].Name - }) - - if len(matchedPolicies) == 0 { + if matchedPolicy == nil { klog.V(2).Infof("no propagationpolicy match for resource(%s)", objectKey) return nil, nil } - klog.V(2).Infof("Matched policy(%s/%s) for resource(%s)", matchedPolicies[0].Namespace, matchedPolicies[0].Name, objectKey) - return matchedPolicies[0], nil + klog.V(2).Infof("Matched policy(%s/%s) for resource(%s)", matchedPolicy.Namespace, matchedPolicy.Name, objectKey) + return matchedPolicy, nil } // 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) } - matchedClusterPolicies := make([]*policyv1alpha1.ClusterPropagationPolicy, 0) + var matchedClusterPolicy *policyv1alpha1.ClusterPropagationPolicy for _, policy := range policyList { if util.ResourceMatchSelectors(object, policy.Spec.ResourceSelectors...) { - matchedClusterPolicies = append(matchedClusterPolicies, policy) + matchedClusterPolicy = GetHigherPriorityClusterPropagationPolicy(matchedClusterPolicy, policy) } } - sort.Slice(matchedClusterPolicies, func(i, j int) bool { - return matchedClusterPolicies[i].Name < matchedClusterPolicies[j].Name - }) - - if len(matchedClusterPolicies) == 0 { + if matchedClusterPolicy == nil { klog.V(2).Infof("no propagationpolicy match for resource(%s)", objectKey) return nil, nil } - klog.V(2).Infof("Matched cluster policy(%s) for resource(%s)", matchedClusterPolicies[0].Name, objectKey) - return matchedClusterPolicies[0], nil + klog.V(2).Infof("Matched cluster policy(%s) for resource(%s)", matchedClusterPolicy.Name, objectKey) + return matchedClusterPolicy, nil } // ApplyPolicy starts propagate the object referenced by object key according to PropagationPolicy.