1
2
3
4
5
6
7
8
9 package v2
10
11 import (
12 "context"
13 "strings"
14
15 "k8s.io/apimachinery/pkg/conversion"
16 k8sRuntime "k8s.io/apimachinery/pkg/runtime"
17 k8sRuntimeUtil "k8s.io/apimachinery/pkg/util/runtime"
18
19 "github.com/datawire/dlib/dlog"
20 "github.com/emissary-ingress/emissary/v3/pkg/api/getambassador.io/v3alpha1"
21 )
22
23
24
25 var (
26 conversionScheme = func() *k8sRuntime.Scheme {
27 scheme := k8sRuntime.NewScheme()
28 k8sRuntimeUtil.Must(AddToScheme(scheme))
29 k8sRuntimeUtil.Must(v3alpha1.AddToScheme(scheme))
30 return scheme
31 }
32 conversionIntermediates = []k8sRuntime.GroupVersioner{
33
34
35 }
36 )
37
38 func convert_v2_TLS_To_v3alpha1_TLS(
39 inTLS *BoolOrString,
40 inSvc string,
41
42 outTLS *string,
43 outSvc *string,
44 outExplicit **v3alpha1.V2ExplicitTLS,
45 ) {
46
47
48 originateTLS := false
49 var bareSvc, explicitScheme string
50 switch {
51 case strings.HasPrefix(strings.ToLower(inSvc), "https://"):
52 bareSvc = inSvc[len("https://"):]
53 explicitScheme = inSvc[:len("https://")]
54 originateTLS = true
55 case strings.HasPrefix(strings.ToLower(inSvc), "http://"):
56 bareSvc = inSvc[len("http://"):]
57 explicitScheme = inSvc[:len("http://")]
58 default:
59 bareSvc = inSvc
60 }
61 var ctxName string
62 switch {
63 case inTLS != nil && inTLS.String != nil && *inTLS.String != "":
64 originateTLS = true
65 ctxName = *inTLS.String
66 case inTLS != nil && inTLS.Bool != nil && *inTLS.Bool:
67 originateTLS = *inTLS.Bool
68 }
69
70 if ctxName != "" {
71 *outTLS = *inTLS.String
72 *outSvc = inSvc
73 *outExplicit = &v3alpha1.V2ExplicitTLS{}
74 } else {
75
76 *outTLS = ""
77 var outExplicitTLS string
78 switch {
79 case inTLS == nil:
80 outExplicitTLS = ""
81 case inTLS.Bool != nil && *inTLS.Bool:
82 outExplicitTLS = "bool:true"
83 case inTLS.Bool != nil && !*inTLS.Bool:
84 outExplicitTLS = "bool:false"
85 case inTLS.String != nil && *inTLS.String == "":
86 outExplicitTLS = "string"
87 case *inTLS == BoolOrString{}:
88 outExplicitTLS = "null"
89 }
90
91
92
93
94
95
96
97
98
99 var outExplicitScheme *string
100 schemeIsHTTPS := strings.ToLower(explicitScheme) == "https://"
101 if schemeIsHTTPS == originateTLS {
102
103 *outSvc = inSvc
104 } else {
105
106 outExplicitScheme = &explicitScheme
107 *outSvc = "https://" + bareSvc
108 }
109
110
111 if outExplicitTLS == "" && outExplicitScheme == nil {
112 *outExplicit = nil
113 } else {
114 *outExplicit = &v3alpha1.V2ExplicitTLS{
115 TLS: outExplicitTLS,
116 ServiceScheme: outExplicitScheme,
117 }
118 }
119 }
120 }
121
122 func convert_v3alpha1_TLS_To_v2_TLS(
123 inTLS string,
124 inSvc string,
125 inExplicit *v3alpha1.V2ExplicitTLS,
126
127 outTLS **BoolOrString,
128 outSvc *string,
129 ) {
130 if inExplicit == nil {
131 inExplicit = &v3alpha1.V2ExplicitTLS{}
132 }
133
134
135 originateTLS := false
136 var bareSvc string
137 switch {
138 case strings.HasPrefix(strings.ToLower(inSvc), "https://"):
139 bareSvc = inSvc[len("https://"):]
140 originateTLS = true
141 case strings.HasPrefix(strings.ToLower(inSvc), "http://"):
142 bareSvc = inSvc[len("http://"):]
143 default:
144 bareSvc = inSvc
145 }
146 if inTLS != "" {
147 originateTLS = true
148 }
149
150 tlsIsTruthy := false
151 if inTLS != "" {
152 *outTLS = &BoolOrString{
153 String: &inTLS,
154 }
155 tlsIsTruthy = true
156 } else {
157 switch inExplicit.TLS {
158 case "null":
159 *outTLS = &BoolOrString{}
160 case "bool:false":
161 val := false
162 *outTLS = &BoolOrString{
163 Bool: &val,
164 }
165 case "bool:true":
166 if originateTLS {
167 val := true
168 *outTLS = &BoolOrString{
169 Bool: &val,
170 }
171 tlsIsTruthy = true
172 }
173 case "string":
174 val := ""
175 *outTLS = &BoolOrString{
176 String: &val,
177 }
178 }
179 }
180 if tlsIsTruthy {
181
182
183 if inExplicit.ServiceScheme != nil {
184 *outSvc = *inExplicit.ServiceScheme + bareSvc
185 } else {
186 *outSvc = inSvc
187 }
188 } else {
189
190
191 if (inExplicit.ServiceScheme != nil) && ((strings.ToLower(*inExplicit.ServiceScheme) == "https://") == originateTLS) {
192 *outSvc = *inExplicit.ServiceScheme + bareSvc
193 } else {
194 *outSvc = inSvc
195 }
196 }
197 }
198
199 const ambassadorIDMangle = "--apiVersion-v3alpha1-only--"
200
201 func Convert_v2_AmbassadorID_To_v3alpha1_AmbassadorID(in *AmbassadorID, out *v3alpha1.AmbassadorID, s conversion.Scope) error {
202 if *in == nil {
203 *out = nil
204 return nil
205 }
206 list := make(v3alpha1.AmbassadorID, 0, len(*in))
207 for _, item := range *in {
208 list = append(list, strings.TrimPrefix(item, ambassadorIDMangle))
209 }
210 *out = list
211 return nil
212 }
213
214
215
216 var MangleAmbassadorID = true
217
218 func Convert_v3alpha1_AmbassadorID_To_v2_AmbassadorID(in *v3alpha1.AmbassadorID, out *AmbassadorID, s conversion.Scope) error {
219 if *in == nil {
220 if MangleAmbassadorID {
221 in = &v3alpha1.AmbassadorID{"default"}
222 } else {
223 *out = nil
224 return nil
225 }
226 }
227 list := make(AmbassadorID, 0, len(*in))
228 for _, item := range *in {
229 item = strings.TrimPrefix(item, ambassadorIDMangle)
230 if MangleAmbassadorID {
231 item = ambassadorIDMangle + item
232 }
233 list = append(list, item)
234 }
235 *out = list
236 return nil
237 }
238
239 func Convert_string_To_v2_BoolOrString(in *string, out *BoolOrString, s conversion.Scope) error {
240 *out = BoolOrString{
241 String: in,
242 }
243 return nil
244 }
245
246 func Convert_string_To_Pointer_v2_BoolOrString(in *string, out **BoolOrString, s conversion.Scope) error {
247 if *in != "" {
248 *out = &BoolOrString{
249 String: in,
250 }
251 } else {
252 *out = nil
253 }
254 return nil
255 }
256
257
258
259
260
261
262
263
264 func Convert_v2_AddedHeader_To_v3alpha1_AddedHeader(in *AddedHeader, out *v3alpha1.AddedHeader, s conversion.Scope) error {
265 if err := autoConvert_v2_AddedHeader_To_v3alpha1_AddedHeader(in, out, s); err != nil {
266 return err
267 }
268 switch {
269
270 case in.Shorthand != nil:
271 *out = v3alpha1.AddedHeader{
272 Value: *in.Shorthand,
273 V2Representation: "string",
274 }
275
276 case in.Full != nil:
277 *out = v3alpha1.AddedHeader{
278 Value: in.Full.Value,
279 Append: in.Full.Append,
280 }
281 default:
282 *out = v3alpha1.AddedHeader{
283 V2Representation: "null",
284 }
285 }
286 return nil
287 }
288
289 func Convert_v3alpha1_AddedHeader_To_v2_AddedHeader(in *v3alpha1.AddedHeader, out *AddedHeader, s conversion.Scope) error {
290 if err := autoConvert_v3alpha1_AddedHeader_To_v2_AddedHeader(in, out, s); err != nil {
291 return err
292 }
293
294
295
296 switch {
297 case in.V2Representation == "string" && in.Append == nil:
298 *out = AddedHeader{
299 Shorthand: &in.Value,
300 }
301 case in.V2Representation == "null" && in.Append == nil && in.Value == "":
302 *out = AddedHeader{}
303 default:
304 *out = AddedHeader{
305 Full: &AddedHeaderFull{
306 Value: in.Value,
307 Append: in.Append,
308 },
309 }
310 }
311 return nil
312 }
313
314 func Convert_v2_AuthServiceSpec_To_v3alpha1_AuthServiceSpec(in *AuthServiceSpec, out *v3alpha1.AuthServiceSpec, s conversion.Scope) error {
315 if err := autoConvert_v2_AuthServiceSpec_To_v3alpha1_AuthServiceSpec(in, out, s); err != nil {
316 return err
317 }
318
319 convert_v2_TLS_To_v3alpha1_TLS(
320 in.TLS, in.AuthService,
321 &out.TLS, &out.AuthService, &out.V2ExplicitTLS)
322 return nil
323 }
324
325 func Convert_v3alpha1_AuthServiceSpec_To_v2_AuthServiceSpec(in *v3alpha1.AuthServiceSpec, out *AuthServiceSpec, s conversion.Scope) error {
326 if err := autoConvert_v3alpha1_AuthServiceSpec_To_v2_AuthServiceSpec(in, out, s); err != nil {
327 return err
328 }
329
330 convert_v3alpha1_TLS_To_v2_TLS(
331 in.TLS, in.AuthService, in.V2ExplicitTLS,
332 &out.TLS, &out.AuthService)
333 return nil
334 }
335
336 func Convert_v2_CORS_To_v3alpha1_CORS(in *CORS, out *v3alpha1.CORS, s conversion.Scope) error {
337 if err := autoConvert_v2_CORS_To_v3alpha1_CORS(in, out, s); err != nil {
338 return err
339 }
340
341
342 if in.Origins != nil {
343 out.Origins = in.Origins.Values
344 out.V2CommaSeparatedOrigins = in.Origins.CommaSeparated
345 }
346
347 return nil
348 }
349
350 func Convert_v3alpha1_CORS_To_v2_CORS(in *v3alpha1.CORS, out *CORS, s conversion.Scope) error {
351 if err := autoConvert_v3alpha1_CORS_To_v2_CORS(in, out, s); err != nil {
352 return err
353 }
354
355
356 if in.Origins != nil {
357 out.Origins = &OriginList{
358 Values: in.Origins,
359 CommaSeparated: in.V2CommaSeparatedOrigins,
360 }
361 }
362
363 return nil
364 }
365
366 func Convert_v2_HostSpec_To_v3alpha1_HostSpec(in *HostSpec, out *v3alpha1.HostSpec, s conversion.Scope) error {
367 if len(in.DeprecatedAmbassadorID) > 0 {
368 in = in.DeepCopy()
369 in.AmbassadorID = append(in.AmbassadorID, in.DeprecatedAmbassadorID...)
370 in.DeprecatedAmbassadorID = nil
371 }
372
373 if err := autoConvert_v2_HostSpec_To_v3alpha1_HostSpec(in, out, s); err != nil {
374 return err
375 }
376
377
378
379
380 return nil
381 }
382
383 func Convert_v3alpha1_HostSpec_To_v2_HostSpec(in *v3alpha1.HostSpec, out *HostSpec, s conversion.Scope) error {
384 if err := autoConvert_v3alpha1_HostSpec_To_v2_HostSpec(in, out, s); err != nil {
385 return err
386 }
387
388 if in.DeprecatedSelector != nil && out.Selector == nil {
389 out.Selector = in.DeprecatedSelector
390 }
391 return nil
392 }
393
394 func Convert_v2_MappingLabelSpecifier_To_v3alpha1_MappingLabelSpecifier(in *MappingLabelSpecifier, out *v3alpha1.MappingLabelSpecifier, s conversion.Scope) error {
395 if err := autoConvert_v2_MappingLabelSpecifier_To_v3alpha1_MappingLabelSpecifier(in, out, s); err != nil {
396 return err
397 }
398 switch {
399
400 case in.String != nil:
401 switch *in.String {
402 case "source_cluster":
403 out.SourceCluster = &v3alpha1.MappingLabelSpecifier_SourceCluster{
404 Key: *in.String,
405 }
406 case "destination_cluster":
407 out.DestinationCluster = &v3alpha1.MappingLabelSpecifier_DestinationCluster{
408 Key: *in.String,
409 }
410 case "remote_address":
411 out.RemoteAddress = &v3alpha1.MappingLabelSpecifier_RemoteAddress{
412 Key: *in.String,
413 }
414 default:
415 out.GenericKey = &v3alpha1.MappingLabelSpecifier_GenericKey{
416 Value: *in.String,
417 V2Shorthand: true,
418 }
419 }
420
421 case in.Header != nil:
422 for k, v := range in.Header {
423 out.RequestHeaders = &v3alpha1.MappingLabelSpecifier_RequestHeaders{
424 Key: k,
425 HeaderName: v.Header,
426 OmitIfNotPresent: v.OmitIfNotPresent,
427 }
428 }
429
430 case in.Generic != nil:
431 out.GenericKey = &v3alpha1.MappingLabelSpecifier_GenericKey{
432 Value: in.Generic.GenericKey,
433 }
434 }
435 return nil
436 }
437
438 func Convert_v3alpha1_MappingLabelSpecifier_To_v2_MappingLabelSpecifier(in *v3alpha1.MappingLabelSpecifier, out *MappingLabelSpecifier, s conversion.Scope) error {
439 if err := autoConvert_v3alpha1_MappingLabelSpecifier_To_v2_MappingLabelSpecifier(in, out, s); err != nil {
440 return err
441 }
442 switch {
443
444 case in.SourceCluster != nil:
445 out.String = &in.SourceCluster.Key
446
447 case in.DestinationCluster != nil:
448 out.String = &in.DestinationCluster.Key
449
450 case in.RequestHeaders != nil:
451 out.Header = MappingLabelSpecHeader{
452 in.RequestHeaders.Key: MappingLabelSpecHeaderStruct{
453 Header: in.RequestHeaders.HeaderName,
454 OmitIfNotPresent: in.RequestHeaders.OmitIfNotPresent,
455 },
456 }
457
458 case in.RemoteAddress != nil:
459 out.String = &in.RemoteAddress.Key
460
461 case in.GenericKey != nil:
462 if in.GenericKey.V2Shorthand && in.GenericKey.Key == "" {
463 out.String = &in.GenericKey.Value
464 } else {
465 out.Generic = &MappingLabelSpecGeneric{
466 V3Key: in.GenericKey.Key,
467 GenericKey: in.GenericKey.Value,
468 }
469 }
470 }
471 return nil
472 }
473
474 func Convert_v2_MappingSpec_To_v3alpha1_MappingSpec(in *MappingSpec, out *v3alpha1.MappingSpec, s conversion.Scope) error {
475 if err := autoConvert_v2_MappingSpec_To_v3alpha1_MappingSpec(in, out, s); err != nil {
476 return err
477 }
478
479
480 convert_v2_TLS_To_v3alpha1_TLS(
481 in.TLS, in.Service,
482 &out.TLS, &out.Service, &out.V2ExplicitTLS)
483
484
485 for k, v := range in.Headers {
486 switch {
487 case v.String != nil:
488 if out.Headers == nil {
489 out.Headers = make(map[string]string)
490 }
491 out.Headers[k] = *v.String
492 case v.Bool != nil && *v.Bool:
493 if out.RegexHeaders == nil {
494 out.RegexHeaders = make(map[string]string)
495 }
496 if _, conflict := out.RegexHeaders[k]; !conflict {
497 out.RegexHeaders[k] = ".*"
498 }
499 out.V2BoolHeaders = append(out.V2BoolHeaders, k)
500 }
501 }
502
503
504 for k, v := range in.QueryParameters {
505 switch {
506 case v.String != nil:
507 if out.QueryParameters == nil {
508 out.QueryParameters = make(map[string]string)
509 }
510 out.QueryParameters[k] = *v.String
511 case v.Bool != nil && *v.Bool:
512 if out.RegexQueryParameters == nil {
513 out.RegexQueryParameters = make(map[string]string)
514 }
515 if _, conflict := out.RegexQueryParameters[k]; !conflict {
516 out.RegexQueryParameters[k] = ".*"
517 }
518 out.V2BoolQueryParameters = append(out.V2BoolQueryParameters, k)
519 }
520 }
521
522 if out.DeprecatedHostRegex != nil && *out.DeprecatedHostRegex {
523 out.DeprecatedHost = out.Hostname
524 out.Hostname = ""
525 } else if out.Hostname == "" {
526 out.Hostname = "*"
527 } else if out.Hostname == "_skip_mapping_with_empty_host_" {
528 out.Hostname = ""
529 }
530
531 return nil
532 }
533
534 func Convert_v3alpha1_MappingSpec_To_v2_MappingSpec(in *v3alpha1.MappingSpec, out *MappingSpec, s conversion.Scope) error {
535 in = in.DeepCopy()
536 if in.Hostname != "" {
537 in.DeprecatedHost = ""
538 if in.DeprecatedHostRegex != nil {
539 v := false
540 in.DeprecatedHostRegex = &v
541 }
542 } else if in.DeprecatedHost == "" && (in.DeprecatedHostRegex == nil || !*in.DeprecatedHostRegex) {
543 in.Hostname = "_skip_mapping_with_empty_host_"
544 }
545
546 if err := autoConvert_v3alpha1_MappingSpec_To_v2_MappingSpec(in, out, s); err != nil {
547 return err
548 }
549
550
551 if in.DeprecatedHost != "" {
552 out.Host = in.DeprecatedHost
553 }
554 if (out.HostRegex == nil || !*out.HostRegex) && out.Host == "*" {
555 out.Host = ""
556 }
557
558
559 convert_v3alpha1_TLS_To_v2_TLS(
560 in.TLS, in.Service, in.V2ExplicitTLS,
561 &out.TLS, &out.Service)
562
563
564 if out.RegexHeaders != nil {
565 for _, name := range in.V2BoolHeaders {
566 if re, exists := out.RegexHeaders[name]; exists {
567 if out.Headers == nil {
568 out.Headers = make(map[string]BoolOrString)
569 }
570 if _, conflict := out.Headers[name]; !conflict {
571 val := true
572 out.Headers[name] = BoolOrString{Bool: &val}
573 if re == ".*" {
574 delete(out.RegexHeaders, name)
575 }
576 }
577 }
578 }
579 }
580
581
582 if out.RegexQueryParameters != nil {
583 for _, name := range in.V2BoolQueryParameters {
584 if re, exists := out.RegexQueryParameters[name]; exists {
585 if out.QueryParameters == nil {
586 out.QueryParameters = make(map[string]BoolOrString)
587 }
588 if _, conflict := out.QueryParameters[name]; !conflict {
589 val := true
590 out.QueryParameters[name] = BoolOrString{Bool: &val}
591 if re == ".*" {
592 delete(out.RegexQueryParameters, name)
593 }
594 }
595 }
596 }
597 }
598
599 return nil
600 }
601
602 func Convert_v2_RateLimitServiceSpec_To_v3alpha1_RateLimitServiceSpec(in *RateLimitServiceSpec, out *v3alpha1.RateLimitServiceSpec, s conversion.Scope) error {
603 if err := autoConvert_v2_RateLimitServiceSpec_To_v3alpha1_RateLimitServiceSpec(in, out, s); err != nil {
604 return err
605 }
606
607 convert_v2_TLS_To_v3alpha1_TLS(
608 in.TLS, in.Service,
609 &out.TLS, &out.Service, &out.V2ExplicitTLS)
610 return nil
611 }
612
613 func Convert_v3alpha1_RateLimitServiceSpec_To_v2_RateLimitServiceSpec(in *v3alpha1.RateLimitServiceSpec, out *RateLimitServiceSpec, s conversion.Scope) error {
614 if err := autoConvert_v3alpha1_RateLimitServiceSpec_To_v2_RateLimitServiceSpec(in, out, s); err != nil {
615 return err
616 }
617
618
619 convert_v3alpha1_TLS_To_v2_TLS(
620 in.TLS, in.Service, in.V2ExplicitTLS,
621 &out.TLS, &out.Service)
622
623 return nil
624 }
625
626 func Convert_v2_TCPMappingSpec_To_v3alpha1_TCPMappingSpec(in *TCPMappingSpec, out *v3alpha1.TCPMappingSpec, s conversion.Scope) error {
627 if err := autoConvert_v2_TCPMappingSpec_To_v3alpha1_TCPMappingSpec(in, out, s); err != nil {
628 return err
629 }
630
631
632 convert_v2_TLS_To_v3alpha1_TLS(
633 in.TLS, in.Service,
634 &out.TLS, &out.Service, &out.V2ExplicitTLS)
635
636 if out.V2ExplicitTLS != nil {
637 if out.V2ExplicitTLS.TLS != "" {
638 out.V2ExplicitTLS = &v3alpha1.V2ExplicitTLS{
639 TLS: out.V2ExplicitTLS.TLS,
640 }
641 } else {
642 out.V2ExplicitTLS = nil
643 }
644 }
645
646 return nil
647 }
648
649 func Convert_v3alpha1_TCPMappingSpec_To_v2_TCPMappingSpec(in *v3alpha1.TCPMappingSpec, out *TCPMappingSpec, s conversion.Scope) error {
650
651 in = in.DeepCopy()
652 if in.V2ExplicitTLS == nil {
653 in.V2ExplicitTLS = &v3alpha1.V2ExplicitTLS{}
654 }
655 if in.V2ExplicitTLS.ServiceScheme == nil || *in.V2ExplicitTLS.ServiceScheme != "" {
656 val := ""
657 in.V2ExplicitTLS.ServiceScheme = &val
658 }
659
660 if err := autoConvert_v3alpha1_TCPMappingSpec_To_v2_TCPMappingSpec(in, out, s); err != nil {
661 return err
662 }
663
664
665 convert_v3alpha1_TLS_To_v2_TLS(
666 in.TLS, in.Service, in.V2ExplicitTLS,
667 &out.TLS, &out.Service)
668
669 return nil
670 }
671
672 func Convert_v2_TracingServiceSpec_To_v3alpha1_TracingServiceSpec(in *TracingServiceSpec, out *v3alpha1.TracingServiceSpec, s conversion.Scope) error {
673 if err := autoConvert_v2_TracingServiceSpec_To_v3alpha1_TracingServiceSpec(in, out, s); err != nil {
674 return err
675 }
676
677
678
679 if in.TagHeaders != nil && in.V3CustomTags == nil {
680 out.CustomTags = []v3alpha1.TracingCustomTag{}
681 for _, tag := range in.TagHeaders {
682 out.CustomTags = append(out.CustomTags, v3alpha1.TracingCustomTag{
683 Tag: tag,
684 Header: &v3alpha1.TracingCustomTagTypeRequestHeader{
685 Name: tag,
686 },
687 })
688 }
689 } else if in.TagHeaders != nil && in.V3CustomTags != nil {
690 dlog.Warn(context.Background(), "CustomTags and TagHeaders cannot be set at the same time in a TracingService. ignoring TagHeaders since it is deprecated.")
691 }
692 return nil
693 }
694
695 func Convert_v3alpha1_TracingServiceSpec_To_v2_TracingServiceSpec(in *v3alpha1.TracingServiceSpec, out *TracingServiceSpec, s conversion.Scope) error {
696 in = in.DeepCopy()
697
698
699 if in.DeprecatedTagHeaders != nil {
700 if in.CustomTags == nil {
701 in.CustomTags = []v3alpha1.TracingCustomTag{}
702 for _, tag := range in.DeprecatedTagHeaders {
703 in.CustomTags = append(in.CustomTags, v3alpha1.TracingCustomTag{
704 Tag: tag,
705 Header: &v3alpha1.TracingCustomTagTypeRequestHeader{
706 Name: tag,
707 },
708 })
709 }
710 } else {
711 dlog.Warn(context.Background(), "CustomTags and TagHeaders cannot be set at the same time in a TracingService. ignoring TagHeaders since it is deprecated.")
712 }
713 }
714
715 if err := autoConvert_v3alpha1_TracingServiceSpec_To_v2_TracingServiceSpec(in, out, s); err != nil {
716 return err
717 }
718
719
720 return nil
721 }
722
View as plain text