Return all errors in alwayspullimages.Validate()

This commit is contained in:
Lee Verberne 2019-06-25 18:11:51 +00:00
parent 1215aa73d2
commit bd5f4117e5
2 changed files with 9 additions and 3 deletions

View File

@ -28,6 +28,7 @@ import (
"io"
apierrors "k8s.io/apimachinery/pkg/api/errors"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/apiserver/pkg/admission"
api "k8s.io/kubernetes/pkg/apis/core"
@ -91,8 +92,7 @@ func (*AlwaysPullImages) Validate(attributes admission.Attributes, o admission.O
}
})
if len(allErrs) > 0 {
// TODO: consider using utilerrors.NewAggregate(allErrs)
return allErrs[0]
return utilerrors.NewAggregate(allErrs)
}
return nil

View File

@ -84,7 +84,13 @@ func TestValidate(t *testing.T) {
},
},
}
expectedError := `pods "123" is forbidden: spec.initContainers[0].imagePullPolicy: Unsupported value: "": supported values: "Always"`
expectedError := `[` +
`pods "123" is forbidden: spec.initContainers[0].imagePullPolicy: Unsupported value: "": supported values: "Always", ` +
`pods "123" is forbidden: spec.initContainers[1].imagePullPolicy: Unsupported value: "Never": supported values: "Always", ` +
`pods "123" is forbidden: spec.initContainers[2].imagePullPolicy: Unsupported value: "IfNotPresent": supported values: "Always", ` +
`pods "123" is forbidden: spec.containers[0].imagePullPolicy: Unsupported value: "": supported values: "Always", ` +
`pods "123" is forbidden: spec.containers[1].imagePullPolicy: Unsupported value: "Never": supported values: "Always", ` +
`pods "123" is forbidden: spec.containers[2].imagePullPolicy: Unsupported value: "IfNotPresent": supported values: "Always"]`
err := handler.Validate(admission.NewAttributesRecord(&pod, nil, api.Kind("Pod").WithVersion("version"), pod.Namespace, pod.Name, api.Resource("pods").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil), nil)
if err == nil {
t.Fatal("missing expected error")