From 4613cce211e30e9cb4e3f9ea6f96a47129e139fc Mon Sep 17 00:00:00 2001 From: Jiaqi Luo <6218999+jiaqiluo@users.noreply.github.com> Date: Tue, 4 Jun 2024 16:11:58 -0700 Subject: [PATCH] Avoid allocating empty strings in the Env slice Previously, we declared the Env slice with the size of the `c.Services.KubeAPI.ExtraEnv` field, which leads to empty strings at the beginning of the Env slice because we use Golang's append function to add new elements, and Docker is not happy with that. We fix this issue by declaring the Env variable as an empty slice. We also enhance the `getUniqStringList` function to properly trim leading and trailing spaces in each element and to ignore empty strings. We add unit tests for the `getUniqStringList` function and update the integration tests. --- cluster/plan.go | 6 ++++- cluster/plan_test.go | 52 ++++++++++++++++++++++++++++++++++++++++++++ scripts/integration | 19 ++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 cluster/plan_test.go diff --git a/cluster/plan.go b/cluster/plan.go index 98621fcf2..5f0fbbedc 100644 --- a/cluster/plan.go +++ b/cluster/plan.go @@ -203,7 +203,7 @@ func (c *Cluster) BuildKubeAPIProcess(host *hosts.Host, serviceOptions v3.Kubern "tls-private-key-file": pki.GetKeyPath(pki.KubeAPICertName), } CommandArrayArgs := make(map[string][]string, len(c.Services.KubeAPI.ExtraArgsArray)) - Env := make([]string, len(c.Services.KubeAPI.ExtraEnv)) + var Env []string if len(c.CloudProvider.Name) > 0 { CommandArgs["cloud-config"] = cloudConfigFileName @@ -1267,6 +1267,10 @@ func getUniqStringList(l []string) []string { m := map[string]bool{} ul := []string{} for _, k := range l { + k = strings.TrimSpace(k) + if k == "" { + continue + } if _, ok := m[k]; !ok { m[k] = true ul = append(ul, k) diff --git a/cluster/plan_test.go b/cluster/plan_test.go new file mode 100644 index 000000000..5dcc3bc0d --- /dev/null +++ b/cluster/plan_test.go @@ -0,0 +1,52 @@ +package cluster + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_getUniqStringList(t *testing.T) { + type args struct { + l []string + } + tests := []struct { + name string + args args + want []string + }{ + { + "contain strings with only spaces", + args{ + []string{" ", "key1=value1", " ", "key2=value2"}, + }, + []string{"key1=value1", "key2=value2"}, + }, + { + "contain strings with trailing or leading spaces", + args{ + []string{" key1=value1", "key1=value1 ", " key2=value2 "}, + }, + []string{"key1=value1", "key2=value2"}, + }, + { + "contain duplicated strings", + args{ + []string{"", "key1=value1", "key1=value1", "key2=value2"}, + }, + []string{"key1=value1", "key2=value2"}, + }, + { + "contain empty string", + args{ + []string{"", "key1=value1", "", "key2=value2"}, + }, + []string{"key1=value1", "key2=value2"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equalf(t, tt.want, getUniqStringList(tt.args.l), "getUniqStringList(%v)", tt.args.l) + }) + } +} diff --git a/scripts/integration b/scripts/integration index cb6b158e9..ad88123ff 100755 --- a/scripts/integration +++ b/scripts/integration @@ -90,6 +90,25 @@ nodes: - address: rke-node-${node} role: [etcd, controlplane, worker] user: ubuntu +services: + etcd: + extra_env: + - TEST_VAR=etcd + kubeproxy: + extra_env: + - TEST_VAR=kube-proxy + scheduler: + extra_env: + - TEST_VAR=scheduler + kubelet: + extra_env: + - TEST_VAR=kubelet + kube-controller: + extra_env: + - TEST_VAR=kube-controller + kube-api: + extra_env: + - TEST_VAR=kube-api EOF if [ "x${NETWORK_PLUGIN}" != "x" ]; then