conversion.go 8.8 KB

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