From d8ec7181b45030d9201bf4cb29ea6295c64185d2 Mon Sep 17 00:00:00 2001 From: Marko Luksa Date: Tue, 8 Nov 2016 17:48:12 +0100 Subject: [PATCH 1/2] Improved validation error message when env.valueFrom contains no (or misspelled) ref --- pkg/api/validation/validation.go | 6 ++++-- pkg/api/validation/validation_test.go | 9 +++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/pkg/api/validation/validation.go b/pkg/api/validation/validation.go index 412b5970607..c228c797985 100644 --- a/pkg/api/validation/validation.go +++ b/pkg/api/validation/validation.go @@ -1350,11 +1350,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 1c270bd529b..721fea49920 100644 --- a/pkg/api/validation/validation_test.go +++ b/pkg/api/validation/validation_test.go @@ -2446,6 +2446,15 @@ 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{{ From 389478f3122df0b75fba97c83bbd458c6e78fb6c Mon Sep 17 00:00:00 2001 From: Marko Luksa Date: Tue, 15 Nov 2016 10:36:34 +0100 Subject: [PATCH 2/2] Improved validation error message when env.valueFrom contains no (or misspelled) ref --- pkg/api/validation/validation_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/api/validation/validation_test.go b/pkg/api/validation/validation_test.go index 721fea49920..85ff0f8c594 100644 --- a/pkg/api/validation/validation_test.go +++ b/pkg/api/validation/validation_test.go @@ -2449,9 +2449,8 @@ func TestValidateEnv(t *testing.T) { { name: "valueFrom without a source", envs: []api.EnvVar{{ - Name: "abc", - ValueFrom: &api.EnvVarSource{ - }, + Name: "abc", + ValueFrom: &api.EnvVarSource{}, }}, expectedError: "[0].valueFrom: Invalid value: \"\": must specify one of: `fieldRef`, `resourceFieldRef`, `configMapKeyRef` or `secretKeyRef`", },