restmapper.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. // TODO: move everything in this file to pkg/api/rest
  14. package meta
  15. import (
  16. "fmt"
  17. "sort"
  18. "strings"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "k8s.io/apimachinery/pkg/runtime/schema"
  21. )
  22. // Implements RESTScope interface
  23. type restScope struct {
  24. name RESTScopeName
  25. }
  26. func (r *restScope) Name() RESTScopeName {
  27. return r.name
  28. }
  29. var RESTScopeNamespace = &restScope{
  30. name: RESTScopeNameNamespace,
  31. }
  32. var RESTScopeRoot = &restScope{
  33. name: RESTScopeNameRoot,
  34. }
  35. // DefaultRESTMapper exposes mappings between the types defined in a
  36. // runtime.Scheme. It assumes that all types defined the provided scheme
  37. // can be mapped with the provided MetadataAccessor and Codec interfaces.
  38. //
  39. // The resource name of a Kind is defined as the lowercase,
  40. // English-plural version of the Kind string.
  41. // When converting from resource to Kind, the singular version of the
  42. // resource name is also accepted for convenience.
  43. //
  44. // TODO: Only accept plural for some operations for increased control?
  45. // (`get pod bar` vs `get pods bar`)
  46. type DefaultRESTMapper struct {
  47. defaultGroupVersions []schema.GroupVersion
  48. resourceToKind map[schema.GroupVersionResource]schema.GroupVersionKind
  49. kindToPluralResource map[schema.GroupVersionKind]schema.GroupVersionResource
  50. kindToScope map[schema.GroupVersionKind]RESTScope
  51. singularToPlural map[schema.GroupVersionResource]schema.GroupVersionResource
  52. pluralToSingular map[schema.GroupVersionResource]schema.GroupVersionResource
  53. }
  54. func (m *DefaultRESTMapper) String() string {
  55. if m == nil {
  56. return "<nil>"
  57. }
  58. return fmt.Sprintf("DefaultRESTMapper{kindToPluralResource=%v}", m.kindToPluralResource)
  59. }
  60. var _ RESTMapper = &DefaultRESTMapper{}
  61. // NewDefaultRESTMapper initializes a mapping between Kind and APIVersion
  62. // to a resource name and back based on the objects in a runtime.Scheme
  63. // and the Kubernetes API conventions. Takes a group name, a priority list of the versions
  64. // to search when an object has no default version (set empty to return an error),
  65. // and a function that retrieves the correct metadata for a given version.
  66. func NewDefaultRESTMapper(defaultGroupVersions []schema.GroupVersion) *DefaultRESTMapper {
  67. resourceToKind := make(map[schema.GroupVersionResource]schema.GroupVersionKind)
  68. kindToPluralResource := make(map[schema.GroupVersionKind]schema.GroupVersionResource)
  69. kindToScope := make(map[schema.GroupVersionKind]RESTScope)
  70. singularToPlural := make(map[schema.GroupVersionResource]schema.GroupVersionResource)
  71. pluralToSingular := make(map[schema.GroupVersionResource]schema.GroupVersionResource)
  72. // TODO: verify name mappings work correctly when versions differ
  73. return &DefaultRESTMapper{
  74. resourceToKind: resourceToKind,
  75. kindToPluralResource: kindToPluralResource,
  76. kindToScope: kindToScope,
  77. defaultGroupVersions: defaultGroupVersions,
  78. singularToPlural: singularToPlural,
  79. pluralToSingular: pluralToSingular,
  80. }
  81. }
  82. func (m *DefaultRESTMapper) Add(kind schema.GroupVersionKind, scope RESTScope) {
  83. plural, singular := UnsafeGuessKindToResource(kind)
  84. m.AddSpecific(kind, plural, singular, scope)
  85. }
  86. func (m *DefaultRESTMapper) AddSpecific(kind schema.GroupVersionKind, plural, singular schema.GroupVersionResource, scope RESTScope) {
  87. m.singularToPlural[singular] = plural
  88. m.pluralToSingular[plural] = singular
  89. m.resourceToKind[singular] = kind
  90. m.resourceToKind[plural] = kind
  91. m.kindToPluralResource[kind] = plural
  92. m.kindToScope[kind] = scope
  93. }
  94. // unpluralizedSuffixes is a list of resource suffixes that are the same plural and singular
  95. // This is only is only necessary because some bits of code are lazy and don't actually use the RESTMapper like they should.
  96. // TODO eliminate this so that different callers can correctly map to resources. This probably means updating all
  97. // callers to use the RESTMapper they mean.
  98. var unpluralizedSuffixes = []string{
  99. "endpoints",
  100. }
  101. // UnsafeGuessKindToResource converts Kind to a resource name.
  102. // Broken. This method only "sort of" works when used outside of this package. It assumes that Kinds and Resources match
  103. // and they aren't guaranteed to do so.
  104. func UnsafeGuessKindToResource(kind schema.GroupVersionKind) ( /*plural*/ schema.GroupVersionResource /*singular*/, schema.GroupVersionResource) {
  105. kindName := kind.Kind
  106. if len(kindName) == 0 {
  107. return schema.GroupVersionResource{}, schema.GroupVersionResource{}
  108. }
  109. singularName := strings.ToLower(kindName)
  110. singular := kind.GroupVersion().WithResource(singularName)
  111. for _, skip := range unpluralizedSuffixes {
  112. if strings.HasSuffix(singularName, skip) {
  113. return singular, singular
  114. }
  115. }
  116. switch string(singularName[len(singularName)-1]) {
  117. case "s":
  118. return kind.GroupVersion().WithResource(singularName + "es"), singular
  119. case "y":
  120. return kind.GroupVersion().WithResource(strings.TrimSuffix(singularName, "y") + "ies"), singular
  121. }
  122. return kind.GroupVersion().WithResource(singularName + "s"), singular
  123. }
  124. // ResourceSingularizer implements RESTMapper
  125. // It converts a resource name from plural to singular (e.g., from pods to pod)
  126. func (m *DefaultRESTMapper) ResourceSingularizer(resourceType string) (string, error) {
  127. partialResource := schema.GroupVersionResource{Resource: resourceType}
  128. resources, err := m.ResourcesFor(partialResource)
  129. if err != nil {
  130. return resourceType, err
  131. }
  132. singular := schema.GroupVersionResource{}
  133. for _, curr := range resources {
  134. currSingular, ok := m.pluralToSingular[curr]
  135. if !ok {
  136. continue
  137. }
  138. if singular.Empty() {
  139. singular = currSingular
  140. continue
  141. }
  142. if currSingular.Resource != singular.Resource {
  143. return resourceType, fmt.Errorf("multiple possible singular resources (%v) found for %v", resources, resourceType)
  144. }
  145. }
  146. if singular.Empty() {
  147. return resourceType, fmt.Errorf("no singular of resource %v has been defined", resourceType)
  148. }
  149. return singular.Resource, nil
  150. }
  151. // coerceResourceForMatching makes the resource lower case and converts internal versions to unspecified (legacy behavior)
  152. func coerceResourceForMatching(resource schema.GroupVersionResource) schema.GroupVersionResource {
  153. resource.Resource = strings.ToLower(resource.Resource)
  154. if resource.Version == runtime.APIVersionInternal {
  155. resource.Version = ""
  156. }
  157. return resource
  158. }
  159. func (m *DefaultRESTMapper) ResourcesFor(input schema.GroupVersionResource) ([]schema.GroupVersionResource, error) {
  160. resource := coerceResourceForMatching(input)
  161. hasResource := len(resource.Resource) > 0
  162. hasGroup := len(resource.Group) > 0
  163. hasVersion := len(resource.Version) > 0
  164. if !hasResource {
  165. return nil, fmt.Errorf("a resource must be present, got: %v", resource)
  166. }
  167. ret := []schema.GroupVersionResource{}
  168. switch {
  169. case hasGroup && hasVersion:
  170. // fully qualified. Find the exact match
  171. for plural, singular := range m.pluralToSingular {
  172. if singular == resource {
  173. ret = append(ret, plural)
  174. break
  175. }
  176. if plural == resource {
  177. ret = append(ret, plural)
  178. break
  179. }
  180. }
  181. case hasGroup:
  182. // given a group, prefer an exact match. If you don't find one, resort to a prefix match on group
  183. foundExactMatch := false
  184. requestedGroupResource := resource.GroupResource()
  185. for plural, singular := range m.pluralToSingular {
  186. if singular.GroupResource() == requestedGroupResource {
  187. foundExactMatch = true
  188. ret = append(ret, plural)
  189. }
  190. if plural.GroupResource() == requestedGroupResource {
  191. foundExactMatch = true
  192. ret = append(ret, plural)
  193. }
  194. }
  195. // if you didn't find an exact match, match on group prefixing. This allows storageclass.storage to match
  196. // storageclass.storage.k8s.io
  197. if !foundExactMatch {
  198. for plural, singular := range m.pluralToSingular {
  199. if !strings.HasPrefix(plural.Group, requestedGroupResource.Group) {
  200. continue
  201. }
  202. if singular.Resource == requestedGroupResource.Resource {
  203. ret = append(ret, plural)
  204. }
  205. if plural.Resource == requestedGroupResource.Resource {
  206. ret = append(ret, plural)
  207. }
  208. }
  209. }
  210. case hasVersion:
  211. for plural, singular := range m.pluralToSingular {
  212. if singular.Version == resource.Version && singular.Resource == resource.Resource {
  213. ret = append(ret, plural)
  214. }
  215. if plural.Version == resource.Version && plural.Resource == resource.Resource {
  216. ret = append(ret, plural)
  217. }
  218. }
  219. default:
  220. for plural, singular := range m.pluralToSingular {
  221. if singular.Resource == resource.Resource {
  222. ret = append(ret, plural)
  223. }
  224. if plural.Resource == resource.Resource {
  225. ret = append(ret, plural)
  226. }
  227. }
  228. }
  229. if len(ret) == 0 {
  230. return nil, &NoResourceMatchError{PartialResource: resource}
  231. }
  232. sort.Sort(resourceByPreferredGroupVersion{ret, m.defaultGroupVersions})
  233. return ret, nil
  234. }
  235. func (m *DefaultRESTMapper) ResourceFor(resource schema.GroupVersionResource) (schema.GroupVersionResource, error) {
  236. resources, err := m.ResourcesFor(resource)
  237. if err != nil {
  238. return schema.GroupVersionResource{}, err
  239. }
  240. if len(resources) == 1 {
  241. return resources[0], nil
  242. }
  243. return schema.GroupVersionResource{}, &AmbiguousResourceError{PartialResource: resource, MatchingResources: resources}
  244. }
  245. func (m *DefaultRESTMapper) KindsFor(input schema.GroupVersionResource) ([]schema.GroupVersionKind, error) {
  246. resource := coerceResourceForMatching(input)
  247. hasResource := len(resource.Resource) > 0
  248. hasGroup := len(resource.Group) > 0
  249. hasVersion := len(resource.Version) > 0
  250. if !hasResource {
  251. return nil, fmt.Errorf("a resource must be present, got: %v", resource)
  252. }
  253. ret := []schema.GroupVersionKind{}
  254. switch {
  255. // fully qualified. Find the exact match
  256. case hasGroup && hasVersion:
  257. kind, exists := m.resourceToKind[resource]
  258. if exists {
  259. ret = append(ret, kind)
  260. }
  261. case hasGroup:
  262. foundExactMatch := false
  263. requestedGroupResource := resource.GroupResource()
  264. for currResource, currKind := range m.resourceToKind {
  265. if currResource.GroupResource() == requestedGroupResource {
  266. foundExactMatch = true
  267. ret = append(ret, currKind)
  268. }
  269. }
  270. // if you didn't find an exact match, match on group prefixing. This allows storageclass.storage to match
  271. // storageclass.storage.k8s.io
  272. if !foundExactMatch {
  273. for currResource, currKind := range m.resourceToKind {
  274. if !strings.HasPrefix(currResource.Group, requestedGroupResource.Group) {
  275. continue
  276. }
  277. if currResource.Resource == requestedGroupResource.Resource {
  278. ret = append(ret, currKind)
  279. }
  280. }
  281. }
  282. case hasVersion:
  283. for currResource, currKind := range m.resourceToKind {
  284. if currResource.Version == resource.Version && currResource.Resource == resource.Resource {
  285. ret = append(ret, currKind)
  286. }
  287. }
  288. default:
  289. for currResource, currKind := range m.resourceToKind {
  290. if currResource.Resource == resource.Resource {
  291. ret = append(ret, currKind)
  292. }
  293. }
  294. }
  295. if len(ret) == 0 {
  296. return nil, &NoResourceMatchError{PartialResource: input}
  297. }
  298. sort.Sort(kindByPreferredGroupVersion{ret, m.defaultGroupVersions})
  299. return ret, nil
  300. }
  301. func (m *DefaultRESTMapper) KindFor(resource schema.GroupVersionResource) (schema.GroupVersionKind, error) {
  302. kinds, err := m.KindsFor(resource)
  303. if err != nil {
  304. return schema.GroupVersionKind{}, err
  305. }
  306. if len(kinds) == 1 {
  307. return kinds[0], nil
  308. }
  309. return schema.GroupVersionKind{}, &AmbiguousResourceError{PartialResource: resource, MatchingKinds: kinds}
  310. }
  311. type kindByPreferredGroupVersion struct {
  312. list []schema.GroupVersionKind
  313. sortOrder []schema.GroupVersion
  314. }
  315. func (o kindByPreferredGroupVersion) Len() int { return len(o.list) }
  316. func (o kindByPreferredGroupVersion) Swap(i, j int) { o.list[i], o.list[j] = o.list[j], o.list[i] }
  317. func (o kindByPreferredGroupVersion) Less(i, j int) bool {
  318. lhs := o.list[i]
  319. rhs := o.list[j]
  320. if lhs == rhs {
  321. return false
  322. }
  323. if lhs.GroupVersion() == rhs.GroupVersion() {
  324. return lhs.Kind < rhs.Kind
  325. }
  326. // otherwise, the difference is in the GroupVersion, so we need to sort with respect to the preferred order
  327. lhsIndex := -1
  328. rhsIndex := -1
  329. for i := range o.sortOrder {
  330. if o.sortOrder[i] == lhs.GroupVersion() {
  331. lhsIndex = i
  332. }
  333. if o.sortOrder[i] == rhs.GroupVersion() {
  334. rhsIndex = i
  335. }
  336. }
  337. if rhsIndex == -1 {
  338. return true
  339. }
  340. return lhsIndex < rhsIndex
  341. }
  342. type resourceByPreferredGroupVersion struct {
  343. list []schema.GroupVersionResource
  344. sortOrder []schema.GroupVersion
  345. }
  346. func (o resourceByPreferredGroupVersion) Len() int { return len(o.list) }
  347. func (o resourceByPreferredGroupVersion) Swap(i, j int) { o.list[i], o.list[j] = o.list[j], o.list[i] }
  348. func (o resourceByPreferredGroupVersion) Less(i, j int) bool {
  349. lhs := o.list[i]
  350. rhs := o.list[j]
  351. if lhs == rhs {
  352. return false
  353. }
  354. if lhs.GroupVersion() == rhs.GroupVersion() {
  355. return lhs.Resource < rhs.Resource
  356. }
  357. // otherwise, the difference is in the GroupVersion, so we need to sort with respect to the preferred order
  358. lhsIndex := -1
  359. rhsIndex := -1
  360. for i := range o.sortOrder {
  361. if o.sortOrder[i] == lhs.GroupVersion() {
  362. lhsIndex = i
  363. }
  364. if o.sortOrder[i] == rhs.GroupVersion() {
  365. rhsIndex = i
  366. }
  367. }
  368. if rhsIndex == -1 {
  369. return true
  370. }
  371. return lhsIndex < rhsIndex
  372. }
  373. // RESTMapping returns a struct representing the resource path and conversion interfaces a
  374. // RESTClient should use to operate on the provided group/kind in order of versions. If a version search
  375. // order is not provided, the search order provided to DefaultRESTMapper will be used to resolve which
  376. // version should be used to access the named group/kind.
  377. func (m *DefaultRESTMapper) RESTMapping(gk schema.GroupKind, versions ...string) (*RESTMapping, error) {
  378. mappings, err := m.RESTMappings(gk, versions...)
  379. if err != nil {
  380. return nil, err
  381. }
  382. if len(mappings) == 0 {
  383. return nil, &NoKindMatchError{GroupKind: gk, SearchedVersions: versions}
  384. }
  385. // since we rely on RESTMappings method
  386. // take the first match and return to the caller
  387. // as this was the existing behavior.
  388. return mappings[0], nil
  389. }
  390. // RESTMappings returns the RESTMappings for the provided group kind. If a version search order
  391. // is not provided, the search order provided to DefaultRESTMapper will be used.
  392. func (m *DefaultRESTMapper) RESTMappings(gk schema.GroupKind, versions ...string) ([]*RESTMapping, error) {
  393. mappings := make([]*RESTMapping, 0)
  394. potentialGVK := make([]schema.GroupVersionKind, 0)
  395. hadVersion := false
  396. // Pick an appropriate version
  397. for _, version := range versions {
  398. if len(version) == 0 || version == runtime.APIVersionInternal {
  399. continue
  400. }
  401. currGVK := gk.WithVersion(version)
  402. hadVersion = true
  403. if _, ok := m.kindToPluralResource[currGVK]; ok {
  404. potentialGVK = append(potentialGVK, currGVK)
  405. break
  406. }
  407. }
  408. // Use the default preferred versions
  409. if !hadVersion && len(potentialGVK) == 0 {
  410. for _, gv := range m.defaultGroupVersions {
  411. if gv.Group != gk.Group {
  412. continue
  413. }
  414. potentialGVK = append(potentialGVK, gk.WithVersion(gv.Version))
  415. }
  416. }
  417. if len(potentialGVK) == 0 {
  418. return nil, &NoKindMatchError{GroupKind: gk, SearchedVersions: versions}
  419. }
  420. for _, gvk := range potentialGVK {
  421. //Ensure we have a REST mapping
  422. res, ok := m.kindToPluralResource[gvk]
  423. if !ok {
  424. continue
  425. }
  426. // Ensure we have a REST scope
  427. scope, ok := m.kindToScope[gvk]
  428. if !ok {
  429. return nil, fmt.Errorf("the provided version %q and kind %q cannot be mapped to a supported scope", gvk.GroupVersion(), gvk.Kind)
  430. }
  431. mappings = append(mappings, &RESTMapping{
  432. Resource: res,
  433. GroupVersionKind: gvk,
  434. Scope: scope,
  435. })
  436. }
  437. if len(mappings) == 0 {
  438. return nil, &NoResourceMatchError{PartialResource: schema.GroupVersionResource{Group: gk.Group, Resource: gk.Kind}}
  439. }
  440. return mappings, nil
  441. }
  442. // MaybeResetRESTMapper calls Reset() on the mapper if it is a ResettableRESTMapper.
  443. func MaybeResetRESTMapper(mapper RESTMapper) {
  444. m, ok := mapper.(ResettableRESTMapper)
  445. if ok {
  446. m.Reset()
  447. }
  448. }