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

Auto commit by PR queue bot
This commit is contained in:
k8s-merge-robot
2015-09-18 01:31:43 -07:00
3 changed files with 103 additions and 24 deletions

View File

@@ -18,6 +18,7 @@ package util
import (
"bytes"
"errors"
"flag"
"fmt"
"io"
@@ -236,8 +237,8 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
}
return &clientSwaggerSchema{
c: client,
ec: client.ExperimentalClient,
cacheDir: dir,
mapper: api.RESTMapper,
}, nil
}
return validation.NullSchema{}, nil
@@ -298,8 +299,8 @@ func getServicePorts(spec api.ServiceSpec) []string {
type clientSwaggerSchema struct {
c *client.Client
ec *client.ExperimentalClient
cacheDir string
mapper meta.RESTMapper
}
const schemaFileName = "schema.json"
@@ -349,29 +350,29 @@ func getSchemaAndValidate(c schemaClient, data []byte, group, version, cacheDir
}
func (c *clientSwaggerSchema) ValidateBytes(data []byte) error {
version, _, err := runtime.UnstructuredJSONScheme.DataVersionAndKind(data)
version, kind, err := runtime.UnstructuredJSONScheme.DataVersionAndKind(data)
if err != nil {
return err
}
if ok := registered.IsRegisteredAPIVersion(version); !ok {
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.
// If experimental fails, return error from stable api.
// TODO: Figure out which group to try once multiple group support is merged
// instead of trying everything.
err = getSchemaAndValidate(c.c.RESTClient, data, "api", version, c.cacheDir)
if err != nil && c.ec != nil {
g, err := latest.Group("experimental")
resource, _ := meta.KindToResource(kind, false)
group, err := c.mapper.GroupForResource(resource)
if err != nil {
return fmt.Errorf("could not find api group for %s: %v", kind, err)
}
if group == "experimental" {
g, err := latest.Group(group)
if err != nil {
return err
}
errExp := getSchemaAndValidate(c.ec.RESTClient, data, "apis"+"/"+g.Group, version, c.cacheDir)
if errExp == nil {
return nil
if c.c.ExperimentalClient == nil {
return errors.New("unable to validate: no experimental client")
}
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: