Fix empty environment variables

This commit is contained in:
Sargun Dhillon
2020-11-13 10:36:17 -08:00
parent 9883707707
commit af77bc8364
2 changed files with 44 additions and 9 deletions

View File

@@ -334,18 +334,11 @@ func makeEnvironmentMap(ctx context.Context, pod *corev1.Pod, container *corev1.
}
func getEnvironmentVariableValue(ctx context.Context, env *corev1.EnvVar, mappingFunc func(string) string, pod *corev1.Pod, container *corev1.Container, rm *manager.ResourceManager, recorder record.EventRecorder) (*string, error) {
// Handle values that have been directly provided.
if env.Value != "" {
// Expand variable references
return pointer.StringPtr(expansion.Expand(env.Value, mappingFunc)), nil
}
if env.ValueFrom != nil {
return getEnvironmentVariableValueWithValueFrom(ctx, env, mappingFunc, pod, container, rm, recorder)
}
log.G(ctx).WithField("env", env).Error("Unhandled environment variable, do not know how to populate")
return nil, nil
// Handle values that have been directly provided after expanding variable references.
return pointer.StringPtr(expansion.Expand(env.Value, mappingFunc)), nil
}
func getEnvironmentVariableValueWithValueFrom(ctx context.Context, env *corev1.EnvVar, mappingFunc func(string) string, pod *corev1.Pod, container *corev1.Container, rm *manager.ResourceManager, recorder record.EventRecorder) (*string, error) {

View File

@@ -1090,3 +1090,45 @@ func TestComposingEnv(t *testing.T) {
// Make sure that no events have been recorded.
assert.Check(t, is.Len(er.Events, 0))
}
// TestEmptyEnvVar tests that env var can be have the value ""
func TestEmptyEnvVar(t *testing.T) {
rm := testutil.FakeResourceManager()
er := testutil.FakeEventRecorder(defaultEventRecorderBufferSize)
// Create a pod object having a single container.
// The container's third environment variable is composed of the previous two.
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: "pod-0",
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Env: []corev1.EnvVar{
{
Name: envVarName1,
},
},
},
},
},
}
// Populate the pods's environment.
err := PopulateEnvironmentVariables(context.Background(), pod, rm, er)
assert.Check(t, err)
// Make sure that the container's environment contains all the expected keys and values.
assert.Check(t, is.DeepEqual(pod.Spec.Containers[0].Env, []corev1.EnvVar{
{
Name: envVarName1,
},
},
sortOpt,
))
// Make sure that no events have been recorded.
assert.Check(t, is.Len(er.Events, 0))
}