aggregated_discovery.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. Copyright 2022 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package discovery
  14. import (
  15. "fmt"
  16. apidiscovery "k8s.io/api/apidiscovery/v2beta1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/runtime/schema"
  19. )
  20. // SplitGroupsAndResources transforms "aggregated" discovery top-level structure into
  21. // the previous "unaggregated" discovery groups and resources.
  22. func SplitGroupsAndResources(aggregatedGroups apidiscovery.APIGroupDiscoveryList) (*metav1.APIGroupList, map[schema.GroupVersion]*metav1.APIResourceList) {
  23. // Aggregated group list will contain the entirety of discovery, including
  24. // groups, versions, and resources.
  25. groups := []*metav1.APIGroup{}
  26. resourcesByGV := map[schema.GroupVersion]*metav1.APIResourceList{}
  27. for _, aggGroup := range aggregatedGroups.Items {
  28. group, resources := convertAPIGroup(aggGroup)
  29. groups = append(groups, group)
  30. for gv, resourceList := range resources {
  31. resourcesByGV[gv] = resourceList
  32. }
  33. }
  34. // Transform slice of groups to group list before returning.
  35. groupList := &metav1.APIGroupList{}
  36. groupList.Groups = make([]metav1.APIGroup, 0, len(groups))
  37. for _, group := range groups {
  38. groupList.Groups = append(groupList.Groups, *group)
  39. }
  40. return groupList, resourcesByGV
  41. }
  42. // convertAPIGroup tranforms an "aggregated" APIGroupDiscovery to an "legacy" APIGroup,
  43. // also returning the map of APIResourceList for resources within GroupVersions.
  44. func convertAPIGroup(g apidiscovery.APIGroupDiscovery) (*metav1.APIGroup, map[schema.GroupVersion]*metav1.APIResourceList) {
  45. // Iterate through versions to convert to group and resources.
  46. group := &metav1.APIGroup{}
  47. gvResources := map[schema.GroupVersion]*metav1.APIResourceList{}
  48. group.Name = g.ObjectMeta.Name
  49. for i, v := range g.Versions {
  50. version := metav1.GroupVersionForDiscovery{}
  51. gv := schema.GroupVersion{Group: g.Name, Version: v.Version}
  52. version.GroupVersion = gv.String()
  53. version.Version = v.Version
  54. group.Versions = append(group.Versions, version)
  55. if i == 0 {
  56. group.PreferredVersion = version
  57. }
  58. resourceList := &metav1.APIResourceList{}
  59. resourceList.GroupVersion = gv.String()
  60. for _, r := range v.Resources {
  61. resource := convertAPIResource(r)
  62. resourceList.APIResources = append(resourceList.APIResources, resource)
  63. // Subresources field in new format get transformed into full APIResources.
  64. for _, subresource := range r.Subresources {
  65. sr := convertAPISubresource(resource, subresource)
  66. resourceList.APIResources = append(resourceList.APIResources, sr)
  67. }
  68. }
  69. gvResources[gv] = resourceList
  70. }
  71. return group, gvResources
  72. }
  73. // convertAPIResource tranforms a APIResourceDiscovery to an APIResource.
  74. func convertAPIResource(in apidiscovery.APIResourceDiscovery) metav1.APIResource {
  75. return metav1.APIResource{
  76. Name: in.Resource,
  77. SingularName: in.SingularResource,
  78. Namespaced: in.Scope == apidiscovery.ScopeNamespace,
  79. Group: in.ResponseKind.Group,
  80. Version: in.ResponseKind.Version,
  81. Kind: in.ResponseKind.Kind,
  82. Verbs: in.Verbs,
  83. ShortNames: in.ShortNames,
  84. Categories: in.Categories,
  85. }
  86. }
  87. // convertAPISubresource tranforms a APISubresourceDiscovery to an APIResource.
  88. func convertAPISubresource(parent metav1.APIResource, in apidiscovery.APISubresourceDiscovery) metav1.APIResource {
  89. return metav1.APIResource{
  90. Name: fmt.Sprintf("%s/%s", parent.Name, in.Subresource),
  91. SingularName: parent.SingularName,
  92. Namespaced: parent.Namespaced,
  93. Group: in.ResponseKind.Group,
  94. Version: in.ResponseKind.Version,
  95. Kind: in.ResponseKind.Kind,
  96. Verbs: in.Verbs,
  97. }
  98. }