test_restmapper.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. Copyright 2018 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 testrestmapper
  14. import (
  15. "k8s.io/apimachinery/pkg/api/meta"
  16. "k8s.io/apimachinery/pkg/runtime"
  17. "k8s.io/apimachinery/pkg/runtime/schema"
  18. "k8s.io/apimachinery/pkg/util/sets"
  19. )
  20. // TestOnlyStaticRESTMapper returns a union RESTMapper of all known types with priorities chosen in the following order:
  21. // 1. legacy kube group preferred version, extensions preferred version, metrics preferred version, legacy
  22. // kube any version, extensions any version, metrics any version, all other groups alphabetical preferred version,
  23. // all other groups alphabetical.
  24. //
  25. // TODO callers of this method should be updated to build their own specific restmapper based on their scheme for their tests
  26. // TODO the things being tested are related to whether various cases are handled, not tied to the particular types being checked.
  27. func TestOnlyStaticRESTMapper(scheme *runtime.Scheme, versionPatterns ...schema.GroupVersion) meta.RESTMapper {
  28. unionMapper := meta.MultiRESTMapper{}
  29. unionedGroups := sets.NewString()
  30. for _, enabledVersion := range scheme.PrioritizedVersionsAllGroups() {
  31. if !unionedGroups.Has(enabledVersion.Group) {
  32. unionedGroups.Insert(enabledVersion.Group)
  33. unionMapper = append(unionMapper, newRESTMapper(enabledVersion.Group, scheme))
  34. }
  35. }
  36. if len(versionPatterns) != 0 {
  37. resourcePriority := []schema.GroupVersionResource{}
  38. kindPriority := []schema.GroupVersionKind{}
  39. for _, versionPriority := range versionPatterns {
  40. resourcePriority = append(resourcePriority, versionPriority.WithResource(meta.AnyResource))
  41. kindPriority = append(kindPriority, versionPriority.WithKind(meta.AnyKind))
  42. }
  43. return meta.PriorityRESTMapper{Delegate: unionMapper, ResourcePriority: resourcePriority, KindPriority: kindPriority}
  44. }
  45. prioritizedGroups := []string{"", "extensions", "metrics"}
  46. resourcePriority, kindPriority := prioritiesForGroups(scheme, prioritizedGroups...)
  47. prioritizedGroupsSet := sets.NewString(prioritizedGroups...)
  48. remainingGroups := sets.String{}
  49. for _, enabledVersion := range scheme.PrioritizedVersionsAllGroups() {
  50. if !prioritizedGroupsSet.Has(enabledVersion.Group) {
  51. remainingGroups.Insert(enabledVersion.Group)
  52. }
  53. }
  54. remainingResourcePriority, remainingKindPriority := prioritiesForGroups(scheme, remainingGroups.List()...)
  55. resourcePriority = append(resourcePriority, remainingResourcePriority...)
  56. kindPriority = append(kindPriority, remainingKindPriority...)
  57. return meta.PriorityRESTMapper{Delegate: unionMapper, ResourcePriority: resourcePriority, KindPriority: kindPriority}
  58. }
  59. // prioritiesForGroups returns the resource and kind priorities for a PriorityRESTMapper, preferring the preferred version of each group first,
  60. // then any non-preferred version of the group second.
  61. func prioritiesForGroups(scheme *runtime.Scheme, groups ...string) ([]schema.GroupVersionResource, []schema.GroupVersionKind) {
  62. resourcePriority := []schema.GroupVersionResource{}
  63. kindPriority := []schema.GroupVersionKind{}
  64. for _, group := range groups {
  65. availableVersions := scheme.PrioritizedVersionsForGroup(group)
  66. if len(availableVersions) > 0 {
  67. resourcePriority = append(resourcePriority, availableVersions[0].WithResource(meta.AnyResource))
  68. kindPriority = append(kindPriority, availableVersions[0].WithKind(meta.AnyKind))
  69. }
  70. }
  71. for _, group := range groups {
  72. resourcePriority = append(resourcePriority, schema.GroupVersionResource{Group: group, Version: meta.AnyVersion, Resource: meta.AnyResource})
  73. kindPriority = append(kindPriority, schema.GroupVersionKind{Group: group, Version: meta.AnyVersion, Kind: meta.AnyKind})
  74. }
  75. return resourcePriority, kindPriority
  76. }
  77. func newRESTMapper(group string, scheme *runtime.Scheme) meta.RESTMapper {
  78. mapper := meta.NewDefaultRESTMapper(scheme.PrioritizedVersionsForGroup(group))
  79. for _, gv := range scheme.PrioritizedVersionsForGroup(group) {
  80. for kind := range scheme.KnownTypes(gv) {
  81. if ignoredKinds.Has(kind) {
  82. continue
  83. }
  84. scope := meta.RESTScopeNamespace
  85. if rootScopedKinds[gv.WithKind(kind).GroupKind()] {
  86. scope = meta.RESTScopeRoot
  87. }
  88. mapper.Add(gv.WithKind(kind), scope)
  89. }
  90. }
  91. return mapper
  92. }
  93. // hardcoded is good enough for the test we're running
  94. var rootScopedKinds = map[schema.GroupKind]bool{
  95. {Group: "admission.k8s.io", Kind: "AdmissionReview"}: true,
  96. {Group: "admissionregistration.k8s.io", Kind: "ValidatingWebhookConfiguration"}: true,
  97. {Group: "admissionregistration.k8s.io", Kind: "MutatingWebhookConfiguration"}: true,
  98. {Group: "authentication.k8s.io", Kind: "TokenReview"}: true,
  99. {Group: "authorization.k8s.io", Kind: "SubjectAccessReview"}: true,
  100. {Group: "authorization.k8s.io", Kind: "SelfSubjectAccessReview"}: true,
  101. {Group: "authorization.k8s.io", Kind: "SelfSubjectRulesReview"}: true,
  102. {Group: "certificates.k8s.io", Kind: "CertificateSigningRequest"}: true,
  103. {Group: "", Kind: "Node"}: true,
  104. {Group: "", Kind: "Namespace"}: true,
  105. {Group: "", Kind: "PersistentVolume"}: true,
  106. {Group: "", Kind: "ComponentStatus"}: true,
  107. {Group: "rbac.authorization.k8s.io", Kind: "ClusterRole"}: true,
  108. {Group: "rbac.authorization.k8s.io", Kind: "ClusterRoleBinding"}: true,
  109. {Group: "scheduling.k8s.io", Kind: "PriorityClass"}: true,
  110. {Group: "storage.k8s.io", Kind: "StorageClass"}: true,
  111. {Group: "storage.k8s.io", Kind: "VolumeAttachment"}: true,
  112. {Group: "apiextensions.k8s.io", Kind: "CustomResourceDefinition"}: true,
  113. {Group: "apiserver.k8s.io", Kind: "AdmissionConfiguration"}: true,
  114. {Group: "audit.k8s.io", Kind: "Event"}: true,
  115. {Group: "audit.k8s.io", Kind: "Policy"}: true,
  116. {Group: "apiregistration.k8s.io", Kind: "APIService"}: true,
  117. {Group: "metrics.k8s.io", Kind: "NodeMetrics"}: true,
  118. {Group: "wardle.example.com", Kind: "Fischer"}: true,
  119. }
  120. // hardcoded is good enough for the test we're running
  121. var ignoredKinds = sets.NewString(
  122. "ListOptions",
  123. "DeleteOptions",
  124. "Status",
  125. "PodLogOptions",
  126. "PodExecOptions",
  127. "PodAttachOptions",
  128. "PodPortForwardOptions",
  129. "PodProxyOptions",
  130. "NodeProxyOptions",
  131. "ServiceProxyOptions",
  132. )