mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
explicitly specify envs to be exported in function
This commit is contained in:
@@ -90,6 +90,9 @@ type RunFns struct {
|
||||
|
||||
// User username used to run the application in container,
|
||||
User runtimeutil.ContainerUser
|
||||
|
||||
// Envs are environment variables that will be exported to container
|
||||
Envs runtimeutil.ContainerEnvs
|
||||
}
|
||||
|
||||
// Execute runs the command
|
||||
@@ -269,6 +272,20 @@ func (r RunFns) getFunctionsFromFunctions() ([]kio.Filter, error) {
|
||||
return r.getFunctionFilters(true, r.Functions...)
|
||||
}
|
||||
|
||||
// mergeContainerEnvs 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) mergeContainerEnvs(envs runtimeutil.ContainerEnvs) runtimeutil.ContainerEnvs {
|
||||
for key, value := range r.Envs.EnvsMap {
|
||||
envs.AddKeyValue(key, value)
|
||||
}
|
||||
|
||||
for _, key := range r.Envs.ExportKeys {
|
||||
envs.AddKey(key)
|
||||
}
|
||||
|
||||
return envs
|
||||
}
|
||||
|
||||
func (r RunFns) getFunctionFilters(global bool, fns ...*yaml.RNode) (
|
||||
[]kio.Filter, error) {
|
||||
var fltrs []kio.Filter
|
||||
@@ -282,10 +299,11 @@ func (r RunFns) getFunctionFilters(global bool, fns ...*yaml.RNode) (
|
||||
}
|
||||
spec.Container.Network.Name = runtimeutil.ContainerNetworkName(r.NetworkName)
|
||||
}
|
||||
// command line username has higher priority
|
||||
if r.User != "" {
|
||||
// command line username and envs has higher priority
|
||||
if !r.User.IsEmpty() {
|
||||
spec.Container.User = r.User
|
||||
}
|
||||
spec.Container.Envs = r.mergeContainerEnvs(spec.Container.Envs)
|
||||
|
||||
c, err := r.functionFilterProvider(*spec, api)
|
||||
if err != nil {
|
||||
@@ -394,6 +412,7 @@ func (r *RunFns) ffp(spec runtimeutil.FunctionSpec, api *yaml.RNode) (kio.Filter
|
||||
Network: spec.Container.Network,
|
||||
StorageMounts: r.StorageMounts,
|
||||
User: spec.Container.User,
|
||||
Envs: spec.Container.Envs,
|
||||
})
|
||||
cf := &c
|
||||
cf.Exec.FunctionConfig = api
|
||||
|
||||
@@ -986,3 +986,104 @@ func getFilterProvider(t *testing.T) func(runtimeutil.FunctionSpec, *yaml.RNode)
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunfns_mergeContainerEnvs(t *testing.T) {
|
||||
testcases := []struct {
|
||||
name string
|
||||
instance RunFns
|
||||
inputEnvs runtimeutil.ContainerEnvs
|
||||
expect runtimeutil.ContainerEnvs
|
||||
}{
|
||||
{
|
||||
name: "all empty",
|
||||
instance: RunFns{},
|
||||
inputEnvs: runtimeutil.NewContainerEnvs(),
|
||||
expect: runtimeutil.NewContainerEnvs(),
|
||||
},
|
||||
{
|
||||
name: "empty command line envs",
|
||||
instance: RunFns{},
|
||||
inputEnvs: runtimeutil.ContainerEnvs{
|
||||
EnvsMap: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
expect: runtimeutil.ContainerEnvs{
|
||||
EnvsMap: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "empty declarative envs",
|
||||
instance: RunFns{
|
||||
Envs: runtimeutil.ContainerEnvs{
|
||||
EnvsMap: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
},
|
||||
inputEnvs: runtimeutil.NewContainerEnvs(),
|
||||
expect: runtimeutil.ContainerEnvs{
|
||||
EnvsMap: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "same key",
|
||||
instance: RunFns{
|
||||
Envs: runtimeutil.ContainerEnvs{
|
||||
EnvsMap: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
ExportKeys: []string{"foo"},
|
||||
},
|
||||
},
|
||||
inputEnvs: runtimeutil.ContainerEnvs{
|
||||
EnvsMap: map[string]string{
|
||||
"foo": "bar1",
|
||||
},
|
||||
ExportKeys: []string{"bar"},
|
||||
},
|
||||
expect: runtimeutil.ContainerEnvs{
|
||||
EnvsMap: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
ExportKeys: []string{"bar", "foo"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "same exported key",
|
||||
instance: RunFns{
|
||||
Envs: runtimeutil.ContainerEnvs{
|
||||
EnvsMap: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
ExportKeys: []string{"foo"},
|
||||
},
|
||||
},
|
||||
inputEnvs: runtimeutil.ContainerEnvs{
|
||||
EnvsMap: map[string]string{
|
||||
"foo1": "bar1",
|
||||
},
|
||||
ExportKeys: []string{"foo"},
|
||||
},
|
||||
expect: runtimeutil.ContainerEnvs{
|
||||
EnvsMap: map[string]string{
|
||||
"foo": "bar",
|
||||
"foo1": "bar1",
|
||||
},
|
||||
ExportKeys: []string{"foo"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for i := range testcases {
|
||||
tc := testcases[i]
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
envs := tc.instance.mergeContainerEnvs(tc.inputEnvs)
|
||||
assert.Equal(t, tc.expect.GetDockerFlags(), envs.GetDockerFlags())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user