mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
Remove redundant env field
This commit is contained in:
@@ -152,8 +152,6 @@ func (c *Filter) setupExec() {
|
|||||||
c.Exec.Args = args
|
c.Exec.Args = args
|
||||||
}
|
}
|
||||||
|
|
||||||
var tmpDirEnvKey string = "TMPDIR"
|
|
||||||
|
|
||||||
// getArgs returns the command + args to run to spawn the container
|
// getArgs returns the command + args to run to spawn the container
|
||||||
func (c *Filter) getCommand() (string, []string) {
|
func (c *Filter) getCommand() (string, []string) {
|
||||||
// run the container using docker. this is simpler than using the docker
|
// run the container using docker. this is simpler than using the docker
|
||||||
@@ -175,7 +173,7 @@ func (c *Filter) getCommand() (string, []string) {
|
|||||||
args = append(args, "--mount", storageMount.String())
|
args = append(args, "--mount", storageMount.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
args = append(args, c.Env.GetDockerFlags()...)
|
args = append(args, runtimeutil.NewContainerEnvFromStringSlice(c.Env).GetDockerFlags()...)
|
||||||
a := append(args, c.Image)
|
a := append(args, c.Image)
|
||||||
return "docker", a
|
return "docker", a
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ package container
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@@ -135,10 +134,11 @@ metadata:
|
|||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
tt.instance.Exec.FunctionConfig = cfg
|
tt.instance.Exec.FunctionConfig = cfg
|
||||||
tt.instance.Env.AddKeyValue("KYAML_TEST", "FOO")
|
tt.instance.Env = append(tt.instance.Env, "KYAML_TEST=FOO")
|
||||||
tt.instance.setupExec()
|
tt.instance.setupExec()
|
||||||
|
|
||||||
tt.expectedArgs = append(tt.expectedArgs, tt.instance.Env.GetDockerFlags()...)
|
tt.expectedArgs = append(tt.expectedArgs,
|
||||||
|
runtimeutil.NewContainerEnvFromStringSlice(tt.instance.Env).GetDockerFlags()...)
|
||||||
tt.expectedArgs = append(tt.expectedArgs, tt.instance.Image)
|
tt.expectedArgs = append(tt.expectedArgs, tt.instance.Image)
|
||||||
|
|
||||||
if !assert.Equal(t, "docker", tt.instance.Exec.Path) {
|
if !assert.Equal(t, "docker", tt.instance.Exec.Path) {
|
||||||
@@ -238,15 +238,3 @@ func TestFilter_ExitCode(t *testing.T) {
|
|||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIgnoreEnv(t *testing.T) {
|
|
||||||
os.Setenv(tmpDirEnvKey, "")
|
|
||||||
|
|
||||||
fltr := Filter{ContainerSpec: runtimeutil.ContainerSpec{Image: "example.com:version"}}
|
|
||||||
_, args := fltr.getCommand()
|
|
||||||
for _, arg := range args {
|
|
||||||
if arg == tmpDirEnvKey {
|
|
||||||
t.Fatalf("%s should not be exported to container", tmpDirEnvKey)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -102,6 +102,18 @@ func (ce *ContainerEnv) AddKey(key string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Raw returns a slice of string which represents the envs.
|
||||||
|
// Example: [foo=bar, baz]
|
||||||
|
func (ce *ContainerEnv) Raw() []string {
|
||||||
|
var ret []string
|
||||||
|
for k, v := range ce.EnvVars {
|
||||||
|
ret = append(ret, k+"="+v)
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = append(ret, ce.VarsToExport...)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
// NewContainerEnv returns a pointer to a new ContainerEnv
|
// NewContainerEnv returns a pointer to a new ContainerEnv
|
||||||
func NewContainerEnv() *ContainerEnv {
|
func NewContainerEnv() *ContainerEnv {
|
||||||
var ce ContainerEnv
|
var ce ContainerEnv
|
||||||
@@ -162,11 +174,8 @@ type ContainerSpec struct {
|
|||||||
// User is the username/uid that application runs as in continer
|
// User is the username/uid that application runs as in continer
|
||||||
User ContainerUser `json:"user,omitempty" yaml:"user,omitempty"`
|
User ContainerUser `json:"user,omitempty" yaml:"user,omitempty"`
|
||||||
|
|
||||||
// EnvRaw is a slice of env string.
|
// Env is a slice of env string that will be exposed to container
|
||||||
EnvRaw []string `json:"envs,omitempty" yaml:"envs,omitempty"`
|
Env []string `json:"envs,omitempty" yaml:"envs,omitempty"`
|
||||||
|
|
||||||
// Env contains environment variables that will be exported to container
|
|
||||||
Env ContainerEnv `json:"-" yaml:"-"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ContainerNetwork
|
// ContainerNetwork
|
||||||
@@ -230,7 +239,6 @@ func GetFunctionSpec(n *yaml.RNode) *FunctionSpec {
|
|||||||
if fn := getFunctionSpecFromAnnotation(n, meta); fn != nil {
|
if fn := getFunctionSpecFromAnnotation(n, meta); fn != nil {
|
||||||
fn.Container.Network.Name = NetworkNameEmpty
|
fn.Container.Network.Name = NetworkNameEmpty
|
||||||
fn.StorageMounts = []StorageMount{}
|
fn.StorageMounts = []StorageMount{}
|
||||||
fn.Container.Env = *NewContainerEnvFromStringSlice(fn.Container.EnvRaw)
|
|
||||||
return fn
|
return fn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1514,6 +1514,6 @@ metadata:
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
fn := GetFunctionSpec(cfg)
|
fn := GetFunctionSpec(cfg)
|
||||||
assert.Equal(t, tc.expected, fn.Container.Env)
|
assert.Equal(t, tc.expected, *NewContainerEnvFromStringSlice(fn.Container.Env))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ type RunFns struct {
|
|||||||
User runtimeutil.ContainerUser
|
User runtimeutil.ContainerUser
|
||||||
|
|
||||||
// Env contains environment variables that will be exported to container
|
// Env contains environment variables that will be exported to container
|
||||||
Env runtimeutil.ContainerEnv
|
Env []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute runs the command
|
// 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
|
// 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.
|
// file (declarative). If they have same key, the imperative value will be respected.
|
||||||
func (r RunFns) mergeContainerEnv(envs runtimeutil.ContainerEnv) runtimeutil.ContainerEnv {
|
func (r RunFns) mergeContainerEnv(envs []string) []string {
|
||||||
for key, value := range r.Env.EnvVars {
|
imperative := runtimeutil.NewContainerEnvFromStringSlice(r.Env)
|
||||||
envs.AddKeyValue(key, value)
|
declarative := runtimeutil.NewContainerEnvFromStringSlice(envs)
|
||||||
|
for key, value := range imperative.EnvVars {
|
||||||
|
declarative.AddKeyValue(key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, key := range r.Env.VarsToExport {
|
for _, key := range imperative.VarsToExport {
|
||||||
envs.AddKey(key)
|
declarative.AddKey(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
return envs
|
return declarative.Raw()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r RunFns) getFunctionFilters(global bool, fns ...*yaml.RNode) (
|
func (r RunFns) getFunctionFilters(global bool, fns ...*yaml.RNode) (
|
||||||
|
|||||||
@@ -991,43 +991,41 @@ func TestRunfns_mergeContainerEnv(t *testing.T) {
|
|||||||
testcases := []struct {
|
testcases := []struct {
|
||||||
name string
|
name string
|
||||||
instance RunFns
|
instance RunFns
|
||||||
inputEnvs runtimeutil.ContainerEnv
|
inputEnvs []string
|
||||||
expect runtimeutil.ContainerEnv
|
expect runtimeutil.ContainerEnv
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "all empty",
|
name: "all empty",
|
||||||
instance: RunFns{},
|
instance: RunFns{},
|
||||||
inputEnvs: *runtimeutil.NewContainerEnv(),
|
|
||||||
expect: *runtimeutil.NewContainerEnv(),
|
expect: *runtimeutil.NewContainerEnv(),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "empty command line envs",
|
name: "empty command line envs",
|
||||||
instance: RunFns{},
|
instance: RunFns{},
|
||||||
inputEnvs: *runtimeutil.NewContainerEnvFromStringSlice([]string{"foo=bar"}),
|
inputEnvs: []string{"foo=bar"},
|
||||||
expect: *runtimeutil.NewContainerEnvFromStringSlice([]string{"foo=bar"}),
|
expect: *runtimeutil.NewContainerEnvFromStringSlice([]string{"foo=bar"}),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "empty declarative envs",
|
name: "empty declarative envs",
|
||||||
instance: RunFns{
|
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",
|
name: "same key",
|
||||||
instance: RunFns{
|
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"}),
|
expect: *runtimeutil.NewContainerEnvFromStringSlice([]string{"foo=bar", "bar", "foo"}),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "same exported key",
|
name: "same exported key",
|
||||||
instance: RunFns{
|
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"}),
|
expect: *runtimeutil.NewContainerEnvFromStringSlice([]string{"foo=bar", "foo1=bar1", "foo"}),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -1036,7 +1034,7 @@ func TestRunfns_mergeContainerEnv(t *testing.T) {
|
|||||||
tc := testcases[i]
|
tc := testcases[i]
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
envs := tc.instance.mergeContainerEnv(tc.inputEnvs)
|
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