Skip to content

Commit

Permalink
Move fielderrors into validation
Browse files Browse the repository at this point in the history
  • Loading branch information
thockin committed Nov 23, 2015
1 parent 3fbf0cb commit 0ff66da
Show file tree
Hide file tree
Showing 42 changed files with 711 additions and 717 deletions.
6 changes: 3 additions & 3 deletions pkg/api/errors/errors.go
Original file line number Diff line number Diff line change
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.ValidationErrorList) 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.ValidationError); 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
Original file line number Diff line number Diff line change
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.ValidationError
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.ValidationErrorList{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
Original file line number Diff line number Diff line change
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.ValidationErrorList
// 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
Original file line number Diff line number Diff line change
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.ValidationErrorList
// 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.ValidationErrorList {
allErrs := utilvalidation.ValidationErrorList{}
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
Original file line number Diff line number Diff line change
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.ValidationErrorList,
expectedKeys map[string]string,
absentKeys []string,
) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/v1/backward_compatibility_test.go
Original file line number Diff line number Diff line change
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.ValidationErrorList {
return validation.ValidatePodSpec(&(obj.(*api.Pod).Spec))
}

Expand Down
9 changes: 4 additions & 5 deletions pkg/api/validation/events.go
Original file line number Diff line number Diff line change
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.ValidationErrorList {
allErrs := validation.ValidationErrorList{}
// 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
Original file line number Diff line number Diff line change
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.ValidationErrorList {
allErrs := validation.ValidationErrorList{}
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.ValidationErrorList {
allErrs := validation.ValidationErrorList{}
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.ValidationErrorList {
// 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.ValidationErrorList{}
switch fieldType {
case "string":
// Be loose about what we accept for 'string' since we use IntOrString in a couple of places
Expand Down
Loading

0 comments on commit 0ff66da

Please sign in to comment.