Skip to content

Commit

Permalink
Wire client feature gates affecting RESTClient content config.
Browse files Browse the repository at this point in the history
Kubernetes-commit: 67b9dc1f3e23529804345deea76d36d07dff59b1
  • Loading branch information
benluddy authored and k8s-publishing-bot committed Oct 25, 2024
1 parent 9d19c65 commit c957b59
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
45 changes: 44 additions & 1 deletion rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@ limitations under the License.
package rest

import (
"fmt"
"mime"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"

"github.com/munnerz/goautoneg"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
clientfeatures "k8s.io/client-go/features"
"k8s.io/client-go/util/flowcontrol"
)

Expand Down Expand Up @@ -115,14 +119,53 @@ func NewRESTClient(baseURL *url.URL, versionedAPIPath string, config ClientConte
return &RESTClient{
base: &base,
versionedAPIPath: versionedAPIPath,
content: config,
content: scrubCBORContentConfigIfDisabled(config),
createBackoffMgr: readExpBackoffConfig,
rateLimiter: rateLimiter,

Client: client,
}, nil
}

func scrubCBORContentConfigIfDisabled(content ClientContentConfig) ClientContentConfig {
if clientfeatures.TestOnlyFeatureGates.Enabled(clientfeatures.TestOnlyClientAllowsCBOR) {
return content
}

if mediatype, _, err := mime.ParseMediaType(content.ContentType); err == nil && mediatype == "application/cbor" {
content.ContentType = "application/json"
}

clauses := goautoneg.ParseAccept(content.AcceptContentTypes)
scrubbed := false
for i, clause := range clauses {
if clause.Type == "application" && clause.SubType == "cbor" {
scrubbed = true
clauses[i].SubType = "json"
}
}
if !scrubbed {
// No application/cbor in AcceptContentTypes, nothing more to do.
return content
}

parts := make([]string, 0, len(clauses))
for _, clause := range clauses {
// ParseAccept does not store the parameter "q" in Params.
params := clause.Params
if clause.Q < 1 { // omit q=1, it's the default
if params == nil {
params = make(map[string]string, 1)
}
params["q"] = strconv.FormatFloat(clause.Q, 'g', 3, 32)
}
parts = append(parts, mime.FormatMediaType(fmt.Sprintf("%s/%s", clause.Type, clause.SubType), params))
}
content.AcceptContentTypes = strings.Join(parts, ",")

return content
}

// GetRateLimiter returns rate limiter for a given client, or nil if it's called on a nil client
func (c *RESTClient) GetRateLimiter() flowcontrol.RateLimiter {
if c == nil {
Expand Down
3 changes: 3 additions & 0 deletions rest/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ func NewRequest(c *RESTClient) *Request {
contentTypeNotSet := len(contentConfig.ContentType) == 0
if contentTypeNotSet {
contentConfig.ContentType = "application/json"
if clientfeatures.TestOnlyFeatureGates.Enabled(clientfeatures.TestOnlyClientAllowsCBOR) && clientfeatures.TestOnlyFeatureGates.Enabled(clientfeatures.TestOnlyClientPrefersCBOR) {
contentConfig.ContentType = "application/cbor"
}
}

r := &Request{
Expand Down

0 comments on commit c957b59

Please sign in to comment.