Fall back to JSON request encoding after CBOR 415.

If a client is configured to encode request bodies to CBOR, but the server does not support CBOR,
the server will respond with HTTP 415 (Unsupported Media Type). By feeding this response back to the
RESTClient, subsequent requests can fall back to JSON, which is assumed to be acceptable.

Kubernetes-commit: 1745dfdd154b1a838765e70b81c861c644bfcffe
This commit is contained in:
Ben Luddy
2024-10-22 17:40:08 -04:00
committed by Kubernetes Publisher
parent b3f6edec9a
commit 15af21a2ae
2 changed files with 65 additions and 7 deletions

View File

@@ -156,7 +156,7 @@ func NewRequest(c *RESTClient) *Request {
timeout = c.Client.Timeout
}
contentConfig := c.content
contentConfig := c.content.GetClientContentConfig()
contentTypeNotSet := len(contentConfig.ContentType) == 0
if contentTypeNotSet {
contentConfig.ContentType = "application/json"
@@ -188,7 +188,7 @@ func NewRequestWithClient(base *url.URL, versionedAPIPath string, content Client
return NewRequest(&RESTClient{
base: base,
versionedAPIPath: versionedAPIPath,
content: content,
content: requestClientContentConfigProvider{base: content},
Client: client,
})
}
@@ -1235,6 +1235,9 @@ func (r *Request) request(ctx context.Context, fn func(*http.Request, *http.Resp
if req.ContentLength >= 0 && !(req.Body != nil && req.ContentLength == 0) {
metrics.RequestSize.Observe(ctx, r.verb, r.URL().Host, float64(req.ContentLength))
}
if resp != nil && resp.StatusCode == http.StatusUnsupportedMediaType {
r.c.content.UnsupportedMediaType(resp.Request.Header.Get("Content-Type"))
}
retry.After(ctx, r, resp, err)
done := func() bool {