Skip to content

Commit a9f681d

Browse files
committed
kubeadm: sort the results of MergeKubeadmEnvVars
MergeKubeadmEnvVars use a map which results in non deterministic output in the end slice EnvVar objects. Before returning the slice, sort it by the Name field. Update the unit test to capture the sorting aspect.
1 parent ffbc494 commit a9f681d

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

cmd/kubeadm/app/util/env.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package util
1818

1919
import (
2020
"os"
21+
"sort"
2122
"strings"
2223

2324
v1 "k8s.io/api/core/v1"
@@ -63,5 +64,8 @@ func MergeKubeadmEnvVars(envList ...[]kubeadmapi.EnvVar) []v1.EnvVar {
6364
for _, v := range m {
6465
merged = append(merged, v)
6566
}
67+
sort.Slice(merged, func(i, j int) bool {
68+
return merged[i].Name < merged[j].Name
69+
})
6670
return merged
6771
}

cmd/kubeadm/app/util/env_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
"reflect"
2121
"testing"
2222

23-
"github.com/stretchr/testify/assert"
24-
2523
v1 "k8s.io/api/core/v1"
2624

2725
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
@@ -41,10 +39,10 @@ func TestMergeKubeadmEnvVars(t *testing.T) {
4139
name: "normal case without duplicated env",
4240
proxyEnv: []kubeadmapi.EnvVar{
4341
{
44-
EnvVar: v1.EnvVar{Name: "Foo1", Value: "Bar1"},
42+
EnvVar: v1.EnvVar{Name: "Foo2", Value: "Bar2"},
4543
},
4644
{
47-
EnvVar: v1.EnvVar{Name: "Foo2", Value: "Bar2"},
45+
EnvVar: v1.EnvVar{Name: "Foo1", Value: "Bar1"},
4846
},
4947
},
5048
extraEnv: []kubeadmapi.EnvVar{
@@ -83,7 +81,7 @@ func TestMergeKubeadmEnvVars(t *testing.T) {
8381
for _, test := range tests {
8482
t.Run(test.name, func(t *testing.T) {
8583
envs := MergeKubeadmEnvVars(test.proxyEnv, test.extraEnv)
86-
if !assert.ElementsMatch(t, envs, test.mergedEnv) {
84+
if !reflect.DeepEqual(envs, test.mergedEnv) {
8785
t.Errorf("expected env: %v, got: %v", test.mergedEnv, envs)
8886
}
8987
})

0 commit comments

Comments
 (0)