k3s/vendor/k8s.io/kubectl/pkg/util/openapi/openapi_getter.go

83 lines
2.1 KiB
Go
Raw Normal View History

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"
openapi_v2 "github.com/googleapis/gnostic/openapiv2"
2019-08-30 18:33:25 +00:00
"k8s.io/client-go/discovery"
)
// 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
openAPISchema *openapi_v2.Document
2019-08-30 18:33:25 +00:00
err error
}
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
func NewOpenAPIGetter(openAPIClient discovery.OpenAPISchemaInterface) *CachedOpenAPIGetter {
return &CachedOpenAPIGetter{
2019-08-30 18:33:25 +00:00
openAPIClient: openAPIClient,
}
}
// OpenAPISchema implements OpenAPISchemaInterface.
func (g *CachedOpenAPIGetter) OpenAPISchema() (*openapi_v2.Document, error) {
2019-08-30 18:33:25 +00:00
g.Do(func() {
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 {
p.err = err
2019-08-30 18:33:25 +00:00
return
}
p.openAPIResources, p.err = NewOpenAPIData(oapi)
2019-08-30 18:33:25 +00:00
})
return p.openAPIResources, p.err
2019-08-30 18:33:25 +00:00
}