mirror of
https://github.com/kubernetes/client-go.git
synced 2025-06-24 14:12:18 +00:00
Revert caching in favor of simple ttl
Kubernetes-commit: 1122091b065b5ce37a93b1048174acaff243bf74
This commit is contained in:
parent
784e3fbb2f
commit
b5bb80748a
@ -58,17 +58,14 @@ func (c *gvkParserCache) objectTypeForGVK(gvk schema.GroupVersionKind) (*typed.P
|
|||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
defer c.mu.Unlock()
|
||||||
// if the ttl on the openAPISchema has expired,
|
// if the ttl on the openAPISchema has expired,
|
||||||
// recheck the discovery client to see if the Open API schema has changed
|
// regenerate the gvk parser
|
||||||
if time.Now().After(c.lastChecked.Add(openAPISchemaTTL)) {
|
if time.Now().After(c.lastChecked.Add(openAPISchemaTTL)) {
|
||||||
c.lastChecked = time.Now()
|
c.lastChecked = time.Now()
|
||||||
if c.discoveryClient.HasOpenAPISchemaChanged() {
|
parser, err := regenerateGVKParser(c.discoveryClient)
|
||||||
// the schema has changed, regenerate the parser
|
if err != nil {
|
||||||
parser, err := regenerateGVKParser(c.discoveryClient)
|
return nil, err
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
c.gvkParser = parser
|
|
||||||
}
|
}
|
||||||
|
c.gvkParser = parser
|
||||||
}
|
}
|
||||||
return c.gvkParser.Type(gvk), nil
|
return c.gvkParser.Type(gvk), nil
|
||||||
}
|
}
|
||||||
|
@ -240,11 +240,6 @@ func (d *CachedDiscoveryClient) OpenAPISchema() (*openapi_v2.Document, error) {
|
|||||||
return d.delegate.OpenAPISchema()
|
return d.delegate.OpenAPISchema()
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasOpenAPISchemaChanged checks wether the open API schema being served is cached or not.
|
|
||||||
func (d *CachedDiscoveryClient) HasOpenAPISchemaChanged() bool {
|
|
||||||
return d.delegate.HasOpenAPISchemaChanged()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fresh is supposed to tell the caller whether or not to retry if the cache
|
// Fresh is supposed to tell the caller whether or not to retry if the cache
|
||||||
// fails to find something (false = retry, true = no need to retry).
|
// fails to find something (false = retry, true = no need to retry).
|
||||||
func (d *CachedDiscoveryClient) Fresh() bool {
|
func (d *CachedDiscoveryClient) Fresh() bool {
|
||||||
|
@ -149,10 +149,6 @@ func (d *memCacheClient) OpenAPISchema() (*openapi_v2.Document, error) {
|
|||||||
return d.delegate.OpenAPISchema()
|
return d.delegate.OpenAPISchema()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *memCacheClient) HasOpenAPISchemaChanged() bool {
|
|
||||||
return d.delegate.HasOpenAPISchemaChanged()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *memCacheClient) Fresh() bool {
|
func (d *memCacheClient) Fresh() bool {
|
||||||
d.lock.RLock()
|
d.lock.RLock()
|
||||||
defer d.lock.RUnlock()
|
defer d.lock.RUnlock()
|
||||||
|
@ -121,15 +121,10 @@ type ServerVersionInterface interface {
|
|||||||
ServerVersion() (*version.Info, error)
|
ServerVersion() (*version.Info, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// OpenAPISchemaInterface has a method to retrieve the open API schema
|
// OpenAPISchemaInterface has a method to retrieve the open API schema.
|
||||||
// and a method to check whether the open API schema has changed.
|
|
||||||
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() (*openapi_v2.Document, error)
|
OpenAPISchema() (*openapi_v2.Document, error)
|
||||||
|
|
||||||
// HasOpenAPISchema changed checks whether the API schema being served
|
|
||||||
// by the apiserver is cached (and thus has not changed).
|
|
||||||
HasOpenAPISchemaChanged() bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DiscoveryClient implements the functions that discover server-supported API groups,
|
// DiscoveryClient implements the functions that discover server-supported API groups,
|
||||||
@ -424,12 +419,6 @@ func (d *DiscoveryClient) ServerVersion() (*version.Info, error) {
|
|||||||
return &info, nil
|
return &info, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasOpenAPISchemaChanged checks whether a HEAD request to openapi endpoint returns
|
|
||||||
// a 304 StatusNotModified meaning it has not changed.
|
|
||||||
func (d *DiscoveryClient) HasOpenAPISchemaChanged() bool {
|
|
||||||
return !d.restClient.Verb("HEAD").AbsPath("/openapi/v2").SetHeader("Accept", mimePb).Do(context.TODO()).FromCache()
|
|
||||||
}
|
|
||||||
|
|
||||||
// OpenAPISchema fetches the open api schema using a rest client and parses the proto.
|
// OpenAPISchema fetches the open api schema using a rest client and parses the proto.
|
||||||
func (d *DiscoveryClient) OpenAPISchema() (*openapi_v2.Document, error) {
|
func (d *DiscoveryClient) OpenAPISchema() (*openapi_v2.Document, error) {
|
||||||
data, err := d.restClient.Get().AbsPath("/openapi/v2").SetHeader("Accept", mimePb).Do(context.TODO()).Raw()
|
data, err := d.restClient.Get().AbsPath("/openapi/v2").SetHeader("Accept", mimePb).Do(context.TODO()).Raw()
|
||||||
|
@ -153,11 +153,6 @@ func (c *FakeDiscovery) OpenAPISchema() (*openapi_v2.Document, error) {
|
|||||||
return &openapi_v2.Document{}, nil
|
return &openapi_v2.Document{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasOpenAPISchemaChanged checks wether the open API schema being served is cached or not.
|
|
||||||
func (c *FakeDiscovery) HasOpenAPISchemaChanged() bool {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// RESTClient returns a RESTClient that is used to communicate with API server
|
// RESTClient returns a RESTClient that is used to communicate with API server
|
||||||
// by this client implementation.
|
// by this client implementation.
|
||||||
func (c *FakeDiscovery) RESTClient() restclient.Interface {
|
func (c *FakeDiscovery) RESTClient() restclient.Interface {
|
||||||
|
@ -1140,21 +1140,12 @@ func (r *Request) transformResponse(resp *http.Response, req *http.Request) Resu
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// store the X-From-Cache header so that we can
|
|
||||||
// return it as part of the result
|
|
||||||
var fromCache bool
|
|
||||||
xFromCacheHeader, ok := resp.Header["X-From-Cache"]
|
|
||||||
if ok {
|
|
||||||
fromCache = len(xFromCacheHeader) == 1 && xFromCacheHeader[0] == "1"
|
|
||||||
}
|
|
||||||
|
|
||||||
return Result{
|
return Result{
|
||||||
body: body,
|
body: body,
|
||||||
contentType: contentType,
|
contentType: contentType,
|
||||||
statusCode: resp.StatusCode,
|
statusCode: resp.StatusCode,
|
||||||
decoder: decoder,
|
decoder: decoder,
|
||||||
warnings: handleWarnings(resp.Header, r.warningHandler),
|
warnings: handleWarnings(resp.Header, r.warningHandler),
|
||||||
fromCache: fromCache,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1281,7 +1272,6 @@ type Result struct {
|
|||||||
contentType string
|
contentType string
|
||||||
err error
|
err error
|
||||||
statusCode int
|
statusCode int
|
||||||
fromCache bool
|
|
||||||
|
|
||||||
decoder runtime.Decoder
|
decoder runtime.Decoder
|
||||||
}
|
}
|
||||||
@ -1318,11 +1308,6 @@ func (r Result) Get() (runtime.Object, error) {
|
|||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// FromCache returns whether the response was returned from the cache.
|
|
||||||
func (r Result) FromCache() bool {
|
|
||||||
return r.fromCache
|
|
||||||
}
|
|
||||||
|
|
||||||
// StatusCode returns the HTTP status code of the request. (Only valid if no
|
// StatusCode returns the HTTP status code of the request. (Only valid if no
|
||||||
// error was returned.)
|
// error was returned.)
|
||||||
func (r Result) StatusCode(statusCode *int) Result {
|
func (r Result) StatusCode(statusCode *int) Result {
|
||||||
|
Loading…
Reference in New Issue
Block a user