Skip to content

Commit 67b9dc1

Browse files
committed
Wire client feature gates affecting RESTClient content config.
1 parent 8fb9622 commit 67b9dc1

File tree

3 files changed

+553
-4
lines changed

3 files changed

+553
-4
lines changed

staging/src/k8s.io/client-go/rest/client.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,20 @@ limitations under the License.
1717
package rest
1818

1919
import (
20+
"fmt"
21+
"mime"
2022
"net/http"
2123
"net/url"
2224
"os"
2325
"strconv"
2426
"strings"
2527
"time"
2628

29+
"github.com/munnerz/goautoneg"
2730
"k8s.io/apimachinery/pkg/runtime"
2831
"k8s.io/apimachinery/pkg/runtime/schema"
2932
"k8s.io/apimachinery/pkg/types"
33+
clientfeatures "k8s.io/client-go/features"
3034
"k8s.io/client-go/util/flowcontrol"
3135
)
3236

@@ -115,14 +119,53 @@ func NewRESTClient(baseURL *url.URL, versionedAPIPath string, config ClientConte
115119
return &RESTClient{
116120
base: &base,
117121
versionedAPIPath: versionedAPIPath,
118-
content: config,
122+
content: scrubCBORContentConfigIfDisabled(config),
119123
createBackoffMgr: readExpBackoffConfig,
120124
rateLimiter: rateLimiter,
121125

122126
Client: client,
123127
}, nil
124128
}
125129

130+
func scrubCBORContentConfigIfDisabled(content ClientContentConfig) ClientContentConfig {
131+
if clientfeatures.TestOnlyFeatureGates.Enabled(clientfeatures.TestOnlyClientAllowsCBOR) {
132+
return content
133+
}
134+
135+
if mediatype, _, err := mime.ParseMediaType(content.ContentType); err == nil && mediatype == "application/cbor" {
136+
content.ContentType = "application/json"
137+
}
138+
139+
clauses := goautoneg.ParseAccept(content.AcceptContentTypes)
140+
scrubbed := false
141+
for i, clause := range clauses {
142+
if clause.Type == "application" && clause.SubType == "cbor" {
143+
scrubbed = true
144+
clauses[i].SubType = "json"
145+
}
146+
}
147+
if !scrubbed {
148+
// No application/cbor in AcceptContentTypes, nothing more to do.
149+
return content
150+
}
151+
152+
parts := make([]string, 0, len(clauses))
153+
for _, clause := range clauses {
154+
// ParseAccept does not store the parameter "q" in Params.
155+
params := clause.Params
156+
if clause.Q < 1 { // omit q=1, it's the default
157+
if params == nil {
158+
params = make(map[string]string, 1)
159+
}
160+
params["q"] = strconv.FormatFloat(clause.Q, 'g', 3, 32)
161+
}
162+
parts = append(parts, mime.FormatMediaType(fmt.Sprintf("%s/%s", clause.Type, clause.SubType), params))
163+
}
164+
content.AcceptContentTypes = strings.Join(parts, ",")
165+
166+
return content
167+
}
168+
126169
// GetRateLimiter returns rate limiter for a given client, or nil if it's called on a nil client
127170
func (c *RESTClient) GetRateLimiter() flowcontrol.RateLimiter {
128171
if c == nil {

staging/src/k8s.io/client-go/rest/request.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ func NewRequest(c *RESTClient) *Request {
160160
contentTypeNotSet := len(contentConfig.ContentType) == 0
161161
if contentTypeNotSet {
162162
contentConfig.ContentType = "application/json"
163+
if clientfeatures.TestOnlyFeatureGates.Enabled(clientfeatures.TestOnlyClientAllowsCBOR) && clientfeatures.TestOnlyFeatureGates.Enabled(clientfeatures.TestOnlyClientPrefersCBOR) {
164+
contentConfig.ContentType = "application/cbor"
165+
}
163166
}
164167

165168
r := &Request{

0 commit comments

Comments
 (0)