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