mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Remove redundant env field
This commit is contained in:
@@ -92,7 +92,7 @@ type RunFns struct {
|
||||
User runtimeutil.ContainerUser
|
||||
|
||||
// Env contains environment variables that will be exported to container
|
||||
Env runtimeutil.ContainerEnv
|
||||
Env []string
|
||||
}
|
||||
|
||||
// Execute runs the command
|
||||
@@ -274,16 +274,18 @@ func (r RunFns) getFunctionsFromFunctions() ([]kio.Filter, error) {
|
||||
|
||||
// mergeContainerEnv will merge the envs specified by command line (imperative) and config
|
||||
// file (declarative). If they have same key, the imperative value will be respected.
|
||||
func (r RunFns) mergeContainerEnv(envs runtimeutil.ContainerEnv) runtimeutil.ContainerEnv {
|
||||
for key, value := range r.Env.EnvVars {
|
||||
envs.AddKeyValue(key, value)
|
||||
func (r RunFns) mergeContainerEnv(envs []string) []string {
|
||||
imperative := runtimeutil.NewContainerEnvFromStringSlice(r.Env)
|
||||
declarative := runtimeutil.NewContainerEnvFromStringSlice(envs)
|
||||
for key, value := range imperative.EnvVars {
|
||||
declarative.AddKeyValue(key, value)
|
||||
}
|
||||
|
||||
for _, key := range r.Env.VarsToExport {
|
||||
envs.AddKey(key)
|
||||
for _, key := range imperative.VarsToExport {
|
||||
declarative.AddKey(key)
|
||||
}
|
||||
|
||||
return envs
|
||||
return declarative.Raw()
|
||||
}
|
||||
|
||||
func (r RunFns) getFunctionFilters(global bool, fns ...*yaml.RNode) (
|
||||
|
||||
@@ -991,43 +991,41 @@ func TestRunfns_mergeContainerEnv(t *testing.T) {
|
||||
testcases := []struct {
|
||||
name string
|
||||
instance RunFns
|
||||
inputEnvs runtimeutil.ContainerEnv
|
||||
inputEnvs []string
|
||||
expect runtimeutil.ContainerEnv
|
||||
}{
|
||||
{
|
||||
name: "all empty",
|
||||
instance: RunFns{},
|
||||
inputEnvs: *runtimeutil.NewContainerEnv(),
|
||||
expect: *runtimeutil.NewContainerEnv(),
|
||||
name: "all empty",
|
||||
instance: RunFns{},
|
||||
expect: *runtimeutil.NewContainerEnv(),
|
||||
},
|
||||
{
|
||||
name: "empty command line envs",
|
||||
instance: RunFns{},
|
||||
inputEnvs: *runtimeutil.NewContainerEnvFromStringSlice([]string{"foo=bar"}),
|
||||
inputEnvs: []string{"foo=bar"},
|
||||
expect: *runtimeutil.NewContainerEnvFromStringSlice([]string{"foo=bar"}),
|
||||
},
|
||||
{
|
||||
name: "empty declarative envs",
|
||||
instance: RunFns{
|
||||
Env: *runtimeutil.NewContainerEnvFromStringSlice([]string{"foo=bar"}),
|
||||
Env: []string{"foo=bar"},
|
||||
},
|
||||
inputEnvs: *runtimeutil.NewContainerEnv(),
|
||||
expect: *runtimeutil.NewContainerEnvFromStringSlice([]string{"foo=bar"}),
|
||||
expect: *runtimeutil.NewContainerEnvFromStringSlice([]string{"foo=bar"}),
|
||||
},
|
||||
{
|
||||
name: "same key",
|
||||
instance: RunFns{
|
||||
Env: *runtimeutil.NewContainerEnvFromStringSlice([]string{"foo=bar", "foo"}),
|
||||
Env: []string{"foo=bar", "foo"},
|
||||
},
|
||||
inputEnvs: *runtimeutil.NewContainerEnvFromStringSlice([]string{"foo=bar1", "bar"}),
|
||||
inputEnvs: []string{"foo=bar1", "bar"},
|
||||
expect: *runtimeutil.NewContainerEnvFromStringSlice([]string{"foo=bar", "bar", "foo"}),
|
||||
},
|
||||
{
|
||||
name: "same exported key",
|
||||
instance: RunFns{
|
||||
Env: *runtimeutil.NewContainerEnvFromStringSlice([]string{"foo=bar", "foo"}),
|
||||
Env: []string{"foo=bar", "foo"},
|
||||
},
|
||||
inputEnvs: *runtimeutil.NewContainerEnvFromStringSlice([]string{"foo1=bar1", "foo"}),
|
||||
inputEnvs: []string{"foo1=bar1", "foo"},
|
||||
expect: *runtimeutil.NewContainerEnvFromStringSlice([]string{"foo=bar", "foo1=bar1", "foo"}),
|
||||
},
|
||||
}
|
||||
@@ -1036,7 +1034,7 @@ func TestRunfns_mergeContainerEnv(t *testing.T) {
|
||||
tc := testcases[i]
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
envs := tc.instance.mergeContainerEnv(tc.inputEnvs)
|
||||
assert.Equal(t, tc.expect.GetDockerFlags(), envs.GetDockerFlags())
|
||||
assert.Equal(t, tc.expect.GetDockerFlags(), runtimeutil.NewContainerEnvFromStringSlice(envs).GetDockerFlags())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user