Skip to content

Commit

Permalink
feat(detector): resource detector matched policy potimization
Browse files Browse the repository at this point in the history
Signed-off-by: chang.qiangqiang <chang.qiangqiang@immomo.com>
  • Loading branch information
CharlesQQ committed Nov 12, 2024
1 parent b5c6660 commit a03b852
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 268 deletions.
33 changes: 22 additions & 11 deletions pkg/detector/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,43 @@ import (

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"

policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
"github.com/karmada-io/karmada/pkg/util"
"github.com/karmada-io/karmada/pkg/util/fedinformer/keys"
)

func getHighestPriorityPropagationPolicy(policies []*policyv1alpha1.PropagationPolicy, resource *unstructured.Unstructured, objectKey keys.ClusterWideKey) *policyv1alpha1.PropagationPolicy {
func getHighestPriorityPropagationPolicy(policies []policyv1alpha1.PropagationPolicy, resource *unstructured.Unstructured, objectKey keys.ClusterWideKey) *policyv1alpha1.PropagationPolicy {
matchedPolicyImplicitPriority := util.PriorityMisMatch
matchedPolicyExplicitPriority := int32(math.MinInt32)
var matchedPolicy *policyv1alpha1.PropagationPolicy

for _, policy := range policies {
implicitPriority := util.ResourceMatchSelectorsPriority(resource, policy.Spec.ResourceSelectors...)
policyPointer := ptr.To(policy)
if !policyPointer.DeletionTimestamp.IsZero() {
klog.V(4).Infof("Propagation policy(%s/%s) cannot match any resource template because it's being deleted.", policyPointer.Namespace, policyPointer.Name)
continue
}
implicitPriority := util.ResourceMatchSelectorsPriority(resource, policyPointer.Spec.ResourceSelectors...)
if implicitPriority <= util.PriorityMisMatch {
continue
}
explicitPriority := policy.ExplicitPriority()
explicitPriority := policyPointer.ExplicitPriority()

if matchedPolicyExplicitPriority < explicitPriority {
matchedPolicyImplicitPriority = implicitPriority
matchedPolicyExplicitPriority = explicitPriority
matchedPolicy = policy
matchedPolicy = policyPointer
continue
}

if matchedPolicyExplicitPriority == explicitPriority {
if implicitPriority > matchedPolicyImplicitPriority {
matchedPolicyImplicitPriority = implicitPriority
matchedPolicy = policy
matchedPolicy = policyPointer
} else if implicitPriority == matchedPolicyImplicitPriority {
matchedPolicy = getHigherPriorityPropagationPolicy(matchedPolicy, policy)
matchedPolicy = getHigherPriorityPropagationPolicy(matchedPolicy, policyPointer)
}
}
}
Expand All @@ -64,31 +70,36 @@ func getHighestPriorityPropagationPolicy(policies []*policyv1alpha1.PropagationP
return matchedPolicy
}

func getHighestPriorityClusterPropagationPolicy(policies []*policyv1alpha1.ClusterPropagationPolicy, resource *unstructured.Unstructured, objectKey keys.ClusterWideKey) *policyv1alpha1.ClusterPropagationPolicy {
func getHighestPriorityClusterPropagationPolicy(policies []policyv1alpha1.ClusterPropagationPolicy, resource *unstructured.Unstructured, objectKey keys.ClusterWideKey) *policyv1alpha1.ClusterPropagationPolicy {
matchedClusterPolicyImplicitPriority := util.PriorityMisMatch
matchedClusterPolicyExplicitPriority := int32(math.MinInt32)
var matchedClusterPolicy *policyv1alpha1.ClusterPropagationPolicy

for _, policy := range policies {
policyPointer := ptr.To(policy)
if !policyPointer.DeletionTimestamp.IsZero() {
klog.V(4).Infof("Cluster propagation policy(%s/%s) cannot match any resource template because it's being deleted.", policyPointer.Namespace, policyPointer.Name)
continue
}
implicitPriority := util.ResourceMatchSelectorsPriority(resource, policy.Spec.ResourceSelectors...)
if implicitPriority <= util.PriorityMisMatch {
continue
}
explicitPriority := policy.ExplicitPriority()
explicitPriority := policyPointer.ExplicitPriority()

if matchedClusterPolicyExplicitPriority < explicitPriority {
matchedClusterPolicyImplicitPriority = implicitPriority
matchedClusterPolicyExplicitPriority = explicitPriority
matchedClusterPolicy = policy
matchedClusterPolicy = policyPointer
continue
}

if matchedClusterPolicyExplicitPriority == explicitPriority {
if implicitPriority > matchedClusterPolicyImplicitPriority {
matchedClusterPolicyImplicitPriority = implicitPriority
matchedClusterPolicy = policy
matchedClusterPolicy = policyPointer
} else if implicitPriority == matchedClusterPolicyImplicitPriority {
matchedClusterPolicy = getHigherPriorityClusterPropagationPolicy(matchedClusterPolicy, policy)
matchedClusterPolicy = getHigherPriorityClusterPropagationPolicy(matchedClusterPolicy, policyPointer)
}
}
}
Expand Down
40 changes: 20 additions & 20 deletions pkg/detector/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func Test_GetHigherPriorityClusterPropagationPolicy(t *testing.T) {

func Test_getHighestPriorityPropagationPolicies(t *testing.T) {
type args struct {
policies []*policyv1alpha1.PropagationPolicy
policies []policyv1alpha1.PropagationPolicy
resource *unstructured.Unstructured
objectKey keys.ClusterWideKey
}
Expand All @@ -219,7 +219,7 @@ func Test_getHighestPriorityPropagationPolicies(t *testing.T) {
{
name: "empty policies",
args: args{
policies: []*policyv1alpha1.PropagationPolicy{},
policies: []policyv1alpha1.PropagationPolicy{},
resource: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
Expand All @@ -237,7 +237,7 @@ func Test_getHighestPriorityPropagationPolicies(t *testing.T) {
{
name: "mo policy match for resource",
args: args{
policies: []*policyv1alpha1.PropagationPolicy{
policies: []policyv1alpha1.PropagationPolicy{
{
ObjectMeta: metav1.ObjectMeta{Name: "match-with-name", Namespace: "test"},
Spec: policyv1alpha1.PropagationSpec{
Expand Down Expand Up @@ -278,7 +278,7 @@ func Test_getHighestPriorityPropagationPolicies(t *testing.T) {
{
name: "different implicit priority policy",
args: args{
policies: []*policyv1alpha1.PropagationPolicy{
policies: []policyv1alpha1.PropagationPolicy{
{
ObjectMeta: metav1.ObjectMeta{Name: "match-with-name", Namespace: "test"},
Spec: policyv1alpha1.PropagationSpec{
Expand Down Expand Up @@ -339,7 +339,7 @@ func Test_getHighestPriorityPropagationPolicies(t *testing.T) {
{
name: "same implicit priority policy",
args: args{
policies: []*policyv1alpha1.PropagationPolicy{
policies: []policyv1alpha1.PropagationPolicy{
{
ObjectMeta: metav1.ObjectMeta{Name: "a-pp", Namespace: "test"},
Spec: policyv1alpha1.PropagationSpec{
Expand Down Expand Up @@ -390,7 +390,7 @@ func Test_getHighestPriorityPropagationPolicies(t *testing.T) {
{
name: "one policy with implicit priority, one policy with explicit priority 1",
args: args{
policies: []*policyv1alpha1.PropagationPolicy{
policies: []policyv1alpha1.PropagationPolicy{
{
ObjectMeta: metav1.ObjectMeta{Name: "a-pp", Namespace: "test"},
Spec: policyv1alpha1.PropagationSpec{
Expand Down Expand Up @@ -449,7 +449,7 @@ func Test_getHighestPriorityPropagationPolicies(t *testing.T) {
{
name: "one policy with explicit priority 1(name match), one policy with explicit priority 2(label selector match)",
args: args{
policies: []*policyv1alpha1.PropagationPolicy{
policies: []policyv1alpha1.PropagationPolicy{
{
ObjectMeta: metav1.ObjectMeta{Name: "a-pp", Namespace: "test"},
Spec: policyv1alpha1.PropagationSpec{
Expand Down Expand Up @@ -512,7 +512,7 @@ func Test_getHighestPriorityPropagationPolicies(t *testing.T) {
{
name: "two policies with explicit priority 1(name match), select the one with lower alphabetical order",
args: args{
policies: []*policyv1alpha1.PropagationPolicy{
policies: []policyv1alpha1.PropagationPolicy{
{
ObjectMeta: metav1.ObjectMeta{Name: "a-pp", Namespace: "test"},
Spec: policyv1alpha1.PropagationSpec{
Expand Down Expand Up @@ -575,7 +575,7 @@ func Test_getHighestPriorityPropagationPolicies(t *testing.T) {
{
name: "one policy with explicit priority 1(name match), one policy with explicit priority 1(label selector match)",
args: args{
policies: []*policyv1alpha1.PropagationPolicy{
policies: []policyv1alpha1.PropagationPolicy{
{
ObjectMeta: metav1.ObjectMeta{Name: "a-pp", Namespace: "test"},
Spec: policyv1alpha1.PropagationSpec{
Expand Down Expand Up @@ -638,7 +638,7 @@ func Test_getHighestPriorityPropagationPolicies(t *testing.T) {
{
name: "one policy with explicit priority -1(name match), one policy with implicit priority(label selector match)",
args: args{
policies: []*policyv1alpha1.PropagationPolicy{
policies: []policyv1alpha1.PropagationPolicy{
{
ObjectMeta: metav1.ObjectMeta{Name: "a-pp", Namespace: "test"},
Spec: policyv1alpha1.PropagationSpec{
Expand Down Expand Up @@ -702,7 +702,7 @@ func Test_getHighestPriorityPropagationPolicies(t *testing.T) {

func Test_getHighestPriorityClusterPropagationPolicies(t *testing.T) {
type args struct {
policies []*policyv1alpha1.ClusterPropagationPolicy
policies []policyv1alpha1.ClusterPropagationPolicy
resource *unstructured.Unstructured
objectKey keys.ClusterWideKey
}
Expand All @@ -714,7 +714,7 @@ func Test_getHighestPriorityClusterPropagationPolicies(t *testing.T) {
{
name: "empty policies",
args: args{
policies: []*policyv1alpha1.ClusterPropagationPolicy{},
policies: []policyv1alpha1.ClusterPropagationPolicy{},
resource: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
Expand All @@ -732,7 +732,7 @@ func Test_getHighestPriorityClusterPropagationPolicies(t *testing.T) {
{
name: "mo policy match for resource",
args: args{
policies: []*policyv1alpha1.ClusterPropagationPolicy{
policies: []policyv1alpha1.ClusterPropagationPolicy{
{
ObjectMeta: metav1.ObjectMeta{Name: "match-with-name", Namespace: "test"},
Spec: policyv1alpha1.PropagationSpec{
Expand Down Expand Up @@ -773,7 +773,7 @@ func Test_getHighestPriorityClusterPropagationPolicies(t *testing.T) {
{
name: "different implicit priority policy",
args: args{
policies: []*policyv1alpha1.ClusterPropagationPolicy{
policies: []policyv1alpha1.ClusterPropagationPolicy{
{
ObjectMeta: metav1.ObjectMeta{Name: "match-with-name", Namespace: "test"},
Spec: policyv1alpha1.PropagationSpec{
Expand Down Expand Up @@ -834,7 +834,7 @@ func Test_getHighestPriorityClusterPropagationPolicies(t *testing.T) {
{
name: "same implicit priority policy",
args: args{
policies: []*policyv1alpha1.ClusterPropagationPolicy{
policies: []policyv1alpha1.ClusterPropagationPolicy{
{
ObjectMeta: metav1.ObjectMeta{Name: "a-pp", Namespace: "test"},
Spec: policyv1alpha1.PropagationSpec{
Expand Down Expand Up @@ -885,7 +885,7 @@ func Test_getHighestPriorityClusterPropagationPolicies(t *testing.T) {
{
name: "one policy with implicit priority, one policy with explicit priority 1",
args: args{
policies: []*policyv1alpha1.ClusterPropagationPolicy{
policies: []policyv1alpha1.ClusterPropagationPolicy{
{
ObjectMeta: metav1.ObjectMeta{Name: "a-pp", Namespace: "test"},
Spec: policyv1alpha1.PropagationSpec{
Expand Down Expand Up @@ -944,7 +944,7 @@ func Test_getHighestPriorityClusterPropagationPolicies(t *testing.T) {
{
name: "one policy with explicit priority 1(name match), one policy with explicit priority 2(label selector match)",
args: args{
policies: []*policyv1alpha1.ClusterPropagationPolicy{
policies: []policyv1alpha1.ClusterPropagationPolicy{
{
ObjectMeta: metav1.ObjectMeta{Name: "a-pp", Namespace: "test"},
Spec: policyv1alpha1.PropagationSpec{
Expand Down Expand Up @@ -1007,7 +1007,7 @@ func Test_getHighestPriorityClusterPropagationPolicies(t *testing.T) {
{
name: "two policies with explicit priority 1(name match), select the one with lower alphabetical order",
args: args{
policies: []*policyv1alpha1.ClusterPropagationPolicy{
policies: []policyv1alpha1.ClusterPropagationPolicy{
{
ObjectMeta: metav1.ObjectMeta{Name: "a-pp", Namespace: "test"},
Spec: policyv1alpha1.PropagationSpec{
Expand Down Expand Up @@ -1070,7 +1070,7 @@ func Test_getHighestPriorityClusterPropagationPolicies(t *testing.T) {
{
name: "one policy with explicit priority 1(name match), one policy with explicit priority 1(label selector match)",
args: args{
policies: []*policyv1alpha1.ClusterPropagationPolicy{
policies: []policyv1alpha1.ClusterPropagationPolicy{
{
ObjectMeta: metav1.ObjectMeta{Name: "a-pp", Namespace: "test"},
Spec: policyv1alpha1.PropagationSpec{
Expand Down Expand Up @@ -1133,7 +1133,7 @@ func Test_getHighestPriorityClusterPropagationPolicies(t *testing.T) {
{
name: "one policy with explicit priority -1(name match), one policy with implicit priority(label selector match)",
args: args{
policies: []*policyv1alpha1.ClusterPropagationPolicy{
policies: []policyv1alpha1.ClusterPropagationPolicy{
{
ObjectMeta: metav1.ObjectMeta{Name: "a-pp", Namespace: "test"},
Spec: policyv1alpha1.PropagationSpec{
Expand Down
Loading

0 comments on commit a03b852

Please sign in to comment.