From 7646d730f2efcfff002ce0a8fce5e45eb63ec0e5 Mon Sep 17 00:00:00 2001 From: Victor Agababov Date: Wed, 21 Oct 2020 16:41:32 -0700 Subject: [PATCH] Check multiple entries in the validation. (#367) This is a list and we check all of them to be conformant, but return only first error, which is a departure from what we do in most of the serving. --- pkg/apis/caching/v1alpha1/image_validation.go | 6 +++--- pkg/apis/caching/v1alpha1/image_validation_test.go | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/pkg/apis/caching/v1alpha1/image_validation.go b/pkg/apis/caching/v1alpha1/image_validation.go index 4e644f95..d43d4627 100644 --- a/pkg/apis/caching/v1alpha1/image_validation.go +++ b/pkg/apis/caching/v1alpha1/image_validation.go @@ -30,7 +30,7 @@ func (rt *Image) Validate(ctx context.Context) *apis.FieldError { return rt.Spec.Validate(ctx).ViaField("spec") } -func (rs *ImageSpec) Validate(ctx context.Context) *apis.FieldError { +func (rs *ImageSpec) Validate(ctx context.Context) (errs *apis.FieldError) { if rs.Image == "" { return apis.ErrMissingField("image") } @@ -39,8 +39,8 @@ func (rs *ImageSpec) Validate(ctx context.Context) *apis.FieldError { // https://github.com/google/go-containerregistry/blob/2f3e3e1/pkg/name/ref.go#L41 for index, ips := range rs.ImagePullSecrets { if equality.Semantic.DeepEqual(ips, corev1.LocalObjectReference{}) { - return apis.ErrMissingField(fmt.Sprintf("imagePullSecrets[%d].name", index)) + errs = errs.Also(apis.ErrMissingField(fmt.Sprintf("imagePullSecrets[%d].name", index))) } } - return nil + return errs } diff --git a/pkg/apis/caching/v1alpha1/image_validation_test.go b/pkg/apis/caching/v1alpha1/image_validation_test.go index 5508632b..b17ab103 100644 --- a/pkg/apis/caching/v1alpha1/image_validation_test.go +++ b/pkg/apis/caching/v1alpha1/image_validation_test.go @@ -51,6 +51,18 @@ func TestImageValidation(t *testing.T) { }, }, want: apis.ErrMissingField("spec.imagePullSecrets[0].name"), + }, { + name: "nested multiple spec errors", + r: &Image{ + Spec: ImageSpec{ + Image: "busybox", + ImagePullSecrets: []corev1.LocalObjectReference{{ + Name: "frankie", + }, {}, {}}, + }, + }, + want: apis.ErrMissingField("spec.imagePullSecrets[1].name"). + Also(apis.ErrMissingField("spec.imagePullSecrets[2].name")), }} for _, test := range tests {