Skip to content

Commit 2e08288

Browse files
committed
Remove conflict logic from PodTolerationRestriction
1 parent 5a50b3f commit 2e08288

File tree

4 files changed

+23
-38
lines changed

4 files changed

+23
-38
lines changed

pkg/util/tolerations/tolerations.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ next:
4646
// another, only the superset is kept.
4747
func MergeTolerations(first, second []api.Toleration) []api.Toleration {
4848
all := append(first, second...)
49-
merged := []api.Toleration{}
49+
var merged []api.Toleration
5050

5151
next:
5252
for i, t := range all {
@@ -58,7 +58,7 @@ next:
5858
if i+1 < len(all) {
5959
for _, t2 := range all[i+1:] {
6060
// If the tolerations are equal, prefer the first.
61-
if isSuperset(t2, t) && !apiequality.Semantic.DeepEqual(&t, &t2) {
61+
if !apiequality.Semantic.DeepEqual(&t, &t2) && isSuperset(t2, t) {
6262
continue next // t is redundant; ignore it
6363
}
6464
}

plugin/pkg/admission/podtolerationrestriction/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ go_test(
1414
"//pkg/apis/core:go_default_library",
1515
"//pkg/features:go_default_library",
1616
"//pkg/scheduler/api:go_default_library",
17-
"//pkg/util/tolerations:go_default_library",
1817
"//plugin/pkg/admission/podtolerationrestriction/apis/podtolerationrestriction:go_default_library",
1918
"//staging/src/k8s.io/api/core/v1:go_default_library",
2019
"//staging/src/k8s.io/apimachinery/pkg/api/resource:go_default_library",
@@ -27,6 +26,7 @@ go_test(
2726
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
2827
"//staging/src/k8s.io/client-go/kubernetes/fake:go_default_library",
2928
"//staging/src/k8s.io/component-base/featuregate/testing:go_default_library",
29+
"//vendor/github.com/stretchr/testify/assert:go_default_library",
3030
],
3131
)
3232

plugin/pkg/admission/podtolerationrestriction/admission.go

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (p *Plugin) Admit(ctx context.Context, a admission.Attributes, o admission.
8484
}
8585

8686
pod := a.GetObject().(*api.Pod)
87-
var finalTolerations []api.Toleration
87+
var extraTolerations []api.Toleration
8888
if a.GetOperation() == admission.Create {
8989
ts, err := p.getNamespaceDefaultTolerations(a.GetNamespace())
9090
if err != nil {
@@ -97,37 +97,20 @@ func (p *Plugin) Admit(ctx context.Context, a admission.Attributes, o admission.
9797
ts = p.pluginConfig.Default
9898
}
9999

100-
if len(ts) > 0 {
101-
if len(pod.Spec.Tolerations) > 0 {
102-
if tolerations.IsConflict(ts, pod.Spec.Tolerations) {
103-
return fmt.Errorf("namespace tolerations and pod tolerations conflict")
104-
}
105-
106-
// modified pod tolerations = namespace tolerations + current pod tolerations
107-
finalTolerations = tolerations.MergeTolerations(ts, pod.Spec.Tolerations)
108-
} else {
109-
finalTolerations = ts
110-
111-
}
112-
} else {
113-
finalTolerations = pod.Spec.Tolerations
114-
}
115-
} else {
116-
finalTolerations = pod.Spec.Tolerations
100+
extraTolerations = ts
117101
}
118102

119103
if qoshelper.GetPodQOS(pod) != api.PodQOSBestEffort {
120-
finalTolerations = tolerations.MergeTolerations(finalTolerations, []api.Toleration{
121-
{
122-
Key: schedulerapi.TaintNodeMemoryPressure,
123-
Operator: api.TolerationOpExists,
124-
Effect: api.TaintEffectNoSchedule,
125-
},
104+
extraTolerations = append(extraTolerations, api.Toleration{
105+
Key: schedulerapi.TaintNodeMemoryPressure,
106+
Operator: api.TolerationOpExists,
107+
Effect: api.TaintEffectNoSchedule,
126108
})
127109
}
128-
// Final merge of tolerations irrespective of pod type, if the user while creating pods gives
129-
// conflicting tolerations(with same key+effect), the existing ones should be overwritten by latest one
130-
pod.Spec.Tolerations = tolerations.MergeTolerations(finalTolerations, []api.Toleration{})
110+
// Final merge of tolerations irrespective of pod type.
111+
if len(extraTolerations) > 0 {
112+
pod.Spec.Tolerations = tolerations.MergeTolerations(pod.Spec.Tolerations, extraTolerations)
113+
}
131114
return p.Validate(ctx, a, o)
132115
}
133116

plugin/pkg/admission/podtolerationrestriction/admission_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"testing"
2323
"time"
2424

25+
"github.com/stretchr/testify/assert"
2526
corev1 "k8s.io/api/core/v1"
2627
"k8s.io/apimachinery/pkg/api/resource"
2728
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -36,7 +37,6 @@ import (
3637
api "k8s.io/kubernetes/pkg/apis/core"
3738
"k8s.io/kubernetes/pkg/features"
3839
schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
39-
"k8s.io/kubernetes/pkg/util/tolerations"
4040
pluginapi "k8s.io/kubernetes/plugin/pkg/admission/podtolerationrestriction/apis/podtolerationrestriction"
4141
)
4242

@@ -139,10 +139,11 @@ func TestPodAdmission(t *testing.T) {
139139
{
140140
pod: bestEffortPod,
141141
defaultClusterTolerations: []api.Toleration{},
142-
namespaceTolerations: []api.Toleration{{Key: "testKey", Operator: "Equal", Value: "testValue", Effect: "NoSchedule", TolerationSeconds: nil}},
143-
podTolerations: []api.Toleration{{Key: "testKey", Operator: "Equal", Value: "testValue1", Effect: "NoSchedule", TolerationSeconds: nil}},
144-
admit: false,
145-
testName: "conflicting pod and namespace tolerations",
142+
namespaceTolerations: []api.Toleration{{Key: "testKey", Operator: "Equal", Value: "testValue", Effect: "NoSchedule"}},
143+
podTolerations: []api.Toleration{{Key: "testKey", Operator: "Equal", Value: "testValue1", Effect: "NoSchedule"}},
144+
mergedTolerations: []api.Toleration{{Key: "testKey", Operator: "Equal", Value: "testValue", Effect: "NoSchedule"}, {Key: "testKey", Operator: "Equal", Value: "testValue1", Effect: "NoSchedule"}},
145+
admit: true,
146+
testName: "duplicate key pod and namespace tolerations",
146147
},
147148
{
148149
pod: bestEffortPod,
@@ -210,10 +211,11 @@ func TestPodAdmission(t *testing.T) {
210211
whitelist: []api.Toleration{},
211212
podTolerations: []api.Toleration{{Key: "testKey", Operator: "Equal", Value: "testValue", Effect: "NoSchedule", TolerationSeconds: nil}, {Key: "testKey", Operator: "Equal", Value: "testValue1", Effect: "NoSchedule", TolerationSeconds: nil}},
212213
mergedTolerations: []api.Toleration{
214+
{Key: "testKey", Operator: "Equal", Value: "testValue", Effect: "NoSchedule", TolerationSeconds: nil},
213215
{Key: "testKey", Operator: "Equal", Value: "testValue1", Effect: "NoSchedule", TolerationSeconds: nil},
214216
},
215217
admit: true,
216-
testName: "Besteffort pod should overwrite for conflicting tolerations",
218+
testName: "Pod with duplicate key tolerations should not be modified",
217219
},
218220
{
219221
pod: guaranteedPod,
@@ -275,8 +277,8 @@ func TestPodAdmission(t *testing.T) {
275277
}
276278

277279
updatedPodTolerations := pod.Spec.Tolerations
278-
if test.admit && !tolerations.EqualTolerations(updatedPodTolerations, test.mergedTolerations) {
279-
t.Errorf("Test: %s, expected: %#v but got: %#v", test.testName, test.mergedTolerations, updatedPodTolerations)
280+
if test.admit {
281+
assert.ElementsMatch(t, updatedPodTolerations, test.mergedTolerations)
280282
}
281283
})
282284
}

0 commit comments

Comments
 (0)