optimization the process of getting PropagationPolicy with higher priority

Signed-off-by: xinjie xu <1912747539@qq.com>
This commit is contained in:
xinjie xu 2021-11-23 20:45:18 +08:00
parent 3b52b0df91
commit c5539fd153
3 changed files with 208 additions and 19 deletions

27
pkg/detector/compare.go Normal file
View File

@ -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
}

View File

@ -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)
}
})
}
}

View File

@ -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.