Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add implicit priority for OP and COP #2609

Merged
merged 1 commit into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 {
chaunceyjiang marked this conversation as resolved.
Show resolved Hide resolved
implicitPriorityI = util.PriorityMatchAll
}
implicitPriorityJ := util.ResourceMatchSelectorsPriority(resource, resourceMatchingPolicies[j].GetOverrideSpec().ResourceSelectors...)
if len(resourceMatchingPolicies[j].GetOverrideSpec().ResourceSelectors) == 0 {
chaunceyjiang marked this conversation as resolved.
Show resolved Hide resolved
implicitPriorityJ = util.PriorityMatchAll
}
if implicitPriorityI != implicitPriorityJ {
return implicitPriorityI < implicitPriorityJ
chaunceyjiang marked this conversation as resolved.
Show resolved Hide resolved
}
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