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:
@@ -5,8 +5,6 @@ package container
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
runtimeexec "sigs.k8s.io/kustomize/kyaml/fn/runtime/exec"
|
||||
"sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil"
|
||||
@@ -177,19 +175,7 @@ func (c *Filter) getCommand() (string, []string) {
|
||||
args = append(args, "--mount", storageMount.String())
|
||||
}
|
||||
|
||||
// TODO: put these env processes into a separate function and call it in the outside of
|
||||
// getCommand
|
||||
os.Setenv("LOG_TO_STDERR", "true")
|
||||
os.Setenv("STRUCTURED_RESULTS", "true")
|
||||
|
||||
// export the local environment vars to the container
|
||||
for _, pair := range os.Environ() {
|
||||
items := strings.Split(pair, "=")
|
||||
if items[0] == "" || items[1] == "" || items[0] == tmpDirEnvKey {
|
||||
continue
|
||||
}
|
||||
args = append(args, "-e", items[0])
|
||||
}
|
||||
args = append(args, c.Envs.GetDockerFlags()...)
|
||||
a := append(args, c.Image)
|
||||
return "docker", a
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -137,20 +136,13 @@ metadata:
|
||||
}
|
||||
tt.instance.Exec.FunctionConfig = cfg
|
||||
|
||||
os.Setenv("KYAML_TEST", "FOO")
|
||||
tt.instance.setupExec()
|
||||
tt.instance.Envs.AddKeyValue("KYAML_TEST", "FOO")
|
||||
tt.expectedArgs = append(tt.expectedArgs, tt.instance.Envs.GetDockerFlags()...)
|
||||
|
||||
// configure expected env
|
||||
for _, e := range os.Environ() {
|
||||
// the process env
|
||||
parts := strings.Split(e, "=")
|
||||
if parts[0] == "" || parts[1] == "" || parts[0] == tmpDirEnvKey {
|
||||
continue
|
||||
}
|
||||
tt.expectedArgs = append(tt.expectedArgs, "-e", parts[0])
|
||||
}
|
||||
tt.expectedArgs = append(tt.expectedArgs, tt.instance.Image)
|
||||
|
||||
tt.instance.setupExec()
|
||||
|
||||
if !assert.Equal(t, "docker", tt.instance.Exec.Path) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ package runtimeutil
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
@@ -40,6 +41,77 @@ const (
|
||||
NetworkNameNone ContainerNetworkName = "none"
|
||||
NetworkNameEmpty ContainerNetworkName = ""
|
||||
)
|
||||
const defaultEnvValue string = "true"
|
||||
|
||||
// ContainerEnvs contains the environment variables for the container
|
||||
type ContainerEnvs struct {
|
||||
// EnvsMap is a key-value map that will be set as env in container
|
||||
EnvsMap map[string]string `json:"envsMap,omitempty" yaml:"envsMap,omitempty"`
|
||||
|
||||
// ExportKeys are only env key. Value will be the value in the host system
|
||||
ExportKeys []string `json:"exportKeys,omitempty" yaml:"exportKeys,omitempty"`
|
||||
}
|
||||
|
||||
// GetDockerFlags returns docker run style env flags
|
||||
func (ce *ContainerEnvs) GetDockerFlags() []string {
|
||||
envs := ce.EnvsMap
|
||||
if envs == nil {
|
||||
envs = make(map[string]string)
|
||||
}
|
||||
|
||||
// default envs
|
||||
envs["LOG_TO_STDERR"] = defaultEnvValue
|
||||
envs["STRUCTURED_RESULTS"] = defaultEnvValue
|
||||
|
||||
flags := []string{}
|
||||
// return in order to keep consistent among different runs
|
||||
keys := []string{}
|
||||
for k := range envs {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, key := range keys {
|
||||
flags = append(flags, "-e", key+"="+envs[key])
|
||||
}
|
||||
|
||||
for _, key := range ce.ExportKeys {
|
||||
flags = append(flags, "-e", key)
|
||||
}
|
||||
|
||||
return flags
|
||||
}
|
||||
|
||||
// AddKeyValue adds a key-value pair into the envs
|
||||
func (ce *ContainerEnvs) AddKeyValue(key, value string) {
|
||||
if ce.EnvsMap == nil {
|
||||
ce.EnvsMap = make(map[string]string)
|
||||
}
|
||||
ce.EnvsMap[key] = value
|
||||
}
|
||||
|
||||
// HasExportedKey returns true if the key is a exported key
|
||||
func (ce *ContainerEnvs) HasExportedKey(key string) bool {
|
||||
for _, k := range ce.ExportKeys {
|
||||
if k == key {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// AddKey adds a key into the envs
|
||||
func (ce *ContainerEnvs) AddKey(key string) {
|
||||
if !ce.HasExportedKey(key) {
|
||||
ce.ExportKeys = append(ce.ExportKeys, key)
|
||||
}
|
||||
}
|
||||
|
||||
// NewContainerEnvs returns an empty instance of ContainerEnvs
|
||||
func NewContainerEnvs() ContainerEnvs {
|
||||
var ce ContainerEnvs
|
||||
ce.EnvsMap = make(map[string]string)
|
||||
return ce
|
||||
}
|
||||
|
||||
// FunctionSpec defines a spec for running a function
|
||||
type FunctionSpec struct {
|
||||
@@ -75,6 +147,9 @@ type ContainerSpec struct {
|
||||
|
||||
// User is the username/uid that application runs as in continer
|
||||
User ContainerUser `json:"user,omitempty" yaml:"user,omitempty"`
|
||||
|
||||
// Envs are environment variables that will be exported to container
|
||||
Envs ContainerEnvs `json:"envs,omitempty" yaml:"envs,omitempty"`
|
||||
}
|
||||
|
||||
// ContainerNetwork
|
||||
|
||||
@@ -1428,3 +1428,43 @@ func Test_StringToStorageMount(t *testing.T) {
|
||||
assert.Equal(t, tc.expectedOut, (&s).String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerEnvs(t *testing.T) {
|
||||
tests := []struct {
|
||||
input ContainerEnvs
|
||||
output []string
|
||||
}{
|
||||
{
|
||||
input: ContainerEnvs{
|
||||
EnvsMap: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
output: []string{"-e", "LOG_TO_STDERR=true", "-e", "STRUCTURED_RESULTS=true", "-e", "foo=bar"},
|
||||
},
|
||||
{
|
||||
input: ContainerEnvs{
|
||||
ExportKeys: []string{"foo"},
|
||||
},
|
||||
output: []string{"-e", "LOG_TO_STDERR=true", "-e", "STRUCTURED_RESULTS=true", "-e", "foo"},
|
||||
},
|
||||
{
|
||||
input: ContainerEnvs{
|
||||
EnvsMap: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
ExportKeys: []string{"baz"},
|
||||
},
|
||||
output: []string{"-e", "LOG_TO_STDERR=true", "-e", "STRUCTURED_RESULTS=true", "-e", "foo=bar", "-e", "baz"},
|
||||
},
|
||||
{
|
||||
input: ContainerEnvs{},
|
||||
output: []string{"-e", "LOG_TO_STDERR=true", "-e", "STRUCTURED_RESULTS=true"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
flags := tc.input.GetDockerFlags()
|
||||
assert.Equal(t, tc.output, flags)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user