add more options for fetching openapiv3 in clients

remove serialized types from client

update tests

Kubernetes-commit: fec79e75a50f334d8384a5377513fc78222ea36a
This commit is contained in:
Alexander Zielenski
2022-10-12 11:43:46 -07:00
committed by Kubernetes Publisher
parent b6d3c8d376
commit 8b6ceae557
5 changed files with 50 additions and 26 deletions

View File

@@ -19,15 +19,19 @@ package cached
import (
"sync"
openapi_v3 "github.com/google/gnostic/openapiv3"
"k8s.io/client-go/openapi"
)
type groupversion struct {
delegate openapi.GroupVersion
once sync.Once
doc *openapi_v3.Document
err error
jsonOnce sync.Once
jsonBytes []byte
jsonErr error
pbOnce sync.Once
pbBytes []byte
pbErr error
}
func newGroupVersion(delegate openapi.GroupVersion) *groupversion {
@@ -36,10 +40,18 @@ func newGroupVersion(delegate openapi.GroupVersion) *groupversion {
}
}
func (g *groupversion) Schema() (*openapi_v3.Document, error) {
g.once.Do(func() {
g.doc, g.err = g.delegate.Schema()
func (g *groupversion) SchemaPB() ([]byte, error) {
g.pbOnce.Do(func() {
g.pbBytes, g.pbErr = g.delegate.SchemaPB()
})
return g.doc, g.err
return g.pbBytes, g.pbErr
}
func (g *groupversion) SchemaJSON() ([]byte, error) {
g.jsonOnce.Do(func() {
g.jsonBytes, g.jsonErr = g.delegate.SchemaJSON()
})
return g.jsonBytes, g.jsonErr
}

View File

@@ -19,15 +19,16 @@ package openapi
import (
"context"
openapi_v3 "github.com/google/gnostic/openapiv3"
"google.golang.org/protobuf/proto"
"k8s.io/kube-openapi/pkg/handler3"
)
const openAPIV3mimePb = "application/com.github.proto-openapi.spec.v3@v1.0+protobuf"
const jsonMime = "application/json"
type GroupVersion interface {
Schema() (*openapi_v3.Document, error)
// Raw data types
SchemaJSON() ([]byte, error)
SchemaPB() ([]byte, error)
}
type groupversion struct {
@@ -39,7 +40,7 @@ func newGroupVersion(client *client, item handler3.OpenAPIV3DiscoveryGroupVersio
return &groupversion{client: client, item: item}
}
func (g *groupversion) Schema() (*openapi_v3.Document, error) {
func (g *groupversion) SchemaPB() ([]byte, error) {
data, err := g.client.restClient.Get().
RequestURI(g.item.ServerRelativeURL).
SetHeader("Accept", openAPIV3mimePb).
@@ -49,11 +50,18 @@ func (g *groupversion) Schema() (*openapi_v3.Document, error) {
if err != nil {
return nil, err
}
return data, nil
}
document := &openapi_v3.Document{}
if err := proto.Unmarshal(data, document); err != nil {
func (g *groupversion) SchemaJSON() ([]byte, error) {
data, err := g.client.restClient.Get().
RequestURI(g.item.ServerRelativeURL).
SetHeader("Accept", jsonMime).
Do(context.TODO()).
Raw()
if err != nil {
return nil, err
}
return document, nil
return data, nil
}