refactor to use Schema(contentType)

Kubernetes-commit: e6e6dd826d76a2b19515cd907062f1f74f9e52c2
This commit is contained in:
Alexander Zielenski
2022-10-14 12:46:36 -07:00
committed by Kubernetes Publisher
parent 8b6ceae557
commit 12cafe2b1b
6 changed files with 171 additions and 128 deletions

View File

@@ -25,13 +25,13 @@ import (
type groupversion struct {
delegate openapi.GroupVersion
jsonOnce sync.Once
jsonBytes []byte
jsonErr error
lock sync.Mutex
docs map[string]docInfo
}
pbOnce sync.Once
pbBytes []byte
pbErr error
type docInfo struct {
data []byte
err error
}
func newGroupVersion(delegate openapi.GroupVersion) *groupversion {
@@ -40,18 +40,19 @@ func newGroupVersion(delegate openapi.GroupVersion) *groupversion {
}
}
func (g *groupversion) SchemaPB() ([]byte, error) {
g.pbOnce.Do(func() {
g.pbBytes, g.pbErr = g.delegate.SchemaPB()
})
func (g *groupversion) Schema(contentType string) ([]byte, error) {
g.lock.Lock()
defer g.lock.Unlock()
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
cachedInfo, ok := g.docs[contentType]
if !ok {
if g.docs == nil {
g.docs = make(map[string]docInfo)
}
cachedInfo.data, cachedInfo.err = g.delegate.Schema(contentType)
g.docs[contentType] = cachedInfo
}
return cachedInfo.data, cachedInfo.err
}

View File

@@ -22,13 +22,10 @@ import (
"k8s.io/kube-openapi/pkg/handler3"
)
const openAPIV3mimePb = "application/com.github.proto-openapi.spec.v3@v1.0+protobuf"
const jsonMime = "application/json"
const ContentTypeOpenAPIV3PB = "application/com.github.proto-openapi.spec.v3@v1.0+protobuf"
type GroupVersion interface {
// Raw data types
SchemaJSON() ([]byte, error)
SchemaPB() ([]byte, error)
Schema(contentType string) ([]byte, error)
}
type groupversion struct {
@@ -40,23 +37,10 @@ func newGroupVersion(client *client, item handler3.OpenAPIV3DiscoveryGroupVersio
return &groupversion{client: client, item: item}
}
func (g *groupversion) SchemaPB() ([]byte, error) {
func (g *groupversion) Schema(contentType string) ([]byte, error) {
data, err := g.client.restClient.Get().
RequestURI(g.item.ServerRelativeURL).
SetHeader("Accept", openAPIV3mimePb).
Do(context.TODO()).
Raw()
if err != nil {
return nil, err
}
return data, nil
}
func (g *groupversion) SchemaJSON() ([]byte, error) {
data, err := g.client.restClient.Get().
RequestURI(g.item.ServerRelativeURL).
SetHeader("Accept", jsonMime).
SetHeader("Accept", contentType).
Do(context.TODO()).
Raw()