-
Notifications
You must be signed in to change notification settings - Fork 226
Lazy marshaling for OpenAPI v2 spec #251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
k8s-ci-robot
merged 1 commit into
kubernetes:master
from
DangerOnTheRanger:lazy-marshaling
Nov 4, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ import ( | |
openapi_v2 "github.com/googleapis/gnostic/openapiv2" | ||
"github.com/munnerz/goautoneg" | ||
"gopkg.in/yaml.v2" | ||
klog "k8s.io/klog/v2" | ||
"k8s.io/kube-openapi/pkg/builder" | ||
"k8s.io/kube-openapi/pkg/common" | ||
"k8s.io/kube-openapi/pkg/validation/spec" | ||
|
@@ -55,13 +56,40 @@ type OpenAPIService struct { | |
|
||
lastModified time.Time | ||
|
||
specBytes []byte | ||
specPb []byte | ||
specPbGz []byte | ||
jsonCache cache | ||
protoCache cache | ||
} | ||
|
||
type cache struct { | ||
BuildCache func() ([]byte, error) | ||
once sync.Once | ||
bytes []byte | ||
etag string | ||
err error | ||
} | ||
|
||
specBytesETag string | ||
specPbETag string | ||
specPbGzETag string | ||
func (c *cache) Get() ([]byte, string, error) { | ||
c.once.Do(func() { | ||
DangerOnTheRanger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
bytes, err := c.BuildCache() | ||
DangerOnTheRanger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// if there is an error updating the cache, there can be situations where | ||
// c.bytes contains a valid value (carried over from the previous update) | ||
// but c.err is also not nil; the cache user is expected to check for this | ||
c.err = err | ||
if c.err == nil { | ||
// don't override previous spec if we had an error | ||
c.bytes = bytes | ||
c.etag = computeETag(c.bytes) | ||
} | ||
DangerOnTheRanger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
return c.bytes, c.etag, c.err | ||
DangerOnTheRanger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func (c *cache) New(cacheBuilder func() ([]byte, error)) cache { | ||
return cache{ | ||
bytes: c.bytes, | ||
etag: c.etag, | ||
BuildCache: cacheBuilder, | ||
} | ||
} | ||
|
||
func init() { | ||
|
@@ -71,6 +99,9 @@ func init() { | |
} | ||
|
||
func computeETag(data []byte) string { | ||
if data == nil { | ||
return "" | ||
DangerOnTheRanger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return fmt.Sprintf("\"%X\"", sha512.Sum512(data)) | ||
} | ||
|
||
|
@@ -83,51 +114,40 @@ func NewOpenAPIService(spec *spec.Swagger) (*OpenAPIService, error) { | |
return o, nil | ||
} | ||
|
||
func (o *OpenAPIService) getSwaggerBytes() ([]byte, string, time.Time) { | ||
o.rwMutex.RLock() | ||
defer o.rwMutex.RUnlock() | ||
return o.specBytes, o.specBytesETag, o.lastModified | ||
} | ||
|
||
func (o *OpenAPIService) getSwaggerPbBytes() ([]byte, string, time.Time) { | ||
func (o *OpenAPIService) getSwaggerBytes() ([]byte, string, time.Time, error) { | ||
o.rwMutex.RLock() | ||
defer o.rwMutex.RUnlock() | ||
return o.specPb, o.specPbETag, o.lastModified | ||
specBytes, etag, err := o.jsonCache.Get() | ||
if err != nil { | ||
return nil, "", time.Time{}, err | ||
} | ||
return specBytes, etag, o.lastModified, nil | ||
} | ||
|
||
func (o *OpenAPIService) getSwaggerPbGzBytes() ([]byte, string, time.Time) { | ||
func (o *OpenAPIService) getSwaggerPbBytes() ([]byte, string, time.Time, error) { | ||
o.rwMutex.RLock() | ||
defer o.rwMutex.RUnlock() | ||
return o.specPbGz, o.specPbGzETag, o.lastModified | ||
} | ||
|
||
func (o *OpenAPIService) UpdateSpec(openapiSpec *spec.Swagger) (err error) { | ||
specBytes, err := json.Marshal(openapiSpec) | ||
specPb, etag, err := o.protoCache.Get() | ||
if err != nil { | ||
return err | ||
return nil, "", time.Time{}, err | ||
} | ||
specPb, err := ToProtoBinary(specBytes) | ||
if err != nil { | ||
return err | ||
} | ||
specPbGz := toGzip(specPb) | ||
|
||
specBytesETag := computeETag(specBytes) | ||
specPbETag := computeETag(specPb) | ||
specPbGzETag := computeETag(specPbGz) | ||
|
||
lastModified := time.Now() | ||
return specPb, etag, o.lastModified, nil | ||
} | ||
|
||
func (o *OpenAPIService) UpdateSpec(openapiSpec *spec.Swagger) (err error) { | ||
o.rwMutex.Lock() | ||
defer o.rwMutex.Unlock() | ||
|
||
o.specBytes = specBytes | ||
o.specPb = specPb | ||
o.specPbGz = specPbGz | ||
o.specBytesETag = specBytesETag | ||
o.specPbETag = specPbETag | ||
o.specPbGzETag = specPbGzETag | ||
o.lastModified = lastModified | ||
o.jsonCache = o.jsonCache.New(func() ([]byte, error) { | ||
return json.Marshal(openapiSpec) | ||
}) | ||
o.protoCache = o.protoCache.New(func() ([]byte, error) { | ||
json, _, err := o.jsonCache.Get() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return ToProtoBinary(json) | ||
}) | ||
o.lastModified = time.Now() | ||
|
||
return nil | ||
} | ||
|
@@ -206,7 +226,7 @@ func (o *OpenAPIService) RegisterOpenAPIVersionedService(servePath string, handl | |
accepted := []struct { | ||
Type string | ||
SubType string | ||
GetDataAndETag func() ([]byte, string, time.Time) | ||
GetDataAndETag func() ([]byte, string, time.Time, error) | ||
}{ | ||
{"application", "json", o.getSwaggerBytes}, | ||
{"application", "[email protected]+protobuf", o.getSwaggerPbBytes}, | ||
|
@@ -230,7 +250,15 @@ func (o *OpenAPIService) RegisterOpenAPIVersionedService(servePath string, handl | |
} | ||
|
||
// serve the first matching media type in the sorted clause list | ||
data, etag, lastModified := accepts.GetDataAndETag() | ||
data, etag, lastModified, err := accepts.GetDataAndETag() | ||
if err != nil { | ||
klog.Errorf("Error in OpenAPI handler: %s", err) | ||
// only return a 503 if we have no older cache data to serve | ||
if data == nil { | ||
w.WriteHeader(http.StatusServiceUnavailable) | ||
return | ||
} | ||
} | ||
w.Header().Set("Etag", etag) | ||
// ServeContent will take care of caching using eTag. | ||
http.ServeContent(w, r, servePath, lastModified, bytes.NewReader(data)) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.