diff --git a/pkg/api/validation/validation.go b/pkg/api/validation/validation.go index 5459c6fda5e..e7783a5c8e2 100644 --- a/pkg/api/validation/validation.go +++ b/pkg/api/validation/validation.go @@ -1374,11 +1374,13 @@ func validateEnvVarValueFrom(ev api.EnvVar, fldPath *field.Path) field.ErrorList allErrs = append(allErrs, validateSecretKeySelector(ev.ValueFrom.SecretKeyRef, fldPath.Child("secretKeyRef"))...) } - if len(ev.Value) != 0 { + if numSources == 0 { + allErrs = append(allErrs, field.Invalid(fldPath, "", "must specify one of: `fieldRef`, `resourceFieldRef`, `configMapKeyRef` or `secretKeyRef`")) + } else if len(ev.Value) != 0 { if numSources != 0 { allErrs = append(allErrs, field.Invalid(fldPath, "", "may not be specified when `value` is not empty")) } - } else if numSources != 1 { + } else if numSources > 1 { allErrs = append(allErrs, field.Invalid(fldPath, "", "may not have more than one field specified at a time")) } diff --git a/pkg/api/validation/validation_test.go b/pkg/api/validation/validation_test.go index e91ee13d64f..094a4a0d19a 100644 --- a/pkg/api/validation/validation_test.go +++ b/pkg/api/validation/validation_test.go @@ -2446,6 +2446,14 @@ func TestValidateEnv(t *testing.T) { }}, expectedError: "[0].valueFrom: Invalid value: \"\": may not be specified when `value` is not empty", }, + { + name: "valueFrom without a source", + envs: []api.EnvVar{{ + Name: "abc", + ValueFrom: &api.EnvVarSource{}, + }}, + expectedError: "[0].valueFrom: Invalid value: \"\": must specify one of: `fieldRef`, `resourceFieldRef`, `configMapKeyRef` or `secretKeyRef`", + }, { name: "valueFrom.fieldRef and valueFrom.secretKeyRef specified", envs: []api.EnvVar{{