@@ -67,6 +67,9 @@ const (
6767 acceptDiscoveryFormats = AcceptV2Beta1 + "," + AcceptV1
6868)
6969
70+ // Aggregated discovery content-type GVK.
71+ var v2Beta1GVK = schema.GroupVersionKind {Group : "apidiscovery.k8s.io" , Version : "v2beta1" , Kind : "APIGroupDiscoveryList" }
72+
7073// DiscoveryInterface holds the methods that discover server-supported API groups,
7174// versions and resources.
7275type DiscoveryInterface interface {
@@ -260,16 +263,15 @@ func (d *DiscoveryClient) downloadLegacy() (
260263 }
261264
262265 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 {
266268 var aggregatedDiscovery apidiscovery.APIGroupDiscoveryList
267269 err = json .Unmarshal (body , & aggregatedDiscovery )
268270 if err != nil {
269271 return nil , nil , nil , err
270272 }
271273 apiGroupList , resourcesByGV , failedGVs = SplitGroupsAndResources (aggregatedDiscovery )
272- default :
274+ } else {
273275 // Default is unaggregated discovery v1.
274276 var v metav1.APIVersions
275277 err = json .Unmarshal (body , & v )
@@ -313,16 +315,15 @@ func (d *DiscoveryClient) downloadAPIs() (
313315 apiGroupList := & metav1.APIGroupList {}
314316 failedGVs := map [schema.GroupVersion ]error {}
315317 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 {
319320 var aggregatedDiscovery apidiscovery.APIGroupDiscoveryList
320321 err = json .Unmarshal (body , & aggregatedDiscovery )
321322 if err != nil {
322323 return nil , nil , nil , err
323324 }
324325 apiGroupList , resourcesByGV , failedGVs = SplitGroupsAndResources (aggregatedDiscovery )
325- default :
326+ } else {
326327 // Default is unaggregated discovery v1.
327328 err = json .Unmarshal (body , apiGroupList )
328329 if err != nil {
@@ -333,26 +334,29 @@ func (d *DiscoveryClient) downloadAPIs() (
333334 return apiGroupList , resourcesByGV , failedGVs , nil
334335}
335336
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.
338340// NOTE: This function is resilient to the ordering of the
339341// content-type parameters, as well as parameters added by
340342// intermediaries such as proxies or gateways. Examples:
341343//
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 ) {
348351 base , params , err := mime .ParseMediaType (contentType )
349352 if err != nil {
350- return false
353+ return false , err
351354 }
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
356360}
357361
358362// ServerGroups returns the supported groups, with information like supported versions and the
0 commit comments