Bumping k8s dependencies to 1.13
This commit is contained in:
98
vendor/k8s.io/kube-openapi/pkg/builder/openapi.go
generated
vendored
98
vendor/k8s.io/kube-openapi/pkg/builder/openapi.go
generated
vendored
@@ -20,7 +20,6 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
restful "github.com/emicklei/go-restful"
|
||||
@@ -45,27 +44,59 @@ type openAPI struct {
|
||||
|
||||
// BuildOpenAPISpec builds OpenAPI spec given a list of webservices (containing routes) and common.Config to customize it.
|
||||
func BuildOpenAPISpec(webServices []*restful.WebService, config *common.Config) (*spec.Swagger, error) {
|
||||
o := newOpenAPI(config)
|
||||
err := o.buildPaths(webServices)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return o.finalizeSwagger()
|
||||
}
|
||||
|
||||
// BuildOpenAPIDefinitionsForResource builds a partial OpenAPI spec given a sample object and common.Config to customize it.
|
||||
func BuildOpenAPIDefinitionsForResource(model interface{}, config *common.Config) (*spec.Definitions, error) {
|
||||
o := newOpenAPI(config)
|
||||
// We can discard the return value of toSchema because all we care about is the side effect of calling it.
|
||||
// All the models created for this resource get added to o.swagger.Definitions
|
||||
_, err := o.toSchema(util.GetCanonicalTypeName(model))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
swagger, err := o.finalizeSwagger()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &swagger.Definitions, nil
|
||||
}
|
||||
|
||||
// BuildOpenAPIDefinitionsForResources returns the OpenAPI spec which includes the definitions for the
|
||||
// passed type names.
|
||||
func BuildOpenAPIDefinitionsForResources(config *common.Config, names ...string) (*spec.Swagger, error) {
|
||||
o := newOpenAPI(config)
|
||||
// We can discard the return value of toSchema because all we care about is the side effect of calling it.
|
||||
// All the models created for this resource get added to o.swagger.Definitions
|
||||
for _, name := range names {
|
||||
_, err := o.toSchema(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return o.finalizeSwagger()
|
||||
}
|
||||
|
||||
// newOpenAPI sets up the openAPI object so we can build the spec.
|
||||
func newOpenAPI(config *common.Config) openAPI {
|
||||
o := openAPI{
|
||||
config: config,
|
||||
swagger: &spec.Swagger{
|
||||
SwaggerProps: spec.SwaggerProps{
|
||||
Swagger: OpenAPIVersion,
|
||||
Definitions: spec.Definitions{},
|
||||
Responses: config.ResponseDefinitions,
|
||||
Paths: &spec.Paths{Paths: map[string]spec.PathItem{}},
|
||||
Info: config.Info,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err := o.init(webServices)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return o.swagger, nil
|
||||
}
|
||||
|
||||
func (o *openAPI) init(webServices []*restful.WebService) error {
|
||||
if o.config.GetOperationIDAndTags == nil {
|
||||
o.config.GetOperationIDAndTags = func(r *restful.Route) (string, []string, error) {
|
||||
return r.Operation, nil, nil
|
||||
@@ -83,33 +114,25 @@ func (o *openAPI) init(webServices []*restful.WebService) error {
|
||||
if o.config.CommonResponses == nil {
|
||||
o.config.CommonResponses = map[int]spec.Response{}
|
||||
}
|
||||
err := o.buildPaths(webServices)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
// finalizeSwagger is called after the spec is built and returns the final spec.
|
||||
// NOTE: finalizeSwagger also make changes to the final spec, as specified in the config.
|
||||
func (o *openAPI) finalizeSwagger() (*spec.Swagger, error) {
|
||||
if o.config.SecurityDefinitions != nil {
|
||||
o.swagger.SecurityDefinitions = *o.config.SecurityDefinitions
|
||||
o.swagger.Security = o.config.DefaultSecurity
|
||||
}
|
||||
if o.config.PostProcessSpec != nil {
|
||||
var err error
|
||||
o.swagger, err = o.config.PostProcessSpec(o.swagger)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getCanonicalizeTypeName(t reflect.Type) string {
|
||||
if t.PkgPath() == "" {
|
||||
return t.Name()
|
||||
}
|
||||
path := t.PkgPath()
|
||||
if strings.Contains(path, "/vendor/") {
|
||||
path = path[strings.Index(path, "/vendor/")+len("/vendor/"):]
|
||||
}
|
||||
return path + "." + t.Name()
|
||||
return o.swagger, nil
|
||||
}
|
||||
|
||||
func (o *openAPI) buildDefinitionRecursively(name string) error {
|
||||
@@ -143,15 +166,10 @@ func (o *openAPI) buildDefinitionRecursively(name string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// buildDefinitionForType build a definition for a given type and return a referable name to it's definition.
|
||||
// buildDefinitionForType build a definition for a given type and return a referable name to its definition.
|
||||
// This is the main function that keep track of definitions used in this spec and is depend on code generated
|
||||
// by k8s.io/kubernetes/cmd/libs/go2idl/openapi-gen.
|
||||
func (o *openAPI) buildDefinitionForType(sample interface{}) (string, error) {
|
||||
t := reflect.TypeOf(sample)
|
||||
if t.Kind() == reflect.Ptr {
|
||||
t = t.Elem()
|
||||
}
|
||||
name := getCanonicalizeTypeName(t)
|
||||
func (o *openAPI) buildDefinitionForType(name string) (string, error) {
|
||||
if err := o.buildDefinitionRecursively(name); err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -302,7 +320,7 @@ func (o *openAPI) buildOperations(route restful.Route, inPathCommonParamsMap map
|
||||
}
|
||||
|
||||
func (o *openAPI) buildResponse(model interface{}, description string) (spec.Response, error) {
|
||||
schema, err := o.toSchema(model)
|
||||
schema, err := o.toSchema(util.GetCanonicalTypeName(model))
|
||||
if err != nil {
|
||||
return spec.Response{}, err
|
||||
}
|
||||
@@ -347,8 +365,8 @@ func (o *openAPI) findCommonParameters(routes []restful.Route) (map[interface{}]
|
||||
return commonParamsMap, nil
|
||||
}
|
||||
|
||||
func (o *openAPI) toSchema(model interface{}) (_ *spec.Schema, err error) {
|
||||
if openAPIType, openAPIFormat := common.GetOpenAPITypeFormat(getCanonicalizeTypeName(reflect.TypeOf(model))); openAPIType != "" {
|
||||
func (o *openAPI) toSchema(name string) (_ *spec.Schema, err error) {
|
||||
if openAPIType, openAPIFormat := common.GetOpenAPITypeFormat(name); openAPIType != "" {
|
||||
return &spec.Schema{
|
||||
SchemaProps: spec.SchemaProps{
|
||||
Type: []string{openAPIType},
|
||||
@@ -356,7 +374,7 @@ func (o *openAPI) toSchema(model interface{}) (_ *spec.Schema, err error) {
|
||||
},
|
||||
}, nil
|
||||
} else {
|
||||
ref, err := o.buildDefinitionForType(model)
|
||||
ref, err := o.buildDefinitionForType(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -380,7 +398,7 @@ func (o *openAPI) buildParameter(restParam restful.ParameterData, bodySample int
|
||||
case restful.BodyParameterKind:
|
||||
if bodySample != nil {
|
||||
ret.In = "body"
|
||||
ret.Schema, err = o.toSchema(bodySample)
|
||||
ret.Schema, err = o.toSchema(util.GetCanonicalTypeName(bodySample))
|
||||
return ret, err
|
||||
} else {
|
||||
// There is not enough information in the body parameter to build the definition.
|
||||
|
42
vendor/k8s.io/kube-openapi/pkg/builder/openapi_test.go
generated
vendored
42
vendor/k8s.io/kube-openapi/pkg/builder/openapi_test.go
generated
vendored
@@ -30,20 +30,10 @@ import (
|
||||
)
|
||||
|
||||
// setUp is a convenience function for setting up for (most) tests.
|
||||
func setUp(t *testing.T, fullMethods bool) (openAPI, *restful.Container, *assert.Assertions) {
|
||||
func setUp(t *testing.T, fullMethods bool) (*openapi.Config, *restful.Container, *assert.Assertions) {
|
||||
assert := assert.New(t)
|
||||
config, container := getConfig(fullMethods)
|
||||
return openAPI{
|
||||
config: config,
|
||||
swagger: &spec.Swagger{
|
||||
SwaggerProps: spec.SwaggerProps{
|
||||
Swagger: OpenAPIVersion,
|
||||
Definitions: spec.Definitions{},
|
||||
Paths: &spec.Paths{Paths: map[string]spec.PathItem{}},
|
||||
Info: config.Info,
|
||||
},
|
||||
},
|
||||
}, container, assert
|
||||
return config, container, assert
|
||||
}
|
||||
|
||||
func noOp(request *restful.Request, response *restful.Response) {}
|
||||
@@ -425,8 +415,8 @@ func getTestOutputDefinition() spec.Schema {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildSwaggerSpec(t *testing.T) {
|
||||
o, container, assert := setUp(t, true)
|
||||
func TestBuildOpenAPISpec(t *testing.T) {
|
||||
config, container, assert := setUp(t, true)
|
||||
expected := &spec.Swagger{
|
||||
SwaggerProps: spec.SwaggerProps{
|
||||
Info: &spec.Info{
|
||||
@@ -449,7 +439,7 @@ func TestBuildSwaggerSpec(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
err := o.init(container.RegisteredWebServices())
|
||||
swagger, err := BuildOpenAPISpec(container.RegisteredWebServices(), config)
|
||||
if !assert.NoError(err) {
|
||||
return
|
||||
}
|
||||
@@ -457,7 +447,27 @@ func TestBuildSwaggerSpec(t *testing.T) {
|
||||
if !assert.NoError(err) {
|
||||
return
|
||||
}
|
||||
actual_json, err := json.Marshal(o.swagger)
|
||||
actual_json, err := json.Marshal(swagger)
|
||||
if !assert.NoError(err) {
|
||||
return
|
||||
}
|
||||
assert.Equal(string(expected_json), string(actual_json))
|
||||
}
|
||||
|
||||
func TestBuildOpenAPIDefinitionsForResource(t *testing.T) {
|
||||
config, _, assert := setUp(t, true)
|
||||
expected := &spec.Definitions{
|
||||
"builder.TestInput": getTestInputDefinition(),
|
||||
}
|
||||
swagger, err := BuildOpenAPIDefinitionsForResource(TestInput{}, config)
|
||||
if !assert.NoError(err) {
|
||||
return
|
||||
}
|
||||
expected_json, err := json.Marshal(expected)
|
||||
if !assert.NoError(err) {
|
||||
return
|
||||
}
|
||||
actual_json, err := json.Marshal(swagger)
|
||||
if !assert.NoError(err) {
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user