Commit 434b7688 authored by k8s-merge-robot's avatar k8s-merge-robot

Merge pull request #13277 from uluyol/sv-detect-group

Auto commit by PR queue bot
parents 1a88be1a f855e758
...@@ -18,6 +18,7 @@ package util ...@@ -18,6 +18,7 @@ package util
import ( import (
"bytes" "bytes"
"errors"
"flag" "flag"
"fmt" "fmt"
"io" "io"
...@@ -236,8 +237,8 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory { ...@@ -236,8 +237,8 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
} }
return &clientSwaggerSchema{ return &clientSwaggerSchema{
c: client, c: client,
ec: client.ExperimentalClient,
cacheDir: dir, cacheDir: dir,
mapper: api.RESTMapper,
}, nil }, nil
} }
return validation.NullSchema{}, nil return validation.NullSchema{}, nil
...@@ -298,8 +299,8 @@ func getServicePorts(spec api.ServiceSpec) []string { ...@@ -298,8 +299,8 @@ func getServicePorts(spec api.ServiceSpec) []string {
type clientSwaggerSchema struct { type clientSwaggerSchema struct {
c *client.Client c *client.Client
ec *client.ExperimentalClient
cacheDir string cacheDir string
mapper meta.RESTMapper
} }
const schemaFileName = "schema.json" const schemaFileName = "schema.json"
...@@ -349,29 +350,29 @@ func getSchemaAndValidate(c schemaClient, data []byte, group, version, cacheDir ...@@ -349,29 +350,29 @@ func getSchemaAndValidate(c schemaClient, data []byte, group, version, cacheDir
} }
func (c *clientSwaggerSchema) ValidateBytes(data []byte) error { func (c *clientSwaggerSchema) ValidateBytes(data []byte) error {
version, _, err := runtime.UnstructuredJSONScheme.DataVersionAndKind(data) version, kind, err := runtime.UnstructuredJSONScheme.DataVersionAndKind(data)
if err != nil { if err != nil {
return err return err
} }
if ok := registered.IsRegisteredAPIVersion(version); !ok { if ok := registered.IsRegisteredAPIVersion(version); !ok {
return fmt.Errorf("API version %q isn't supported, only supports API versions %q", version, registered.RegisteredVersions) return fmt.Errorf("API version %q isn't supported, only supports API versions %q", version, registered.RegisteredVersions)
} }
// First try stable api, if we can't validate using that, try experimental. resource, _ := meta.KindToResource(kind, false)
// If experimental fails, return error from stable api. group, err := c.mapper.GroupForResource(resource)
// TODO: Figure out which group to try once multiple group support is merged if err != nil {
// instead of trying everything. return fmt.Errorf("could not find api group for %s: %v", kind, err)
err = getSchemaAndValidate(c.c.RESTClient, data, "api", version, c.cacheDir) }
if err != nil && c.ec != nil { if group == "experimental" {
g, err := latest.Group("experimental") g, err := latest.Group(group)
if err != nil { if err != nil {
return err return err
} }
errExp := getSchemaAndValidate(c.ec.RESTClient, data, "apis"+"/"+g.Group, version, c.cacheDir) if c.c.ExperimentalClient == nil {
if errExp == nil { return errors.New("unable to validate: no experimental client")
return nil
} }
return getSchemaAndValidate(c.c.ExperimentalClient.RESTClient, data, "apis/"+g.Group, version, c.cacheDir)
} }
return err return getSchemaAndValidate(c.c.RESTClient, data, "api", version, c.cacheDir)
} }
// DefaultClientConfig creates a clientcmd.ClientConfig with the following hierarchy: // DefaultClientConfig creates a clientcmd.ClientConfig with the following hierarchy:
......
...@@ -140,16 +140,18 @@ func startMasterOrDie(masterConfig *master.Config) (*master.Master, *httptest.Se ...@@ -140,16 +140,18 @@ func startMasterOrDie(masterConfig *master.Config) (*master.Master, *httptest.Se
} }
masterConfig = &master.Config{ masterConfig = &master.Config{
DatabaseStorage: etcdStorage, DatabaseStorage: etcdStorage,
ExpDatabaseStorage: expEtcdStorage, ExpDatabaseStorage: expEtcdStorage,
KubeletClient: client.FakeKubeletClient{}, KubeletClient: client.FakeKubeletClient{},
EnableLogsSupport: false, EnableExp: true,
EnableProfiling: true, EnableLogsSupport: false,
EnableUISupport: false, EnableProfiling: true,
APIPrefix: "/api", EnableSwaggerSupport: true,
APIGroupPrefix: "/apis", EnableUISupport: false,
Authorizer: apiserver.NewAlwaysAllowAuthorizer(), APIPrefix: "/api",
AdmissionControl: admit.NewAlwaysAdmit(), APIGroupPrefix: "/apis",
Authorizer: apiserver.NewAlwaysAllowAuthorizer(),
AdmissionControl: admit.NewAlwaysAdmit(),
} }
} else { } else {
etcdStorage = masterConfig.DatabaseStorage etcdStorage = masterConfig.DatabaseStorage
......
// +build integration,!no-etcd
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
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 integration
import (
"testing"
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
"k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/test/integration/framework"
)
func TestKubectlValidation(t *testing.T) {
testCases := []struct {
data string
err bool
}{
{`{"apiVersion": "v1", "kind": "thisObjectShouldNotExistInAnyGroup"}`, true},
{`{"apiVersion": "invalidVersion", "kind": "Pod"}`, true},
{`{"apiVersion": "v1", "kind": "Pod"}`, false},
// The following test the experimental api.
// TOOD: Replace with something more robust. These may move.
{`{"apiVersion": "v1", "kind": "DaemonSet"}`, false},
{`{"apiVersion": "v1", "kind": "Job"}`, false},
{`{"apiVersion": "vNotAVersion", "kind": "Job"}`, true},
}
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
overrides := clientcmd.ConfigOverrides{
ClusterInfo: *cluster,
Context: *ctx,
CurrentContext: "test",
}
cmdConfig := clientcmd.NewNonInteractiveClientConfig(*cfg, "test", &overrides)
factory := util.NewFactory(cmdConfig)
schema, err := factory.Validator(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)
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment