Fix empty environment variables
This commit is contained in:
@@ -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) {
|
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 {
|
if env.ValueFrom != nil {
|
||||||
return getEnvironmentVariableValueWithValueFrom(ctx, env, mappingFunc, pod, container, rm, recorder)
|
return getEnvironmentVariableValueWithValueFrom(ctx, env, mappingFunc, pod, container, rm, recorder)
|
||||||
}
|
}
|
||||||
|
// Handle values that have been directly provided after expanding variable references.
|
||||||
log.G(ctx).WithField("env", env).Error("Unhandled environment variable, do not know how to populate")
|
return pointer.StringPtr(expansion.Expand(env.Value, mappingFunc)), nil
|
||||||
return nil, 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) {
|
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) {
|
||||||
|
|||||||
@@ -1090,3 +1090,45 @@ func TestComposingEnv(t *testing.T) {
|
|||||||
// Make sure that no events have been recorded.
|
// Make sure that no events have been recorded.
|
||||||
assert.Check(t, is.Len(er.Events, 0))
|
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))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user