Skip to content

Commit 7460db3

Browse files
drivebyerdrivebyer
and
drivebyer
authored
Fix lost toleration when key duplicated (#1314)
Co-authored-by: drivebyer <[email protected]>
1 parent 880414e commit 7460db3

File tree

2 files changed

+92
-5
lines changed

2 files changed

+92
-5
lines changed

pkg/util/merge/merge.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -507,11 +507,15 @@ func Tolerations(defaultTolerations, overrideTolerations []corev1.Toleration) []
507507
mergedTolerations := make([]corev1.Toleration, 0)
508508
defaultMap := createTolerationsMap(defaultTolerations)
509509
for _, v := range overrideTolerations {
510-
defaultMap[v.Key] = v
510+
if _, ok := defaultMap[v.Key]; ok {
511+
defaultMap[v.Key] = append(defaultMap[v.Key], v)
512+
} else {
513+
defaultMap[v.Key] = []corev1.Toleration{v}
514+
}
511515
}
512516

513517
for _, v := range defaultMap {
514-
mergedTolerations = append(mergedTolerations, v)
518+
mergedTolerations = append(mergedTolerations, v...)
515519
}
516520

517521
if len(mergedTolerations) == 0 {
@@ -525,10 +529,14 @@ func Tolerations(defaultTolerations, overrideTolerations []corev1.Toleration) []
525529
return mergedTolerations
526530
}
527531

528-
func createTolerationsMap(tolerations []corev1.Toleration) map[string]corev1.Toleration {
529-
tolerationsMap := make(map[string]corev1.Toleration)
532+
func createTolerationsMap(tolerations []corev1.Toleration) map[string][]corev1.Toleration {
533+
tolerationsMap := make(map[string][]corev1.Toleration)
530534
for _, t := range tolerations {
531-
tolerationsMap[t.Key] = t
535+
if _, ok := tolerationsMap[t.Key]; ok {
536+
tolerationsMap[t.Key] = append(tolerationsMap[t.Key], t)
537+
} else {
538+
tolerationsMap[t.Key] = []corev1.Toleration{t}
539+
}
532540
}
533541
return tolerationsMap
534542
}

pkg/util/merge/merge_test.go

+79
Original file line numberDiff line numberDiff line change
@@ -670,3 +670,82 @@ func TestMergeHostAliases(t *testing.T) {
670670
assert.Equal(t, "1.2.3.5", merged[1].IP)
671671
assert.Equal(t, []string{"abc"}, merged[1].Hostnames)
672672
}
673+
674+
func TestTolerations(t *testing.T) {
675+
type args struct {
676+
defaultTolerations []corev1.Toleration
677+
overrideTolerations []corev1.Toleration
678+
}
679+
tests := []struct {
680+
name string
681+
args args
682+
want []corev1.Toleration
683+
}{
684+
{
685+
name: "override tolerations is nil",
686+
args: args{
687+
defaultTolerations: []corev1.Toleration{
688+
{
689+
Key: "key1",
690+
Value: "value1",
691+
Operator: corev1.TolerationOpEqual,
692+
},
693+
{
694+
Key: "key1",
695+
Value: "value2",
696+
Operator: corev1.TolerationOpExists,
697+
},
698+
},
699+
overrideTolerations: nil,
700+
},
701+
want: []corev1.Toleration{
702+
{
703+
Key: "key1",
704+
Value: "value1",
705+
Operator: corev1.TolerationOpEqual,
706+
},
707+
{
708+
Key: "key1",
709+
Value: "value2",
710+
Operator: corev1.TolerationOpExists,
711+
},
712+
},
713+
},
714+
715+
{
716+
name: "default tolerations is nil",
717+
args: args{
718+
defaultTolerations: nil,
719+
overrideTolerations: []corev1.Toleration{
720+
{
721+
Key: "key1",
722+
Value: "value1",
723+
Operator: corev1.TolerationOpEqual,
724+
},
725+
{
726+
Key: "key1",
727+
Value: "value2",
728+
Operator: corev1.TolerationOpExists,
729+
},
730+
},
731+
},
732+
want: []corev1.Toleration{
733+
{
734+
Key: "key1",
735+
Value: "value1",
736+
Operator: corev1.TolerationOpEqual,
737+
},
738+
{
739+
Key: "key1",
740+
Value: "value2",
741+
Operator: corev1.TolerationOpExists,
742+
},
743+
},
744+
},
745+
}
746+
for _, tt := range tests {
747+
t.Run(tt.name, func(t *testing.T) {
748+
assert.Equalf(t, tt.want, Tolerations(tt.args.defaultTolerations, tt.args.overrideTolerations), "Tolerations(%v, %v)", tt.args.defaultTolerations, tt.args.overrideTolerations)
749+
})
750+
}
751+
}

0 commit comments

Comments
 (0)