Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
Fix appending content
Browse files Browse the repository at this point in the history
  • Loading branch information
ctron committed Jun 23, 2020
1 parent 5132967 commit dd5e515
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
25 changes: 14 additions & 11 deletions pkg/util/install/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,23 +575,26 @@ func AppendEnvVarValue(container *corev1.Container, name string, value string) {
container.Env = make([]corev1.EnvVar, 0)
}

opts := ""

for _, env := range container.Env {
for i, env := range container.Env {
if env.Name == name {
opts = env.Value
}
}
opts := env.Value

if len(opts) > 0 {
opts += " "
}
if len(opts) > 0 {
opts += " "
}

opts += value
opts += value

env.Value = opts
container.Env[i] = env

return
}
}

container.Env = append(container.Env, corev1.EnvVar{
Name: name,
Value: opts,
Value: value,
})
}

Expand Down
27 changes: 27 additions & 0 deletions pkg/util/install/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,30 @@ func TestComputeConfigMapHash(t *testing.T) {
assert.Equal(t, hash(data1), hash(data2))
assert.NotEqual(t, hash(bar), hash(data2))
}

func TestAppendEnvVar(t *testing.T) {

container := corev1.Container{}

assert.Nil(t, container.Env)
assert.Len(t, container.Env, 0)

// append first element

AppendEnvVarValue(&container, JavaOptsEnvVarName, "foo")

assert.NotNil(t, container.Env)
assert.Len(t, container.Env, 1)
assert.Equal(t, JavaOptsEnvVarName, container.Env[0].Name)
assert.Equal(t, "foo", container.Env[0].Value)

// append second element

AppendEnvVarValue(&container, JavaOptsEnvVarName, "bar")

assert.NotNil(t, container.Env)
assert.Len(t, container.Env, 1)
assert.Equal(t, JavaOptsEnvVarName, container.Env[0].Name)
assert.Equal(t, "foo bar", container.Env[0].Value)

}

0 comments on commit dd5e515

Please sign in to comment.