group_version.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. Copyright 2015 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 schema
  14. import (
  15. "fmt"
  16. "strings"
  17. )
  18. // ParseResourceArg takes the common style of string which may be either `resource.group.com` or `resource.version.group.com`
  19. // and parses it out into both possibilities. This code takes no responsibility for knowing which representation was intended
  20. // but with a knowledge of all GroupVersions, calling code can take a very good guess. If there are only two segments, then
  21. // `*GroupVersionResource` is nil.
  22. // `resource.group.com` -> `group=com, version=group, resource=resource` and `group=group.com, resource=resource`
  23. func ParseResourceArg(arg string) (*GroupVersionResource, GroupResource) {
  24. var gvr *GroupVersionResource
  25. if strings.Count(arg, ".") >= 2 {
  26. s := strings.SplitN(arg, ".", 3)
  27. gvr = &GroupVersionResource{Group: s[2], Version: s[1], Resource: s[0]}
  28. }
  29. return gvr, ParseGroupResource(arg)
  30. }
  31. // ParseKindArg takes the common style of string which may be either `Kind.group.com` or `Kind.version.group.com`
  32. // and parses it out into both possibilities. This code takes no responsibility for knowing which representation was intended
  33. // but with a knowledge of all GroupKinds, calling code can take a very good guess. If there are only two segments, then
  34. // `*GroupVersionResource` is nil.
  35. // `Kind.group.com` -> `group=com, version=group, kind=Kind` and `group=group.com, kind=Kind`
  36. func ParseKindArg(arg string) (*GroupVersionKind, GroupKind) {
  37. var gvk *GroupVersionKind
  38. if strings.Count(arg, ".") >= 2 {
  39. s := strings.SplitN(arg, ".", 3)
  40. gvk = &GroupVersionKind{Group: s[2], Version: s[1], Kind: s[0]}
  41. }
  42. return gvk, ParseGroupKind(arg)
  43. }
  44. // GroupResource specifies a Group and a Resource, but does not force a version. This is useful for identifying
  45. // concepts during lookup stages without having partially valid types
  46. type GroupResource struct {
  47. Group string
  48. Resource string
  49. }
  50. func (gr GroupResource) WithVersion(version string) GroupVersionResource {
  51. return GroupVersionResource{Group: gr.Group, Version: version, Resource: gr.Resource}
  52. }
  53. func (gr GroupResource) Empty() bool {
  54. return len(gr.Group) == 0 && len(gr.Resource) == 0
  55. }
  56. func (gr *GroupResource) String() string {
  57. if len(gr.Group) == 0 {
  58. return gr.Resource
  59. }
  60. return gr.Resource + "." + gr.Group
  61. }
  62. func ParseGroupKind(gk string) GroupKind {
  63. i := strings.Index(gk, ".")
  64. if i == -1 {
  65. return GroupKind{Kind: gk}
  66. }
  67. return GroupKind{Group: gk[i+1:], Kind: gk[:i]}
  68. }
  69. // ParseGroupResource turns "resource.group" string into a GroupResource struct. Empty strings are allowed
  70. // for each field.
  71. func ParseGroupResource(gr string) GroupResource {
  72. if i := strings.Index(gr, "."); i == -1 {
  73. return GroupResource{Resource: gr}
  74. } else {
  75. return GroupResource{Group: gr[i+1:], Resource: gr[:i]}
  76. }
  77. }
  78. // GroupVersionResource unambiguously identifies a resource. It doesn't anonymously include GroupVersion
  79. // to avoid automatic coercion. It doesn't use a GroupVersion to avoid custom marshalling
  80. type GroupVersionResource struct {
  81. Group string
  82. Version string
  83. Resource string
  84. }
  85. func (gvr GroupVersionResource) Empty() bool {
  86. return len(gvr.Group) == 0 && len(gvr.Version) == 0 && len(gvr.Resource) == 0
  87. }
  88. func (gvr GroupVersionResource) GroupResource() GroupResource {
  89. return GroupResource{Group: gvr.Group, Resource: gvr.Resource}
  90. }
  91. func (gvr GroupVersionResource) GroupVersion() GroupVersion {
  92. return GroupVersion{Group: gvr.Group, Version: gvr.Version}
  93. }
  94. func (gvr *GroupVersionResource) String() string {
  95. return strings.Join([]string{gvr.Group, "/", gvr.Version, ", Resource=", gvr.Resource}, "")
  96. }
  97. // GroupKind specifies a Group and a Kind, but does not force a version. This is useful for identifying
  98. // concepts during lookup stages without having partially valid types
  99. type GroupKind struct {
  100. Group string
  101. Kind string
  102. }
  103. func (gk GroupKind) Empty() bool {
  104. return len(gk.Group) == 0 && len(gk.Kind) == 0
  105. }
  106. func (gk GroupKind) WithVersion(version string) GroupVersionKind {
  107. return GroupVersionKind{Group: gk.Group, Version: version, Kind: gk.Kind}
  108. }
  109. func (gk *GroupKind) String() string {
  110. if len(gk.Group) == 0 {
  111. return gk.Kind
  112. }
  113. return gk.Kind + "." + gk.Group
  114. }
  115. // GroupVersionKind unambiguously identifies a kind. It doesn't anonymously include GroupVersion
  116. // to avoid automatic coercion. It doesn't use a GroupVersion to avoid custom marshalling
  117. type GroupVersionKind struct {
  118. Group string
  119. Version string
  120. Kind string
  121. }
  122. // Empty returns true if group, version, and kind are empty
  123. func (gvk GroupVersionKind) Empty() bool {
  124. return len(gvk.Group) == 0 && len(gvk.Version) == 0 && len(gvk.Kind) == 0
  125. }
  126. func (gvk GroupVersionKind) GroupKind() GroupKind {
  127. return GroupKind{Group: gvk.Group, Kind: gvk.Kind}
  128. }
  129. func (gvk GroupVersionKind) GroupVersion() GroupVersion {
  130. return GroupVersion{Group: gvk.Group, Version: gvk.Version}
  131. }
  132. func (gvk GroupVersionKind) String() string {
  133. return gvk.Group + "/" + gvk.Version + ", Kind=" + gvk.Kind
  134. }
  135. // GroupVersion contains the "group" and the "version", which uniquely identifies the API.
  136. type GroupVersion struct {
  137. Group string
  138. Version string
  139. }
  140. // Empty returns true if group and version are empty
  141. func (gv GroupVersion) Empty() bool {
  142. return len(gv.Group) == 0 && len(gv.Version) == 0
  143. }
  144. // String puts "group" and "version" into a single "group/version" string. For the legacy v1
  145. // it returns "v1".
  146. func (gv GroupVersion) String() string {
  147. // special case the internal apiVersion for the legacy kube types
  148. if gv.Empty() {
  149. return ""
  150. }
  151. // special case of "v1" for backward compatibility
  152. if len(gv.Group) == 0 && gv.Version == "v1" {
  153. return gv.Version
  154. }
  155. if len(gv.Group) > 0 {
  156. return gv.Group + "/" + gv.Version
  157. }
  158. return gv.Version
  159. }
  160. // KindForGroupVersionKinds identifies the preferred GroupVersionKind out of a list. It returns ok false
  161. // if none of the options match the group. It prefers a match to group and version over just group.
  162. // TODO: Move GroupVersion to a package under pkg/runtime, since it's used by scheme.
  163. // TODO: Introduce an adapter type between GroupVersion and runtime.GroupVersioner, and use LegacyCodec(GroupVersion)
  164. // in fewer places.
  165. func (gv GroupVersion) KindForGroupVersionKinds(kinds []GroupVersionKind) (target GroupVersionKind, ok bool) {
  166. for _, gvk := range kinds {
  167. if gvk.Group == gv.Group && gvk.Version == gv.Version {
  168. return gvk, true
  169. }
  170. }
  171. for _, gvk := range kinds {
  172. if gvk.Group == gv.Group {
  173. return gv.WithKind(gvk.Kind), true
  174. }
  175. }
  176. return GroupVersionKind{}, false
  177. }
  178. // ParseGroupVersion turns "group/version" string into a GroupVersion struct. It reports error
  179. // if it cannot parse the string.
  180. func ParseGroupVersion(gv string) (GroupVersion, error) {
  181. // this can be the internal version for the legacy kube types
  182. // TODO once we've cleared the last uses as strings, this special case should be removed.
  183. if (len(gv) == 0) || (gv == "/") {
  184. return GroupVersion{}, nil
  185. }
  186. switch strings.Count(gv, "/") {
  187. case 0:
  188. return GroupVersion{"", gv}, nil
  189. case 1:
  190. i := strings.Index(gv, "/")
  191. return GroupVersion{gv[:i], gv[i+1:]}, nil
  192. default:
  193. return GroupVersion{}, fmt.Errorf("unexpected GroupVersion string: %v", gv)
  194. }
  195. }
  196. // WithKind creates a GroupVersionKind based on the method receiver's GroupVersion and the passed Kind.
  197. func (gv GroupVersion) WithKind(kind string) GroupVersionKind {
  198. return GroupVersionKind{Group: gv.Group, Version: gv.Version, Kind: kind}
  199. }
  200. // WithResource creates a GroupVersionResource based on the method receiver's GroupVersion and the passed Resource.
  201. func (gv GroupVersion) WithResource(resource string) GroupVersionResource {
  202. return GroupVersionResource{Group: gv.Group, Version: gv.Version, Resource: resource}
  203. }
  204. // GroupVersions can be used to represent a set of desired group versions.
  205. // TODO: Move GroupVersions to a package under pkg/runtime, since it's used by scheme.
  206. // TODO: Introduce an adapter type between GroupVersions and runtime.GroupVersioner, and use LegacyCodec(GroupVersion)
  207. // in fewer places.
  208. type GroupVersions []GroupVersion
  209. // KindForGroupVersionKinds identifies the preferred GroupVersionKind out of a list. It returns ok false
  210. // if none of the options match the group.
  211. func (gvs GroupVersions) KindForGroupVersionKinds(kinds []GroupVersionKind) (GroupVersionKind, bool) {
  212. var targets []GroupVersionKind
  213. for _, gv := range gvs {
  214. target, ok := gv.KindForGroupVersionKinds(kinds)
  215. if !ok {
  216. continue
  217. }
  218. targets = append(targets, target)
  219. }
  220. if len(targets) == 1 {
  221. return targets[0], true
  222. }
  223. if len(targets) > 1 {
  224. return bestMatch(kinds, targets), true
  225. }
  226. return GroupVersionKind{}, false
  227. }
  228. // bestMatch tries to pick best matching GroupVersionKind and falls back to the first
  229. // found if no exact match exists.
  230. func bestMatch(kinds []GroupVersionKind, targets []GroupVersionKind) GroupVersionKind {
  231. for _, gvk := range targets {
  232. for _, k := range kinds {
  233. if k == gvk {
  234. return k
  235. }
  236. }
  237. }
  238. return targets[0]
  239. }
  240. // ToAPIVersionAndKind is a convenience method for satisfying runtime.Object on types that
  241. // do not use TypeMeta.
  242. func (gvk *GroupVersionKind) ToAPIVersionAndKind() (string, string) {
  243. if gvk == nil {
  244. return "", ""
  245. }
  246. return gvk.GroupVersion().String(), gvk.Kind
  247. }
  248. // FromAPIVersionAndKind returns a GVK representing the provided fields for types that
  249. // do not use TypeMeta. This method exists to support test types and legacy serializations
  250. // that have a distinct group and kind.
  251. // TODO: further reduce usage of this method.
  252. func FromAPIVersionAndKind(apiVersion, kind string) GroupVersionKind {
  253. if gv, err := ParseGroupVersion(apiVersion); err == nil {
  254. return GroupVersionKind{Group: gv.Group, Version: gv.Version, Kind: kind}
  255. }
  256. return GroupVersionKind{Kind: kind}
  257. }