diff --git a/pkg/cmd/util/factory_client_access.go b/pkg/cmd/util/factory_client_access.go index 66a20e48..4340c03e 100644 --- a/pkg/cmd/util/factory_client_access.go +++ b/pkg/cmd/util/factory_client_access.go @@ -33,7 +33,6 @@ import ( restclient "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" "k8s.io/kubectl/pkg/util/openapi" - openapivalidation "k8s.io/kubectl/pkg/util/openapi/validation" "k8s.io/kubectl/pkg/validation" ) @@ -159,7 +158,7 @@ func (f *factoryImpl) Validator(validationDirective string) (validation.Schema, } schema := validation.ConjunctiveSchema{ - openapivalidation.NewSchemaValidation(resources), + validation.NewSchemaValidation(resources), validation.NoDoubleKeySchema{}, } diff --git a/pkg/validation/schema.go b/pkg/validation/schema.go index 56123e28..fb8841b9 100644 --- a/pkg/validation/schema.go +++ b/pkg/validation/schema.go @@ -26,7 +26,6 @@ import ( utilerrors "k8s.io/apimachinery/pkg/util/errors" "k8s.io/cli-runtime/pkg/resource" "k8s.io/klog/v2" - schemavalidation "k8s.io/kubectl/pkg/util/openapi/validation" ) // Schema is an interface that knows how to validate an API object serialized to a byte array. @@ -127,12 +126,12 @@ type paramVerifyingSchema struct { // ValidateBytes validates bytes per a ParamVerifyingSchema func (c *paramVerifyingSchema) ValidateBytes(data []byte) error { - obj, err := schemavalidation.Parse(data) + obj, err := parse(data) if err != nil { return err } - gvk, errs := schemavalidation.GetObjectKind(obj) + gvk, errs := getObjectKind(obj) if errs != nil { return utilerrors.NewAggregate(errs) } diff --git a/pkg/validation/testdata/v1/invalidPod.yaml b/pkg/validation/testdata/v1/invalidPod.yaml deleted file mode 100644 index 9557c55f..00000000 --- a/pkg/validation/testdata/v1/invalidPod.yaml +++ /dev/null @@ -1,11 +0,0 @@ -apiVersion: v1 -kind: Pod -metadata: - labels: - name: redis-master - name: name -spec: - containers: - - args: "this is a bad command" - image: gcr.io/fake_project/fake_image:fake_tag - name: master diff --git a/pkg/validation/testdata/v1/invalidPod1.json b/pkg/validation/testdata/v1/invalidPod1.json deleted file mode 100644 index 384d1857..00000000 --- a/pkg/validation/testdata/v1/invalidPod1.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "kind": "Pod", - "apiVersion": "v1", - "metadata": { - "name": "name", - "labels": { - "name": "redis-master" - } - }, - "spec": { - "containers": [ - { - "name": "master", - "image": "gcr.io/fake_project/fake_image:fake_tag", - "args": "this is a bad command" - } - ] - } -} diff --git a/pkg/validation/testdata/v1/invalidPod2.json b/pkg/validation/testdata/v1/invalidPod2.json deleted file mode 100644 index 56e8f93b..00000000 --- a/pkg/validation/testdata/v1/invalidPod2.json +++ /dev/null @@ -1,35 +0,0 @@ -{ - "kind": "Pod", - "apiVersion": "v1", - "metadata": { - "name": "apache-php", - "labels": { - "name": "apache-php" - } - }, - "spec": { - "volumes": [{ - "name": "shared-disk" - }], - "containers": [ - { - "name": "apache-php", - "image": "gcr.io/fake_project/fake_image:fake_tag", - "ports": [ - { - "name": "apache", - "hostPort": "13380", - "containerPort": 80, - "protocol": "TCP" - } - ], - "volumeMounts": [ - { - "name": "shared-disk", - "mountPath": "/var/www/html" - } - ] - } - ] - } -} diff --git a/pkg/validation/testdata/v1/invalidPod3.json b/pkg/validation/testdata/v1/invalidPod3.json deleted file mode 100644 index 69e0e853..00000000 --- a/pkg/validation/testdata/v1/invalidPod3.json +++ /dev/null @@ -1,35 +0,0 @@ -{ - "kind": "Pod", - "apiVersion": "v1", - "metadata": { - "name": "apache-php", - "labels": { - "name": "apache-php" - } - }, - "spec": { - "volumes": [ - "name": "shared-disk" - ], - "containers": [ - { - "name": "apache-php", - "image": "gcr.io/fake_project/fake_image:fake_tag", - "ports": [ - { - "name": "apache", - "hostPort": 13380, - "containerPort": 80, - "protocol": "TCP" - } - ], - "volumeMounts": [ - { - "name": "shared-disk", - "mountPath": "/var/www/html" - } - ] - } - ] - } -} diff --git a/pkg/validation/testdata/v1/invalidPod4.yaml b/pkg/validation/testdata/v1/invalidPod4.yaml deleted file mode 100644 index a6958db5..00000000 --- a/pkg/validation/testdata/v1/invalidPod4.yaml +++ /dev/null @@ -1,14 +0,0 @@ -apiVersion: v1 -kind: Pod -metadata: - labels: - name: redis-master - name: name -spec: - containers: - - image: gcr.io/fake_project/fake_image:fake_tag - name: master - args: - - - command: - - diff --git a/pkg/validation/testdata/v1/validPod.yaml b/pkg/validation/testdata/v1/validPod.yaml deleted file mode 100644 index 3849ba7a..00000000 --- a/pkg/validation/testdata/v1/validPod.yaml +++ /dev/null @@ -1,16 +0,0 @@ -apiVersion: v1 -kind: Pod -metadata: - labels: - name: redis-master - name: name -spec: - containers: - - args: - - this - - is - - an - - ok - - command - image: gcr.io/fake_project/fake_image:fake_tag - name: master diff --git a/pkg/util/openapi/validation/validation.go b/pkg/validation/validation.go similarity index 83% rename from pkg/util/openapi/validation/validation.go rename to pkg/validation/validation.go index 62ce3df2..3ba3213d 100644 --- a/pkg/util/openapi/validation/validation.go +++ b/pkg/validation/validation.go @@ -27,28 +27,28 @@ import ( "k8s.io/kubectl/pkg/util/openapi" ) -// SchemaValidation validates the object against an OpenAPI schema. -type SchemaValidation struct { +// schemaValidation validates the object against an OpenAPI schema. +type schemaValidation struct { resources openapi.Resources } -// NewSchemaValidation creates a new SchemaValidation that can be used +// NewSchemaValidation creates a new Schema that can be used // to validate objects. -func NewSchemaValidation(resources openapi.Resources) *SchemaValidation { - return &SchemaValidation{ +func NewSchemaValidation(resources openapi.Resources) Schema { + 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) +func (v *schemaValidation) ValidateBytes(data []byte) error { + obj, err := parse(data) if err != nil { return err } - gvk, errs := GetObjectKind(obj) + gvk, errs := getObjectKind(obj) if errs != nil { return utilerrors.NewAggregate(errs) } @@ -60,7 +60,7 @@ func (v *SchemaValidation) ValidateBytes(data []byte) error { return utilerrors.NewAggregate(v.validateResource(obj, gvk)) } -func (v *SchemaValidation) validateList(object interface{}) []error { +func (v *schemaValidation) validateList(object interface{}) []error { fields, ok := object.(map[string]interface{}) if !ok || fields == nil { return []error{errors.New("invalid object to validate")} @@ -71,7 +71,7 @@ func (v *SchemaValidation) validateList(object interface{}) []error { return []error{errors.New("invalid object to validate")} } for _, item := range fields["items"].([]interface{}) { - if gvk, errs := GetObjectKind(item); errs != nil { + if gvk, errs := getObjectKind(item); errs != nil { allErrors = append(allErrors, errs...) } else { allErrors = append(allErrors, v.validateResource(item, gvk)...) @@ -80,7 +80,7 @@ func (v *SchemaValidation) validateList(object interface{}) []error { return allErrors } -func (v *SchemaValidation) validateResource(obj interface{}, gvk schema.GroupVersionKind) []error { +func (v *schemaValidation) validateResource(obj interface{}, gvk schema.GroupVersionKind) []error { resource := v.resources.LookupResource(gvk) if resource == nil { // resource is not present, let's just skip validation. @@ -90,7 +90,7 @@ func (v *SchemaValidation) validateResource(obj interface{}, gvk schema.GroupVer return validation.ValidateModel(obj, resource, gvk.Kind) } -func Parse(data []byte) (interface{}, error) { +func parse(data []byte) (interface{}, error) { var obj interface{} out, err := yaml.ToJSON(data) if err != nil { @@ -102,7 +102,7 @@ func Parse(data []byte) (interface{}, error) { return obj, nil } -func GetObjectKind(object interface{}) (schema.GroupVersionKind, []error) { +func getObjectKind(object interface{}) (schema.GroupVersionKind, []error) { var listErrors []error fields, ok := object.(map[string]interface{}) if !ok || fields == nil { diff --git a/pkg/util/openapi/validation/validation_suite_test.go b/pkg/validation/validation_suite_test.go similarity index 100% rename from pkg/util/openapi/validation/validation_suite_test.go rename to pkg/validation/validation_suite_test.go diff --git a/pkg/util/openapi/validation/validation_test.go b/pkg/validation/validation_test.go similarity index 98% rename from pkg/util/openapi/validation/validation_test.go rename to pkg/validation/validation_test.go index 659b6563..55b4c100 100644 --- a/pkg/util/openapi/validation/validation_test.go +++ b/pkg/validation/validation_test.go @@ -30,10 +30,10 @@ import ( "k8s.io/kubectl/pkg/util/openapi" ) -var fakeSchema = testing.Fake{Path: filepath.Join("..", "..", "..", "..", "testdata", "openapi", "swagger.json")} +var fakeSchema = testing.Fake{Path: filepath.Join("..", "..", "testdata", "openapi", "swagger.json")} var _ = Describe("resource validation using OpenAPI Schema", func() { - var validator *SchemaValidation + var validator Schema BeforeEach(func() { s, err := fakeSchema.OpenAPISchema() Expect(err).To(BeNil())