-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
factory_functions.go
808 lines (753 loc) · 23.6 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
// Copyright 2018-2020 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package k8s
import (
"reflect"
"sort"
"github.com/cilium/cilium/pkg/annotation"
"github.com/cilium/cilium/pkg/comparator"
cilium_v2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
"github.com/cilium/cilium/pkg/k8s/types"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/strings"
v1 "k8s.io/api/core/v1"
"k8s.io/api/discovery/v1beta1"
networkingv1 "k8s.io/api/networking/v1"
"k8s.io/client-go/tools/cache"
)
func CopyObjToV1NetworkPolicy(obj interface{}) *types.NetworkPolicy {
k8sNP, ok := obj.(*types.NetworkPolicy)
if !ok {
log.WithField(logfields.Object, logfields.Repr(obj)).
Warn("Ignoring invalid k8s v1 NetworkPolicy")
return nil
}
return k8sNP.DeepCopy()
}
func CopyObjToV1Services(obj interface{}) *types.Service {
svc, ok := obj.(*types.Service)
if !ok {
log.WithField(logfields.Object, logfields.Repr(obj)).
Warn("Ignoring invalid k8s v1 Service")
return nil
}
return svc.DeepCopy()
}
func CopyObjToV1Endpoints(obj interface{}) *types.Endpoints {
ep, ok := obj.(*types.Endpoints)
if !ok {
log.WithField(logfields.Object, logfields.Repr(obj)).
Warn("Ignoring invalid k8s v1 Endpoints")
return nil
}
return ep.DeepCopy()
}
func CopyObjToV1EndpointSlice(obj interface{}) *types.EndpointSlice {
ep, ok := obj.(*types.EndpointSlice)
if !ok {
log.WithField(logfields.Object, logfields.Repr(obj)).
Warn("Ignoring invalid k8s v1 EndpointSlice")
return nil
}
return ep.DeepCopy()
}
func CopyObjToV2CNP(obj interface{}) *types.SlimCNP {
cnp, ok := obj.(*types.SlimCNP)
if !ok {
log.WithField(logfields.Object, logfields.Repr(obj)).
Warn("Ignoring invalid k8s v2 CiliumNetworkPolicy")
return nil
}
return cnp.DeepCopy()
}
func CopyObjToV1Pod(obj interface{}) *types.Pod {
pod, ok := obj.(*types.Pod)
if !ok {
log.WithField(logfields.Object, logfields.Repr(obj)).
Warn("Ignoring invalid k8s v1 Pod")
return nil
}
return pod.DeepCopy()
}
func CopyObjToV1Node(obj interface{}) *types.Node {
node, ok := obj.(*types.Node)
if !ok {
log.WithField(logfields.Object, logfields.Repr(obj)).
Warn("Ignoring invalid k8s v1 Node")
return nil
}
return node.DeepCopy()
}
func CopyObjToV1Namespace(obj interface{}) *types.Namespace {
ns, ok := obj.(*types.Namespace)
if !ok {
log.WithField(logfields.Object, logfields.Repr(obj)).
Warn("Ignoring invalid k8s v1 Namespace")
return nil
}
return ns.DeepCopy()
}
func EqualV1NetworkPolicy(np1, np2 *types.NetworkPolicy) bool {
// As Cilium uses all of the Spec from a NP it's not probably not worth
// it to create a dedicated deep equal function to compare both network
// policies.
return np1.Name == np2.Name &&
np1.Namespace == np2.Namespace &&
reflect.DeepEqual(np1.Spec, np2.Spec)
}
func EqualV1Services(k8sSVC1, k8sSVC2 *types.Service) 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)
svcID2, svc2 := ParseService(k8sSVC2)
if svcID1 != svcID2 {
return false
}
// Please write all the equalness logic inside the K8sServiceInfo.Equals()
// method.
return svc1.DeepEquals(svc2)
}
func EqualV1Endpoints(ep1, ep2 *types.Endpoints) bool {
// We only care about the Name, Namespace and Subsets of a particular
// endpoint.
return ep1.Name == ep2.Name &&
ep1.Namespace == ep2.Namespace &&
reflect.DeepEqual(ep1.Subsets, ep2.Subsets)
}
func EqualV1EndpointSlice(ep1, ep2 *types.EndpointSlice) bool {
// We only care about the Name, Namespace and Subsets of a particular
// endpoint.
// AddressType is omitted because it's immutable after EndpointSlice's
// creation.
return ep1.Name == ep2.Name &&
ep1.Namespace == ep2.Namespace &&
reflect.DeepEqual(ep1.Endpoints, ep2.Endpoints) &&
reflect.DeepEqual(ep1.Ports, ep2.Ports)
}
func EqualV2CNP(cnp1, cnp2 *types.SlimCNP) bool {
if !(cnp1.Name == cnp2.Name && cnp1.Namespace == cnp2.Namespace) {
return false
}
// Ignore v1.LastAppliedConfigAnnotation annotation
lastAppliedCfgAnnotation1, ok1 := cnp1.GetAnnotations()[v1.LastAppliedConfigAnnotation]
lastAppliedCfgAnnotation2, ok2 := cnp2.GetAnnotations()[v1.LastAppliedConfigAnnotation]
defer func() {
if ok1 && cnp1.GetAnnotations() != nil {
cnp1.GetAnnotations()[v1.LastAppliedConfigAnnotation] = lastAppliedCfgAnnotation1
}
if ok2 && cnp2.GetAnnotations() != nil {
cnp2.GetAnnotations()[v1.LastAppliedConfigAnnotation] = lastAppliedCfgAnnotation2
}
}()
delete(cnp1.GetAnnotations(), v1.LastAppliedConfigAnnotation)
delete(cnp2.GetAnnotations(), v1.LastAppliedConfigAnnotation)
return comparator.MapStringEquals(cnp1.GetAnnotations(), cnp2.GetAnnotations()) &&
reflect.DeepEqual(cnp1.Spec, cnp2.Spec) &&
reflect.DeepEqual(cnp1.Specs, cnp2.Specs)
}
// 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 EqualV1PodContainers(c1, c2 types.PodContainer) bool {
if c1.Name != c2.Name ||
c1.Image != c2.Image {
return false
}
if len(c1.VolumeMountsPaths) != len(c2.VolumeMountsPaths) {
return false
}
for i, vlmMount1 := range c1.VolumeMountsPaths {
if vlmMount1 != c2.VolumeMountsPaths[i] {
return false
}
}
return true
}
func EqualV1Pod(pod1, pod2 *types.Pod) bool {
// We only care about the HostIP, the PodIP and the labels of the pods.
if pod1.StatusHostIP != pod2.StatusHostIP ||
pod1.SpecServiceAccountName != pod2.SpecServiceAccountName ||
pod1.SpecHostNetwork != pod2.SpecHostNetwork {
return false
}
if !strings.EqualStrings(pod1.StatusPodIPs, pod2.StatusPodIPs) {
return false
}
if !AnnotationsEqual([]string{annotation.ProxyVisibility}, pod1.GetAnnotations(), pod2.GetAnnotations()) {
return false
}
oldPodLabels := pod1.GetLabels()
newPodLabels := pod2.GetLabels()
if !comparator.MapStringEquals(oldPodLabels, newPodLabels) {
return false
}
if len(pod1.SpecContainers) != len(pod2.SpecContainers) {
return false
}
for i, c1 := range pod1.SpecContainers {
if !EqualV1PodContainers(c1, pod2.SpecContainers[i]) {
return false
}
}
return true
}
func EqualV1Node(node1, node2 *types.Node) bool {
if node1.GetObjectMeta().GetName() != node2.GetObjectMeta().GetName() {
return false
}
anno1 := node1.GetAnnotations()
anno2 := node2.GetAnnotations()
annotationsWeCareAbout := []string{
annotation.CiliumHostIP,
annotation.CiliumHostIPv6,
annotation.V4HealthName,
annotation.V6HealthName,
}
for _, an := range annotationsWeCareAbout {
if anno1[an] != anno2[an] {
return false
}
}
if len(node1.SpecTaints) != len(node2.SpecTaints) {
return false
}
for i, taint2 := range node2.SpecTaints {
taint1 := node1.SpecTaints[i]
if !taint1.MatchTaint(&taint2) {
return false
}
if taint1.Value != taint2.Value {
return false
}
if !taint1.TimeAdded.Equal(taint2.TimeAdded) {
return false
}
}
return true
}
func EqualV1Namespace(ns1, ns2 *types.Namespace) bool {
// we only care about namespace labels.
return ns1.Name == ns2.Name &&
comparator.MapStringEquals(ns1.GetLabels(), ns2.GetLabels())
}
// ConvertToNetworkPolicy converts a *networkingv1.NetworkPolicy into a
// *types.NetworkPolicy or a cache.DeletedFinalStateUnknown into
// a cache.DeletedFinalStateUnknown with a *types.NetworkPolicy in its Obj.
// If the given obj can't be cast into either *networkingv1.NetworkPolicy
// nor cache.DeletedFinalStateUnknown, the original obj is returned.
func ConvertToNetworkPolicy(obj interface{}) interface{} {
// TODO check which fields we really need
switch concreteObj := obj.(type) {
case *networkingv1.NetworkPolicy:
return &types.NetworkPolicy{
NetworkPolicy: concreteObj,
}
case cache.DeletedFinalStateUnknown:
netPol, ok := concreteObj.Obj.(*networkingv1.NetworkPolicy)
if !ok {
return obj
}
return cache.DeletedFinalStateUnknown{
Key: concreteObj.Key,
Obj: &types.NetworkPolicy{
NetworkPolicy: netPol,
},
}
default:
return obj
}
}
// ConvertToK8sService converts a *v1.Service into a
// *types.Service or a cache.DeletedFinalStateUnknown into
// a cache.DeletedFinalStateUnknown with a *types.Service in its Obj.
// If the given obj can't be cast into either *v1.Service
// nor cache.DeletedFinalStateUnknown, the original obj is returned.
func ConvertToK8sService(obj interface{}) interface{} {
// TODO check which fields we really need
switch concreteObj := obj.(type) {
case *v1.Service:
return &types.Service{
Service: concreteObj,
}
case cache.DeletedFinalStateUnknown:
svc, ok := concreteObj.Obj.(*v1.Service)
if !ok {
return obj
}
return cache.DeletedFinalStateUnknown{
Key: concreteObj.Key,
Obj: &types.Service{
Service: svc,
},
}
default:
return obj
}
}
// ConvertToK8sEndpoints converts a *v1.Endpoints into a
// *types.Endpoints or a cache.DeletedFinalStateUnknown into
// a cache.DeletedFinalStateUnknown with a *types.Endpoints in its Obj.
// If the given obj can't be cast into either *v1.Endpoints
// nor cache.DeletedFinalStateUnknown, the original obj is returned.
func ConvertToK8sEndpoints(obj interface{}) interface{} {
// TODO check which fields we really need
switch concreteObj := obj.(type) {
case *v1.Endpoints:
return &types.Endpoints{
Endpoints: concreteObj,
}
case cache.DeletedFinalStateUnknown:
eps, ok := concreteObj.Obj.(*v1.Endpoints)
if !ok {
return obj
}
return cache.DeletedFinalStateUnknown{
Key: concreteObj.Key,
Obj: &types.Endpoints{
Endpoints: eps,
},
}
default:
return obj
}
}
// ConvertToK8sEndpointSlice converts a *v1beta1.EndpointSlice into a
// *types.Endpoints or a cache.DeletedFinalStateUnknown into
// a cache.DeletedFinalStateUnknown with a *types.Endpoints in its Obj.
// If the given obj can't be cast into either *v1.Endpoints
// nor cache.DeletedFinalStateUnknown, the original obj is returned.
func ConvertToK8sEndpointSlice(obj interface{}) interface{} {
// TODO check which fields we really need
switch concreteObj := obj.(type) {
case *v1beta1.EndpointSlice:
return &types.EndpointSlice{
EndpointSlice: concreteObj,
}
case cache.DeletedFinalStateUnknown:
eps, ok := concreteObj.Obj.(*v1beta1.EndpointSlice)
if !ok {
return obj
}
return cache.DeletedFinalStateUnknown{
Key: concreteObj.Key,
Obj: &types.EndpointSlice{
EndpointSlice: eps,
},
}
default:
return obj
}
}
// ConvertToCCNPWithStatus converts a *cilium_v2.CiliumClusterwideNetworkPolicy
// into *types.SlimCNP or a cache.DeletedFinalStateUnknown into
// a cache.DeletedFinalStateUnknown with a *types.SlimCNP 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 ConvertToCCNPWithStatus(obj interface{}) interface{} {
switch concreteObj := obj.(type) {
case *cilium_v2.CiliumClusterwideNetworkPolicy:
t := &types.SlimCNP{
CiliumNetworkPolicy: concreteObj.CiliumNetworkPolicy,
}
t.Status = concreteObj.Status
return t
case cache.DeletedFinalStateUnknown:
cnp, ok := concreteObj.Obj.(*cilium_v2.CiliumClusterwideNetworkPolicy)
if !ok {
return obj
}
t := &types.SlimCNP{
CiliumNetworkPolicy: cnp.CiliumNetworkPolicy,
}
t.Status = cnp.Status
return cache.DeletedFinalStateUnknown{
Key: concreteObj.Key,
Obj: t,
}
default:
return obj
}
}
// ConvertToCNPWithStatus converts a *cilium_v2.CiliumNetworkPolicy or a
// *cilium_v2.CiliumClusterwideNetworkPolicy into a
// *types.SlimCNP or a cache.DeletedFinalStateUnknown into
// a cache.DeletedFinalStateUnknown with a *types.SlimCNP 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 ConvertToCNPWithStatus(obj interface{}) interface{} {
switch concreteObj := obj.(type) {
case *cilium_v2.CiliumNetworkPolicy:
return &types.SlimCNP{
CiliumNetworkPolicy: concreteObj,
}
case cache.DeletedFinalStateUnknown:
cnp, ok := concreteObj.Obj.(*cilium_v2.CiliumNetworkPolicy)
if !ok {
return obj
}
return cache.DeletedFinalStateUnknown{
Key: concreteObj.Key,
Obj: &types.SlimCNP{
CiliumNetworkPolicy: cnp,
},
}
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.
// WARNING calling this function will set *all* fields of the given CNP as
// empty.
func ConvertToCCNP(obj interface{}) interface{} {
switch concreteObj := obj.(type) {
case *cilium_v2.CiliumClusterwideNetworkPolicy:
cnp := &types.SlimCNP{
CiliumNetworkPolicy: &cilium_v2.CiliumNetworkPolicy{
TypeMeta: concreteObj.TypeMeta,
ObjectMeta: concreteObj.ObjectMeta,
Spec: concreteObj.Spec,
Specs: concreteObj.Specs,
},
}
*concreteObj = cilium_v2.CiliumClusterwideNetworkPolicy{}
return cnp
case cache.DeletedFinalStateUnknown:
cnp, ok := concreteObj.Obj.(*cilium_v2.CiliumClusterwideNetworkPolicy)
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,
},
},
}
*cnp = cilium_v2.CiliumClusterwideNetworkPolicy{}
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.
// WARNING calling this function will set *all* fields of the given CNP as
// empty.
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,
},
}
*concreteObj = cilium_v2.CiliumNetworkPolicy{}
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,
},
},
}
*cnp = cilium_v2.CiliumNetworkPolicy{}
return dfsu
default:
return obj
}
}
func getPodIPs(podStats v1.PodStatus) []string {
// make it a set first
ipsMap := make(map[string]struct{}, 1+len(podStats.PodIPs))
if podStats.PodIP != "" {
ipsMap[podStats.PodIP] = struct{}{}
}
for _, podIP := range podStats.PodIPs {
if podIP.IP != "" {
ipsMap[podIP.IP] = struct{}{}
}
}
if len(ipsMap) == 0 {
return nil
}
ips := make([]string, 0, len(ipsMap))
for ipStr := range ipsMap {
ips = append(ips, ipStr)
}
sort.Strings(ips)
return ips
}
// ConvertToPod converts a *v1.Pod into a
// *types.Pod or a cache.DeletedFinalStateUnknown into
// a cache.DeletedFinalStateUnknown with a *types.Pod in its Obj.
// If the given obj can't be cast into either *v1.Pod
// nor cache.DeletedFinalStateUnknown, the original obj is returned.
// WARNING calling this function will set *all* fields of the given Pod as
// empty.
func ConvertToPod(obj interface{}) interface{} {
switch concreteObj := obj.(type) {
case *v1.Pod:
var containers []types.PodContainer
for _, c := range concreteObj.Spec.Containers {
var vmps []string
for _, cvm := range c.VolumeMounts {
vmps = append(vmps, cvm.MountPath)
}
pc := types.PodContainer{
Name: c.Name,
Image: c.Image,
VolumeMountsPaths: vmps,
}
containers = append(containers, pc)
}
p := &types.Pod{
TypeMeta: concreteObj.TypeMeta,
ObjectMeta: concreteObj.ObjectMeta,
StatusPodIPs: getPodIPs(concreteObj.Status),
StatusHostIP: concreteObj.Status.HostIP,
SpecServiceAccountName: concreteObj.Spec.ServiceAccountName,
SpecHostNetwork: concreteObj.Spec.HostNetwork,
SpecContainers: containers,
}
*concreteObj = v1.Pod{}
return p
case cache.DeletedFinalStateUnknown:
pod, ok := concreteObj.Obj.(*v1.Pod)
if !ok {
return obj
}
var containers []types.PodContainer
for _, c := range pod.Spec.Containers {
var vmps []string
for _, cvm := range c.VolumeMounts {
vmps = append(vmps, cvm.MountPath)
}
pc := types.PodContainer{
Name: c.Name,
Image: c.Image,
VolumeMountsPaths: vmps,
}
containers = append(containers, pc)
}
dfsu := cache.DeletedFinalStateUnknown{
Key: concreteObj.Key,
Obj: &types.Pod{
TypeMeta: pod.TypeMeta,
ObjectMeta: pod.ObjectMeta,
StatusPodIPs: getPodIPs(pod.Status),
StatusHostIP: pod.Status.HostIP,
SpecServiceAccountName: pod.Spec.ServiceAccountName,
SpecHostNetwork: pod.Spec.HostNetwork,
SpecContainers: containers,
},
}
*pod = v1.Pod{}
return dfsu
default:
return obj
}
}
// 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.
// WARNING calling this function will set *all* fields of the given Node as
// empty.
func ConvertToNode(obj interface{}) interface{} {
switch concreteObj := obj.(type) {
case *v1.Node:
p := &types.Node{
TypeMeta: concreteObj.TypeMeta,
ObjectMeta: concreteObj.ObjectMeta,
StatusAddresses: concreteObj.Status.Addresses,
SpecPodCIDR: concreteObj.Spec.PodCIDR,
SpecPodCIDRs: concreteObj.Spec.PodCIDRs,
SpecTaints: concreteObj.Spec.Taints,
}
*concreteObj = v1.Node{}
return p
case cache.DeletedFinalStateUnknown:
node, ok := concreteObj.Obj.(*v1.Node)
if !ok {
return obj
}
dfsu := cache.DeletedFinalStateUnknown{
Key: concreteObj.Key,
Obj: &types.Node{
TypeMeta: node.TypeMeta,
ObjectMeta: node.ObjectMeta,
StatusAddresses: node.Status.Addresses,
SpecPodCIDR: node.Spec.PodCIDR,
SpecPodCIDRs: node.Spec.PodCIDRs,
SpecTaints: node.Spec.Taints,
},
}
*node = v1.Node{}
return dfsu
default:
return obj
}
}
// ConvertToNamespace converts a *v1.Namespace into a
// *types.Namespace or a cache.DeletedFinalStateUnknown into
// a cache.DeletedFinalStateUnknown with a *types.Namespace in its Obj.
// If the given obj can't be cast into either *v1.Namespace
// nor cache.DeletedFinalStateUnknown, the original obj is returned.
// WARNING calling this function will set *all* fields of the given Namespace as
// empty.
func ConvertToNamespace(obj interface{}) interface{} {
switch concreteObj := obj.(type) {
case *v1.Namespace:
p := &types.Namespace{
TypeMeta: concreteObj.TypeMeta,
ObjectMeta: concreteObj.ObjectMeta,
}
*concreteObj = v1.Namespace{}
return p
case cache.DeletedFinalStateUnknown:
namespace, ok := concreteObj.Obj.(*v1.Namespace)
if !ok {
return obj
}
dfsu := cache.DeletedFinalStateUnknown{
Key: concreteObj.Key,
Obj: &types.Namespace{
TypeMeta: namespace.TypeMeta,
ObjectMeta: namespace.ObjectMeta,
},
}
*namespace = v1.Namespace{}
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
}
}
// CopyObjToCiliumNode attempts to cast object to a CiliumNode object and
// returns a deep copy if the castin succeeds. Otherwise, nil is returned.
func CopyObjToCiliumNode(obj interface{}) *cilium_v2.CiliumNode {
cn, ok := obj.(*cilium_v2.CiliumNode)
if !ok {
log.WithField(logfields.Object, logfields.Repr(obj)).
Warn("Ignoring invalid CiliumNode")
return nil
}
return cn.DeepCopy()
}
// 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: concreteObj.TypeMeta,
ObjectMeta: concreteObj.ObjectMeta,
Encryption: concreteObj.Status.Encryption.DeepCopy(),
Identity: concreteObj.Status.Identity.DeepCopy(),
Networking: concreteObj.Status.Networking.DeepCopy(),
}
*concreteObj = cilium_v2.CiliumEndpoint{}
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: ciliumEndpoint.TypeMeta,
ObjectMeta: ciliumEndpoint.ObjectMeta,
Encryption: ciliumEndpoint.Status.Encryption.DeepCopy(),
Identity: ciliumEndpoint.Status.Identity.DeepCopy(),
Networking: ciliumEndpoint.Status.Networking.DeepCopy(),
},
}
*ciliumEndpoint = cilium_v2.CiliumEndpoint{}
return dfsu
default:
return obj
}
}
// CopyObjToCiliumEndpoint attempts to cast object to a CiliumEndpoint object
// and returns a deep copy if the castin succeeds. Otherwise, nil is returned.
func CopyObjToCiliumEndpoint(obj interface{}) *types.CiliumEndpoint {
ce, ok := obj.(*types.CiliumEndpoint)
if !ok {
log.WithField(logfields.Object, logfields.Repr(obj)).
Warn("Ignoring invalid CiliumEndpoint")
return nil
}
return ce.DeepCopy()
}