openapi: Validate unregistered type, if they can be found
This commit is contained in:
		@@ -17,7 +17,6 @@ go_library(
 | 
				
			|||||||
    ],
 | 
					    ],
 | 
				
			||||||
    tags = ["automanaged"],
 | 
					    tags = ["automanaged"],
 | 
				
			||||||
    deps = [
 | 
					    deps = [
 | 
				
			||||||
        "//pkg/api:go_default_library",
 | 
					 | 
				
			||||||
        "//pkg/api/util:go_default_library",
 | 
					        "//pkg/api/util:go_default_library",
 | 
				
			||||||
        "//pkg/kubectl/cmd/util/openapi:go_default_library",
 | 
					        "//pkg/kubectl/cmd/util/openapi:go_default_library",
 | 
				
			||||||
        "//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
 | 
					        "//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -18,14 +18,12 @@ package validation
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
					 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"k8s.io/apimachinery/pkg/runtime/schema"
 | 
						"k8s.io/apimachinery/pkg/runtime/schema"
 | 
				
			||||||
	utilerrors "k8s.io/apimachinery/pkg/util/errors"
 | 
						utilerrors "k8s.io/apimachinery/pkg/util/errors"
 | 
				
			||||||
	"k8s.io/apimachinery/pkg/util/json"
 | 
						"k8s.io/apimachinery/pkg/util/json"
 | 
				
			||||||
	"k8s.io/apimachinery/pkg/util/yaml"
 | 
						"k8s.io/apimachinery/pkg/util/yaml"
 | 
				
			||||||
	"k8s.io/kubernetes/pkg/api"
 | 
					 | 
				
			||||||
	apiutil "k8s.io/kubernetes/pkg/api/util"
 | 
						apiutil "k8s.io/kubernetes/pkg/api/util"
 | 
				
			||||||
	"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
 | 
						"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@@ -76,15 +74,10 @@ func (v *SchemaValidation) validateList(object interface{}) []error {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (v *SchemaValidation) validateResource(obj interface{}, gvk schema.GroupVersionKind) []error {
 | 
					func (v *SchemaValidation) validateResource(obj interface{}, gvk schema.GroupVersionKind) []error {
 | 
				
			||||||
	if !api.Registry.IsEnabledVersion(gvk.GroupVersion()) {
 | 
					 | 
				
			||||||
		// if we don't have this in our scheme, just skip
 | 
					 | 
				
			||||||
		// validation because its an object we don't recognize
 | 
					 | 
				
			||||||
		return nil
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	resource := v.resources.LookupResource(gvk)
 | 
						resource := v.resources.LookupResource(gvk)
 | 
				
			||||||
	if resource == nil {
 | 
						if resource == nil {
 | 
				
			||||||
		return []error{fmt.Errorf("unknown object type %#v", gvk)}
 | 
							// resource is not present, let's just skip validation.
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	rootValidation, err := itemFactory(openapi.NewPath(gvk.Kind), obj)
 | 
						rootValidation, err := itemFactory(openapi.NewPath(gvk.Kind), obj)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -43,7 +43,6 @@ filegroup(
 | 
				
			|||||||
        "//test/integration/federation:all-srcs",
 | 
					        "//test/integration/federation:all-srcs",
 | 
				
			||||||
        "//test/integration/framework:all-srcs",
 | 
					        "//test/integration/framework:all-srcs",
 | 
				
			||||||
        "//test/integration/garbagecollector:all-srcs",
 | 
					        "//test/integration/garbagecollector:all-srcs",
 | 
				
			||||||
        "//test/integration/kubectl:all-srcs",
 | 
					 | 
				
			||||||
        "//test/integration/master:all-srcs",
 | 
					        "//test/integration/master:all-srcs",
 | 
				
			||||||
        "//test/integration/metrics:all-srcs",
 | 
					        "//test/integration/metrics:all-srcs",
 | 
				
			||||||
        "//test/integration/objectmeta:all-srcs",
 | 
					        "//test/integration/objectmeta:all-srcs",
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,35 +0,0 @@
 | 
				
			|||||||
package(default_visibility = ["//visibility:public"])
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
load(
 | 
					 | 
				
			||||||
    "@io_bazel_rules_go//go:def.bzl",
 | 
					 | 
				
			||||||
    "go_test",
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
go_test(
 | 
					 | 
				
			||||||
    name = "go_default_test",
 | 
					 | 
				
			||||||
    size = "large",
 | 
					 | 
				
			||||||
    srcs = [
 | 
					 | 
				
			||||||
        "kubectl_test.go",
 | 
					 | 
				
			||||||
        "main_test.go",
 | 
					 | 
				
			||||||
    ],
 | 
					 | 
				
			||||||
    tags = ["integration"],
 | 
					 | 
				
			||||||
    deps = [
 | 
					 | 
				
			||||||
        "//pkg/kubectl/cmd/util:go_default_library",
 | 
					 | 
				
			||||||
        "//test/integration/framework:go_default_library",
 | 
					 | 
				
			||||||
        "//vendor/k8s.io/client-go/tools/clientcmd:go_default_library",
 | 
					 | 
				
			||||||
        "//vendor/k8s.io/client-go/tools/clientcmd/api:go_default_library",
 | 
					 | 
				
			||||||
    ],
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
filegroup(
 | 
					 | 
				
			||||||
    name = "package-srcs",
 | 
					 | 
				
			||||||
    srcs = glob(["**"]),
 | 
					 | 
				
			||||||
    tags = ["automanaged"],
 | 
					 | 
				
			||||||
    visibility = ["//visibility:private"],
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
filegroup(
 | 
					 | 
				
			||||||
    name = "all-srcs",
 | 
					 | 
				
			||||||
    srcs = [":package-srcs"],
 | 
					 | 
				
			||||||
    tags = ["automanaged"],
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
@@ -1,76 +0,0 @@
 | 
				
			|||||||
/*
 | 
					 | 
				
			||||||
Copyright 2015 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 kubectl
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"testing"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	"k8s.io/client-go/tools/clientcmd"
 | 
					 | 
				
			||||||
	clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
 | 
					 | 
				
			||||||
	"k8s.io/kubernetes/pkg/kubectl/cmd/util"
 | 
					 | 
				
			||||||
	"k8s.io/kubernetes/test/integration/framework"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func TestKubectlValidation(t *testing.T) {
 | 
					 | 
				
			||||||
	testCases := []struct {
 | 
					 | 
				
			||||||
		data string
 | 
					 | 
				
			||||||
		// Validation should not fail on missing type information.
 | 
					 | 
				
			||||||
		err bool
 | 
					 | 
				
			||||||
	}{
 | 
					 | 
				
			||||||
		{`{"apiVersion": "v1", "kind": "thisObjectShouldNotExistInAnyGroup"}`, true},
 | 
					 | 
				
			||||||
		{`{"apiVersion": "invalidVersion", "kind": "Pod"}`, false},
 | 
					 | 
				
			||||||
		{`{"apiVersion": "v1", "kind": "Pod"}`, false},
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		// The following test the experimental api.
 | 
					 | 
				
			||||||
		// TODO: Replace with something more robust. These may move.
 | 
					 | 
				
			||||||
		{`{"apiVersion": "extensions/v1beta1", "kind": "Ingress"}`, false},
 | 
					 | 
				
			||||||
		{`{"apiVersion": "extensions/v1beta1", "kind": "DaemonSet"}`, false},
 | 
					 | 
				
			||||||
		{`{"apiVersion": "vNotAVersion", "kind": "DaemonSet"}`, false},
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	components := framework.NewMasterComponents(&framework.Config{})
 | 
					 | 
				
			||||||
	defer components.Stop(true, true)
 | 
					 | 
				
			||||||
	ctx := clientcmdapi.NewContext()
 | 
					 | 
				
			||||||
	cfg := clientcmdapi.NewConfig()
 | 
					 | 
				
			||||||
	cluster := clientcmdapi.NewCluster()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	cluster.Server = components.ApiServer.URL
 | 
					 | 
				
			||||||
	cluster.InsecureSkipTLSVerify = true
 | 
					 | 
				
			||||||
	cfg.Contexts = map[string]*clientcmdapi.Context{"test": ctx}
 | 
					 | 
				
			||||||
	cfg.CurrentContext = "test"
 | 
					 | 
				
			||||||
	overrides := clientcmd.ConfigOverrides{
 | 
					 | 
				
			||||||
		ClusterInfo: *cluster,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	cmdConfig := clientcmd.NewNonInteractiveClientConfig(*cfg, "test", &overrides, nil)
 | 
					 | 
				
			||||||
	factory := util.NewFactory(cmdConfig)
 | 
					 | 
				
			||||||
	schema, err := factory.Validator(true, true, "")
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		t.Errorf("failed to get validator: %v", err)
 | 
					 | 
				
			||||||
		return
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	for i, test := range testCases {
 | 
					 | 
				
			||||||
		err := schema.ValidateBytes([]byte(test.data))
 | 
					 | 
				
			||||||
		if err == nil {
 | 
					 | 
				
			||||||
			if test.err {
 | 
					 | 
				
			||||||
				t.Errorf("case %d: expected error", i)
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		} else {
 | 
					 | 
				
			||||||
			if !test.err {
 | 
					 | 
				
			||||||
				t.Errorf("case %d: unexpected error: %v", i, err)
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -1,27 +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 kubectl
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"testing"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	"k8s.io/kubernetes/test/integration/framework"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func TestMain(m *testing.M) {
 | 
					 | 
				
			||||||
	framework.EtcdMain(m.Run)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
		Reference in New Issue
	
	Block a user