Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation cleanup part 1 #17245

Merged
merged 5 commits into from Nov 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/api/errors/errors.go
Expand Up @@ -25,7 +25,7 @@ import (
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/runtime"
utilerrors "k8s.io/kubernetes/pkg/util/errors"
"k8s.io/kubernetes/pkg/util/fielderrors"
"k8s.io/kubernetes/pkg/util/validation"
)

// HTTP Status codes not in the golang http package.
Expand Down Expand Up @@ -159,10 +159,10 @@ func NewConflict(kind, name string, err error) error {
}

// NewInvalid returns an error indicating the item is invalid and cannot be processed.
func NewInvalid(kind, name string, errs fielderrors.ValidationErrorList) error {
func NewInvalid(kind, name string, errs validation.ErrorList) error {
causes := make([]unversioned.StatusCause, 0, len(errs))
for i := range errs {
if err, ok := errs[i].(*fielderrors.ValidationError); ok {
if err, ok := errs[i].(*validation.Error); ok {
causes = append(causes, unversioned.StatusCause{
Type: unversioned.CauseType(err.Type),
Message: err.ErrorBody(),
Expand Down
16 changes: 8 additions & 8 deletions pkg/api/errors/errors_test.go
Expand Up @@ -24,7 +24,7 @@ import (

"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/fielderrors"
"k8s.io/kubernetes/pkg/util/validation"
)

func TestErrorNew(t *testing.T) {
Expand Down Expand Up @@ -88,11 +88,11 @@ func TestErrorNew(t *testing.T) {

func TestNewInvalid(t *testing.T) {
testCases := []struct {
Err *fielderrors.ValidationError
Err *validation.Error
Details *unversioned.StatusDetails
}{
{
fielderrors.NewFieldDuplicate("field[0].name", "bar"),
validation.NewFieldDuplicate("field[0].name", "bar"),
&unversioned.StatusDetails{
Kind: "kind",
Name: "name",
Expand All @@ -103,7 +103,7 @@ func TestNewInvalid(t *testing.T) {
},
},
{
fielderrors.NewFieldInvalid("field[0].name", "bar", "detail"),
validation.NewFieldInvalid("field[0].name", "bar", "detail"),
&unversioned.StatusDetails{
Kind: "kind",
Name: "name",
Expand All @@ -114,7 +114,7 @@ func TestNewInvalid(t *testing.T) {
},
},
{
fielderrors.NewFieldNotFound("field[0].name", "bar"),
validation.NewFieldNotFound("field[0].name", "bar"),
&unversioned.StatusDetails{
Kind: "kind",
Name: "name",
Expand All @@ -125,7 +125,7 @@ func TestNewInvalid(t *testing.T) {
},
},
{
fielderrors.NewFieldValueNotSupported("field[0].name", "bar", nil),
validation.NewFieldValueNotSupported("field[0].name", "bar", nil),
&unversioned.StatusDetails{
Kind: "kind",
Name: "name",
Expand All @@ -136,7 +136,7 @@ func TestNewInvalid(t *testing.T) {
},
},
{
fielderrors.NewFieldRequired("field[0].name"),
validation.NewFieldRequired("field[0].name"),
&unversioned.StatusDetails{
Kind: "kind",
Name: "name",
Expand All @@ -150,7 +150,7 @@ func TestNewInvalid(t *testing.T) {
for i, testCase := range testCases {
vErr, expected := testCase.Err, testCase.Details
expected.Causes[0].Message = vErr.ErrorBody()
err := NewInvalid("kind", "name", fielderrors.ValidationErrorList{vErr})
err := NewInvalid("kind", "name", validation.ErrorList{vErr})
status := err.(*StatusError).ErrStatus
if status.Code != 422 || status.Reason != unversioned.StatusReasonInvalid {
t.Errorf("%d: unexpected status: %#v", i, status)
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/rest/create.go
Expand Up @@ -21,7 +21,7 @@ import (
"k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/api/validation"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/fielderrors"
utilvalidation "k8s.io/kubernetes/pkg/util/validation"
)

// RESTCreateStrategy defines the minimum validation, accepted input, and
Expand All @@ -42,7 +42,7 @@ type RESTCreateStrategy interface {
PrepareForCreate(obj runtime.Object)
// Validate is invoked after default fields in the object have been filled in before
// the object is persisted. This method should not mutate the object.
Validate(ctx api.Context, obj runtime.Object) fielderrors.ValidationErrorList
Validate(ctx api.Context, obj runtime.Object) utilvalidation.ErrorList
// Canonicalize is invoked after validation has succeeded but before the
// object has been persisted. This method may mutate the object.
Canonicalize(obj runtime.Object)
Expand Down
8 changes: 4 additions & 4 deletions pkg/api/rest/update.go
Expand Up @@ -21,7 +21,7 @@ import (
"k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/api/validation"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/fielderrors"
utilvalidation "k8s.io/kubernetes/pkg/util/validation"
)

// RESTUpdateStrategy defines the minimum validation, accepted input, and
Expand All @@ -42,7 +42,7 @@ type RESTUpdateStrategy interface {
// ValidateUpdate is invoked after default fields in the object have been
// filled in before the object is persisted. This method should not mutate
// the object.
ValidateUpdate(ctx api.Context, obj, old runtime.Object) fielderrors.ValidationErrorList
ValidateUpdate(ctx api.Context, obj, old runtime.Object) utilvalidation.ErrorList
// Canonicalize is invoked after validation has succeeded but before the
// object has been persisted. This method may mutate the object.
Canonicalize(obj runtime.Object)
Expand All @@ -53,8 +53,8 @@ type RESTUpdateStrategy interface {
}

// TODO: add other common fields that require global validation.
func validateCommonFields(obj, old runtime.Object) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
func validateCommonFields(obj, old runtime.Object) utilvalidation.ErrorList {
allErrs := utilvalidation.ErrorList{}
objectMeta, err := api.ObjectMetaFor(obj)
if err != nil {
return append(allErrs, errors.NewInternalError(err))
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/testing/compat/compatibility_tester.go
Expand Up @@ -28,7 +28,7 @@ import (

"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/fielderrors"
"k8s.io/kubernetes/pkg/util/validation"

"k8s.io/kubernetes/pkg/kubectl"
)
Expand All @@ -42,7 +42,7 @@ func TestCompatibility(
t *testing.T,
version string,
input []byte,
validator func(obj runtime.Object) fielderrors.ValidationErrorList,
validator func(obj runtime.Object) validation.ErrorList,
expectedKeys map[string]string,
absentKeys []string,
) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/v1/backward_compatibility_test.go
Expand Up @@ -23,7 +23,7 @@ import (
"k8s.io/kubernetes/pkg/api/testing/compat"
"k8s.io/kubernetes/pkg/api/validation"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/fielderrors"
utilvalidation "k8s.io/kubernetes/pkg/util/validation"
)

func TestCompatibility_v1_PodSecurityContext(t *testing.T) {
Expand Down Expand Up @@ -217,7 +217,7 @@ func TestCompatibility_v1_PodSecurityContext(t *testing.T) {
},
}

validator := func(obj runtime.Object) fielderrors.ValidationErrorList {
validator := func(obj runtime.Object) utilvalidation.ErrorList {
return validation.ValidatePodSpec(&(obj.(*api.Pod).Spec))
}

Expand Down
9 changes: 4 additions & 5 deletions pkg/api/validation/events.go
Expand Up @@ -18,20 +18,19 @@ package validation

import (
"k8s.io/kubernetes/pkg/api"
errs "k8s.io/kubernetes/pkg/util/fielderrors"
"k8s.io/kubernetes/pkg/util/validation"
)

// ValidateEvent makes sure that the event makes sense.
func ValidateEvent(event *api.Event) errs.ValidationErrorList {
allErrs := errs.ValidationErrorList{}
func ValidateEvent(event *api.Event) validation.ErrorList {
allErrs := validation.ErrorList{}
// TODO: There is no namespace required for node.
if event.InvolvedObject.Kind != "Node" &&
event.Namespace != event.InvolvedObject.Namespace {
allErrs = append(allErrs, errs.NewFieldInvalid("involvedObject.namespace", event.InvolvedObject.Namespace, "namespace does not match involvedObject"))
allErrs = append(allErrs, validation.NewFieldInvalid("involvedObject.namespace", event.InvolvedObject.Namespace, "namespace does not match involvedObject"))
}
if !validation.IsDNS1123Subdomain(event.Namespace) {
allErrs = append(allErrs, errs.NewFieldInvalid("namespace", event.Namespace, ""))
allErrs = append(allErrs, validation.NewFieldInvalid("namespace", event.Namespace, ""))
}
return allErrs
}
14 changes: 7 additions & 7 deletions pkg/api/validation/schema.go
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/golang/glog"
apiutil "k8s.io/kubernetes/pkg/api/util"
utilerrors "k8s.io/kubernetes/pkg/util/errors"
errs "k8s.io/kubernetes/pkg/util/fielderrors"
"k8s.io/kubernetes/pkg/util/validation"
"k8s.io/kubernetes/pkg/util/yaml"
)

Expand Down Expand Up @@ -70,8 +70,8 @@ func NewSwaggerSchemaFromBytes(data []byte) (Schema, error) {
// validateList unpack a list and validate every item in the list.
// It return nil if every item is ok.
// Otherwise it return an error list contain errors of every item.
func (s *SwaggerSchema) validateList(obj map[string]interface{}) errs.ValidationErrorList {
allErrs := errs.ValidationErrorList{}
func (s *SwaggerSchema) validateList(obj map[string]interface{}) validation.ErrorList {
allErrs := validation.ErrorList{}
items, exists := obj["items"]
if !exists {
return append(allErrs, fmt.Errorf("no items field in %#v", obj))
Expand Down Expand Up @@ -160,8 +160,8 @@ func (s *SwaggerSchema) ValidateBytes(data []byte) error {
return utilerrors.NewAggregate(allErrs)
}

func (s *SwaggerSchema) ValidateObject(obj interface{}, fieldName, typeName string) errs.ValidationErrorList {
allErrs := errs.ValidationErrorList{}
func (s *SwaggerSchema) ValidateObject(obj interface{}, fieldName, typeName string) validation.ErrorList {
allErrs := validation.ErrorList{}
models := s.api.Models
model, ok := models.At(typeName)
if !ok {
Expand Down Expand Up @@ -215,7 +215,7 @@ func (s *SwaggerSchema) ValidateObject(obj interface{}, fieldName, typeName stri
// This matches type name in the swagger spec, such as "v1.Binding".
var versionRegexp = regexp.MustCompile(`^v.+\..*`)

func (s *SwaggerSchema) validateField(value interface{}, fieldName, fieldType string, fieldDetails *swagger.ModelProperty) errs.ValidationErrorList {
func (s *SwaggerSchema) validateField(value interface{}, fieldName, fieldType string, fieldDetails *swagger.ModelProperty) validation.ErrorList {
// TODO: caesarxuchao: because we have multiple group/versions and objects
// may reference objects in other group, the commented out way of checking
// if a filedType is a type defined by us is outdated. We use a hacky way
Expand All @@ -229,7 +229,7 @@ func (s *SwaggerSchema) validateField(value interface{}, fieldName, fieldType st
// if strings.HasPrefix(fieldType, apiVersion) {
return s.ValidateObject(value, fieldName, fieldType)
}
allErrs := errs.ValidationErrorList{}
allErrs := validation.ErrorList{}
switch fieldType {
case "string":
// Be loose about what we accept for 'string' since we use IntOrString in a couple of places
Expand Down