2019-08-30 18:33:25 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package openapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
openapi_v2 "github.com/googleapis/gnostic/openapiv2"
|
2019-08-30 18:33:25 +00:00
|
|
|
"k8s.io/client-go/discovery"
|
|
|
|
)
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
// CachedOpenAPIGetter fetches the openapi schema once and then caches it in memory
|
|
|
|
type CachedOpenAPIGetter struct {
|
|
|
|
openAPIClient discovery.OpenAPISchemaInterface
|
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
// Cached results
|
|
|
|
sync.Once
|
2021-03-18 22:40:29 +00:00
|
|
|
openAPISchema *openapi_v2.Document
|
2019-08-30 18:33:25 +00:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
var _ discovery.OpenAPISchemaInterface = &CachedOpenAPIGetter{}
|
2019-08-30 18:33:25 +00:00
|
|
|
|
|
|
|
// NewOpenAPIGetter returns an object to return OpenAPIDatas which reads
|
|
|
|
// from a server, and then stores in memory for subsequent invocations
|
2021-03-18 22:40:29 +00:00
|
|
|
func NewOpenAPIGetter(openAPIClient discovery.OpenAPISchemaInterface) *CachedOpenAPIGetter {
|
|
|
|
return &CachedOpenAPIGetter{
|
2019-08-30 18:33:25 +00:00
|
|
|
openAPIClient: openAPIClient,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
// OpenAPISchema implements OpenAPISchemaInterface.
|
|
|
|
func (g *CachedOpenAPIGetter) OpenAPISchema() (*openapi_v2.Document, error) {
|
2019-08-30 18:33:25 +00:00
|
|
|
g.Do(func() {
|
2021-03-18 22:40:29 +00:00
|
|
|
g.openAPISchema, g.err = g.openAPIClient.OpenAPISchema()
|
|
|
|
})
|
|
|
|
|
|
|
|
// Return the saved result.
|
|
|
|
return g.openAPISchema, g.err
|
|
|
|
}
|
|
|
|
|
|
|
|
type CachedOpenAPIParser struct {
|
|
|
|
openAPIClient discovery.OpenAPISchemaInterface
|
|
|
|
|
|
|
|
// Cached results
|
|
|
|
sync.Once
|
|
|
|
openAPIResources Resources
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOpenAPIParser(openAPIClient discovery.OpenAPISchemaInterface) *CachedOpenAPIParser {
|
|
|
|
return &CachedOpenAPIParser{
|
|
|
|
openAPIClient: openAPIClient,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CachedOpenAPIParser) Parse() (Resources, error) {
|
|
|
|
p.Do(func() {
|
|
|
|
oapi, err := p.openAPIClient.OpenAPISchema()
|
2019-08-30 18:33:25 +00:00
|
|
|
if err != nil {
|
2021-03-18 22:40:29 +00:00
|
|
|
p.err = err
|
2019-08-30 18:33:25 +00:00
|
|
|
return
|
|
|
|
}
|
2021-03-18 22:40:29 +00:00
|
|
|
p.openAPIResources, p.err = NewOpenAPIData(oapi)
|
2019-08-30 18:33:25 +00:00
|
|
|
})
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
return p.openAPIResources, p.err
|
2019-08-30 18:33:25 +00:00
|
|
|
}
|