@@ -67,6 +67,9 @@ const (
67
67
acceptDiscoveryFormats = AcceptV2Beta1 + "," + AcceptV1
68
68
)
69
69
70
+ // Aggregated discovery content-type GVK.
71
+ var v2Beta1GVK = schema.GroupVersionKind {Group : "apidiscovery.k8s.io" , Version : "v2beta1" , Kind : "APIGroupDiscoveryList" }
72
+
70
73
// DiscoveryInterface holds the methods that discover server-supported API groups,
71
74
// versions and resources.
72
75
type DiscoveryInterface interface {
@@ -260,16 +263,15 @@ func (d *DiscoveryClient) downloadLegacy() (
260
263
}
261
264
262
265
var resourcesByGV map [schema.GroupVersion ]* metav1.APIResourceList
263
- // Switch on content-type server responded with: aggregated or unaggregated.
264
- switch {
265
- case isV2Beta1ContentType (responseContentType ):
266
+ // Based on the content-type server responded with: aggregated or unaggregated.
267
+ if isGVK , _ := ContentTypeIsGVK (responseContentType , v2Beta1GVK ); isGVK {
266
268
var aggregatedDiscovery apidiscovery.APIGroupDiscoveryList
267
269
err = json .Unmarshal (body , & aggregatedDiscovery )
268
270
if err != nil {
269
271
return nil , nil , nil , err
270
272
}
271
273
apiGroupList , resourcesByGV , failedGVs = SplitGroupsAndResources (aggregatedDiscovery )
272
- default :
274
+ } else {
273
275
// Default is unaggregated discovery v1.
274
276
var v metav1.APIVersions
275
277
err = json .Unmarshal (body , & v )
@@ -313,16 +315,15 @@ func (d *DiscoveryClient) downloadAPIs() (
313
315
apiGroupList := & metav1.APIGroupList {}
314
316
failedGVs := map [schema.GroupVersion ]error {}
315
317
var resourcesByGV map [schema.GroupVersion ]* metav1.APIResourceList
316
- // Switch on content-type server responded with: aggregated or unaggregated.
317
- switch {
318
- case isV2Beta1ContentType (responseContentType ):
318
+ // Based on the content-type server responded with: aggregated or unaggregated.
319
+ if isGVK , _ := ContentTypeIsGVK (responseContentType , v2Beta1GVK ); isGVK {
319
320
var aggregatedDiscovery apidiscovery.APIGroupDiscoveryList
320
321
err = json .Unmarshal (body , & aggregatedDiscovery )
321
322
if err != nil {
322
323
return nil , nil , nil , err
323
324
}
324
325
apiGroupList , resourcesByGV , failedGVs = SplitGroupsAndResources (aggregatedDiscovery )
325
- default :
326
+ } else {
326
327
// Default is unaggregated discovery v1.
327
328
err = json .Unmarshal (body , apiGroupList )
328
329
if err != nil {
@@ -333,26 +334,29 @@ func (d *DiscoveryClient) downloadAPIs() (
333
334
return apiGroupList , resourcesByGV , failedGVs , nil
334
335
}
335
336
336
- // isV2Beta1ContentType checks of the content-type string is both
337
- // "application/json" and contains the v2beta1 content-type params.
337
+ // ContentTypeIsGVK checks of the content-type string is both
338
+ // "application/json" and matches the provided GVK. An error
339
+ // is returned if the content type string is malformed.
338
340
// NOTE: This function is resilient to the ordering of the
339
341
// content-type parameters, as well as parameters added by
340
342
// intermediaries such as proxies or gateways. Examples:
341
343
//
342
- // "application/json; g=apidiscovery.k8s.io;v=v2beta1;as=APIGroupDiscoveryList" = true
343
- // "application/json; as=APIGroupDiscoveryList;v=v2beta1;g=apidiscovery.k8s.io" = true
344
- // "application/json; as=APIGroupDiscoveryList;v=v2beta1;g=apidiscovery.k8s.io;charset=utf-8" = true
345
- // "application/json" = false
346
- // "application/json; charset=UTF-8" = false
347
- func isV2Beta1ContentType (contentType string ) bool {
344
+ // ("application/json; g=apidiscovery.k8s.io;v=v2beta1;as=APIGroupDiscoveryList", {apidiscovery.k8s.io, v2beta1, APIGroupDiscoveryList}) = (true, nil)
345
+ // ("application/json; as=APIGroupDiscoveryList;v=v2beta1;g=apidiscovery.k8s.io", {apidiscovery.k8s.io, v2beta1, APIGroupDiscoveryList}) = (true, nil)
346
+ // ("application/json; as=APIGroupDiscoveryList;v=v2beta1;g=apidiscovery.k8s.io;charset=utf-8", {apidiscovery.k8s.io, v2beta1, APIGroupDiscoveryList}) = (true, nil)
347
+ // ("application/json", any GVK) = (false, nil)
348
+ // ("application/json; charset=UTF-8", any GVK) = (false, nil)
349
+ // ("malformed content type string", any GVK) = (false, error)
350
+ func ContentTypeIsGVK (contentType string , gvk schema.GroupVersionKind ) (bool , error ) {
348
351
base , params , err := mime .ParseMediaType (contentType )
349
352
if err != nil {
350
- return false
353
+ return false , err
351
354
}
352
- return runtime .ContentTypeJSON == base &&
353
- params ["g" ] == "apidiscovery.k8s.io" &&
354
- params ["v" ] == "v2beta1" &&
355
- params ["as" ] == "APIGroupDiscoveryList"
355
+ gvkMatch := runtime .ContentTypeJSON == base &&
356
+ params ["g" ] == gvk .Group &&
357
+ params ["v" ] == gvk .Version &&
358
+ params ["as" ] == gvk .Kind
359
+ return gvkMatch , nil
356
360
}
357
361
358
362
// ServerGroups returns the supported groups, with information like supported versions and the
0 commit comments