conversion.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 v1
  14. import (
  15. "fmt"
  16. "strconv"
  17. "strings"
  18. "k8s.io/apimachinery/pkg/api/resource"
  19. "k8s.io/apimachinery/pkg/conversion"
  20. "k8s.io/apimachinery/pkg/fields"
  21. "k8s.io/apimachinery/pkg/labels"
  22. "k8s.io/apimachinery/pkg/runtime"
  23. "k8s.io/apimachinery/pkg/util/intstr"
  24. )
  25. func AddConversionFuncs(scheme *runtime.Scheme) error {
  26. return scheme.AddConversionFuncs(
  27. Convert_v1_TypeMeta_To_v1_TypeMeta,
  28. Convert_v1_ListMeta_To_v1_ListMeta,
  29. Convert_intstr_IntOrString_To_intstr_IntOrString,
  30. Convert_Pointer_v1_Duration_To_v1_Duration,
  31. Convert_v1_Duration_To_Pointer_v1_Duration,
  32. Convert_Slice_string_To_v1_Time,
  33. Convert_v1_Time_To_v1_Time,
  34. Convert_v1_MicroTime_To_v1_MicroTime,
  35. Convert_resource_Quantity_To_resource_Quantity,
  36. Convert_string_To_labels_Selector,
  37. Convert_labels_Selector_To_string,
  38. Convert_string_To_fields_Selector,
  39. Convert_fields_Selector_To_string,
  40. Convert_Pointer_bool_To_bool,
  41. Convert_bool_To_Pointer_bool,
  42. Convert_Pointer_string_To_string,
  43. Convert_string_To_Pointer_string,
  44. Convert_Pointer_int64_To_int,
  45. Convert_int_To_Pointer_int64,
  46. Convert_Pointer_int32_To_int32,
  47. Convert_int32_To_Pointer_int32,
  48. Convert_Pointer_int64_To_int64,
  49. Convert_int64_To_Pointer_int64,
  50. Convert_Pointer_float64_To_float64,
  51. Convert_float64_To_Pointer_float64,
  52. Convert_Map_string_To_string_To_v1_LabelSelector,
  53. Convert_v1_LabelSelector_To_Map_string_To_string,
  54. Convert_Slice_string_To_Slice_int32,
  55. Convert_Slice_string_To_v1_DeletionPropagation,
  56. )
  57. }
  58. func Convert_Pointer_float64_To_float64(in **float64, out *float64, s conversion.Scope) error {
  59. if *in == nil {
  60. *out = 0
  61. return nil
  62. }
  63. *out = float64(**in)
  64. return nil
  65. }
  66. func Convert_float64_To_Pointer_float64(in *float64, out **float64, s conversion.Scope) error {
  67. temp := float64(*in)
  68. *out = &temp
  69. return nil
  70. }
  71. func Convert_Pointer_int32_To_int32(in **int32, out *int32, s conversion.Scope) error {
  72. if *in == nil {
  73. *out = 0
  74. return nil
  75. }
  76. *out = int32(**in)
  77. return nil
  78. }
  79. func Convert_int32_To_Pointer_int32(in *int32, out **int32, s conversion.Scope) error {
  80. temp := int32(*in)
  81. *out = &temp
  82. return nil
  83. }
  84. func Convert_Pointer_int64_To_int64(in **int64, out *int64, s conversion.Scope) error {
  85. if *in == nil {
  86. *out = 0
  87. return nil
  88. }
  89. *out = int64(**in)
  90. return nil
  91. }
  92. func Convert_int64_To_Pointer_int64(in *int64, out **int64, s conversion.Scope) error {
  93. temp := int64(*in)
  94. *out = &temp
  95. return nil
  96. }
  97. func Convert_Pointer_int64_To_int(in **int64, out *int, s conversion.Scope) error {
  98. if *in == nil {
  99. *out = 0
  100. return nil
  101. }
  102. *out = int(**in)
  103. return nil
  104. }
  105. func Convert_int_To_Pointer_int64(in *int, out **int64, s conversion.Scope) error {
  106. temp := int64(*in)
  107. *out = &temp
  108. return nil
  109. }
  110. func Convert_Pointer_string_To_string(in **string, out *string, s conversion.Scope) error {
  111. if *in == nil {
  112. *out = ""
  113. return nil
  114. }
  115. *out = **in
  116. return nil
  117. }
  118. func Convert_string_To_Pointer_string(in *string, out **string, s conversion.Scope) error {
  119. if in == nil {
  120. stringVar := ""
  121. *out = &stringVar
  122. return nil
  123. }
  124. *out = in
  125. return nil
  126. }
  127. func Convert_Pointer_bool_To_bool(in **bool, out *bool, s conversion.Scope) error {
  128. if *in == nil {
  129. *out = false
  130. return nil
  131. }
  132. *out = **in
  133. return nil
  134. }
  135. func Convert_bool_To_Pointer_bool(in *bool, out **bool, s conversion.Scope) error {
  136. if in == nil {
  137. boolVar := false
  138. *out = &boolVar
  139. return nil
  140. }
  141. *out = in
  142. return nil
  143. }
  144. // +k8s:conversion-fn=drop
  145. func Convert_v1_TypeMeta_To_v1_TypeMeta(in, out *TypeMeta, s conversion.Scope) error {
  146. // These values are explicitly not copied
  147. //out.APIVersion = in.APIVersion
  148. //out.Kind = in.Kind
  149. return nil
  150. }
  151. // +k8s:conversion-fn=copy-only
  152. func Convert_v1_ListMeta_To_v1_ListMeta(in, out *ListMeta, s conversion.Scope) error {
  153. *out = *in
  154. return nil
  155. }
  156. // +k8s:conversion-fn=copy-only
  157. func Convert_intstr_IntOrString_To_intstr_IntOrString(in, out *intstr.IntOrString, s conversion.Scope) error {
  158. *out = *in
  159. return nil
  160. }
  161. // +k8s:conversion-fn=copy-only
  162. func Convert_v1_Time_To_v1_Time(in *Time, out *Time, s conversion.Scope) error {
  163. // Cannot deep copy these, because time.Time has unexported fields.
  164. *out = *in
  165. return nil
  166. }
  167. // +k8s:conversion-fn=copy-only
  168. func Convert_v1_MicroTime_To_v1_MicroTime(in *MicroTime, out *MicroTime, s conversion.Scope) error {
  169. // Cannot deep copy these, because time.Time has unexported fields.
  170. *out = *in
  171. return nil
  172. }
  173. func Convert_Pointer_v1_Duration_To_v1_Duration(in **Duration, out *Duration, s conversion.Scope) error {
  174. if *in == nil {
  175. *out = Duration{} // zero duration
  176. return nil
  177. }
  178. *out = **in // copy
  179. return nil
  180. }
  181. func Convert_v1_Duration_To_Pointer_v1_Duration(in *Duration, out **Duration, s conversion.Scope) error {
  182. temp := *in //copy
  183. *out = &temp
  184. return nil
  185. }
  186. // Convert_Slice_string_To_v1_Time allows converting a URL query parameter value
  187. func Convert_Slice_string_To_v1_Time(input *[]string, out *Time, s conversion.Scope) error {
  188. str := ""
  189. if len(*input) > 0 {
  190. str = (*input)[0]
  191. }
  192. return out.UnmarshalQueryParameter(str)
  193. }
  194. func Convert_string_To_labels_Selector(in *string, out *labels.Selector, s conversion.Scope) error {
  195. selector, err := labels.Parse(*in)
  196. if err != nil {
  197. return err
  198. }
  199. *out = selector
  200. return nil
  201. }
  202. func Convert_string_To_fields_Selector(in *string, out *fields.Selector, s conversion.Scope) error {
  203. selector, err := fields.ParseSelector(*in)
  204. if err != nil {
  205. return err
  206. }
  207. *out = selector
  208. return nil
  209. }
  210. func Convert_labels_Selector_To_string(in *labels.Selector, out *string, s conversion.Scope) error {
  211. if *in == nil {
  212. return nil
  213. }
  214. *out = (*in).String()
  215. return nil
  216. }
  217. func Convert_fields_Selector_To_string(in *fields.Selector, out *string, s conversion.Scope) error {
  218. if *in == nil {
  219. return nil
  220. }
  221. *out = (*in).String()
  222. return nil
  223. }
  224. // +k8s:conversion-fn=copy-only
  225. func Convert_resource_Quantity_To_resource_Quantity(in *resource.Quantity, out *resource.Quantity, s conversion.Scope) error {
  226. *out = *in
  227. return nil
  228. }
  229. func Convert_Map_string_To_string_To_v1_LabelSelector(in *map[string]string, out *LabelSelector, s conversion.Scope) error {
  230. if in == nil {
  231. return nil
  232. }
  233. for labelKey, labelValue := range *in {
  234. AddLabelToSelector(out, labelKey, labelValue)
  235. }
  236. return nil
  237. }
  238. func Convert_v1_LabelSelector_To_Map_string_To_string(in *LabelSelector, out *map[string]string, s conversion.Scope) error {
  239. var err error
  240. *out, err = LabelSelectorAsMap(in)
  241. return err
  242. }
  243. // Convert_Slice_string_To_Slice_int32 converts multiple query parameters or
  244. // a single query parameter with a comma delimited value to multiple int32.
  245. // This is used for port forwarding which needs the ports as int32.
  246. func Convert_Slice_string_To_Slice_int32(in *[]string, out *[]int32, s conversion.Scope) error {
  247. for _, s := range *in {
  248. for _, v := range strings.Split(s, ",") {
  249. x, err := strconv.ParseUint(v, 10, 16)
  250. if err != nil {
  251. return fmt.Errorf("cannot convert to []int32: %v", err)
  252. }
  253. *out = append(*out, int32(x))
  254. }
  255. }
  256. return nil
  257. }
  258. // Convert_Slice_string_To_v1_DeletionPropagation allows converting a URL query parameter propagationPolicy
  259. func Convert_Slice_string_To_v1_DeletionPropagation(input *[]string, out *DeletionPropagation, s conversion.Scope) error {
  260. if len(*input) > 0 {
  261. *out = DeletionPropagation((*input)[0])
  262. } else {
  263. *out = ""
  264. }
  265. return nil
  266. }