Fix content type fallback when a client defaults to CBOR.

With the ClientsAllowCBOR client-go feature gate enabled, a 415 response to a CBOR-encoded REST
causes all subsequent requests from the client to fall back to a JSON request encoding. This
mechanism had only worked as intended when CBOR was explicitly configured in the
ClientContentConfig. When both ClientsAllowCBOR and ClientsPreferCBOR are enabled, an
unconfigured (empty) content type defaults to CBOR instead of JSON. Both ways of configuring a
client to use the CBOR request encoding are now subject to the same fallback mechanism.

Kubernetes-commit: a77f4c7ba2e761461daaf115a38903fc91916dd6
This commit is contained in:
Ben Luddy 2024-11-07 00:05:03 -05:00 committed by Kubernetes Publisher
parent c57dbd8dec
commit 49537610e8
2 changed files with 27 additions and 20 deletions

View File

@ -238,7 +238,8 @@ func (c *RESTClient) Delete() *Request {
// APIVersion returns the APIVersion this RESTClient is expected to use. // APIVersion returns the APIVersion this RESTClient is expected to use.
func (c *RESTClient) APIVersion() schema.GroupVersion { func (c *RESTClient) APIVersion() schema.GroupVersion {
return c.content.GetClientContentConfig().GroupVersion config, _ := c.content.GetClientContentConfig()
return config.GroupVersion
} }
// requestClientContentConfigProvider observes HTTP 415 (Unsupported Media Type) responses to detect // requestClientContentConfigProvider observes HTTP 415 (Unsupported Media Type) responses to detect
@ -257,25 +258,35 @@ type requestClientContentConfigProvider struct {
} }
// GetClientContentConfig returns the ClientContentConfig that should be used for new requests by // GetClientContentConfig returns the ClientContentConfig that should be used for new requests by
// this client. // this client and true if the request ContentType was selected by default.
func (p *requestClientContentConfigProvider) GetClientContentConfig() ClientContentConfig { func (p *requestClientContentConfigProvider) GetClientContentConfig() (ClientContentConfig, bool) {
config := p.base
defaulted := config.ContentType == ""
if defaulted {
config.ContentType = "application/json"
}
if !clientfeatures.FeatureGates().Enabled(clientfeatures.ClientsAllowCBOR) { if !clientfeatures.FeatureGates().Enabled(clientfeatures.ClientsAllowCBOR) {
return p.base return config, defaulted
}
if defaulted && clientfeatures.FeatureGates().Enabled(clientfeatures.ClientsPreferCBOR) {
config.ContentType = "application/cbor"
} }
if sawUnsupportedMediaTypeForCBOR := p.sawUnsupportedMediaTypeForCBOR.Load(); !sawUnsupportedMediaTypeForCBOR { if sawUnsupportedMediaTypeForCBOR := p.sawUnsupportedMediaTypeForCBOR.Load(); !sawUnsupportedMediaTypeForCBOR {
return p.base return config, defaulted
} }
if mediaType, _, _ := mime.ParseMediaType(p.base.ContentType); mediaType != runtime.ContentTypeCBOR { if mediaType, _, _ := mime.ParseMediaType(config.ContentType); mediaType != runtime.ContentTypeCBOR {
return p.base return config, defaulted
} }
config := p.base // The effective ContentType is CBOR and the client has previously received an HTTP 415 in
// The default ClientContentConfig sets ContentType to CBOR and the client has previously // response to a CBOR request. Override ContentType to JSON.
// received an HTTP 415 in response to a CBOR request. Override ContentType to JSON.
config.ContentType = runtime.ContentTypeJSON config.ContentType = runtime.ContentTypeJSON
return config return config, defaulted
} }
// UnsupportedMediaType reports that the server has responded to a request with HTTP 415 Unsupported // UnsupportedMediaType reports that the server has responded to a request with HTTP 415 Unsupported

View File

@ -156,14 +156,10 @@ func NewRequest(c *RESTClient) *Request {
timeout = c.Client.Timeout timeout = c.Client.Timeout
} }
contentConfig := c.content.GetClientContentConfig() // A request needs to know whether the content type was explicitly configured or selected by
contentTypeNotSet := len(contentConfig.ContentType) == 0 // default in order to support the per-request Protobuf override used by clients generated
if contentTypeNotSet { // with --prefers-protobuf.
contentConfig.ContentType = "application/json" contentConfig, contentTypeDefaulted := c.content.GetClientContentConfig()
if clientfeatures.FeatureGates().Enabled(clientfeatures.ClientsAllowCBOR) && clientfeatures.FeatureGates().Enabled(clientfeatures.ClientsPreferCBOR) {
contentConfig.ContentType = "application/cbor"
}
}
r := &Request{ r := &Request{
c: c, c: c,
@ -176,7 +172,7 @@ func NewRequest(c *RESTClient) *Request {
warningHandler: c.warningHandler, warningHandler: c.warningHandler,
contentConfig: contentConfig, contentConfig: contentConfig,
contentTypeNotSet: contentTypeNotSet, contentTypeNotSet: contentTypeDefaulted,
} }
r.setAcceptHeader() r.setAcceptHeader()