Merge pull request #54957 from apelisse/update-kube-openapi
Automatic merge from submit-queue (batch tested with PRs 55004, 54957). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Update kube-openapi to use validation **What this PR does / why we need it**: Moves openapi validation code to kube-openapi, so that we can move the rest of the code to apimachinery repository, so that later we can use it from both the client and the server. **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*: Fixes #Nothing **Special notes for your reviewer**: **Release note**: ```release-note NONE ```
This commit is contained in:
@@ -8,11 +8,7 @@ load(
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"errors.go",
|
||||
"types.go",
|
||||
"validation.go",
|
||||
],
|
||||
srcs = ["validation.go"],
|
||||
importpath = "k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/validation",
|
||||
deps = [
|
||||
"//pkg/kubectl/cmd/util/openapi:go_default_library",
|
||||
@@ -20,20 +16,20 @@ go_library(
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/errors:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/json:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/yaml:go_default_library",
|
||||
"//vendor/k8s.io/kube-openapi/pkg/util/proto:go_default_library",
|
||||
"//vendor/k8s.io/kube-openapi/pkg/util/proto/validation:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_xtest",
|
||||
name = "go_default_test",
|
||||
srcs = [
|
||||
"validation_suite_test.go",
|
||||
"validation_test.go",
|
||||
],
|
||||
data = ["//api/openapi-spec:swagger-spec"],
|
||||
importpath = "k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/validation_test",
|
||||
importpath = "k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/validation",
|
||||
library = ":go_default_library",
|
||||
deps = [
|
||||
":go_default_library",
|
||||
"//pkg/api/testapi:go_default_library",
|
||||
"//pkg/kubectl/cmd/util/openapi:go_default_library",
|
||||
"//pkg/kubectl/cmd/util/openapi/testing:go_default_library",
|
||||
@@ -42,6 +38,7 @@ go_test(
|
||||
"//vendor/github.com/onsi/ginkgo/types:go_default_library",
|
||||
"//vendor/github.com/onsi/gomega:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/errors:go_default_library",
|
||||
"//vendor/k8s.io/kube-openapi/pkg/util/proto/validation:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
@@ -1,79 +0,0 @@
|
||||
/*
|
||||
Copyright 2017 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package validation
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Errors struct {
|
||||
errors []error
|
||||
}
|
||||
|
||||
func (e *Errors) Errors() []error {
|
||||
return e.errors
|
||||
}
|
||||
|
||||
func (e *Errors) AppendErrors(err ...error) {
|
||||
e.errors = append(e.errors, err...)
|
||||
}
|
||||
|
||||
type ValidationError struct {
|
||||
Path string
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e ValidationError) Error() string {
|
||||
return fmt.Sprintf("ValidationError(%s): %v", e.Path, e.Err)
|
||||
}
|
||||
|
||||
type InvalidTypeError struct {
|
||||
Path string
|
||||
Expected string
|
||||
Actual string
|
||||
}
|
||||
|
||||
func (e InvalidTypeError) Error() string {
|
||||
return fmt.Sprintf("invalid type for %s: got %q, expected %q", e.Path, e.Actual, e.Expected)
|
||||
}
|
||||
|
||||
type MissingRequiredFieldError struct {
|
||||
Path string
|
||||
Field string
|
||||
}
|
||||
|
||||
func (e MissingRequiredFieldError) Error() string {
|
||||
return fmt.Sprintf("missing required field %q in %s", e.Field, e.Path)
|
||||
}
|
||||
|
||||
type UnknownFieldError struct {
|
||||
Path string
|
||||
Field string
|
||||
}
|
||||
|
||||
func (e UnknownFieldError) Error() string {
|
||||
return fmt.Sprintf("unknown field %q in %s", e.Field, e.Path)
|
||||
}
|
||||
|
||||
type InvalidObjectTypeError struct {
|
||||
Path string
|
||||
Type string
|
||||
}
|
||||
|
||||
func (e InvalidObjectTypeError) Error() string {
|
||||
return fmt.Sprintf("unknown object type %q in %s", e.Type, e.Path)
|
||||
}
|
||||
@@ -1,289 +0,0 @@
|
||||
/*
|
||||
Copyright 2017 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package validation
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sort"
|
||||
|
||||
"k8s.io/kube-openapi/pkg/util/proto"
|
||||
)
|
||||
|
||||
type ValidationItem interface {
|
||||
proto.SchemaVisitor
|
||||
|
||||
Errors() []error
|
||||
Path() *proto.Path
|
||||
}
|
||||
|
||||
type baseItem struct {
|
||||
errors Errors
|
||||
path proto.Path
|
||||
}
|
||||
|
||||
// Errors returns the list of errors found for this item.
|
||||
func (item *baseItem) Errors() []error {
|
||||
return item.errors.Errors()
|
||||
}
|
||||
|
||||
// AddValidationError wraps the given error into a ValidationError and
|
||||
// attaches it to this item.
|
||||
func (item *baseItem) AddValidationError(err error) {
|
||||
item.errors.AppendErrors(ValidationError{Path: item.path.String(), Err: err})
|
||||
}
|
||||
|
||||
// AddError adds a regular (non-validation related) error to the list.
|
||||
func (item *baseItem) AddError(err error) {
|
||||
item.errors.AppendErrors(err)
|
||||
}
|
||||
|
||||
// CopyErrors adds a list of errors to this item. This is useful to copy
|
||||
// errors from subitems.
|
||||
func (item *baseItem) CopyErrors(errs []error) {
|
||||
item.errors.AppendErrors(errs...)
|
||||
}
|
||||
|
||||
// Path returns the path of this item, helps print useful errors.
|
||||
func (item *baseItem) Path() *proto.Path {
|
||||
return &item.path
|
||||
}
|
||||
|
||||
// mapItem represents a map entry in the yaml.
|
||||
type mapItem struct {
|
||||
baseItem
|
||||
|
||||
Map map[string]interface{}
|
||||
}
|
||||
|
||||
func (item *mapItem) sortedKeys() []string {
|
||||
sortedKeys := []string{}
|
||||
for key := range item.Map {
|
||||
sortedKeys = append(sortedKeys, key)
|
||||
}
|
||||
sort.Strings(sortedKeys)
|
||||
return sortedKeys
|
||||
}
|
||||
|
||||
var _ ValidationItem = &mapItem{}
|
||||
|
||||
func (item *mapItem) VisitPrimitive(schema *proto.Primitive) {
|
||||
item.AddValidationError(InvalidTypeError{Path: schema.GetPath().String(), Expected: schema.Type, Actual: "map"})
|
||||
}
|
||||
|
||||
func (item *mapItem) VisitArray(schema *proto.Array) {
|
||||
item.AddValidationError(InvalidTypeError{Path: schema.GetPath().String(), Expected: "array", Actual: "map"})
|
||||
}
|
||||
|
||||
func (item *mapItem) VisitMap(schema *proto.Map) {
|
||||
for _, key := range item.sortedKeys() {
|
||||
subItem, err := itemFactory(item.Path().FieldPath(key), item.Map[key])
|
||||
if err != nil {
|
||||
item.AddError(err)
|
||||
continue
|
||||
}
|
||||
schema.SubType.Accept(subItem)
|
||||
item.CopyErrors(subItem.Errors())
|
||||
}
|
||||
}
|
||||
|
||||
func (item *mapItem) VisitKind(schema *proto.Kind) {
|
||||
// Verify each sub-field.
|
||||
for _, key := range item.sortedKeys() {
|
||||
if item.Map[key] == nil {
|
||||
continue
|
||||
}
|
||||
subItem, err := itemFactory(item.Path().FieldPath(key), item.Map[key])
|
||||
if err != nil {
|
||||
item.AddError(err)
|
||||
continue
|
||||
}
|
||||
if _, ok := schema.Fields[key]; !ok {
|
||||
item.AddValidationError(UnknownFieldError{Path: schema.GetPath().String(), Field: key})
|
||||
continue
|
||||
}
|
||||
schema.Fields[key].Accept(subItem)
|
||||
item.CopyErrors(subItem.Errors())
|
||||
}
|
||||
|
||||
// Verify that all required fields are present.
|
||||
for _, required := range schema.RequiredFields {
|
||||
if v, ok := item.Map[required]; !ok || v == nil {
|
||||
item.AddValidationError(MissingRequiredFieldError{Path: schema.GetPath().String(), Field: required})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (item *mapItem) VisitReference(schema proto.Reference) {
|
||||
// passthrough
|
||||
schema.SubSchema().Accept(item)
|
||||
}
|
||||
|
||||
// arrayItem represents a yaml array.
|
||||
type arrayItem struct {
|
||||
baseItem
|
||||
|
||||
Array []interface{}
|
||||
}
|
||||
|
||||
var _ ValidationItem = &arrayItem{}
|
||||
|
||||
func (item *arrayItem) VisitPrimitive(schema *proto.Primitive) {
|
||||
item.AddValidationError(InvalidTypeError{Path: schema.GetPath().String(), Expected: schema.Type, Actual: "array"})
|
||||
}
|
||||
|
||||
func (item *arrayItem) VisitArray(schema *proto.Array) {
|
||||
for i, v := range item.Array {
|
||||
path := item.Path().ArrayPath(i)
|
||||
if v == nil {
|
||||
item.AddValidationError(InvalidObjectTypeError{Type: "nil", Path: path.String()})
|
||||
continue
|
||||
}
|
||||
subItem, err := itemFactory(path, v)
|
||||
if err != nil {
|
||||
item.AddError(err)
|
||||
continue
|
||||
}
|
||||
schema.SubType.Accept(subItem)
|
||||
item.CopyErrors(subItem.Errors())
|
||||
}
|
||||
}
|
||||
|
||||
func (item *arrayItem) VisitMap(schema *proto.Map) {
|
||||
item.AddValidationError(InvalidTypeError{Path: schema.GetPath().String(), Expected: "array", Actual: "map"})
|
||||
}
|
||||
|
||||
func (item *arrayItem) VisitKind(schema *proto.Kind) {
|
||||
item.AddValidationError(InvalidTypeError{Path: schema.GetPath().String(), Expected: "array", Actual: "map"})
|
||||
}
|
||||
|
||||
func (item *arrayItem) VisitReference(schema proto.Reference) {
|
||||
// passthrough
|
||||
schema.SubSchema().Accept(item)
|
||||
}
|
||||
|
||||
// primitiveItem represents a yaml value.
|
||||
type primitiveItem struct {
|
||||
baseItem
|
||||
|
||||
Value interface{}
|
||||
Kind string
|
||||
}
|
||||
|
||||
var _ ValidationItem = &primitiveItem{}
|
||||
|
||||
func (item *primitiveItem) VisitPrimitive(schema *proto.Primitive) {
|
||||
// Some types of primitives can match more than one (a number
|
||||
// can be a string, but not the other way around). Return from
|
||||
// the switch if we have a valid possible type conversion
|
||||
// NOTE(apelisse): This logic is blindly copied from the
|
||||
// existing swagger logic, and I'm not sure I agree with it.
|
||||
switch schema.Type {
|
||||
case proto.Boolean:
|
||||
switch item.Kind {
|
||||
case proto.Boolean:
|
||||
return
|
||||
}
|
||||
case proto.Integer:
|
||||
switch item.Kind {
|
||||
case proto.Integer, proto.Number:
|
||||
return
|
||||
}
|
||||
case proto.Number:
|
||||
switch item.Kind {
|
||||
case proto.Number:
|
||||
return
|
||||
}
|
||||
case proto.String:
|
||||
return
|
||||
}
|
||||
|
||||
item.AddValidationError(InvalidTypeError{Path: schema.GetPath().String(), Expected: schema.Type, Actual: item.Kind})
|
||||
}
|
||||
|
||||
func (item *primitiveItem) VisitArray(schema *proto.Array) {
|
||||
item.AddValidationError(InvalidTypeError{Path: schema.GetPath().String(), Expected: "array", Actual: item.Kind})
|
||||
}
|
||||
|
||||
func (item *primitiveItem) VisitMap(schema *proto.Map) {
|
||||
item.AddValidationError(InvalidTypeError{Path: schema.GetPath().String(), Expected: "map", Actual: item.Kind})
|
||||
}
|
||||
|
||||
func (item *primitiveItem) VisitKind(schema *proto.Kind) {
|
||||
item.AddValidationError(InvalidTypeError{Path: schema.GetPath().String(), Expected: "map", Actual: item.Kind})
|
||||
}
|
||||
|
||||
func (item *primitiveItem) VisitReference(schema proto.Reference) {
|
||||
// passthrough
|
||||
schema.SubSchema().Accept(item)
|
||||
}
|
||||
|
||||
// itemFactory creates the relevant item type/visitor based on the current yaml type.
|
||||
func itemFactory(path proto.Path, v interface{}) (ValidationItem, error) {
|
||||
// We need to special case for no-type fields in yaml (e.g. empty item in list)
|
||||
if v == nil {
|
||||
return nil, InvalidObjectTypeError{Type: "nil", Path: path.String()}
|
||||
}
|
||||
kind := reflect.TypeOf(v).Kind()
|
||||
switch kind {
|
||||
case reflect.Bool:
|
||||
return &primitiveItem{
|
||||
baseItem: baseItem{path: path},
|
||||
Value: v,
|
||||
Kind: proto.Boolean,
|
||||
}, nil
|
||||
case reflect.Int,
|
||||
reflect.Int8,
|
||||
reflect.Int16,
|
||||
reflect.Int32,
|
||||
reflect.Int64,
|
||||
reflect.Uint,
|
||||
reflect.Uint8,
|
||||
reflect.Uint16,
|
||||
reflect.Uint32,
|
||||
reflect.Uint64:
|
||||
return &primitiveItem{
|
||||
baseItem: baseItem{path: path},
|
||||
Value: v,
|
||||
Kind: proto.Integer,
|
||||
}, nil
|
||||
case reflect.Float32,
|
||||
reflect.Float64:
|
||||
return &primitiveItem{
|
||||
baseItem: baseItem{path: path},
|
||||
Value: v,
|
||||
Kind: proto.Number,
|
||||
}, nil
|
||||
case reflect.String:
|
||||
return &primitiveItem{
|
||||
baseItem: baseItem{path: path},
|
||||
Value: v,
|
||||
Kind: proto.String,
|
||||
}, nil
|
||||
case reflect.Array,
|
||||
reflect.Slice:
|
||||
return &arrayItem{
|
||||
baseItem: baseItem{path: path},
|
||||
Array: v.([]interface{}),
|
||||
}, nil
|
||||
case reflect.Map:
|
||||
return &mapItem{
|
||||
baseItem: baseItem{path: path},
|
||||
Map: v.(map[string]interface{}),
|
||||
}, nil
|
||||
}
|
||||
return nil, InvalidObjectTypeError{Type: kind.String(), Path: path.String()}
|
||||
}
|
||||
@@ -24,20 +24,25 @@ import (
|
||||
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
||||
"k8s.io/apimachinery/pkg/util/json"
|
||||
"k8s.io/apimachinery/pkg/util/yaml"
|
||||
"k8s.io/kube-openapi/pkg/util/proto"
|
||||
"k8s.io/kube-openapi/pkg/util/proto/validation"
|
||||
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
|
||||
)
|
||||
|
||||
// SchemaValidation validates the object against an OpenAPI schema.
|
||||
type SchemaValidation struct {
|
||||
resources openapi.Resources
|
||||
}
|
||||
|
||||
// NewSchemaValidation creates a new SchemaValidation that can be used
|
||||
// to validate objects.
|
||||
func NewSchemaValidation(resources openapi.Resources) *SchemaValidation {
|
||||
return &SchemaValidation{
|
||||
resources: resources,
|
||||
}
|
||||
}
|
||||
|
||||
// ValidateBytes will validates the object against using the Resources
|
||||
// object.
|
||||
func (v *SchemaValidation) ValidateBytes(data []byte) error {
|
||||
obj, err := parse(data)
|
||||
if err != nil {
|
||||
@@ -80,12 +85,7 @@ func (v *SchemaValidation) validateResource(obj interface{}, gvk schema.GroupVer
|
||||
return nil
|
||||
}
|
||||
|
||||
rootValidation, err := itemFactory(proto.NewPath(gvk.Kind), obj)
|
||||
if err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
resource.Accept(rootValidation)
|
||||
return rootValidation.Errors()
|
||||
return validation.ValidateModel(obj, resource, gvk.Kind)
|
||||
}
|
||||
|
||||
func parse(data []byte) (interface{}, error) {
|
||||
|
||||
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package validation_test
|
||||
package validation
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
|
||||
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package validation_test
|
||||
package validation
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
@@ -23,23 +23,23 @@ import (
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
||||
"k8s.io/kube-openapi/pkg/util/proto/validation"
|
||||
// This dependency is needed to register API types.
|
||||
_ "k8s.io/kubernetes/pkg/api/testapi"
|
||||
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
|
||||
tst "k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/testing"
|
||||
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/validation"
|
||||
)
|
||||
|
||||
var fakeSchema = tst.Fake{Path: filepath.Join("..", "..", "..", "..", "..", "..", "api", "openapi-spec", "swagger.json")}
|
||||
|
||||
var _ = Describe("resource validation using OpenAPI Schema", func() {
|
||||
var validator *validation.SchemaValidation
|
||||
var validator *SchemaValidation
|
||||
BeforeEach(func() {
|
||||
s, err := fakeSchema.OpenAPISchema()
|
||||
Expect(err).To(BeNil())
|
||||
resources, err := openapi.NewOpenAPIData(s)
|
||||
Expect(err).To(BeNil())
|
||||
validator = validation.NewSchemaValidation(resources)
|
||||
validator = NewSchemaValidation(resources)
|
||||
Expect(validator).ToNot(BeNil())
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user