Skip to content

Commit

Permalink
add implicit priority for OP and COP
Browse files Browse the repository at this point in the history
Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
  • Loading branch information
chaunceyjiang committed Nov 29, 2022
1 parent b0eb90b commit 200a587
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
19 changes: 14 additions & 5 deletions pkg/util/overridemanager/overridemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,20 @@ func (o *overrideManagerImpl) getOverridersFromOverridePolicies(policies []Gener
resourceMatchingPolicies = append(resourceMatchingPolicies, policy)
}
}

sort.SliceStable(resourceMatchingPolicies, func(i, j int) bool {
implicitPriorityI := util.ResourceMatchSelectorsPriority(resource, resourceMatchingPolicies[i].GetOverrideSpec().ResourceSelectors...)
if len(resourceMatchingPolicies[i].GetOverrideSpec().ResourceSelectors) == 0 {
implicitPriorityI = util.PriorityMatchAll
}
implicitPriorityJ := util.ResourceMatchSelectorsPriority(resource, resourceMatchingPolicies[j].GetOverrideSpec().ResourceSelectors...)
if len(resourceMatchingPolicies[j].GetOverrideSpec().ResourceSelectors) == 0 {
implicitPriorityJ = util.PriorityMatchAll
}
if implicitPriorityI != implicitPriorityJ {
return implicitPriorityI < implicitPriorityJ
}
return resourceMatchingPolicies[i].GetName() < resourceMatchingPolicies[j].GetName()
})
clusterMatchingPolicyOverriders := make([]policyOverriders, 0)
for _, policy := range resourceMatchingPolicies {
overrideRules := policy.GetOverrideSpec().OverrideRules
Expand Down Expand Up @@ -222,10 +235,6 @@ func (o *overrideManagerImpl) getOverridersFromOverridePolicies(policies []Gener
// select policy in which at least one PlaintextOverrider matches target resource.
// TODO(RainbowMango): check if the overrider instructions can be applied to target resource.

sort.Slice(clusterMatchingPolicyOverriders, func(i, j int) bool {
return clusterMatchingPolicyOverriders[i].name < clusterMatchingPolicyOverriders[j].name
})

return clusterMatchingPolicyOverriders
}

Expand Down
22 changes: 12 additions & 10 deletions pkg/util/overridemanager/overridemanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func TestGetMatchingOverridePolicies(t *testing.T) {
},
},
}
// high implicit priority
overridePolicy1 := &policyv1alpha1.OverridePolicy{
ObjectMeta: metav1.ObjectMeta{
Namespace: metav1.NamespaceDefault,
Expand Down Expand Up @@ -77,6 +78,7 @@ func TestGetMatchingOverridePolicies(t *testing.T) {
},
},
}
// low implicit priority
overridePolicy2 := &policyv1alpha1.OverridePolicy{
ObjectMeta: metav1.ObjectMeta{
Namespace: metav1.NamespaceDefault,
Expand Down Expand Up @@ -137,16 +139,16 @@ func TestGetMatchingOverridePolicies(t *testing.T) {
resource: deploymentObj,
cluster: cluster1,
wantedOverriders: []policyOverriders{
{
name: overridePolicy1.Name,
namespace: overridePolicy1.Namespace,
overriders: overriders1,
},
{
name: overridePolicy2.Name,
namespace: overridePolicy2.Namespace,
overriders: overriders3,
},
{
name: overridePolicy1.Name,
namespace: overridePolicy1.Namespace,
overriders: overriders1,
},
},
},
{
Expand All @@ -155,6 +157,11 @@ func TestGetMatchingOverridePolicies(t *testing.T) {
resource: deploymentObj,
cluster: cluster2,
wantedOverriders: []policyOverriders{
{
name: overridePolicy2.Name,
namespace: overridePolicy2.Namespace,
overriders: overriders3,
},
{
name: overridePolicy1.Name,
namespace: overridePolicy1.Namespace,
Expand All @@ -165,11 +172,6 @@ func TestGetMatchingOverridePolicies(t *testing.T) {
namespace: overridePolicy1.Namespace,
overriders: overriders2,
},
{
name: overridePolicy2.Name,
namespace: overridePolicy2.Namespace,
overriders: overriders3,
},
},
},
{
Expand Down

0 comments on commit 200a587

Please sign in to comment.