-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
factory_functions.go
1134 lines (1070 loc) · 35.1 KB
/
factory_functions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package k8s
import (
v1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
"k8s.io/client-go/tools/cache"
"github.com/cilium/cilium/pkg/comparator"
dpTypes "github.com/cilium/cilium/pkg/datapath/types"
cilium_v2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
cilium_v2alpha1 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2alpha1"
slim_corev1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/api/core/v1"
slim_discover_v1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/api/discovery/v1"
slim_discover_v1beta1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/api/discovery/v1beta1"
slim_networkingv1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/api/networking/v1"
slim_metav1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/apis/meta/v1"
"github.com/cilium/cilium/pkg/k8s/types"
"github.com/cilium/cilium/pkg/logging/logfields"
)
func ObjToV1NetworkPolicy(obj interface{}) *slim_networkingv1.NetworkPolicy {
k8sNP, ok := obj.(*slim_networkingv1.NetworkPolicy)
if ok {
return k8sNP
}
deletedObj, ok := obj.(cache.DeletedFinalStateUnknown)
if ok {
// Delete was not observed by the watcher but is
// removed from kube-apiserver. This is the last
// known state and the object no longer exists.
k8sNP, ok := deletedObj.Obj.(*slim_networkingv1.NetworkPolicy)
if ok {
return k8sNP
}
}
log.WithField(logfields.Object, logfields.Repr(obj)).
Warn("Ignoring invalid k8s v1 NetworkPolicy")
return nil
}
func ObjToV1Ingress(obj interface{}) *slim_networkingv1.Ingress {
ing, ok := obj.(*slim_networkingv1.Ingress)
if ok {
return ing
}
deletedObj, ok := obj.(cache.DeletedFinalStateUnknown)
if ok {
// Delete was not observed by the watcher but is
// removed from kube-apiserver. This is the last
// known state and the object no longer exists.
svc, ok := deletedObj.Obj.(*slim_networkingv1.Ingress)
if ok {
return svc
}
}
log.WithField(logfields.Object, logfields.Repr(obj)).
Warn("Ignoring invalid v1 Ingress")
return nil
}
func ObjToV1Services(obj interface{}) *slim_corev1.Service {
svc, ok := obj.(*slim_corev1.Service)
if ok {
return svc
}
deletedObj, ok := obj.(cache.DeletedFinalStateUnknown)
if ok {
// Delete was not observed by the watcher but is
// removed from kube-apiserver. This is the last
// known state and the object no longer exists.
svc, ok := deletedObj.Obj.(*slim_corev1.Service)
if ok {
return svc
}
}
log.WithField(logfields.Object, logfields.Repr(obj)).
Warn("Ignoring invalid k8s v1 Service")
return nil
}
func ObjToV1Endpoints(obj interface{}) *slim_corev1.Endpoints {
ep, ok := obj.(*slim_corev1.Endpoints)
if ok {
return ep
}
deletedObj, ok := obj.(cache.DeletedFinalStateUnknown)
if ok {
// Delete was not observed by the watcher but is
// removed from kube-apiserver. This is the last
// known state and the object no longer exists.
ep, ok := deletedObj.Obj.(*slim_corev1.Endpoints)
if ok {
return ep
}
}
log.WithField(logfields.Object, logfields.Repr(obj)).
Warn("Ignoring invalid k8s v1 Endpoints")
return nil
}
func ObjToV1Beta1EndpointSlice(obj interface{}) *slim_discover_v1beta1.EndpointSlice {
ep, ok := obj.(*slim_discover_v1beta1.EndpointSlice)
if ok {
return ep
}
deletedObj, ok := obj.(cache.DeletedFinalStateUnknown)
if ok {
// Delete was not observed by the watcher but is
// removed from kube-apiserver. This is the last
// known state and the object no longer exists.
ep, ok := deletedObj.Obj.(*slim_discover_v1beta1.EndpointSlice)
if ok {
return ep
}
}
log.WithField(logfields.Object, logfields.Repr(obj)).
Warn("Ignoring invalid k8s v1beta1 EndpointSlice")
return nil
}
func ObjToV1EndpointSlice(obj interface{}) *slim_discover_v1.EndpointSlice {
ep, ok := obj.(*slim_discover_v1.EndpointSlice)
if ok {
return ep
}
deletedObj, ok := obj.(cache.DeletedFinalStateUnknown)
if ok {
// Delete was not observed by the watcher but is
// removed from kube-apiserver. This is the last
// known state and the object no longer exists.
ep, ok := deletedObj.Obj.(*slim_discover_v1.EndpointSlice)
if ok {
return ep
}
}
log.WithField(logfields.Object, logfields.Repr(obj)).
Warn("Ignoring invalid k8s v1 EndpointSlice")
return nil
}
func ObjToSlimCNP(obj interface{}) *types.SlimCNP {
cnp, ok := obj.(*types.SlimCNP)
if ok {
return cnp
}
deletedObj, ok := obj.(cache.DeletedFinalStateUnknown)
if ok {
// Delete was not observed by the watcher but is
// removed from kube-apiserver. This is the last
// known state and the object no longer exists.
cnp, ok := deletedObj.Obj.(*types.SlimCNP)
if ok {
return cnp
}
}
log.WithField(logfields.Object, logfields.Repr(obj)).
Warn("Ignoring invalid k8s v2 CiliumNetworkPolicy")
return nil
}
func ObjTov1Pod(obj interface{}) *slim_corev1.Pod {
pod, ok := obj.(*slim_corev1.Pod)
if ok {
return pod
}
deletedObj, ok := obj.(cache.DeletedFinalStateUnknown)
if ok {
// Delete was not observed by the watcher but is
// removed from kube-apiserver. This is the last
// known state and the object no longer exists.
pod, ok := deletedObj.Obj.(*slim_corev1.Pod)
if ok {
return pod
}
}
log.WithField(logfields.Object, logfields.Repr(obj)).
Warn("Ignoring invalid k8s v1 Pod")
return nil
}
func ObjToV1Node(obj interface{}) *v1.Node {
node, ok := obj.(*v1.Node)
if ok {
return node
}
deletedObj, ok := obj.(cache.DeletedFinalStateUnknown)
if ok {
// Delete was not observed by the watcher but is
// removed from kube-apiserver. This is the last
// known state and the object no longer exists.
node, ok := deletedObj.Obj.(*v1.Node)
if ok {
return node
}
}
log.WithField(logfields.Object, logfields.Repr(obj)).
Warn("Ignoring invalid k8s v1 Node")
return nil
}
func ObjToV1Namespace(obj interface{}) *slim_corev1.Namespace {
ns, ok := obj.(*slim_corev1.Namespace)
if ok {
return ns
}
deletedObj, ok := obj.(cache.DeletedFinalStateUnknown)
if ok {
// Delete was not observed by the watcher but is
// removed from kube-apiserver. This is the last
// known state and the object no longer exists.
ns, ok := deletedObj.Obj.(*slim_corev1.Namespace)
if ok {
return ns
}
}
log.WithField(logfields.Object, logfields.Repr(obj)).
Warn("Ignoring invalid k8s v1 Namespace")
return nil
}
func ObjToV1PartialObjectMetadata(obj interface{}) *slim_metav1.PartialObjectMetadata {
pom, ok := obj.(*slim_metav1.PartialObjectMetadata)
if ok {
return pom
}
deletedObj, ok := obj.(cache.DeletedFinalStateUnknown)
if ok {
// Delete was not observed by the watcher but is
// removed from kube-apiserver. This is the last
// known state and the object no longer exists.
pom, ok := deletedObj.Obj.(*slim_metav1.PartialObjectMetadata)
if ok {
return pom
}
}
log.WithField(logfields.Object, logfields.Repr(obj)).
Warn("Ignoring invalid k8s v1 PartialObjectMetadata")
return nil
}
func ObjToV1Secret(obj interface{}) *slim_corev1.Secret {
secret, ok := obj.(*slim_corev1.Secret)
if ok {
return secret
}
deletedObj, ok := obj.(cache.DeletedFinalStateUnknown)
if ok {
// Delete was not observed by the watcher but is
// removed from kube-apiserver. This is the last
// known state and the object no longer exists.
secret, ok := deletedObj.Obj.(*slim_corev1.Secret)
if ok {
return secret
}
}
log.WithField(logfields.Object, logfields.Repr(obj)).
Warn("Ignoring invalid k8s v1 Secret")
return nil
}
func EqualV1Services(k8sSVC1, k8sSVC2 *slim_corev1.Service, nodeAddressing dpTypes.NodeAddressing) bool {
// Service annotations are used to mark services as global, shared, etc.
if !comparator.MapStringEquals(k8sSVC1.GetAnnotations(), k8sSVC2.GetAnnotations()) {
return false
}
svcID1, svc1 := ParseService(k8sSVC1, nodeAddressing)
svcID2, svc2 := ParseService(k8sSVC2, nodeAddressing)
if svcID1 != svcID2 {
return false
}
// Please write all the equalness logic inside the K8sServiceInfo.Equals()
// method.
return svc1.DeepEqual(svc2)
}
// AnnotationsEqual returns whether the annotation with any key in
// relevantAnnotations is equal in anno1 and anno2.
func AnnotationsEqual(relevantAnnotations []string, anno1, anno2 map[string]string) bool {
for _, an := range relevantAnnotations {
if anno1[an] != anno2[an] {
return false
}
}
return true
}
func convertToK8sServicePorts(ports []v1.ServicePort) []slim_corev1.ServicePort {
if ports == nil {
return nil
}
slimPorts := make([]slim_corev1.ServicePort, 0, len(ports))
for _, v1Port := range ports {
slimPorts = append(slimPorts,
slim_corev1.ServicePort{
Name: v1Port.Name,
Protocol: slim_corev1.Protocol(v1Port.Protocol),
Port: v1Port.Port,
NodePort: v1Port.NodePort,
},
)
}
return slimPorts
}
func ConvertToK8sV1ServicePorts(slimPorts []slim_corev1.ServicePort) []v1.ServicePort {
if slimPorts == nil {
return nil
}
ports := make([]v1.ServicePort, 0, len(slimPorts))
for _, port := range slimPorts {
ports = append(ports,
v1.ServicePort{
Name: port.Name,
Protocol: v1.Protocol(port.Protocol),
Port: port.Port,
NodePort: port.NodePort,
},
)
}
return ports
}
func convertToK8sServiceAffinityConfig(saCfg *v1.SessionAffinityConfig) *slim_corev1.SessionAffinityConfig {
if saCfg == nil {
return nil
}
if saCfg.ClientIP == nil {
return &slim_corev1.SessionAffinityConfig{}
}
return &slim_corev1.SessionAffinityConfig{
ClientIP: &slim_corev1.ClientIPConfig{
TimeoutSeconds: saCfg.ClientIP.TimeoutSeconds,
},
}
}
func ConvertToK8sV1ServiceAffinityConfig(saCfg *slim_corev1.SessionAffinityConfig) *v1.SessionAffinityConfig {
if saCfg == nil {
return nil
}
if saCfg.ClientIP == nil {
return &v1.SessionAffinityConfig{}
}
return &v1.SessionAffinityConfig{
ClientIP: &v1.ClientIPConfig{
TimeoutSeconds: saCfg.ClientIP.TimeoutSeconds,
},
}
}
func convertToK8sLoadBalancerIngress(lbIngs []v1.LoadBalancerIngress) []slim_corev1.LoadBalancerIngress {
if lbIngs == nil {
return nil
}
slimLBIngs := make([]slim_corev1.LoadBalancerIngress, 0, len(lbIngs))
for _, lbIng := range lbIngs {
slimLBIngs = append(slimLBIngs,
slim_corev1.LoadBalancerIngress{
IP: lbIng.IP,
},
)
}
return slimLBIngs
}
func ConvertToK8sV1LoadBalancerIngress(slimLBIngs []slim_corev1.LoadBalancerIngress) []v1.LoadBalancerIngress {
if slimLBIngs == nil {
return nil
}
lbIngs := make([]v1.LoadBalancerIngress, 0, len(slimLBIngs))
for _, lbIng := range slimLBIngs {
ports := make([]v1.PortStatus, 0, len(lbIng.Ports))
for _, port := range lbIng.Ports {
ports = append(ports, v1.PortStatus{
Port: port.Port,
Protocol: v1.Protocol(port.Protocol),
Error: port.Error,
})
}
lbIngs = append(lbIngs,
v1.LoadBalancerIngress{
IP: lbIng.IP,
Hostname: lbIng.Hostname,
Ports: ports,
},
)
}
return lbIngs
}
func ConvertToNetworkV1IngressLoadBalancerIngress(slimLBIngs []slim_corev1.LoadBalancerIngress) []networkingv1.IngressLoadBalancerIngress {
if slimLBIngs == nil {
return nil
}
ingLBIngs := make([]networkingv1.IngressLoadBalancerIngress, 0, len(slimLBIngs))
for _, lbIng := range slimLBIngs {
ports := make([]networkingv1.IngressPortStatus, 0, len(lbIng.Ports))
for _, port := range lbIng.Ports {
ports = append(ports, networkingv1.IngressPortStatus{
Port: port.Port,
Protocol: v1.Protocol(port.Protocol),
Error: port.Error,
})
}
ingLBIngs = append(ingLBIngs,
networkingv1.IngressLoadBalancerIngress{
IP: lbIng.IP,
Hostname: lbIng.Hostname,
Ports: ports,
})
}
return ingLBIngs
}
func ConvertToSlimIngressLoadBalancerStatus(slimLBStatus *slim_corev1.LoadBalancerStatus) *slim_networkingv1.IngressLoadBalancerStatus {
ingLBIngs := make([]slim_networkingv1.IngressLoadBalancerIngress, 0, len(slimLBStatus.Ingress))
for _, lbIng := range slimLBStatus.Ingress {
ports := make([]slim_networkingv1.IngressPortStatus, 0, len(lbIng.Ports))
for _, port := range lbIng.Ports {
ports = append(ports, slim_networkingv1.IngressPortStatus{
Port: port.Port,
Protocol: slim_corev1.Protocol(port.Protocol),
Error: port.Error,
})
}
ingLBIngs = append(ingLBIngs,
slim_networkingv1.IngressLoadBalancerIngress{
IP: lbIng.IP,
Hostname: lbIng.Hostname,
Ports: ports,
})
}
return &slim_networkingv1.IngressLoadBalancerStatus{
Ingress: ingLBIngs,
}
}
// ConvertToK8sService converts a *v1.Service into a
// *slim_corev1.Service or a cache.DeletedFinalStateUnknown into
// a cache.DeletedFinalStateUnknown with a *slim_corev1.Service in its Obj.
// If the given obj can't be cast into either *slim_corev1.Service
// nor cache.DeletedFinalStateUnknown, the original obj is returned.
func ConvertToK8sService(obj interface{}) interface{} {
switch concreteObj := obj.(type) {
case *v1.Service:
return &slim_corev1.Service{
TypeMeta: slim_metav1.TypeMeta{
Kind: concreteObj.TypeMeta.Kind,
APIVersion: concreteObj.TypeMeta.APIVersion,
},
ObjectMeta: slim_metav1.ObjectMeta{
Name: concreteObj.ObjectMeta.Name,
Namespace: concreteObj.ObjectMeta.Namespace,
ResourceVersion: concreteObj.ObjectMeta.ResourceVersion,
UID: concreteObj.ObjectMeta.UID,
Labels: concreteObj.ObjectMeta.Labels,
Annotations: concreteObj.ObjectMeta.Annotations,
},
Spec: slim_corev1.ServiceSpec{
Ports: convertToK8sServicePorts(concreteObj.Spec.Ports),
Selector: concreteObj.Spec.Selector,
ClusterIP: concreteObj.Spec.ClusterIP,
Type: slim_corev1.ServiceType(concreteObj.Spec.Type),
ExternalIPs: concreteObj.Spec.ExternalIPs,
SessionAffinity: slim_corev1.ServiceAffinity(concreteObj.Spec.SessionAffinity),
LoadBalancerIP: concreteObj.Spec.LoadBalancerIP,
ExternalTrafficPolicy: slim_corev1.ServiceExternalTrafficPolicyType(concreteObj.Spec.ExternalTrafficPolicy),
HealthCheckNodePort: concreteObj.Spec.HealthCheckNodePort,
SessionAffinityConfig: convertToK8sServiceAffinityConfig(concreteObj.Spec.SessionAffinityConfig),
},
Status: slim_corev1.ServiceStatus{
LoadBalancer: slim_corev1.LoadBalancerStatus{
Ingress: convertToK8sLoadBalancerIngress(concreteObj.Status.LoadBalancer.Ingress),
},
},
}
case cache.DeletedFinalStateUnknown:
svc, ok := concreteObj.Obj.(*v1.Service)
if !ok {
return obj
}
return cache.DeletedFinalStateUnknown{
Key: concreteObj.Key,
Obj: &slim_corev1.Service{
TypeMeta: slim_metav1.TypeMeta{
Kind: svc.TypeMeta.Kind,
APIVersion: svc.TypeMeta.APIVersion,
},
ObjectMeta: slim_metav1.ObjectMeta{
Name: svc.ObjectMeta.Name,
Namespace: svc.ObjectMeta.Namespace,
ResourceVersion: svc.ObjectMeta.ResourceVersion,
UID: svc.ObjectMeta.UID,
Labels: svc.ObjectMeta.Labels,
Annotations: svc.ObjectMeta.Annotations,
},
Spec: slim_corev1.ServiceSpec{
Ports: convertToK8sServicePorts(svc.Spec.Ports),
Selector: svc.Spec.Selector,
ClusterIP: svc.Spec.ClusterIP,
Type: slim_corev1.ServiceType(svc.Spec.Type),
ExternalIPs: svc.Spec.ExternalIPs,
SessionAffinity: slim_corev1.ServiceAffinity(svc.Spec.SessionAffinity),
LoadBalancerIP: svc.Spec.LoadBalancerIP,
ExternalTrafficPolicy: slim_corev1.ServiceExternalTrafficPolicyType(svc.Spec.ExternalTrafficPolicy),
HealthCheckNodePort: svc.Spec.HealthCheckNodePort,
SessionAffinityConfig: convertToK8sServiceAffinityConfig(svc.Spec.SessionAffinityConfig),
},
Status: slim_corev1.ServiceStatus{
LoadBalancer: slim_corev1.LoadBalancerStatus{
Ingress: convertToK8sLoadBalancerIngress(svc.Status.LoadBalancer.Ingress),
},
},
},
}
default:
return obj
}
}
// ConvertToCCNP converts a *cilium_v2.CiliumClusterwideNetworkPolicy into a
// *types.SlimCNP without the Status field of the given CNP, or a
// cache.DeletedFinalStateUnknown into a cache.DeletedFinalStateUnknown with a
// *types.SlimCNP, also without the Status field of the given CNP, in its Obj.
// If the given obj can't be cast into either *cilium_v2.CiliumClusterwideNetworkPolicy
// nor cache.DeletedFinalStateUnknown, the original obj is returned.
func ConvertToCCNP(obj interface{}) interface{} {
switch concreteObj := obj.(type) {
case *cilium_v2.CiliumClusterwideNetworkPolicy:
ccnp := &types.SlimCNP{
CiliumNetworkPolicy: &cilium_v2.CiliumNetworkPolicy{
TypeMeta: concreteObj.TypeMeta,
ObjectMeta: concreteObj.ObjectMeta,
Spec: concreteObj.Spec,
Specs: concreteObj.Specs,
},
}
return ccnp
case cache.DeletedFinalStateUnknown:
ccnp, ok := concreteObj.Obj.(*cilium_v2.CiliumClusterwideNetworkPolicy)
if !ok {
return obj
}
slimCNP := &types.SlimCNP{
CiliumNetworkPolicy: &cilium_v2.CiliumNetworkPolicy{
TypeMeta: ccnp.TypeMeta,
ObjectMeta: ccnp.ObjectMeta,
Spec: ccnp.Spec,
Specs: ccnp.Specs,
},
}
dfsu := cache.DeletedFinalStateUnknown{
Key: concreteObj.Key,
Obj: slimCNP,
}
return dfsu
default:
return obj
}
}
// ConvertToCNP converts a *cilium_v2.CiliumNetworkPolicy into a
// *types.SlimCNP without the Status field of the given CNP, or a
// cache.DeletedFinalStateUnknown into a cache.DeletedFinalStateUnknown with a
// *types.SlimCNP, also without the Status field of the given CNP, in its Obj.
// If the given obj can't be cast into either *cilium_v2.CiliumNetworkPolicy
// nor cache.DeletedFinalStateUnknown, the original obj is returned.
func ConvertToCNP(obj interface{}) interface{} {
switch concreteObj := obj.(type) {
case *cilium_v2.CiliumNetworkPolicy:
cnp := &types.SlimCNP{
CiliumNetworkPolicy: &cilium_v2.CiliumNetworkPolicy{
TypeMeta: concreteObj.TypeMeta,
ObjectMeta: concreteObj.ObjectMeta,
Spec: concreteObj.Spec,
Specs: concreteObj.Specs,
},
}
return cnp
case cache.DeletedFinalStateUnknown:
cnp, ok := concreteObj.Obj.(*cilium_v2.CiliumNetworkPolicy)
if !ok {
return obj
}
dfsu := cache.DeletedFinalStateUnknown{
Key: concreteObj.Key,
Obj: &types.SlimCNP{
CiliumNetworkPolicy: &cilium_v2.CiliumNetworkPolicy{
TypeMeta: cnp.TypeMeta,
ObjectMeta: cnp.ObjectMeta,
Spec: cnp.Spec,
Specs: cnp.Specs,
},
},
}
return dfsu
default:
return obj
}
}
func convertToAddress(v1Addrs []v1.NodeAddress) []slim_corev1.NodeAddress {
if v1Addrs == nil {
return nil
}
addrs := make([]slim_corev1.NodeAddress, 0, len(v1Addrs))
for _, addr := range v1Addrs {
addrs = append(
addrs,
slim_corev1.NodeAddress{
Type: slim_corev1.NodeAddressType(addr.Type),
Address: addr.Address,
},
)
}
return addrs
}
func convertToTaints(v1Taints []v1.Taint) []slim_corev1.Taint {
if v1Taints == nil {
return nil
}
taints := make([]slim_corev1.Taint, 0, len(v1Taints))
for _, taint := range v1Taints {
var ta *slim_metav1.Time
if taint.TimeAdded != nil {
t := slim_metav1.NewTime(taint.TimeAdded.Time)
ta = &t
}
taints = append(
taints,
slim_corev1.Taint{
Key: taint.Key,
Value: taint.Value,
Effect: slim_corev1.TaintEffect(taint.Effect),
TimeAdded: ta,
},
)
}
return taints
}
// ConvertToNode converts a *v1.Node into a
// *types.Node or a cache.DeletedFinalStateUnknown into
// a cache.DeletedFinalStateUnknown with a *types.Node in its Obj.
// If the given obj can't be cast into either *v1.Node
// nor cache.DeletedFinalStateUnknown, the original obj is returned.
func ConvertToNode(obj interface{}) interface{} {
switch concreteObj := obj.(type) {
case *v1.Node:
p := &slim_corev1.Node{
TypeMeta: slim_metav1.TypeMeta{
Kind: concreteObj.TypeMeta.Kind,
APIVersion: concreteObj.TypeMeta.APIVersion,
},
ObjectMeta: slim_metav1.ObjectMeta{
Name: concreteObj.ObjectMeta.Name,
Namespace: concreteObj.ObjectMeta.Namespace,
UID: concreteObj.ObjectMeta.UID,
ResourceVersion: concreteObj.ObjectMeta.ResourceVersion,
Labels: concreteObj.ObjectMeta.Labels,
Annotations: concreteObj.ObjectMeta.Annotations,
},
Spec: slim_corev1.NodeSpec{
PodCIDR: concreteObj.Spec.PodCIDR,
PodCIDRs: concreteObj.Spec.PodCIDRs,
Taints: convertToTaints(concreteObj.Spec.Taints),
},
Status: slim_corev1.NodeStatus{
Addresses: convertToAddress(concreteObj.Status.Addresses),
},
}
return p
case cache.DeletedFinalStateUnknown:
node, ok := concreteObj.Obj.(*v1.Node)
if !ok {
return obj
}
dfsu := cache.DeletedFinalStateUnknown{
Key: concreteObj.Key,
Obj: &slim_corev1.Node{
TypeMeta: slim_metav1.TypeMeta{
Kind: node.TypeMeta.Kind,
APIVersion: node.TypeMeta.APIVersion,
},
ObjectMeta: slim_metav1.ObjectMeta{
Name: node.ObjectMeta.Name,
Namespace: node.ObjectMeta.Namespace,
UID: node.ObjectMeta.UID,
ResourceVersion: node.ObjectMeta.ResourceVersion,
Labels: node.ObjectMeta.Labels,
Annotations: node.ObjectMeta.Annotations,
},
Spec: slim_corev1.NodeSpec{
PodCIDR: node.Spec.PodCIDR,
PodCIDRs: node.Spec.PodCIDRs,
Taints: convertToTaints(node.Spec.Taints),
},
Status: slim_corev1.NodeStatus{
Addresses: convertToAddress(node.Status.Addresses),
},
},
}
return dfsu
default:
return obj
}
}
// ConvertToCiliumNode converts a *cilium_v2.CiliumNode into a
// *cilium_v2.CiliumNode or a cache.DeletedFinalStateUnknown into
// a cache.DeletedFinalStateUnknown with a *cilium_v2.CiliumNode in its Obj.
// If the given obj can't be cast into either *cilium_v2.CiliumNode
// nor cache.DeletedFinalStateUnknown, the original obj is returned.
func ConvertToCiliumNode(obj interface{}) interface{} {
// TODO create a slim type of the CiliumNode
switch concreteObj := obj.(type) {
case *cilium_v2.CiliumNode:
return concreteObj
case cache.DeletedFinalStateUnknown:
ciliumNode, ok := concreteObj.Obj.(*cilium_v2.CiliumNode)
if !ok {
return obj
}
return cache.DeletedFinalStateUnknown{
Key: concreteObj.Key,
Obj: ciliumNode,
}
default:
return obj
}
}
// ConvertToCiliumExternalWorkload converts a *cilium_v2.CiliumExternalWorkload into a
// *cilium_v2.CiliumExternalWorkload or a cache.DeletedFinalStateUnknown into
// a cache.DeletedFinalStateUnknown with a *cilium_v2.CiliumExternalWorkload in its Obj.
// If the given obj can't be cast into either *cilium_v2.CiliumExternalWorkload
// nor cache.DeletedFinalStateUnknown, the original obj is returned.
func ConvertToCiliumExternalWorkload(obj interface{}) interface{} {
// TODO create a slim type of the CiliumExternalWorkload
switch concreteObj := obj.(type) {
case *cilium_v2.CiliumExternalWorkload:
return concreteObj
case cache.DeletedFinalStateUnknown:
ciliumExternalWorkload, ok := concreteObj.Obj.(*cilium_v2.CiliumExternalWorkload)
if !ok {
return obj
}
return cache.DeletedFinalStateUnknown{
Key: concreteObj.Key,
Obj: ciliumExternalWorkload,
}
default:
return obj
}
}
// ConvertToCiliumLocalRedirectPolicy converts a *cilium_v2.CiliumLocalRedirectPolicy into a
// *cilium_v2.CiliumLocalRedirectPolicy or a cache.DeletedFinalStateUnknown into
// a cache.DeletedFinalStateUnknown with a *cilium_v2.CiliumLocalRedirectPolicy in its Obj.
// If the given obj can't be cast into either *cilium_v2.CiliumLocalRedirectPolicy
// nor cache.DeletedFinalStateUnknown, the original obj is returned.
func ConvertToCiliumLocalRedirectPolicy(obj interface{}) interface{} {
// TODO create a slim type of the CiliumLocalRedirectPolicy
switch concreteObj := obj.(type) {
case *cilium_v2.CiliumLocalRedirectPolicy:
return concreteObj
case cache.DeletedFinalStateUnknown:
ciliumLocalRedirectPolicy, ok := concreteObj.Obj.(*cilium_v2.CiliumLocalRedirectPolicy)
if !ok {
return obj
}
return cache.DeletedFinalStateUnknown{
Key: concreteObj.Key,
Obj: ciliumLocalRedirectPolicy,
}
default:
return obj
}
}
// ConvertToCiliumEgressGatewayPolicy converts a *cilium_v2.CiliumEgressGatewayPolicy into a
// *cilium_v2.CiliumEgressGatewayPolicy or a cache.DeletedFinalStateUnknown into
// a cache.DeletedFinalStateUnknown with a *cilium_v2.CiliumEgressGatewayPolicy in its Obj.
// If the given obj can't be cast into either *cilium_v2.CiliumEgressGatewayPolicy
// nor cache.DeletedFinalStateUnknown, the original obj is returned.
func ConvertToCiliumEgressGatewayPolicy(obj interface{}) interface{} {
// TODO create a slim type of the CiliumEgressGatewayPolicy
switch concreteObj := obj.(type) {
case *cilium_v2.CiliumEgressGatewayPolicy:
return concreteObj
case cache.DeletedFinalStateUnknown:
ciliumEgressGatewayPolicy, ok := concreteObj.Obj.(*cilium_v2.CiliumEgressGatewayPolicy)
if !ok {
return obj
}
return cache.DeletedFinalStateUnknown{
Key: concreteObj.Key,
Obj: ciliumEgressGatewayPolicy,
}
default:
return obj
}
}
// ConvertToCiliumClusterwideEnvoyConfig converts a *cilium_v2.CiliumClusterwideEnvoyConfig into a
// *cilium_v2.CiliumClusterwideEnvoyConfig or a cache.DeletedFinalStateUnknown into
// a cache.DeletedFinalStateUnknown with a *cilium_v2.CiliumClusterwideEnvoyConfig in its Obj.
// If the given obj can't be cast into either *cilium_v2.CiliumClusterwideEnvoyConfig
// nor cache.DeletedFinalStateUnknown, the original obj is returned.
func ConvertToCiliumClusterwideEnvoyConfig(obj interface{}) interface{} {
switch concreteObj := obj.(type) {
case *cilium_v2.CiliumClusterwideEnvoyConfig:
return concreteObj
case cache.DeletedFinalStateUnknown:
ciliumClusterwideEnvoyConfig, ok := concreteObj.Obj.(*cilium_v2.CiliumClusterwideEnvoyConfig)
if !ok {
return obj
}
return cache.DeletedFinalStateUnknown{
Key: concreteObj.Key,
Obj: ciliumClusterwideEnvoyConfig,
}
default:
return obj
}
}
// ConvertToCiliumEnvoyConfig converts a *cilium_v2.CiliumEnvoyConfig into a
// *cilium_v2.CiliumEnvoyConfig or a cache.DeletedFinalStateUnknown into
// a cache.DeletedFinalStateUnknown with a *cilium_v2.CiliumEnvoyConfig in its Obj.
// If the given obj can't be cast into either *cilium_v2.CiliumEnvoyConfig
// nor cache.DeletedFinalStateUnknown, the original obj is returned.
func ConvertToCiliumEnvoyConfig(obj interface{}) interface{} {
switch concreteObj := obj.(type) {
case *cilium_v2.CiliumEnvoyConfig:
return concreteObj
case cache.DeletedFinalStateUnknown:
ciliumEnvoyConfig, ok := concreteObj.Obj.(*cilium_v2.CiliumEnvoyConfig)
if !ok {
return obj
}
return cache.DeletedFinalStateUnknown{
Key: concreteObj.Key,
Obj: ciliumEnvoyConfig,
}
default:
return obj
}
}
// ObjToCiliumNode attempts to cast object to a CiliumNode object and
// returns the CiliumNode objext if the cast succeeds. Otherwise, nil is returned.
func ObjToCiliumNode(obj interface{}) *cilium_v2.CiliumNode {
cn, ok := obj.(*cilium_v2.CiliumNode)
if ok {
return cn
}
deletedObj, ok := obj.(cache.DeletedFinalStateUnknown)
if ok {
// Delete was not observed by the watcher but is
// removed from kube-apiserver. This is the last
// known state and the object no longer exists.
cn, ok := deletedObj.Obj.(*cilium_v2.CiliumNode)
if ok {
return cn
}
}
log.WithField(logfields.Object, logfields.Repr(obj)).
Warn("Ignoring invalid v2 CiliumNode")
return nil
}
// ConvertToCiliumEndpoint converts a *cilium_v2.CiliumEndpoint into a
// *types.CiliumEndpoint or a cache.DeletedFinalStateUnknown into a
// cache.DeletedFinalStateUnknown with a *types.CiliumEndpoint in its Obj.
// If the given obj can't be cast into either *cilium_v2.CiliumEndpoint nor
// cache.DeletedFinalStateUnknown, the original obj is returned.
func ConvertToCiliumEndpoint(obj interface{}) interface{} {
switch concreteObj := obj.(type) {
case *cilium_v2.CiliumEndpoint:
p := &types.CiliumEndpoint{
TypeMeta: slim_metav1.TypeMeta{
Kind: concreteObj.TypeMeta.Kind,
APIVersion: concreteObj.TypeMeta.APIVersion,
},
ObjectMeta: slim_metav1.ObjectMeta{
Name: concreteObj.ObjectMeta.Name,
Namespace: concreteObj.ObjectMeta.Namespace,
UID: concreteObj.ObjectMeta.UID,
ResourceVersion: concreteObj.ObjectMeta.ResourceVersion,
// We don't need to store labels nor annotations because
// they are not used by the CEP handlers.
Labels: nil,
Annotations: nil,
},
Encryption: func() *cilium_v2.EncryptionSpec {
enc := concreteObj.Status.Encryption
return &enc
}(),
Identity: concreteObj.Status.Identity,
Networking: concreteObj.Status.Networking,
NamedPorts: concreteObj.Status.NamedPorts,
}
return p
case cache.DeletedFinalStateUnknown:
ciliumEndpoint, ok := concreteObj.Obj.(*cilium_v2.CiliumEndpoint)
if !ok {
return obj
}
dfsu := cache.DeletedFinalStateUnknown{
Key: concreteObj.Key,
Obj: &types.CiliumEndpoint{
TypeMeta: slim_metav1.TypeMeta{
Kind: ciliumEndpoint.TypeMeta.Kind,
APIVersion: ciliumEndpoint.TypeMeta.APIVersion,
},
ObjectMeta: slim_metav1.ObjectMeta{
Name: ciliumEndpoint.ObjectMeta.Name,
Namespace: ciliumEndpoint.ObjectMeta.Namespace,
UID: ciliumEndpoint.ObjectMeta.UID,
ResourceVersion: ciliumEndpoint.ObjectMeta.ResourceVersion,
// We don't need to store labels nor annotations because
// they are not used by the CEP handlers.
Labels: nil,
Annotations: nil,
},
Encryption: func() *cilium_v2.EncryptionSpec {
enc := ciliumEndpoint.Status.Encryption
return &enc
}(),
Identity: ciliumEndpoint.Status.Identity,
Networking: ciliumEndpoint.Status.Networking,
NamedPorts: ciliumEndpoint.Status.NamedPorts,
},
}
return dfsu
default:
return obj
}
}
// ObjToCiliumEndpoint attempts to cast object to a CiliumEndpoint object
// and returns the CiliumEndpoint object if the cast succeeds. Otherwise, nil is returned.
func ObjToCiliumEndpoint(obj interface{}) *types.CiliumEndpoint {
ce, ok := obj.(*types.CiliumEndpoint)
if ok {
return ce
}
deletedObj, ok := obj.(cache.DeletedFinalStateUnknown)
if ok {
// Delete was not observed by the watcher but is
// removed from kube-apiserver. This is the last
// known state and the object no longer exists.
ce, ok := deletedObj.Obj.(*types.CiliumEndpoint)
if ok {
return ce
}
}
log.WithField(logfields.Object, logfields.Repr(obj)).
Warn("Ignoring invalid v2 CiliumEndpoint")
return nil
}
// ObjToCLRP attempts to cast object to a CLRP object and
// returns the CLRP object if the cast succeeds. Otherwise, nil is returned.
func ObjToCLRP(obj interface{}) *cilium_v2.CiliumLocalRedirectPolicy {
cLRP, ok := obj.(*cilium_v2.CiliumLocalRedirectPolicy)
if ok {
return cLRP
}
deletedObj, ok := obj.(cache.DeletedFinalStateUnknown)
if ok {
// Delete was not observed by the watcher but is
// removed from kube-apiserver. This is the last
// known state and the object no longer exists.
cn, ok := deletedObj.Obj.(*cilium_v2.CiliumLocalRedirectPolicy)
if ok {
return cn
}
}