multirestmapper.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. Copyright 2014 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 meta
  14. import (
  15. "fmt"
  16. "strings"
  17. "k8s.io/kubernetes/pkg/api/unversioned"
  18. utilerrors "k8s.io/kubernetes/pkg/util/errors"
  19. "k8s.io/kubernetes/pkg/util/sets"
  20. )
  21. // MultiRESTMapper is a wrapper for multiple RESTMappers.
  22. type MultiRESTMapper []RESTMapper
  23. func (m MultiRESTMapper) String() string {
  24. nested := []string{}
  25. for _, t := range m {
  26. currString := fmt.Sprintf("%v", t)
  27. splitStrings := strings.Split(currString, "\n")
  28. nested = append(nested, strings.Join(splitStrings, "\n\t"))
  29. }
  30. return fmt.Sprintf("MultiRESTMapper{\n\t%s\n}", strings.Join(nested, "\n\t"))
  31. }
  32. // ResourceSingularizer converts a REST resource name from plural to singular (e.g., from pods to pod)
  33. // This implementation supports multiple REST schemas and return the first match.
  34. func (m MultiRESTMapper) ResourceSingularizer(resource string) (singular string, err error) {
  35. for _, t := range m {
  36. singular, err = t.ResourceSingularizer(resource)
  37. if err == nil {
  38. return
  39. }
  40. }
  41. return
  42. }
  43. func (m MultiRESTMapper) ResourcesFor(resource unversioned.GroupVersionResource) ([]unversioned.GroupVersionResource, error) {
  44. allGVRs := []unversioned.GroupVersionResource{}
  45. for _, t := range m {
  46. gvrs, err := t.ResourcesFor(resource)
  47. // ignore "no match" errors, but any other error percolates back up
  48. if IsNoMatchError(err) {
  49. continue
  50. }
  51. if err != nil {
  52. return nil, err
  53. }
  54. // walk the existing values to de-dup
  55. for _, curr := range gvrs {
  56. found := false
  57. for _, existing := range allGVRs {
  58. if curr == existing {
  59. found = true
  60. break
  61. }
  62. }
  63. if !found {
  64. allGVRs = append(allGVRs, curr)
  65. }
  66. }
  67. }
  68. if len(allGVRs) == 0 {
  69. return nil, &NoResourceMatchError{PartialResource: resource}
  70. }
  71. return allGVRs, nil
  72. }
  73. func (m MultiRESTMapper) KindsFor(resource unversioned.GroupVersionResource) (gvk []unversioned.GroupVersionKind, err error) {
  74. allGVKs := []unversioned.GroupVersionKind{}
  75. for _, t := range m {
  76. gvks, err := t.KindsFor(resource)
  77. // ignore "no match" errors, but any other error percolates back up
  78. if IsNoMatchError(err) {
  79. continue
  80. }
  81. if err != nil {
  82. return nil, err
  83. }
  84. // walk the existing values to de-dup
  85. for _, curr := range gvks {
  86. found := false
  87. for _, existing := range allGVKs {
  88. if curr == existing {
  89. found = true
  90. break
  91. }
  92. }
  93. if !found {
  94. allGVKs = append(allGVKs, curr)
  95. }
  96. }
  97. }
  98. if len(allGVKs) == 0 {
  99. return nil, &NoResourceMatchError{PartialResource: resource}
  100. }
  101. return allGVKs, nil
  102. }
  103. func (m MultiRESTMapper) ResourceFor(resource unversioned.GroupVersionResource) (unversioned.GroupVersionResource, error) {
  104. resources, err := m.ResourcesFor(resource)
  105. if err != nil {
  106. return unversioned.GroupVersionResource{}, err
  107. }
  108. if len(resources) == 1 {
  109. return resources[0], nil
  110. }
  111. return unversioned.GroupVersionResource{}, &AmbiguousResourceError{PartialResource: resource, MatchingResources: resources}
  112. }
  113. func (m MultiRESTMapper) KindFor(resource unversioned.GroupVersionResource) (unversioned.GroupVersionKind, error) {
  114. kinds, err := m.KindsFor(resource)
  115. if err != nil {
  116. return unversioned.GroupVersionKind{}, err
  117. }
  118. if len(kinds) == 1 {
  119. return kinds[0], nil
  120. }
  121. return unversioned.GroupVersionKind{}, &AmbiguousResourceError{PartialResource: resource, MatchingKinds: kinds}
  122. }
  123. // RESTMapping provides the REST mapping for the resource based on the
  124. // kind and version. This implementation supports multiple REST schemas and
  125. // return the first match.
  126. func (m MultiRESTMapper) RESTMapping(gk unversioned.GroupKind, versions ...string) (*RESTMapping, error) {
  127. allMappings := []*RESTMapping{}
  128. errors := []error{}
  129. for _, t := range m {
  130. currMapping, err := t.RESTMapping(gk, versions...)
  131. // ignore "no match" errors, but any other error percolates back up
  132. if IsNoMatchError(err) {
  133. continue
  134. }
  135. if err != nil {
  136. errors = append(errors, err)
  137. continue
  138. }
  139. allMappings = append(allMappings, currMapping)
  140. }
  141. // if we got exactly one mapping, then use it even if other requested failed
  142. if len(allMappings) == 1 {
  143. return allMappings[0], nil
  144. }
  145. if len(allMappings) > 1 {
  146. var kinds []unversioned.GroupVersionKind
  147. for _, m := range allMappings {
  148. kinds = append(kinds, m.GroupVersionKind)
  149. }
  150. return nil, &AmbiguousKindError{PartialKind: gk.WithVersion(""), MatchingKinds: kinds}
  151. }
  152. if len(errors) > 0 {
  153. return nil, utilerrors.NewAggregate(errors)
  154. }
  155. return nil, &NoKindMatchError{PartialKind: gk.WithVersion("")}
  156. }
  157. // RESTMappings returns all possible RESTMappings for the provided group kind, or an error
  158. // if the type is not recognized.
  159. func (m MultiRESTMapper) RESTMappings(gk unversioned.GroupKind) ([]*RESTMapping, error) {
  160. var allMappings []*RESTMapping
  161. var errors []error
  162. for _, t := range m {
  163. currMappings, err := t.RESTMappings(gk)
  164. // ignore "no match" errors, but any other error percolates back up
  165. if IsNoMatchError(err) {
  166. continue
  167. }
  168. if err != nil {
  169. errors = append(errors, err)
  170. continue
  171. }
  172. allMappings = append(allMappings, currMappings...)
  173. }
  174. if len(errors) > 0 {
  175. return nil, utilerrors.NewAggregate(errors)
  176. }
  177. if len(allMappings) == 0 {
  178. return nil, &NoKindMatchError{PartialKind: gk.WithVersion("")}
  179. }
  180. return allMappings, nil
  181. }
  182. // AliasesForResource finds the first alias response for the provided mappers.
  183. func (m MultiRESTMapper) AliasesForResource(alias string) ([]string, bool) {
  184. seenAliases := sets.NewString()
  185. allAliases := []string{}
  186. handled := false
  187. for _, t := range m {
  188. if currAliases, currOk := t.AliasesForResource(alias); currOk {
  189. for _, currAlias := range currAliases {
  190. if !seenAliases.Has(currAlias) {
  191. allAliases = append(allAliases, currAlias)
  192. seenAliases.Insert(currAlias)
  193. }
  194. }
  195. handled = true
  196. }
  197. }
  198. return allAliases, handled
  199. }