conversion.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 api
  14. import (
  15. "fmt"
  16. "k8s.io/kubernetes/pkg/api/resource"
  17. "k8s.io/kubernetes/pkg/api/unversioned"
  18. "k8s.io/kubernetes/pkg/conversion"
  19. "k8s.io/kubernetes/pkg/fields"
  20. "k8s.io/kubernetes/pkg/labels"
  21. "k8s.io/kubernetes/pkg/runtime"
  22. "k8s.io/kubernetes/pkg/util/intstr"
  23. utillabels "k8s.io/kubernetes/pkg/util/labels"
  24. "k8s.io/kubernetes/pkg/util/validation/field"
  25. )
  26. func addConversionFuncs(scheme *runtime.Scheme) error {
  27. return scheme.AddConversionFuncs(
  28. Convert_unversioned_TypeMeta_To_unversioned_TypeMeta,
  29. Convert_unversioned_ListMeta_To_unversioned_ListMeta,
  30. Convert_intstr_IntOrString_To_intstr_IntOrString,
  31. Convert_unversioned_Time_To_unversioned_Time,
  32. Convert_Slice_string_To_unversioned_Time,
  33. Convert_resource_Quantity_To_resource_Quantity,
  34. Convert_string_To_labels_Selector,
  35. Convert_labels_Selector_To_string,
  36. Convert_string_To_fields_Selector,
  37. Convert_fields_Selector_To_string,
  38. Convert_Pointer_bool_To_bool,
  39. Convert_bool_To_Pointer_bool,
  40. Convert_Pointer_string_To_string,
  41. Convert_string_To_Pointer_string,
  42. Convert_Pointer_int64_To_int,
  43. Convert_int_To_Pointer_int64,
  44. Convert_Pointer_int32_To_int32,
  45. Convert_int32_To_Pointer_int32,
  46. Convert_Pointer_float64_To_float64,
  47. Convert_float64_To_Pointer_float64,
  48. Convert_map_to_unversioned_LabelSelector,
  49. Convert_unversioned_LabelSelector_to_map,
  50. )
  51. }
  52. func Convert_Pointer_float64_To_float64(in **float64, out *float64, s conversion.Scope) error {
  53. if *in == nil {
  54. *out = 0
  55. return nil
  56. }
  57. *out = float64(**in)
  58. return nil
  59. }
  60. func Convert_float64_To_Pointer_float64(in *float64, out **float64, s conversion.Scope) error {
  61. temp := float64(*in)
  62. *out = &temp
  63. return nil
  64. }
  65. func Convert_Pointer_int32_To_int32(in **int32, out *int32, s conversion.Scope) error {
  66. if *in == nil {
  67. *out = 0
  68. return nil
  69. }
  70. *out = int32(**in)
  71. return nil
  72. }
  73. func Convert_int32_To_Pointer_int32(in *int32, out **int32, s conversion.Scope) error {
  74. temp := int32(*in)
  75. *out = &temp
  76. return nil
  77. }
  78. func Convert_Pointer_int64_To_int(in **int64, out *int, s conversion.Scope) error {
  79. if *in == nil {
  80. *out = 0
  81. return nil
  82. }
  83. *out = int(**in)
  84. return nil
  85. }
  86. func Convert_int_To_Pointer_int64(in *int, out **int64, s conversion.Scope) error {
  87. temp := int64(*in)
  88. *out = &temp
  89. return nil
  90. }
  91. func Convert_Pointer_string_To_string(in **string, out *string, s conversion.Scope) error {
  92. if *in == nil {
  93. *out = ""
  94. return nil
  95. }
  96. *out = **in
  97. return nil
  98. }
  99. func Convert_string_To_Pointer_string(in *string, out **string, s conversion.Scope) error {
  100. if in == nil {
  101. stringVar := ""
  102. *out = &stringVar
  103. return nil
  104. }
  105. *out = in
  106. return nil
  107. }
  108. func Convert_Pointer_bool_To_bool(in **bool, out *bool, s conversion.Scope) error {
  109. if *in == nil {
  110. *out = false
  111. return nil
  112. }
  113. *out = **in
  114. return nil
  115. }
  116. func Convert_bool_To_Pointer_bool(in *bool, out **bool, s conversion.Scope) error {
  117. if in == nil {
  118. boolVar := false
  119. *out = &boolVar
  120. return nil
  121. }
  122. *out = in
  123. return nil
  124. }
  125. func Convert_unversioned_TypeMeta_To_unversioned_TypeMeta(in, out *unversioned.TypeMeta, s conversion.Scope) error {
  126. // These values are explicitly not copied
  127. //out.APIVersion = in.APIVersion
  128. //out.Kind = in.Kind
  129. return nil
  130. }
  131. func Convert_unversioned_ListMeta_To_unversioned_ListMeta(in, out *unversioned.ListMeta, s conversion.Scope) error {
  132. *out = *in
  133. return nil
  134. }
  135. func Convert_intstr_IntOrString_To_intstr_IntOrString(in, out *intstr.IntOrString, s conversion.Scope) error {
  136. *out = *in
  137. return nil
  138. }
  139. func Convert_unversioned_Time_To_unversioned_Time(in *unversioned.Time, out *unversioned.Time, s conversion.Scope) error {
  140. // Cannot deep copy these, because time.Time has unexported fields.
  141. *out = *in
  142. return nil
  143. }
  144. // Convert_Slice_string_To_unversioned_Time allows converting a URL query parameter value
  145. func Convert_Slice_string_To_unversioned_Time(input *[]string, out *unversioned.Time, s conversion.Scope) error {
  146. str := ""
  147. if len(*input) > 0 {
  148. str = (*input)[0]
  149. }
  150. return out.UnmarshalQueryParameter(str)
  151. }
  152. func Convert_string_To_labels_Selector(in *string, out *labels.Selector, s conversion.Scope) error {
  153. selector, err := labels.Parse(*in)
  154. if err != nil {
  155. return err
  156. }
  157. *out = selector
  158. return nil
  159. }
  160. func Convert_string_To_fields_Selector(in *string, out *fields.Selector, s conversion.Scope) error {
  161. selector, err := fields.ParseSelector(*in)
  162. if err != nil {
  163. return err
  164. }
  165. *out = selector
  166. return nil
  167. }
  168. func Convert_labels_Selector_To_string(in *labels.Selector, out *string, s conversion.Scope) error {
  169. if *in == nil {
  170. return nil
  171. }
  172. *out = (*in).String()
  173. return nil
  174. }
  175. func Convert_fields_Selector_To_string(in *fields.Selector, out *string, s conversion.Scope) error {
  176. if *in == nil {
  177. return nil
  178. }
  179. *out = (*in).String()
  180. return nil
  181. }
  182. func Convert_resource_Quantity_To_resource_Quantity(in *resource.Quantity, out *resource.Quantity, s conversion.Scope) error {
  183. *out = *in
  184. return nil
  185. }
  186. func Convert_map_to_unversioned_LabelSelector(in *map[string]string, out *unversioned.LabelSelector, s conversion.Scope) error {
  187. if in == nil {
  188. return nil
  189. }
  190. out = new(unversioned.LabelSelector)
  191. for labelKey, labelValue := range *in {
  192. utillabels.AddLabelToSelector(out, labelKey, labelValue)
  193. }
  194. return nil
  195. }
  196. func Convert_unversioned_LabelSelector_to_map(in *unversioned.LabelSelector, out *map[string]string, s conversion.Scope) error {
  197. var err error
  198. *out, err = unversioned.LabelSelectorAsMap(in)
  199. if err != nil {
  200. err = field.Invalid(field.NewPath("labelSelector"), *in, fmt.Sprintf("cannot convert to old selector: %v", err))
  201. }
  202. return err
  203. }