@@ -17,16 +17,20 @@ limitations under the License.
17
17
package rest
18
18
19
19
import (
20
+ "fmt"
21
+ "mime"
20
22
"net/http"
21
23
"net/url"
22
24
"os"
23
25
"strconv"
24
26
"strings"
25
27
"time"
26
28
29
+ "github.com/munnerz/goautoneg"
27
30
"k8s.io/apimachinery/pkg/runtime"
28
31
"k8s.io/apimachinery/pkg/runtime/schema"
29
32
"k8s.io/apimachinery/pkg/types"
33
+ clientfeatures "k8s.io/client-go/features"
30
34
"k8s.io/client-go/util/flowcontrol"
31
35
)
32
36
@@ -115,14 +119,53 @@ func NewRESTClient(baseURL *url.URL, versionedAPIPath string, config ClientConte
115
119
return & RESTClient {
116
120
base : & base ,
117
121
versionedAPIPath : versionedAPIPath ,
118
- content : config ,
122
+ content : scrubCBORContentConfigIfDisabled ( config ) ,
119
123
createBackoffMgr : readExpBackoffConfig ,
120
124
rateLimiter : rateLimiter ,
121
125
122
126
Client : client ,
123
127
}, nil
124
128
}
125
129
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
+
126
169
// GetRateLimiter returns rate limiter for a given client, or nil if it's called on a nil client
127
170
func (c * RESTClient ) GetRateLimiter () flowcontrol.RateLimiter {
128
171
if c == nil {
0 commit comments