group_version.go 4.6 KB

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