aggregated_discovery.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. // StaleGroupVersionError encasulates failed GroupVersion marked "stale"
  21. // in the returned AggregatedDiscovery format.
  22. type StaleGroupVersionError struct {
  23. gv schema.GroupVersion
  24. }
  25. func (s StaleGroupVersionError) Error() string {
  26. return fmt.Sprintf("stale GroupVersion discovery: %v", s.gv)
  27. }
  28. // SplitGroupsAndResources transforms "aggregated" discovery top-level structure into
  29. // the previous "unaggregated" discovery groups and resources.
  30. func SplitGroupsAndResources(aggregatedGroups apidiscovery.APIGroupDiscoveryList) (
  31. *metav1.APIGroupList,
  32. map[schema.GroupVersion]*metav1.APIResourceList,
  33. map[schema.GroupVersion]error) {
  34. // Aggregated group list will contain the entirety of discovery, including
  35. // groups, versions, and resources. GroupVersions marked "stale" are failed.
  36. groups := []*metav1.APIGroup{}
  37. failedGVs := map[schema.GroupVersion]error{}
  38. resourcesByGV := map[schema.GroupVersion]*metav1.APIResourceList{}
  39. for _, aggGroup := range aggregatedGroups.Items {
  40. group, resources, failed := convertAPIGroup(aggGroup)
  41. groups = append(groups, group)
  42. for gv, resourceList := range resources {
  43. resourcesByGV[gv] = resourceList
  44. }
  45. for gv, err := range failed {
  46. failedGVs[gv] = err
  47. }
  48. }
  49. // Transform slice of groups to group list before returning.
  50. groupList := &metav1.APIGroupList{}
  51. groupList.Groups = make([]metav1.APIGroup, 0, len(groups))
  52. for _, group := range groups {
  53. groupList.Groups = append(groupList.Groups, *group)
  54. }
  55. return groupList, resourcesByGV, failedGVs
  56. }
  57. // convertAPIGroup tranforms an "aggregated" APIGroupDiscovery to an "legacy" APIGroup,
  58. // also returning the map of APIResourceList for resources within GroupVersions.
  59. func convertAPIGroup(g apidiscovery.APIGroupDiscovery) (
  60. *metav1.APIGroup,
  61. map[schema.GroupVersion]*metav1.APIResourceList,
  62. map[schema.GroupVersion]error) {
  63. // Iterate through versions to convert to group and resources.
  64. group := &metav1.APIGroup{}
  65. gvResources := map[schema.GroupVersion]*metav1.APIResourceList{}
  66. failedGVs := map[schema.GroupVersion]error{}
  67. group.Name = g.ObjectMeta.Name
  68. for _, v := range g.Versions {
  69. gv := schema.GroupVersion{Group: g.Name, Version: v.Version}
  70. if v.Freshness == apidiscovery.DiscoveryFreshnessStale {
  71. failedGVs[gv] = StaleGroupVersionError{gv: gv}
  72. continue
  73. }
  74. version := metav1.GroupVersionForDiscovery{}
  75. version.GroupVersion = gv.String()
  76. version.Version = v.Version
  77. group.Versions = append(group.Versions, version)
  78. // PreferredVersion is first non-stale Version
  79. if group.PreferredVersion == (metav1.GroupVersionForDiscovery{}) {
  80. group.PreferredVersion = version
  81. }
  82. resourceList := &metav1.APIResourceList{}
  83. resourceList.GroupVersion = gv.String()
  84. for _, r := range v.Resources {
  85. resource, err := convertAPIResource(r)
  86. if err == nil {
  87. resourceList.APIResources = append(resourceList.APIResources, resource)
  88. }
  89. // Subresources field in new format get transformed into full APIResources.
  90. // It is possible a partial result with an error was returned to be used
  91. // as the parent resource for the subresource.
  92. for _, subresource := range r.Subresources {
  93. sr, err := convertAPISubresource(resource, subresource)
  94. if err == nil {
  95. resourceList.APIResources = append(resourceList.APIResources, sr)
  96. }
  97. }
  98. }
  99. gvResources[gv] = resourceList
  100. }
  101. return group, gvResources, failedGVs
  102. }
  103. var emptyKind = metav1.GroupVersionKind{}
  104. // convertAPIResource tranforms a APIResourceDiscovery to an APIResource. We are
  105. // resilient to missing GVK, since this resource might be the parent resource
  106. // for a subresource. If the parent is missing a GVK, it is not returned in
  107. // discovery, and the subresource MUST have the GVK.
  108. func convertAPIResource(in apidiscovery.APIResourceDiscovery) (metav1.APIResource, error) {
  109. result := metav1.APIResource{
  110. Name: in.Resource,
  111. SingularName: in.SingularResource,
  112. Namespaced: in.Scope == apidiscovery.ScopeNamespace,
  113. Verbs: in.Verbs,
  114. ShortNames: in.ShortNames,
  115. Categories: in.Categories,
  116. }
  117. var err error
  118. if in.ResponseKind != nil && (*in.ResponseKind) != emptyKind {
  119. result.Group = in.ResponseKind.Group
  120. result.Version = in.ResponseKind.Version
  121. result.Kind = in.ResponseKind.Kind
  122. } else {
  123. err = fmt.Errorf("discovery resource %s missing GVK", in.Resource)
  124. }
  125. // Can return partial result with error, which can be the parent for a
  126. // subresource. Do not add this result to the returned discovery resources.
  127. return result, err
  128. }
  129. // convertAPISubresource tranforms a APISubresourceDiscovery to an APIResource.
  130. func convertAPISubresource(parent metav1.APIResource, in apidiscovery.APISubresourceDiscovery) (metav1.APIResource, error) {
  131. result := metav1.APIResource{}
  132. if in.ResponseKind == nil || (*in.ResponseKind) == emptyKind {
  133. return result, fmt.Errorf("subresource %s/%s missing GVK", parent.Name, in.Subresource)
  134. }
  135. result.Name = fmt.Sprintf("%s/%s", parent.Name, in.Subresource)
  136. result.SingularName = parent.SingularName
  137. result.Namespaced = parent.Namespaced
  138. result.Group = in.ResponseKind.Group
  139. result.Version = in.ResponseKind.Version
  140. result.Kind = in.ResponseKind.Kind
  141. result.Verbs = in.Verbs
  142. return result, nil
  143. }