openapi: Fetch protobuf rather than Json

This is much faster.

Kubernetes-commit: 224dba9a136e3e4014d88b3c2546418066bac026
This commit is contained in:
Antoine Pelisse 2017-05-26 14:00:24 -07:00 committed by Kubernetes Publisher
parent 5e440ec3fc
commit 13c848bad2
7 changed files with 89 additions and 50 deletions

36
Godeps/Godeps.json generated
View File

@ -98,10 +98,6 @@
"ImportPath": "github.com/ghodss/yaml", "ImportPath": "github.com/ghodss/yaml",
"Rev": "73d445a93680fa1a78ae23a5839bad48f32ba1ee" "Rev": "73d445a93680fa1a78ae23a5839bad48f32ba1ee"
}, },
{
"ImportPath": "github.com/go-openapi/analysis",
"Rev": "b44dc874b601d9e4e2f6e19140e794ba24bead3b"
},
{ {
"ImportPath": "github.com/go-openapi/jsonpointer", "ImportPath": "github.com/go-openapi/jsonpointer",
"Rev": "46af16f9f7b149af66e5d1bd010e3574dc06de98" "Rev": "46af16f9f7b149af66e5d1bd010e3574dc06de98"
@ -110,10 +106,6 @@
"ImportPath": "github.com/go-openapi/jsonreference", "ImportPath": "github.com/go-openapi/jsonreference",
"Rev": "13c6e3589ad90f49bd3e3bbe2c2cb3d7a4142272" "Rev": "13c6e3589ad90f49bd3e3bbe2c2cb3d7a4142272"
}, },
{
"ImportPath": "github.com/go-openapi/loads",
"Rev": "18441dfa706d924a39a030ee2c3b1d8d81917b38"
},
{ {
"ImportPath": "github.com/go-openapi/spec", "ImportPath": "github.com/go-openapi/spec",
"Rev": "6aced65f8501fe1217321abf0749d354824ba2ff" "Rev": "6aced65f8501fe1217321abf0749d354824ba2ff"
@ -142,10 +134,38 @@
"ImportPath": "github.com/golang/protobuf/proto", "ImportPath": "github.com/golang/protobuf/proto",
"Rev": "4bd1920723d7b7c925de087aa32e2187708897f7" "Rev": "4bd1920723d7b7c925de087aa32e2187708897f7"
}, },
{
"ImportPath": "github.com/golang/protobuf/ptypes",
"Rev": "4bd1920723d7b7c925de087aa32e2187708897f7"
},
{
"ImportPath": "github.com/golang/protobuf/ptypes/any",
"Rev": "4bd1920723d7b7c925de087aa32e2187708897f7"
},
{
"ImportPath": "github.com/golang/protobuf/ptypes/duration",
"Rev": "4bd1920723d7b7c925de087aa32e2187708897f7"
},
{
"ImportPath": "github.com/golang/protobuf/ptypes/timestamp",
"Rev": "4bd1920723d7b7c925de087aa32e2187708897f7"
},
{ {
"ImportPath": "github.com/google/gofuzz", "ImportPath": "github.com/google/gofuzz",
"Rev": "44d81051d367757e1c7c6a5a86423ece9afcf63c" "Rev": "44d81051d367757e1c7c6a5a86423ece9afcf63c"
}, },
{
"ImportPath": "github.com/googleapis/gnostic/OpenAPIv2",
"Rev": "68f4ded48ba9414dab2ae69b3f0d69971da73aa5"
},
{
"ImportPath": "github.com/googleapis/gnostic/compiler",
"Rev": "68f4ded48ba9414dab2ae69b3f0d69971da73aa5"
},
{
"ImportPath": "github.com/googleapis/gnostic/extensions",
"Rev": "68f4ded48ba9414dab2ae69b3f0d69971da73aa5"
},
{ {
"ImportPath": "github.com/hashicorp/golang-lru", "ImportPath": "github.com/hashicorp/golang-lru",
"Rev": "a0d98a5f288019575c6d1f4bb1573fef2d1fcdc4" "Rev": "a0d98a5f288019575c6d1f4bb1573fef2d1fcdc4"

View File

@ -19,9 +19,9 @@ go_library(
tags = ["automanaged"], tags = ["automanaged"],
deps = [ deps = [
"//vendor/github.com/emicklei/go-restful-swagger12:go_default_library", "//vendor/github.com/emicklei/go-restful-swagger12:go_default_library",
"//vendor/github.com/go-openapi/loads:go_default_library",
"//vendor/github.com/go-openapi/spec:go_default_library",
"//vendor/github.com/golang/glog:go_default_library", "//vendor/github.com/golang/glog:go_default_library",
"//vendor/github.com/golang/protobuf/proto:go_default_library",
"//vendor/github.com/googleapis/gnostic/OpenAPIv2:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library", "//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library", "//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/meta:go_default_library", "//vendor/k8s.io/apimachinery/pkg/api/meta:go_default_library",
@ -46,7 +46,8 @@ go_test(
tags = ["automanaged"], tags = ["automanaged"],
deps = [ deps = [
"//vendor/github.com/emicklei/go-restful-swagger12:go_default_library", "//vendor/github.com/emicklei/go-restful-swagger12:go_default_library",
"//vendor/github.com/go-openapi/spec:go_default_library", "//vendor/github.com/gogo/protobuf/proto:go_default_library",
"//vendor/github.com/googleapis/gnostic/OpenAPIv2:go_default_library",
"//vendor/github.com/stretchr/testify/assert:go_default_library", "//vendor/github.com/stretchr/testify/assert:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library", "//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library", "//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",

View File

@ -24,9 +24,9 @@ import (
"strings" "strings"
"github.com/emicklei/go-restful-swagger12" "github.com/emicklei/go-restful-swagger12"
"github.com/golang/protobuf/proto"
"github.com/googleapis/gnostic/OpenAPIv2"
"github.com/go-openapi/loads"
"github.com/go-openapi/spec"
"k8s.io/api/core/v1" "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@ -97,7 +97,7 @@ type SwaggerSchemaInterface interface {
// OpenAPISchemaInterface has a method to retrieve the open API schema. // OpenAPISchemaInterface has a method to retrieve the open API schema.
type OpenAPISchemaInterface interface { type OpenAPISchemaInterface interface {
// OpenAPISchema retrieves and parses the swagger API schema the server supports. // OpenAPISchema retrieves and parses the swagger API schema the server supports.
OpenAPISchema() (*spec.Swagger, error) OpenAPISchema() (*openapi_v2.Document, error)
} }
// DiscoveryClient implements the functions that discover server-supported API groups, // DiscoveryClient implements the functions that discover server-supported API groups,
@ -375,19 +375,18 @@ func (d *DiscoveryClient) SwaggerSchema(version schema.GroupVersion) (*swagger.A
return &schema, nil return &schema, nil
} }
// OpenAPISchema fetches the open api schema using a rest client and parses the json. // OpenAPISchema fetches the open api schema using a rest client and parses the proto.
// Warning: this is very expensive (~1.2s) func (d *DiscoveryClient) OpenAPISchema() (*openapi_v2.Document, error) {
func (d *DiscoveryClient) OpenAPISchema() (*spec.Swagger, error) { data, err := d.restClient.Get().AbsPath("/swagger-2.0.0.pb-v1").Do().Raw()
data, err := d.restClient.Get().AbsPath("/swagger.json").Do().Raw()
if err != nil { if err != nil {
return nil, err return nil, err
} }
msg := json.RawMessage(data) document := &openapi_v2.Document{}
doc, err := loads.Analyzed(msg, "") err = proto.Unmarshal(data, document)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return doc.Spec(), err return document, nil
} }
// withRetries retries the given recovery function in case the groups supported by the server change after ServerGroup() returns. // withRetries retries the given recovery function in case the groups supported by the server change after ServerGroup() returns.

View File

@ -19,14 +19,16 @@ package discovery_test
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"mime"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"reflect" "reflect"
"testing" "testing"
"github.com/emicklei/go-restful-swagger12" "github.com/emicklei/go-restful-swagger12"
"github.com/gogo/protobuf/proto"
"github.com/googleapis/gnostic/OpenAPIv2"
"github.com/go-openapi/spec"
"k8s.io/api/core/v1" "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
@ -327,30 +329,44 @@ func TestGetSwaggerSchemaFail(t *testing.T) {
} }
} }
var returnedOpenAPI = spec.Swagger{ var returnedOpenAPI = openapi_v2.Document{
SwaggerProps: spec.SwaggerProps{ Definitions: &openapi_v2.Definitions{
Definitions: spec.Definitions{ AdditionalProperties: []*openapi_v2.NamedSchema{
"fake.type.1": spec.Schema{ {
SchemaProps: spec.SchemaProps{ Name: "fake.type.1",
Properties: map[string]spec.Schema{ Value: &openapi_v2.Schema{
"count": { Properties: &openapi_v2.Properties{
SchemaProps: spec.SchemaProps{ AdditionalProperties: []*openapi_v2.NamedSchema{
Type: []string{"integer"}, {
Name: "count",
Value: &openapi_v2.Schema{
Type: &openapi_v2.TypeItem{
Value: []string{"integer"},
}, },
}, },
}, },
}, },
}, },
"fake.type.2": spec.Schema{ },
SchemaProps: spec.SchemaProps{ },
Properties: map[string]spec.Schema{ {
"count": { Name: "fake.type.2",
SchemaProps: spec.SchemaProps{ Value: &openapi_v2.Schema{
Type: []string{"array"}, Properties: &openapi_v2.Properties{
Items: &spec.SchemaOrArray{ AdditionalProperties: []*openapi_v2.NamedSchema{
Schema: &spec.Schema{ {
SchemaProps: spec.SchemaProps{ Name: "count",
Type: []string{"string"}, Value: &openapi_v2.Schema{
Type: &openapi_v2.TypeItem{
Value: []string{"array"},
},
Items: &openapi_v2.ItemsItem{
Schema: []*openapi_v2.Schema{
{
Type: &openapi_v2.TypeItem{
Value: []string{"string"},
},
},
}, },
}, },
}, },
@ -366,19 +382,20 @@ var returnedOpenAPI = spec.Swagger{
func openapiSchemaFakeServer() (*httptest.Server, error) { func openapiSchemaFakeServer() (*httptest.Server, error) {
var sErr error var sErr error
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/swagger.json" { if req.URL.Path != "/swagger-2.0.0.pb-v1" {
sErr = fmt.Errorf("Unexpected url %v", req.URL) sErr = fmt.Errorf("Unexpected url %v", req.URL)
} }
if req.Method != "GET" { if req.Method != "GET" {
sErr = fmt.Errorf("Unexpected method %v", req.Method) sErr = fmt.Errorf("Unexpected method %v", req.Method)
} }
output, err := json.Marshal(returnedOpenAPI) mime.AddExtensionType(".pb-v1", "application/com.github.googleapis.gnostic.OpenAPIv2@68f4ded+protobuf")
output, err := proto.Marshal(&returnedOpenAPI)
if err != nil { if err != nil {
sErr = err sErr = err
return return
} }
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
w.Write(output) w.Write(output)
})) }))

View File

@ -13,7 +13,7 @@ go_library(
tags = ["automanaged"], tags = ["automanaged"],
deps = [ deps = [
"//vendor/github.com/emicklei/go-restful-swagger12:go_default_library", "//vendor/github.com/emicklei/go-restful-swagger12:go_default_library",
"//vendor/github.com/go-openapi/spec:go_default_library", "//vendor/github.com/googleapis/gnostic/OpenAPIv2:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library", "//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library", "//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",

View File

@ -20,8 +20,8 @@ import (
"fmt" "fmt"
"github.com/emicklei/go-restful-swagger12" "github.com/emicklei/go-restful-swagger12"
"github.com/googleapis/gnostic/OpenAPIv2"
"github.com/go-openapi/spec"
"k8s.io/api/core/v1" "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
@ -93,7 +93,9 @@ func (c *FakeDiscovery) SwaggerSchema(version schema.GroupVersion) (*swagger.Api
return &swagger.ApiDeclaration{}, nil return &swagger.ApiDeclaration{}, nil
} }
func (c *FakeDiscovery) OpenAPISchema() (*spec.Swagger, error) { return &spec.Swagger{}, nil } func (c *FakeDiscovery) OpenAPISchema() (*openapi_v2.Document, error) {
return &openapi_v2.Document{}, nil
}
func (c *FakeDiscovery) RESTClient() restclient.Interface { func (c *FakeDiscovery) RESTClient() restclient.Interface {
return nil return nil

View File

@ -29,7 +29,7 @@ import (
"k8s.io/client-go/rest/fake" "k8s.io/client-go/rest/fake"
"github.com/emicklei/go-restful-swagger12" "github.com/emicklei/go-restful-swagger12"
"github.com/go-openapi/spec" "github.com/googleapis/gnostic/OpenAPIv2"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -348,6 +348,6 @@ func (c *fakeCachedDiscoveryInterface) SwaggerSchema(version schema.GroupVersion
return &swagger.ApiDeclaration{}, nil return &swagger.ApiDeclaration{}, nil
} }
func (c *fakeCachedDiscoveryInterface) OpenAPISchema() (*spec.Swagger, error) { func (c *fakeCachedDiscoveryInterface) OpenAPISchema() (*openapi_v2.Document, error) {
return &spec.Swagger{}, nil return &openapi_v2.Document{}, nil
} }