sslobodr | d046be8 | 2019-01-16 10:02:22 -0500 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2015 The Kubernetes Authors. |
| 3 | |
| 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 |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 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. |
| 15 | */ |
| 16 | |
| 17 | package v1 |
| 18 | |
| 19 | import ( |
| 20 | "encoding/json" |
| 21 | "fmt" |
| 22 | "strings" |
| 23 | |
| 24 | "k8s.io/apimachinery/pkg/runtime/schema" |
| 25 | ) |
| 26 | |
| 27 | // GroupResource specifies a Group and a Resource, but does not force a version. This is useful for identifying |
| 28 | // concepts during lookup stages without having partially valid types |
| 29 | // |
| 30 | // +protobuf.options.(gogoproto.goproto_stringer)=false |
| 31 | type GroupResource struct { |
| 32 | Group string `json:"group" protobuf:"bytes,1,opt,name=group"` |
| 33 | Resource string `json:"resource" protobuf:"bytes,2,opt,name=resource"` |
| 34 | } |
| 35 | |
| 36 | func (gr *GroupResource) String() string { |
| 37 | if len(gr.Group) == 0 { |
| 38 | return gr.Resource |
| 39 | } |
| 40 | return gr.Resource + "." + gr.Group |
| 41 | } |
| 42 | |
| 43 | // GroupVersionResource unambiguously identifies a resource. It doesn't anonymously include GroupVersion |
| 44 | // to avoid automatic coersion. It doesn't use a GroupVersion to avoid custom marshalling |
| 45 | // |
| 46 | // +protobuf.options.(gogoproto.goproto_stringer)=false |
| 47 | type GroupVersionResource struct { |
| 48 | Group string `json:"group" protobuf:"bytes,1,opt,name=group"` |
| 49 | Version string `json:"version" protobuf:"bytes,2,opt,name=version"` |
| 50 | Resource string `json:"resource" protobuf:"bytes,3,opt,name=resource"` |
| 51 | } |
| 52 | |
| 53 | func (gvr *GroupVersionResource) String() string { |
| 54 | return strings.Join([]string{gvr.Group, "/", gvr.Version, ", Resource=", gvr.Resource}, "") |
| 55 | } |
| 56 | |
| 57 | // GroupKind specifies a Group and a Kind, but does not force a version. This is useful for identifying |
| 58 | // concepts during lookup stages without having partially valid types |
| 59 | // |
| 60 | // +protobuf.options.(gogoproto.goproto_stringer)=false |
| 61 | type GroupKind struct { |
| 62 | Group string `json:"group" protobuf:"bytes,1,opt,name=group"` |
| 63 | Kind string `json:"kind" protobuf:"bytes,2,opt,name=kind"` |
| 64 | } |
| 65 | |
| 66 | func (gk *GroupKind) String() string { |
| 67 | if len(gk.Group) == 0 { |
| 68 | return gk.Kind |
| 69 | } |
| 70 | return gk.Kind + "." + gk.Group |
| 71 | } |
| 72 | |
| 73 | // GroupVersionKind unambiguously identifies a kind. It doesn't anonymously include GroupVersion |
| 74 | // to avoid automatic coersion. It doesn't use a GroupVersion to avoid custom marshalling |
| 75 | // |
| 76 | // +protobuf.options.(gogoproto.goproto_stringer)=false |
| 77 | type GroupVersionKind struct { |
| 78 | Group string `json:"group" protobuf:"bytes,1,opt,name=group"` |
| 79 | Version string `json:"version" protobuf:"bytes,2,opt,name=version"` |
| 80 | Kind string `json:"kind" protobuf:"bytes,3,opt,name=kind"` |
| 81 | } |
| 82 | |
| 83 | func (gvk GroupVersionKind) String() string { |
| 84 | return gvk.Group + "/" + gvk.Version + ", Kind=" + gvk.Kind |
| 85 | } |
| 86 | |
| 87 | // GroupVersion contains the "group" and the "version", which uniquely identifies the API. |
| 88 | // |
| 89 | // +protobuf.options.(gogoproto.goproto_stringer)=false |
| 90 | type GroupVersion struct { |
| 91 | Group string `json:"group" protobuf:"bytes,1,opt,name=group"` |
| 92 | Version string `json:"version" protobuf:"bytes,2,opt,name=version"` |
| 93 | } |
| 94 | |
| 95 | // Empty returns true if group and version are empty |
| 96 | func (gv GroupVersion) Empty() bool { |
| 97 | return len(gv.Group) == 0 && len(gv.Version) == 0 |
| 98 | } |
| 99 | |
| 100 | // String puts "group" and "version" into a single "group/version" string. For the legacy v1 |
| 101 | // it returns "v1". |
| 102 | func (gv GroupVersion) String() string { |
| 103 | // special case the internal apiVersion for the legacy kube types |
| 104 | if gv.Empty() { |
| 105 | return "" |
| 106 | } |
| 107 | |
| 108 | // special case of "v1" for backward compatibility |
| 109 | if len(gv.Group) == 0 && gv.Version == "v1" { |
| 110 | return gv.Version |
| 111 | } |
| 112 | if len(gv.Group) > 0 { |
| 113 | return gv.Group + "/" + gv.Version |
| 114 | } |
| 115 | return gv.Version |
| 116 | } |
| 117 | |
| 118 | // MarshalJSON implements the json.Marshaller interface. |
| 119 | func (gv GroupVersion) MarshalJSON() ([]byte, error) { |
| 120 | s := gv.String() |
| 121 | if strings.Count(s, "/") > 1 { |
| 122 | return []byte{}, fmt.Errorf("illegal GroupVersion %v: contains more than one /", s) |
| 123 | } |
| 124 | return json.Marshal(s) |
| 125 | } |
| 126 | |
| 127 | func (gv *GroupVersion) unmarshal(value []byte) error { |
| 128 | var s string |
| 129 | if err := json.Unmarshal(value, &s); err != nil { |
| 130 | return err |
| 131 | } |
| 132 | parsed, err := schema.ParseGroupVersion(s) |
| 133 | if err != nil { |
| 134 | return err |
| 135 | } |
| 136 | gv.Group, gv.Version = parsed.Group, parsed.Version |
| 137 | return nil |
| 138 | } |
| 139 | |
| 140 | // UnmarshalJSON implements the json.Unmarshaller interface. |
| 141 | func (gv *GroupVersion) UnmarshalJSON(value []byte) error { |
| 142 | return gv.unmarshal(value) |
| 143 | } |
| 144 | |
| 145 | // UnmarshalTEXT implements the Ugorji's encoding.TextUnmarshaler interface. |
| 146 | func (gv *GroupVersion) UnmarshalText(value []byte) error { |
| 147 | return gv.unmarshal(value) |
| 148 | } |