2 Copyright 2015 The Kubernetes Authors.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
24 // ParseResourceArg takes the common style of string which may be either `resource.group.com` or `resource.version.group.com`
25 // and parses it out into both possibilities. This code takes no responsibility for knowing which representation was intended
26 // but with a knowledge of all GroupVersions, calling code can take a very good guess. If there are only two segments, then
27 // `*GroupVersionResource` is nil.
28 // `resource.group.com` -> `group=com, version=group, resource=resource` and `group=group.com, resource=resource`
29 func ParseResourceArg(arg string) (*GroupVersionResource, GroupResource) {
30 var gvr *GroupVersionResource
31 if strings.Count(arg, ".") >= 2 {
32 s := strings.SplitN(arg, ".", 3)
33 gvr = &GroupVersionResource{Group: s[2], Version: s[1], Resource: s[0]}
36 return gvr, ParseGroupResource(arg)
39 // ParseKindArg takes the common style of string which may be either `Kind.group.com` or `Kind.version.group.com`
40 // and parses it out into both possibilities. This code takes no responsibility for knowing which representation was intended
41 // but with a knowledge of all GroupKinds, calling code can take a very good guess. If there are only two segments, then
42 // `*GroupVersionResource` is nil.
43 // `Kind.group.com` -> `group=com, version=group, kind=Kind` and `group=group.com, kind=Kind`
44 func ParseKindArg(arg string) (*GroupVersionKind, GroupKind) {
45 var gvk *GroupVersionKind
46 if strings.Count(arg, ".") >= 2 {
47 s := strings.SplitN(arg, ".", 3)
48 gvk = &GroupVersionKind{Group: s[2], Version: s[1], Kind: s[0]}
51 return gvk, ParseGroupKind(arg)
54 // GroupResource specifies a Group and a Resource, but does not force a version. This is useful for identifying
55 // concepts during lookup stages without having partially valid types
56 type GroupResource struct {
61 func (gr GroupResource) WithVersion(version string) GroupVersionResource {
62 return GroupVersionResource{Group: gr.Group, Version: version, Resource: gr.Resource}
65 func (gr GroupResource) Empty() bool {
66 return len(gr.Group) == 0 && len(gr.Resource) == 0
69 func (gr GroupResource) String() string {
70 if len(gr.Group) == 0 {
73 return gr.Resource + "." + gr.Group
76 func ParseGroupKind(gk string) GroupKind {
77 i := strings.Index(gk, ".")
79 return GroupKind{Kind: gk}
82 return GroupKind{Group: gk[i+1:], Kind: gk[:i]}
85 // ParseGroupResource turns "resource.group" string into a GroupResource struct. Empty strings are allowed
87 func ParseGroupResource(gr string) GroupResource {
88 if i := strings.Index(gr, "."); i >= 0 {
89 return GroupResource{Group: gr[i+1:], Resource: gr[:i]}
91 return GroupResource{Resource: gr}
94 // GroupVersionResource unambiguously identifies a resource. It doesn't anonymously include GroupVersion
95 // to avoid automatic coercion. It doesn't use a GroupVersion to avoid custom marshalling
96 type GroupVersionResource struct {
102 func (gvr GroupVersionResource) Empty() bool {
103 return len(gvr.Group) == 0 && len(gvr.Version) == 0 && len(gvr.Resource) == 0
106 func (gvr GroupVersionResource) GroupResource() GroupResource {
107 return GroupResource{Group: gvr.Group, Resource: gvr.Resource}
110 func (gvr GroupVersionResource) GroupVersion() GroupVersion {
111 return GroupVersion{Group: gvr.Group, Version: gvr.Version}
114 func (gvr GroupVersionResource) String() string {
115 return strings.Join([]string{gvr.Group, "/", gvr.Version, ", Resource=", gvr.Resource}, "")
118 // GroupKind specifies a Group and a Kind, but does not force a version. This is useful for identifying
119 // concepts during lookup stages without having partially valid types
120 type GroupKind struct {
125 func (gk GroupKind) Empty() bool {
126 return len(gk.Group) == 0 && len(gk.Kind) == 0
129 func (gk GroupKind) WithVersion(version string) GroupVersionKind {
130 return GroupVersionKind{Group: gk.Group, Version: version, Kind: gk.Kind}
133 func (gk GroupKind) String() string {
134 if len(gk.Group) == 0 {
137 return gk.Kind + "." + gk.Group
140 // GroupVersionKind unambiguously identifies a kind. It doesn't anonymously include GroupVersion
141 // to avoid automatic coercion. It doesn't use a GroupVersion to avoid custom marshalling
142 type GroupVersionKind struct {
148 // Empty returns true if group, version, and kind are empty
149 func (gvk GroupVersionKind) Empty() bool {
150 return len(gvk.Group) == 0 && len(gvk.Version) == 0 && len(gvk.Kind) == 0
153 func (gvk GroupVersionKind) GroupKind() GroupKind {
154 return GroupKind{Group: gvk.Group, Kind: gvk.Kind}
157 func (gvk GroupVersionKind) GroupVersion() GroupVersion {
158 return GroupVersion{Group: gvk.Group, Version: gvk.Version}
161 func (gvk GroupVersionKind) String() string {
162 return gvk.Group + "/" + gvk.Version + ", Kind=" + gvk.Kind
165 // GroupVersion contains the "group" and the "version", which uniquely identifies the API.
166 type GroupVersion struct {
171 // Empty returns true if group and version are empty
172 func (gv GroupVersion) Empty() bool {
173 return len(gv.Group) == 0 && len(gv.Version) == 0
176 // String puts "group" and "version" into a single "group/version" string. For the legacy v1
178 func (gv GroupVersion) String() string {
179 // special case the internal apiVersion for the legacy kube types
184 // special case of "v1" for backward compatibility
185 if len(gv.Group) == 0 && gv.Version == "v1" {
188 if len(gv.Group) > 0 {
189 return gv.Group + "/" + gv.Version
194 // KindForGroupVersionKinds identifies the preferred GroupVersionKind out of a list. It returns ok false
195 // if none of the options match the group. It prefers a match to group and version over just group.
196 // TODO: Move GroupVersion to a package under pkg/runtime, since it's used by scheme.
197 // TODO: Introduce an adapter type between GroupVersion and runtime.GroupVersioner, and use LegacyCodec(GroupVersion)
199 func (gv GroupVersion) KindForGroupVersionKinds(kinds []GroupVersionKind) (target GroupVersionKind, ok bool) {
200 for _, gvk := range kinds {
201 if gvk.Group == gv.Group && gvk.Version == gv.Version {
205 for _, gvk := range kinds {
206 if gvk.Group == gv.Group {
207 return gv.WithKind(gvk.Kind), true
210 return GroupVersionKind{}, false
213 // ParseGroupVersion turns "group/version" string into a GroupVersion struct. It reports error
214 // if it cannot parse the string.
215 func ParseGroupVersion(gv string) (GroupVersion, error) {
216 // this can be the internal version for the legacy kube types
217 // TODO once we've cleared the last uses as strings, this special case should be removed.
218 if (len(gv) == 0) || (gv == "/") {
219 return GroupVersion{}, nil
222 switch strings.Count(gv, "/") {
224 return GroupVersion{"", gv}, nil
226 i := strings.Index(gv, "/")
227 return GroupVersion{gv[:i], gv[i+1:]}, nil
229 return GroupVersion{}, fmt.Errorf("unexpected GroupVersion string: %v", gv)
233 // WithKind creates a GroupVersionKind based on the method receiver's GroupVersion and the passed Kind.
234 func (gv GroupVersion) WithKind(kind string) GroupVersionKind {
235 return GroupVersionKind{Group: gv.Group, Version: gv.Version, Kind: kind}
238 // WithResource creates a GroupVersionResource based on the method receiver's GroupVersion and the passed Resource.
239 func (gv GroupVersion) WithResource(resource string) GroupVersionResource {
240 return GroupVersionResource{Group: gv.Group, Version: gv.Version, Resource: resource}
243 // GroupVersions can be used to represent a set of desired group versions.
244 // TODO: Move GroupVersions to a package under pkg/runtime, since it's used by scheme.
245 // TODO: Introduce an adapter type between GroupVersions and runtime.GroupVersioner, and use LegacyCodec(GroupVersion)
247 type GroupVersions []GroupVersion
249 // KindForGroupVersionKinds identifies the preferred GroupVersionKind out of a list. It returns ok false
250 // if none of the options match the group.
251 func (gvs GroupVersions) KindForGroupVersionKinds(kinds []GroupVersionKind) (GroupVersionKind, bool) {
252 var targets []GroupVersionKind
253 for _, gv := range gvs {
254 target, ok := gv.KindForGroupVersionKinds(kinds)
258 targets = append(targets, target)
260 if len(targets) == 1 {
261 return targets[0], true
263 if len(targets) > 1 {
264 return bestMatch(kinds, targets), true
266 return GroupVersionKind{}, false
269 // bestMatch tries to pick best matching GroupVersionKind and falls back to the first
270 // found if no exact match exists.
271 func bestMatch(kinds []GroupVersionKind, targets []GroupVersionKind) GroupVersionKind {
272 for _, gvk := range targets {
273 for _, k := range kinds {
282 // ToAPIVersionAndKind is a convenience method for satisfying runtime.Object on types that
283 // do not use TypeMeta.
284 func (gvk GroupVersionKind) ToAPIVersionAndKind() (string, string) {
288 return gvk.GroupVersion().String(), gvk.Kind
291 // FromAPIVersionAndKind returns a GVK representing the provided fields for types that
292 // do not use TypeMeta. This method exists to support test types and legacy serializations
293 // that have a distinct group and kind.
294 // TODO: further reduce usage of this method.
295 func FromAPIVersionAndKind(apiVersion, kind string) GroupVersionKind {
296 if gv, err := ParseGroupVersion(apiVersion); err == nil {
297 return GroupVersionKind{Group: gv.Group, Version: gv.Version, Kind: kind}
299 return GroupVersionKind{Kind: kind}