play kube: prepare supporting other env source than config maps

Rework envVarsFromConfigMap() and envVarValue() to simplify supporting
other env sources than config maps. For this we pass the whole spec
generator options struct as parameter instead of just the config maps
list. Then we rename envVarsFromConfigMap() to envVarsFrom() and in
envVarValue() we reposition the loop over the config maps to only run
it when a configMapRef element exists.

Signed-off-by: Alban Bedel <albeu@free.fr>
This commit is contained in:
Alban Bedel
2021-03-27 20:27:58 +01:00
parent ec47312eeb
commit 9f92b8b0d8
2 changed files with 41 additions and 27 deletions

View File

@ -210,12 +210,12 @@ func ToSpecGen(ctx context.Context, opts *CtrSpecGenOptions) (*specgen.SpecGener
}
for _, env := range opts.Container.Env {
value := envVarValue(env, opts.ConfigMaps)
value := envVarValue(env, opts)
envs[env.Name] = value
}
for _, envFrom := range opts.Container.EnvFrom {
cmEnvs := envVarsFromConfigMap(envFrom, opts.ConfigMaps)
cmEnvs := envVarsFrom(envFrom, opts)
for k, v := range cmEnvs {
envs[k] = v
@ -325,14 +325,14 @@ func quantityToInt64(quantity *resource.Quantity) (int64, error) {
return 0, errors.Errorf("Quantity cannot be represented as int64: %v", quantity)
}
// envVarsFromConfigMap returns all key-value pairs as env vars from a configMap that matches the envFrom setting of a container
func envVarsFromConfigMap(envFrom v1.EnvFromSource, configMaps []v1.ConfigMap) map[string]string {
// envVarsFrom returns all key-value pairs as env vars from a configMap that matches the envFrom setting of a container
func envVarsFrom(envFrom v1.EnvFromSource, opts *CtrSpecGenOptions) map[string]string {
envs := map[string]string{}
if envFrom.ConfigMapRef != nil {
cmName := envFrom.ConfigMapRef.Name
for _, c := range configMaps {
for _, c := range opts.ConfigMaps {
if cmName == c.Name {
envs = c.Data
break
@ -345,10 +345,10 @@ func envVarsFromConfigMap(envFrom v1.EnvFromSource, configMaps []v1.ConfigMap) m
// envVarValue returns the environment variable value configured within the container's env setting.
// It gets the value from a configMap if specified, otherwise returns env.Value
func envVarValue(env v1.EnvVar, configMaps []v1.ConfigMap) string {
for _, c := range configMaps {
if env.ValueFrom != nil {
if env.ValueFrom.ConfigMapKeyRef != nil {
func envVarValue(env v1.EnvVar, opts *CtrSpecGenOptions) string {
if env.ValueFrom != nil {
if env.ValueFrom.ConfigMapKeyRef != nil {
for _, c := range opts.ConfigMaps {
if env.ValueFrom.ConfigMapKeyRef.Name == c.Name {
if value, ok := c.Data[env.ValueFrom.ConfigMapKeyRef.Key]; ok {
return value

View File

@ -8,12 +8,12 @@ import (
v12 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestEnvVarsFromConfigMap(t *testing.T) {
func TestEnvVarsFrom(t *testing.T) {
tests := []struct {
name string
envFrom v1.EnvFromSource
configMapList []v1.ConfigMap
expected map[string]string
name string
envFrom v1.EnvFromSource
options CtrSpecGenOptions
expected map[string]string
}{
{
"ConfigMapExists",
@ -24,7 +24,9 @@ func TestEnvVarsFromConfigMap(t *testing.T) {
},
},
},
configMapList,
CtrSpecGenOptions{
ConfigMaps: configMapList,
},
map[string]string{
"myvar": "foo",
},
@ -38,7 +40,9 @@ func TestEnvVarsFromConfigMap(t *testing.T) {
},
},
},
configMapList,
CtrSpecGenOptions{
ConfigMaps: configMapList,
},
map[string]string{},
},
{
@ -50,7 +54,9 @@ func TestEnvVarsFromConfigMap(t *testing.T) {
},
},
},
[]v1.ConfigMap{},
CtrSpecGenOptions{
ConfigMaps: []v1.ConfigMap{},
},
map[string]string{},
},
}
@ -58,7 +64,7 @@ func TestEnvVarsFromConfigMap(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
result := envVarsFromConfigMap(test.envFrom, test.configMapList)
result := envVarsFrom(test.envFrom, &test.options)
assert.Equal(t, test.expected, result)
})
}
@ -66,10 +72,10 @@ func TestEnvVarsFromConfigMap(t *testing.T) {
func TestEnvVarValue(t *testing.T) {
tests := []struct {
name string
envVar v1.EnvVar
configMapList []v1.ConfigMap
expected string
name string
envVar v1.EnvVar
options CtrSpecGenOptions
expected string
}{
{
"ConfigMapExists",
@ -84,7 +90,9 @@ func TestEnvVarValue(t *testing.T) {
},
},
},
configMapList,
CtrSpecGenOptions{
ConfigMaps: configMapList,
},
"foo",
},
{
@ -100,7 +108,9 @@ func TestEnvVarValue(t *testing.T) {
},
},
},
configMapList,
CtrSpecGenOptions{
ConfigMaps: configMapList,
},
"",
},
{
@ -116,7 +126,9 @@ func TestEnvVarValue(t *testing.T) {
},
},
},
configMapList,
CtrSpecGenOptions{
ConfigMaps: configMapList,
},
"",
},
{
@ -132,7 +144,9 @@ func TestEnvVarValue(t *testing.T) {
},
},
},
[]v1.ConfigMap{},
CtrSpecGenOptions{
ConfigMaps: []v1.ConfigMap{},
},
"",
},
}
@ -140,7 +154,7 @@ func TestEnvVarValue(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
result := envVarValue(test.envVar, test.configMapList)
result := envVarValue(test.envVar, &test.options)
assert.Equal(t, test.expected, result)
})
}