mapper.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 resource
  14. import (
  15. "fmt"
  16. "reflect"
  17. "k8s.io/kubernetes/pkg/api/meta"
  18. "k8s.io/kubernetes/pkg/api/unversioned"
  19. "k8s.io/kubernetes/pkg/runtime"
  20. )
  21. // DisabledClientForMapping allows callers to avoid allowing remote calls when handling
  22. // resources.
  23. type DisabledClientForMapping struct {
  24. ClientMapper
  25. }
  26. func (f DisabledClientForMapping) ClientForMapping(mapping *meta.RESTMapping) (RESTClient, error) {
  27. return nil, nil
  28. }
  29. // Mapper is a convenience struct for holding references to the three interfaces
  30. // needed to create Info for arbitrary objects.
  31. type Mapper struct {
  32. runtime.ObjectTyper
  33. meta.RESTMapper
  34. ClientMapper
  35. runtime.Decoder
  36. }
  37. // InfoForData creates an Info object for the given data. An error is returned
  38. // if any of the decoding or client lookup steps fail. Name and namespace will be
  39. // set into Info if the mapping's MetadataAccessor can retrieve them.
  40. func (m *Mapper) InfoForData(data []byte, source string) (*Info, error) {
  41. versions := &runtime.VersionedObjects{}
  42. _, gvk, err := m.Decode(data, nil, versions)
  43. if err != nil {
  44. return nil, fmt.Errorf("unable to decode %q: %v", source, err)
  45. }
  46. obj, versioned := versions.Last(), versions.First()
  47. mapping, err := m.RESTMapping(gvk.GroupKind(), gvk.Version)
  48. if err != nil {
  49. return nil, fmt.Errorf("unable to recognize %q: %v", source, err)
  50. }
  51. client, err := m.ClientForMapping(mapping)
  52. if err != nil {
  53. return nil, fmt.Errorf("unable to connect to a server to handle %q: %v", mapping.Resource, err)
  54. }
  55. name, _ := mapping.MetadataAccessor.Name(obj)
  56. namespace, _ := mapping.MetadataAccessor.Namespace(obj)
  57. resourceVersion, _ := mapping.MetadataAccessor.ResourceVersion(obj)
  58. return &Info{
  59. Mapping: mapping,
  60. Client: client,
  61. Namespace: namespace,
  62. Name: name,
  63. Source: source,
  64. VersionedObject: versioned,
  65. Object: obj,
  66. ResourceVersion: resourceVersion,
  67. }, nil
  68. }
  69. // InfoForObject creates an Info object for the given Object. An error is returned
  70. // if the object cannot be introspected. Name and namespace will be set into Info
  71. // if the mapping's MetadataAccessor can retrieve them.
  72. func (m *Mapper) InfoForObject(obj runtime.Object, preferredGVKs []unversioned.GroupVersionKind) (*Info, error) {
  73. groupVersionKinds, _, err := m.ObjectKinds(obj)
  74. if err != nil {
  75. return nil, fmt.Errorf("unable to get type info from the object %q: %v", reflect.TypeOf(obj), err)
  76. }
  77. groupVersionKind := groupVersionKinds[0]
  78. if len(groupVersionKinds) > 1 && len(preferredGVKs) > 0 {
  79. groupVersionKind = preferredObjectKind(groupVersionKinds, preferredGVKs)
  80. }
  81. mapping, err := m.RESTMapping(groupVersionKind.GroupKind(), groupVersionKind.Version)
  82. if err != nil {
  83. return nil, fmt.Errorf("unable to recognize %v: %v", groupVersionKind, err)
  84. }
  85. client, err := m.ClientForMapping(mapping)
  86. if err != nil {
  87. return nil, fmt.Errorf("unable to connect to a server to handle %q: %v", mapping.Resource, err)
  88. }
  89. name, _ := mapping.MetadataAccessor.Name(obj)
  90. namespace, _ := mapping.MetadataAccessor.Namespace(obj)
  91. resourceVersion, _ := mapping.MetadataAccessor.ResourceVersion(obj)
  92. return &Info{
  93. Mapping: mapping,
  94. Client: client,
  95. Namespace: namespace,
  96. Name: name,
  97. Object: obj,
  98. ResourceVersion: resourceVersion,
  99. }, nil
  100. }
  101. // preferredObjectKind picks the possibility that most closely matches the priority list in this order:
  102. // GroupVersionKind matches (exact match)
  103. // GroupKind matches
  104. // Group matches
  105. func preferredObjectKind(possibilities []unversioned.GroupVersionKind, preferences []unversioned.GroupVersionKind) unversioned.GroupVersionKind {
  106. // Exact match
  107. for _, priority := range preferences {
  108. for _, possibility := range possibilities {
  109. if possibility == priority {
  110. return possibility
  111. }
  112. }
  113. }
  114. // GroupKind match
  115. for _, priority := range preferences {
  116. for _, possibility := range possibilities {
  117. if possibility.GroupKind() == priority.GroupKind() {
  118. return possibility
  119. }
  120. }
  121. }
  122. // Group match
  123. for _, priority := range preferences {
  124. for _, possibility := range possibilities {
  125. if possibility.Group == priority.Group {
  126. return possibility
  127. }
  128. }
  129. }
  130. // Just pick the first
  131. return possibilities[0]
  132. }