mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-07 19:23:40 +00:00
Merge pull request #10007 from feihujiang/improveCreateSchemaValidate
improve kubectl create --validate, aggregate errors and handle requir…
This commit is contained in:
commit
f02c685532
@ -22,6 +22,8 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/errors"
|
||||||
|
errs "github.com/GoogleCloudPlatform/kubernetes/pkg/util/fielderrors"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/yaml"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/yaml"
|
||||||
"github.com/emicklei/go-restful/swagger"
|
"github.com/emicklei/go-restful/swagger"
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
@ -85,15 +87,20 @@ func (s *SwaggerSchema) ValidateBytes(data []byte) error {
|
|||||||
if kind == nil {
|
if kind == nil {
|
||||||
return fmt.Errorf("kind not set")
|
return fmt.Errorf("kind not set")
|
||||||
}
|
}
|
||||||
return s.ValidateObject(obj, apiVersion.(string), "", apiVersion.(string)+"."+kind.(string))
|
allErrs := s.ValidateObject(obj, apiVersion.(string), "", apiVersion.(string)+"."+kind.(string))
|
||||||
|
if len(allErrs) == 1 {
|
||||||
|
return allErrs[0]
|
||||||
|
}
|
||||||
|
return errors.NewAggregate(allErrs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SwaggerSchema) ValidateObject(obj interface{}, apiVersion, fieldName, typeName string) error {
|
func (s *SwaggerSchema) ValidateObject(obj interface{}, apiVersion, fieldName, typeName string) errs.ValidationErrorList {
|
||||||
|
allErrs := errs.ValidationErrorList{}
|
||||||
models := s.api.Models
|
models := s.api.Models
|
||||||
// TODO: handle required fields here too.
|
// TODO: handle required fields here too.
|
||||||
model, ok := models.At(typeName)
|
model, ok := models.At(typeName)
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("couldn't find type: %s", typeName)
|
return append(allErrs, fmt.Errorf("couldn't find type: %s", typeName))
|
||||||
}
|
}
|
||||||
properties := model.Properties
|
properties := model.Properties
|
||||||
if len(properties.List) == 0 {
|
if len(properties.List) == 0 {
|
||||||
@ -102,11 +109,17 @@ func (s *SwaggerSchema) ValidateObject(obj interface{}, apiVersion, fieldName, t
|
|||||||
}
|
}
|
||||||
fields, ok := obj.(map[string]interface{})
|
fields, ok := obj.(map[string]interface{})
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("expected object of type map[string]interface{} as value of %s field", fieldName)
|
return append(allErrs, fmt.Errorf("field %s: expected object of type map[string]interface{}, but the actual type is %T", fieldName, obj))
|
||||||
}
|
}
|
||||||
if len(fieldName) > 0 {
|
if len(fieldName) > 0 {
|
||||||
fieldName = fieldName + "."
|
fieldName = fieldName + "."
|
||||||
}
|
}
|
||||||
|
//handle required fields
|
||||||
|
for _, requiredKey := range model.Required {
|
||||||
|
if _, ok := fields[requiredKey]; !ok {
|
||||||
|
allErrs = append(allErrs, fmt.Errorf("field %s: is required", requiredKey))
|
||||||
|
}
|
||||||
|
}
|
||||||
for key, value := range fields {
|
for key, value := range fields {
|
||||||
details, ok := properties.At(key)
|
details, ok := properties.At(key)
|
||||||
if !ok {
|
if !ok {
|
||||||
@ -117,7 +130,7 @@ func (s *SwaggerSchema) ValidateObject(obj interface{}, apiVersion, fieldName, t
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if details.Type == nil && details.Ref == nil {
|
if details.Type == nil && details.Ref == nil {
|
||||||
return fmt.Errorf("could not find the type of %s from object: %v", key, details)
|
allErrs = append(allErrs, fmt.Errorf("could not find the type of %s from object: %v", key, details))
|
||||||
}
|
}
|
||||||
var fieldType string
|
var fieldType string
|
||||||
if details.Type != nil {
|
if details.Type != nil {
|
||||||
@ -129,19 +142,20 @@ func (s *SwaggerSchema) ValidateObject(obj interface{}, apiVersion, fieldName, t
|
|||||||
glog.V(2).Infof("Skipping nil field: %s", key)
|
glog.V(2).Infof("Skipping nil field: %s", key)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
err := s.validateField(value, apiVersion, fieldName+key, fieldType, &details)
|
errs := s.validateField(value, apiVersion, fieldName+key, fieldType, &details)
|
||||||
if err != nil {
|
if len(errs) > 0 {
|
||||||
glog.Errorf("Validation failed for: %s, %v", key, value)
|
glog.Errorf("Validation failed for: %s, %v", key, value)
|
||||||
return err
|
allErrs = append(allErrs, errs...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return allErrs
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SwaggerSchema) validateField(value interface{}, apiVersion, fieldName, fieldType string, fieldDetails *swagger.ModelProperty) error {
|
func (s *SwaggerSchema) validateField(value interface{}, apiVersion, fieldName, fieldType string, fieldDetails *swagger.ModelProperty) errs.ValidationErrorList {
|
||||||
if strings.HasPrefix(fieldType, apiVersion) {
|
if strings.HasPrefix(fieldType, apiVersion) {
|
||||||
return s.ValidateObject(value, apiVersion, fieldName, fieldType)
|
return s.ValidateObject(value, apiVersion, fieldName, fieldType)
|
||||||
}
|
}
|
||||||
|
allErrs := errs.ValidationErrorList{}
|
||||||
switch fieldType {
|
switch fieldType {
|
||||||
case "string":
|
case "string":
|
||||||
// Be loose about what we accept for 'string' since we use IntOrString in a couple of places
|
// Be loose about what we accept for 'string' since we use IntOrString in a couple of places
|
||||||
@ -149,16 +163,16 @@ func (s *SwaggerSchema) validateField(value interface{}, apiVersion, fieldName,
|
|||||||
_, isNumber := value.(float64)
|
_, isNumber := value.(float64)
|
||||||
_, isInteger := value.(int)
|
_, isInteger := value.(int)
|
||||||
if !isString && !isNumber && !isInteger {
|
if !isString && !isNumber && !isInteger {
|
||||||
return NewInvalidTypeError(reflect.String, reflect.TypeOf(value).Kind(), fieldName)
|
return append(allErrs, NewInvalidTypeError(reflect.String, reflect.TypeOf(value).Kind(), fieldName))
|
||||||
}
|
}
|
||||||
case "array":
|
case "array":
|
||||||
arr, ok := value.([]interface{})
|
arr, ok := value.([]interface{})
|
||||||
if !ok {
|
if !ok {
|
||||||
return NewInvalidTypeError(reflect.Array, reflect.TypeOf(value).Kind(), fieldName)
|
return append(allErrs, NewInvalidTypeError(reflect.Array, reflect.TypeOf(value).Kind(), fieldName))
|
||||||
}
|
}
|
||||||
var arrType string
|
var arrType string
|
||||||
if fieldDetails.Items.Ref == nil && fieldDetails.Items.Type == nil {
|
if fieldDetails.Items.Ref == nil && fieldDetails.Items.Type == nil {
|
||||||
return NewInvalidTypeError(reflect.Array, reflect.TypeOf(value).Kind(), fieldName)
|
return append(allErrs, NewInvalidTypeError(reflect.Array, reflect.TypeOf(value).Kind(), fieldName))
|
||||||
}
|
}
|
||||||
if fieldDetails.Items.Ref != nil {
|
if fieldDetails.Items.Ref != nil {
|
||||||
arrType = *fieldDetails.Items.Ref
|
arrType = *fieldDetails.Items.Ref
|
||||||
@ -166,9 +180,9 @@ func (s *SwaggerSchema) validateField(value interface{}, apiVersion, fieldName,
|
|||||||
arrType = *fieldDetails.Items.Type
|
arrType = *fieldDetails.Items.Type
|
||||||
}
|
}
|
||||||
for ix := range arr {
|
for ix := range arr {
|
||||||
err := s.validateField(arr[ix], apiVersion, fmt.Sprintf("%s[%d]", fieldName, ix), arrType, nil)
|
errs := s.validateField(arr[ix], apiVersion, fmt.Sprintf("%s[%d]", fieldName, ix), arrType, nil)
|
||||||
if err != nil {
|
if len(errs) > 0 {
|
||||||
return err
|
allErrs = append(allErrs, errs...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case "uint64":
|
case "uint64":
|
||||||
@ -177,19 +191,19 @@ func (s *SwaggerSchema) validateField(value interface{}, apiVersion, fieldName,
|
|||||||
_, isNumber := value.(float64)
|
_, isNumber := value.(float64)
|
||||||
_, isInteger := value.(int)
|
_, isInteger := value.(int)
|
||||||
if !isNumber && !isInteger {
|
if !isNumber && !isInteger {
|
||||||
return NewInvalidTypeError(reflect.Int, reflect.TypeOf(value).Kind(), fieldName)
|
return append(allErrs, NewInvalidTypeError(reflect.Int, reflect.TypeOf(value).Kind(), fieldName))
|
||||||
}
|
}
|
||||||
case "float64":
|
case "float64":
|
||||||
if _, ok := value.(float64); !ok {
|
if _, ok := value.(float64); !ok {
|
||||||
return NewInvalidTypeError(reflect.Float64, reflect.TypeOf(value).Kind(), fieldName)
|
return append(allErrs, NewInvalidTypeError(reflect.Float64, reflect.TypeOf(value).Kind(), fieldName))
|
||||||
}
|
}
|
||||||
case "boolean":
|
case "boolean":
|
||||||
if _, ok := value.(bool); !ok {
|
if _, ok := value.(bool); !ok {
|
||||||
return NewInvalidTypeError(reflect.Bool, reflect.TypeOf(value).Kind(), fieldName)
|
return append(allErrs, NewInvalidTypeError(reflect.Bool, reflect.TypeOf(value).Kind(), fieldName))
|
||||||
}
|
}
|
||||||
case "any":
|
case "any":
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("unexpected type: %v", fieldType)
|
return append(allErrs, fmt.Errorf("unexpected type: %v", fieldType))
|
||||||
}
|
}
|
||||||
return nil
|
return allErrs
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user