-
Notifications
You must be signed in to change notification settings - Fork 457
/
Copy pathcontroller.go
1431 lines (1217 loc) · 64.7 KB
/
controller.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
package controller
import (
"context"
"fmt"
"runtime"
"strings"
"time"
"github.com/puzpuzpuz/xsync/v3"
"golang.org/x/time/rate"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
kubeinformers "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes/scheme"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
appsv1 "k8s.io/client-go/listers/apps/v1"
certListerv1 "k8s.io/client-go/listers/certificates/v1"
v1 "k8s.io/client-go/listers/core/v1"
netv1 "k8s.io/client-go/listers/networking/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"
"k8s.io/utils/keymutex"
kubevirtController "kubevirt.io/kubevirt/pkg/controller"
v1alpha1 "sigs.k8s.io/network-policy-api/apis/v1alpha1"
anpinformer "sigs.k8s.io/network-policy-api/pkg/client/informers/externalversions"
anplister "sigs.k8s.io/network-policy-api/pkg/client/listers/apis/v1alpha1"
kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
kubeovninformer "github.com/kubeovn/kube-ovn/pkg/client/informers/externalversions"
kubeovnlister "github.com/kubeovn/kube-ovn/pkg/client/listers/kubeovn/v1"
ovnipam "github.com/kubeovn/kube-ovn/pkg/ipam"
"github.com/kubeovn/kube-ovn/pkg/ovs"
"github.com/kubeovn/kube-ovn/pkg/util"
)
const controllerAgentName = "kube-ovn-controller"
const (
logicalSwitchKey = "ls"
logicalRouterKey = "lr"
portGroupKey = "pg"
networkPolicyKey = "np"
sgKey = "sg"
associatedSgKeyPrefix = "associated_sg_"
sgsKey = "security_groups"
u2oKey = "u2o"
adminNetworkPolicyKey = "anp"
baselineAdminNetworkPolicyKey = "banp"
)
// Controller is kube-ovn main controller that watch ns/pod/node/svc/ep and operate ovn
type Controller struct {
config *Configuration
ipam *ovnipam.IPAM
namedPort *NamedPort
anpPrioNameMap map[int32]string
anpNamePrioMap map[string]int32
OVNNbClient ovs.NbClient
OVNSbClient ovs.SbClient
// ExternalGatewayType define external gateway type, centralized
ExternalGatewayType string
podsLister v1.PodLister
podsSynced cache.InformerSynced
addOrUpdatePodQueue workqueue.TypedRateLimitingInterface[string]
deletePodQueue workqueue.TypedRateLimitingInterface[string]
deletingPodObjMap *xsync.MapOf[string, *corev1.Pod]
deletingNodeObjMap *xsync.MapOf[string, *corev1.Node]
updatePodSecurityQueue workqueue.TypedRateLimitingInterface[string]
podKeyMutex keymutex.KeyMutex
vpcsLister kubeovnlister.VpcLister
vpcSynced cache.InformerSynced
addOrUpdateVpcQueue workqueue.TypedRateLimitingInterface[string]
delVpcQueue workqueue.TypedRateLimitingInterface[*kubeovnv1.Vpc]
updateVpcStatusQueue workqueue.TypedRateLimitingInterface[string]
vpcKeyMutex keymutex.KeyMutex
vpcNatGatewayLister kubeovnlister.VpcNatGatewayLister
vpcNatGatewaySynced cache.InformerSynced
addOrUpdateVpcNatGatewayQueue workqueue.TypedRateLimitingInterface[string]
delVpcNatGatewayQueue workqueue.TypedRateLimitingInterface[string]
initVpcNatGatewayQueue workqueue.TypedRateLimitingInterface[string]
updateVpcEipQueue workqueue.TypedRateLimitingInterface[string]
updateVpcFloatingIPQueue workqueue.TypedRateLimitingInterface[string]
updateVpcDnatQueue workqueue.TypedRateLimitingInterface[string]
updateVpcSnatQueue workqueue.TypedRateLimitingInterface[string]
updateVpcSubnetQueue workqueue.TypedRateLimitingInterface[string]
vpcNatGwKeyMutex keymutex.KeyMutex
vpcEgressGatewayLister kubeovnlister.VpcEgressGatewayLister
vpcEgressGatewaySynced cache.InformerSynced
addOrUpdateVpcEgressGatewayQueue workqueue.TypedRateLimitingInterface[string]
delVpcEgressGatewayQueue workqueue.TypedRateLimitingInterface[string]
vpcEgressGatewayKeyMutex keymutex.KeyMutex
switchLBRuleLister kubeovnlister.SwitchLBRuleLister
switchLBRuleSynced cache.InformerSynced
addSwitchLBRuleQueue workqueue.TypedRateLimitingInterface[string]
updateSwitchLBRuleQueue workqueue.TypedRateLimitingInterface[*SlrInfo]
delSwitchLBRuleQueue workqueue.TypedRateLimitingInterface[*SlrInfo]
vpcDNSLister kubeovnlister.VpcDnsLister
vpcDNSSynced cache.InformerSynced
addOrUpdateVpcDNSQueue workqueue.TypedRateLimitingInterface[string]
delVpcDNSQueue workqueue.TypedRateLimitingInterface[string]
subnetsLister kubeovnlister.SubnetLister
subnetSynced cache.InformerSynced
addOrUpdateSubnetQueue workqueue.TypedRateLimitingInterface[string]
deleteSubnetQueue workqueue.TypedRateLimitingInterface[*kubeovnv1.Subnet]
updateSubnetStatusQueue workqueue.TypedRateLimitingInterface[string]
syncVirtualPortsQueue workqueue.TypedRateLimitingInterface[string]
subnetKeyMutex keymutex.KeyMutex
ippoolLister kubeovnlister.IPPoolLister
ippoolSynced cache.InformerSynced
addOrUpdateIPPoolQueue workqueue.TypedRateLimitingInterface[string]
updateIPPoolStatusQueue workqueue.TypedRateLimitingInterface[string]
deleteIPPoolQueue workqueue.TypedRateLimitingInterface[*kubeovnv1.IPPool]
ippoolKeyMutex keymutex.KeyMutex
ipsLister kubeovnlister.IPLister
ipSynced cache.InformerSynced
addIPQueue workqueue.TypedRateLimitingInterface[string]
updateIPQueue workqueue.TypedRateLimitingInterface[string]
delIPQueue workqueue.TypedRateLimitingInterface[*kubeovnv1.IP]
virtualIpsLister kubeovnlister.VipLister
virtualIpsSynced cache.InformerSynced
addVirtualIPQueue workqueue.TypedRateLimitingInterface[string]
updateVirtualIPQueue workqueue.TypedRateLimitingInterface[string]
updateVirtualParentsQueue workqueue.TypedRateLimitingInterface[string]
delVirtualIPQueue workqueue.TypedRateLimitingInterface[*kubeovnv1.Vip]
iptablesEipsLister kubeovnlister.IptablesEIPLister
iptablesEipSynced cache.InformerSynced
addIptablesEipQueue workqueue.TypedRateLimitingInterface[string]
updateIptablesEipQueue workqueue.TypedRateLimitingInterface[string]
resetIptablesEipQueue workqueue.TypedRateLimitingInterface[string]
delIptablesEipQueue workqueue.TypedRateLimitingInterface[string]
iptablesFipsLister kubeovnlister.IptablesFIPRuleLister
iptablesFipSynced cache.InformerSynced
addIptablesFipQueue workqueue.TypedRateLimitingInterface[string]
updateIptablesFipQueue workqueue.TypedRateLimitingInterface[string]
delIptablesFipQueue workqueue.TypedRateLimitingInterface[string]
iptablesDnatRulesLister kubeovnlister.IptablesDnatRuleLister
iptablesDnatRuleSynced cache.InformerSynced
addIptablesDnatRuleQueue workqueue.TypedRateLimitingInterface[string]
updateIptablesDnatRuleQueue workqueue.TypedRateLimitingInterface[string]
delIptablesDnatRuleQueue workqueue.TypedRateLimitingInterface[string]
iptablesSnatRulesLister kubeovnlister.IptablesSnatRuleLister
iptablesSnatRuleSynced cache.InformerSynced
addIptablesSnatRuleQueue workqueue.TypedRateLimitingInterface[string]
updateIptablesSnatRuleQueue workqueue.TypedRateLimitingInterface[string]
delIptablesSnatRuleQueue workqueue.TypedRateLimitingInterface[string]
ovnEipsLister kubeovnlister.OvnEipLister
ovnEipSynced cache.InformerSynced
addOvnEipQueue workqueue.TypedRateLimitingInterface[string]
updateOvnEipQueue workqueue.TypedRateLimitingInterface[string]
resetOvnEipQueue workqueue.TypedRateLimitingInterface[string]
delOvnEipQueue workqueue.TypedRateLimitingInterface[string]
ovnFipsLister kubeovnlister.OvnFipLister
ovnFipSynced cache.InformerSynced
addOvnFipQueue workqueue.TypedRateLimitingInterface[string]
updateOvnFipQueue workqueue.TypedRateLimitingInterface[string]
delOvnFipQueue workqueue.TypedRateLimitingInterface[string]
ovnSnatRulesLister kubeovnlister.OvnSnatRuleLister
ovnSnatRuleSynced cache.InformerSynced
addOvnSnatRuleQueue workqueue.TypedRateLimitingInterface[string]
updateOvnSnatRuleQueue workqueue.TypedRateLimitingInterface[string]
delOvnSnatRuleQueue workqueue.TypedRateLimitingInterface[string]
ovnDnatRulesLister kubeovnlister.OvnDnatRuleLister
ovnDnatRuleSynced cache.InformerSynced
addOvnDnatRuleQueue workqueue.TypedRateLimitingInterface[string]
updateOvnDnatRuleQueue workqueue.TypedRateLimitingInterface[string]
delOvnDnatRuleQueue workqueue.TypedRateLimitingInterface[string]
providerNetworksLister kubeovnlister.ProviderNetworkLister
providerNetworkSynced cache.InformerSynced
vlansLister kubeovnlister.VlanLister
vlanSynced cache.InformerSynced
addVlanQueue workqueue.TypedRateLimitingInterface[string]
delVlanQueue workqueue.TypedRateLimitingInterface[string]
updateVlanQueue workqueue.TypedRateLimitingInterface[string]
vlanKeyMutex keymutex.KeyMutex
namespacesLister v1.NamespaceLister
namespacesSynced cache.InformerSynced
addNamespaceQueue workqueue.TypedRateLimitingInterface[string]
nsKeyMutex keymutex.KeyMutex
nodesLister v1.NodeLister
nodesSynced cache.InformerSynced
addNodeQueue workqueue.TypedRateLimitingInterface[string]
updateNodeQueue workqueue.TypedRateLimitingInterface[string]
deleteNodeQueue workqueue.TypedRateLimitingInterface[string]
nodeKeyMutex keymutex.KeyMutex
servicesLister v1.ServiceLister
serviceSynced cache.InformerSynced
addServiceQueue workqueue.TypedRateLimitingInterface[string]
deleteServiceQueue workqueue.TypedRateLimitingInterface[*vpcService]
updateServiceQueue workqueue.TypedRateLimitingInterface[*updateSvcObject]
svcKeyMutex keymutex.KeyMutex
endpointsLister v1.EndpointsLister
endpointsSynced cache.InformerSynced
addOrUpdateEndpointQueue workqueue.TypedRateLimitingInterface[string]
epKeyMutex keymutex.KeyMutex
deploymentsLister appsv1.DeploymentLister
deploymentsSynced cache.InformerSynced
npsLister netv1.NetworkPolicyLister
npsSynced cache.InformerSynced
updateNpQueue workqueue.TypedRateLimitingInterface[string]
deleteNpQueue workqueue.TypedRateLimitingInterface[string]
npKeyMutex keymutex.KeyMutex
sgsLister kubeovnlister.SecurityGroupLister
sgSynced cache.InformerSynced
addOrUpdateSgQueue workqueue.TypedRateLimitingInterface[string]
delSgQueue workqueue.TypedRateLimitingInterface[string]
syncSgPortsQueue workqueue.TypedRateLimitingInterface[string]
sgKeyMutex keymutex.KeyMutex
qosPoliciesLister kubeovnlister.QoSPolicyLister
qosPolicySynced cache.InformerSynced
addQoSPolicyQueue workqueue.TypedRateLimitingInterface[string]
updateQoSPolicyQueue workqueue.TypedRateLimitingInterface[string]
delQoSPolicyQueue workqueue.TypedRateLimitingInterface[string]
configMapsLister v1.ConfigMapLister
configMapsSynced cache.InformerSynced
anpsLister anplister.AdminNetworkPolicyLister
anpsSynced cache.InformerSynced
addAnpQueue workqueue.TypedRateLimitingInterface[string]
updateAnpQueue workqueue.TypedRateLimitingInterface[*AdminNetworkPolicyChangedDelta]
deleteAnpQueue workqueue.TypedRateLimitingInterface[*v1alpha1.AdminNetworkPolicy]
anpKeyMutex keymutex.KeyMutex
banpsLister anplister.BaselineAdminNetworkPolicyLister
banpsSynced cache.InformerSynced
addBanpQueue workqueue.TypedRateLimitingInterface[string]
updateBanpQueue workqueue.TypedRateLimitingInterface[*AdminNetworkPolicyChangedDelta]
deleteBanpQueue workqueue.TypedRateLimitingInterface[*v1alpha1.BaselineAdminNetworkPolicy]
banpKeyMutex keymutex.KeyMutex
csrLister certListerv1.CertificateSigningRequestLister
csrSynced cache.InformerSynced
addOrUpdateCsrQueue workqueue.TypedRateLimitingInterface[string]
vmiMigrationSynced cache.InformerSynced
addOrUpdateVMIMigrationQueue workqueue.TypedRateLimitingInterface[string]
kubevirtInformerFactory kubevirtController.KubeInformerFactory
recorder record.EventRecorder
informerFactory kubeinformers.SharedInformerFactory
cmInformerFactory kubeinformers.SharedInformerFactory
deployInformerFactory kubeinformers.SharedInformerFactory
kubeovnInformerFactory kubeovninformer.SharedInformerFactory
anpInformerFactory anpinformer.SharedInformerFactory
}
func newTypedRateLimitingQueue[T comparable](name string, rateLimiter workqueue.TypedRateLimiter[T]) workqueue.TypedRateLimitingInterface[T] {
if rateLimiter == nil {
rateLimiter = workqueue.DefaultTypedControllerRateLimiter[T]()
}
return workqueue.NewTypedRateLimitingQueueWithConfig(rateLimiter, workqueue.TypedRateLimitingQueueConfig[T]{Name: name})
}
// Run creates and runs a new ovn controller
func Run(ctx context.Context, config *Configuration) {
klog.V(4).Info("Creating event broadcaster")
eventBroadcaster := record.NewBroadcasterWithCorrelatorOptions(record.CorrelatorOptions{BurstSize: 100})
eventBroadcaster.StartLogging(klog.Infof)
eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: config.KubeFactoryClient.CoreV1().Events("")})
recorder := eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: controllerAgentName})
custCrdRateLimiter := workqueue.NewTypedMaxOfRateLimiter(
workqueue.NewTypedItemExponentialFailureRateLimiter[string](time.Duration(config.CustCrdRetryMinDelay)*time.Second, time.Duration(config.CustCrdRetryMaxDelay)*time.Second),
&workqueue.TypedBucketRateLimiter[string]{Limiter: rate.NewLimiter(rate.Limit(10), 100)},
)
selector, err := labels.Parse(util.VpcEgressGatewayLabel)
if err != nil {
util.LogFatalAndExit(err, "failed to create label selector for vpc egress gateway workload")
}
informerFactory := kubeinformers.NewSharedInformerFactoryWithOptions(config.KubeFactoryClient, 0,
kubeinformers.WithTweakListOptions(func(listOption *metav1.ListOptions) {
listOption.AllowWatchBookmarks = true
}))
cmInformerFactory := kubeinformers.NewSharedInformerFactoryWithOptions(config.KubeFactoryClient, 0,
kubeinformers.WithTweakListOptions(func(listOption *metav1.ListOptions) {
listOption.AllowWatchBookmarks = true
}), kubeinformers.WithNamespace(config.PodNamespace))
// deployment informer used to list/watch vpc egress gateway workloads
deployInformerFactory := kubeinformers.NewSharedInformerFactoryWithOptions(config.KubeFactoryClient, 0,
kubeinformers.WithTweakListOptions(func(listOption *metav1.ListOptions) {
listOption.AllowWatchBookmarks = true
listOption.LabelSelector = selector.String()
}))
kubeovnInformerFactory := kubeovninformer.NewSharedInformerFactoryWithOptions(config.KubeOvnFactoryClient, 0,
kubeovninformer.WithTweakListOptions(func(listOption *metav1.ListOptions) {
listOption.AllowWatchBookmarks = true
}))
anpInformerFactory := anpinformer.NewSharedInformerFactoryWithOptions(config.AnpClient, 0,
anpinformer.WithTweakListOptions(func(listOption *metav1.ListOptions) {
listOption.AllowWatchBookmarks = true
}))
kubevirtInformerFactory := kubevirtController.NewKubeInformerFactory(config.KubevirtClient.RestClient(), config.KubevirtClient, nil, util.KubevirtNamespace)
vpcInformer := kubeovnInformerFactory.Kubeovn().V1().Vpcs()
vpcNatGatewayInformer := kubeovnInformerFactory.Kubeovn().V1().VpcNatGateways()
vpcEgressGatewayInformer := kubeovnInformerFactory.Kubeovn().V1().VpcEgressGateways()
subnetInformer := kubeovnInformerFactory.Kubeovn().V1().Subnets()
ippoolInformer := kubeovnInformerFactory.Kubeovn().V1().IPPools()
ipInformer := kubeovnInformerFactory.Kubeovn().V1().IPs()
virtualIPInformer := kubeovnInformerFactory.Kubeovn().V1().Vips()
iptablesEipInformer := kubeovnInformerFactory.Kubeovn().V1().IptablesEIPs()
iptablesFipInformer := kubeovnInformerFactory.Kubeovn().V1().IptablesFIPRules()
iptablesDnatRuleInformer := kubeovnInformerFactory.Kubeovn().V1().IptablesDnatRules()
iptablesSnatRuleInformer := kubeovnInformerFactory.Kubeovn().V1().IptablesSnatRules()
vlanInformer := kubeovnInformerFactory.Kubeovn().V1().Vlans()
providerNetworkInformer := kubeovnInformerFactory.Kubeovn().V1().ProviderNetworks()
sgInformer := kubeovnInformerFactory.Kubeovn().V1().SecurityGroups()
podInformer := informerFactory.Core().V1().Pods()
namespaceInformer := informerFactory.Core().V1().Namespaces()
nodeInformer := informerFactory.Core().V1().Nodes()
serviceInformer := informerFactory.Core().V1().Services()
endpointInformer := informerFactory.Core().V1().Endpoints()
deploymentInformer := deployInformerFactory.Apps().V1().Deployments()
qosPolicyInformer := kubeovnInformerFactory.Kubeovn().V1().QoSPolicies()
configMapInformer := cmInformerFactory.Core().V1().ConfigMaps()
npInformer := informerFactory.Networking().V1().NetworkPolicies()
switchLBRuleInformer := kubeovnInformerFactory.Kubeovn().V1().SwitchLBRules()
vpcDNSInformer := kubeovnInformerFactory.Kubeovn().V1().VpcDnses()
ovnEipInformer := kubeovnInformerFactory.Kubeovn().V1().OvnEips()
ovnFipInformer := kubeovnInformerFactory.Kubeovn().V1().OvnFips()
ovnSnatRuleInformer := kubeovnInformerFactory.Kubeovn().V1().OvnSnatRules()
ovnDnatRuleInformer := kubeovnInformerFactory.Kubeovn().V1().OvnDnatRules()
anpInformer := anpInformerFactory.Policy().V1alpha1().AdminNetworkPolicies()
banpInformer := anpInformerFactory.Policy().V1alpha1().BaselineAdminNetworkPolicies()
csrInformer := informerFactory.Certificates().V1().CertificateSigningRequests()
vmiMigrationInformer := kubevirtInformerFactory.VirtualMachineInstanceMigration()
numKeyLocks := runtime.NumCPU() * 2
if numKeyLocks < config.WorkerNum*2 {
numKeyLocks = config.WorkerNum * 2
}
controller := &Controller{
config: config,
deletingPodObjMap: xsync.NewMapOf[string, *corev1.Pod](),
deletingNodeObjMap: xsync.NewMapOf[string, *corev1.Node](),
ipam: ovnipam.NewIPAM(),
namedPort: NewNamedPort(),
vpcsLister: vpcInformer.Lister(),
vpcSynced: vpcInformer.Informer().HasSynced,
addOrUpdateVpcQueue: newTypedRateLimitingQueue[string]("AddOrUpdateVpc", nil),
delVpcQueue: newTypedRateLimitingQueue[*kubeovnv1.Vpc]("DeleteVpc", nil),
updateVpcStatusQueue: newTypedRateLimitingQueue[string]("UpdateVpcStatus", nil),
vpcKeyMutex: keymutex.NewHashed(numKeyLocks),
vpcNatGatewayLister: vpcNatGatewayInformer.Lister(),
vpcNatGatewaySynced: vpcNatGatewayInformer.Informer().HasSynced,
addOrUpdateVpcNatGatewayQueue: newTypedRateLimitingQueue("AddOrUpdateVpcNatGw", custCrdRateLimiter),
initVpcNatGatewayQueue: newTypedRateLimitingQueue("InitVpcNatGw", custCrdRateLimiter),
delVpcNatGatewayQueue: newTypedRateLimitingQueue("DeleteVpcNatGw", custCrdRateLimiter),
updateVpcEipQueue: newTypedRateLimitingQueue("UpdateVpcEip", custCrdRateLimiter),
updateVpcFloatingIPQueue: newTypedRateLimitingQueue("UpdateVpcFloatingIp", custCrdRateLimiter),
updateVpcDnatQueue: newTypedRateLimitingQueue("UpdateVpcDnat", custCrdRateLimiter),
updateVpcSnatQueue: newTypedRateLimitingQueue("UpdateVpcSnat", custCrdRateLimiter),
updateVpcSubnetQueue: newTypedRateLimitingQueue("UpdateVpcSubnet", custCrdRateLimiter),
vpcNatGwKeyMutex: keymutex.NewHashed(numKeyLocks),
vpcEgressGatewayLister: vpcEgressGatewayInformer.Lister(),
vpcEgressGatewaySynced: vpcEgressGatewayInformer.Informer().HasSynced,
addOrUpdateVpcEgressGatewayQueue: newTypedRateLimitingQueue("AddOrUpdateVpcEgressGateway", custCrdRateLimiter),
delVpcEgressGatewayQueue: newTypedRateLimitingQueue("DeleteVpcEgressGateway", custCrdRateLimiter),
vpcEgressGatewayKeyMutex: keymutex.NewHashed(numKeyLocks),
subnetsLister: subnetInformer.Lister(),
subnetSynced: subnetInformer.Informer().HasSynced,
addOrUpdateSubnetQueue: newTypedRateLimitingQueue[string]("AddSubnet", nil),
deleteSubnetQueue: newTypedRateLimitingQueue[*kubeovnv1.Subnet]("DeleteSubnet", nil),
updateSubnetStatusQueue: newTypedRateLimitingQueue[string]("UpdateSubnetStatus", nil),
syncVirtualPortsQueue: newTypedRateLimitingQueue[string]("SyncVirtualPort", nil),
subnetKeyMutex: keymutex.NewHashed(numKeyLocks),
ippoolLister: ippoolInformer.Lister(),
ippoolSynced: ippoolInformer.Informer().HasSynced,
addOrUpdateIPPoolQueue: newTypedRateLimitingQueue[string]("AddIPPool", nil),
updateIPPoolStatusQueue: newTypedRateLimitingQueue[string]("UpdateIPPoolStatus", nil),
deleteIPPoolQueue: newTypedRateLimitingQueue[*kubeovnv1.IPPool]("DeleteIPPool", nil),
ippoolKeyMutex: keymutex.NewHashed(numKeyLocks),
ipsLister: ipInformer.Lister(),
ipSynced: ipInformer.Informer().HasSynced,
addIPQueue: newTypedRateLimitingQueue[string]("AddIP", nil),
updateIPQueue: newTypedRateLimitingQueue[string]("UpdateIP", nil),
delIPQueue: newTypedRateLimitingQueue[*kubeovnv1.IP]("DeleteIP", nil),
virtualIpsLister: virtualIPInformer.Lister(),
virtualIpsSynced: virtualIPInformer.Informer().HasSynced,
addVirtualIPQueue: newTypedRateLimitingQueue[string]("AddVirtualIP", nil),
updateVirtualIPQueue: newTypedRateLimitingQueue[string]("UpdateVirtualIP", nil),
updateVirtualParentsQueue: newTypedRateLimitingQueue[string]("UpdateVirtualParents", nil),
delVirtualIPQueue: newTypedRateLimitingQueue[*kubeovnv1.Vip]("DeleteVirtualIP", nil),
iptablesEipsLister: iptablesEipInformer.Lister(),
iptablesEipSynced: iptablesEipInformer.Informer().HasSynced,
addIptablesEipQueue: newTypedRateLimitingQueue("AddIptablesEip", custCrdRateLimiter),
updateIptablesEipQueue: newTypedRateLimitingQueue("UpdateIptablesEip", custCrdRateLimiter),
resetIptablesEipQueue: newTypedRateLimitingQueue("ResetIptablesEip", custCrdRateLimiter),
delIptablesEipQueue: newTypedRateLimitingQueue("DeleteIptablesEip", custCrdRateLimiter),
iptablesFipsLister: iptablesFipInformer.Lister(),
iptablesFipSynced: iptablesFipInformer.Informer().HasSynced,
addIptablesFipQueue: newTypedRateLimitingQueue("AddIptablesFip", custCrdRateLimiter),
updateIptablesFipQueue: newTypedRateLimitingQueue("UpdateIptablesFip", custCrdRateLimiter),
delIptablesFipQueue: newTypedRateLimitingQueue("DeleteIptablesFip", custCrdRateLimiter),
iptablesDnatRulesLister: iptablesDnatRuleInformer.Lister(),
iptablesDnatRuleSynced: iptablesDnatRuleInformer.Informer().HasSynced,
addIptablesDnatRuleQueue: newTypedRateLimitingQueue("AddIptablesDnatRule", custCrdRateLimiter),
updateIptablesDnatRuleQueue: newTypedRateLimitingQueue("UpdateIptablesDnatRule", custCrdRateLimiter),
delIptablesDnatRuleQueue: newTypedRateLimitingQueue("DeleteIptablesDnatRule", custCrdRateLimiter),
iptablesSnatRulesLister: iptablesSnatRuleInformer.Lister(),
iptablesSnatRuleSynced: iptablesSnatRuleInformer.Informer().HasSynced,
addIptablesSnatRuleQueue: newTypedRateLimitingQueue("AddIptablesSnatRule", custCrdRateLimiter),
updateIptablesSnatRuleQueue: newTypedRateLimitingQueue("UpdateIptablesSnatRule", custCrdRateLimiter),
delIptablesSnatRuleQueue: newTypedRateLimitingQueue("DeleteIptablesSnatRule", custCrdRateLimiter),
vlansLister: vlanInformer.Lister(),
vlanSynced: vlanInformer.Informer().HasSynced,
addVlanQueue: newTypedRateLimitingQueue[string]("AddVlan", nil),
delVlanQueue: newTypedRateLimitingQueue[string]("DeleteVlan", nil),
updateVlanQueue: newTypedRateLimitingQueue[string]("UpdateVlan", nil),
vlanKeyMutex: keymutex.NewHashed(numKeyLocks),
providerNetworksLister: providerNetworkInformer.Lister(),
providerNetworkSynced: providerNetworkInformer.Informer().HasSynced,
podsLister: podInformer.Lister(),
podsSynced: podInformer.Informer().HasSynced,
addOrUpdatePodQueue: newTypedRateLimitingQueue[string]("AddOrUpdatePod", nil),
deletePodQueue: workqueue.NewTypedRateLimitingQueueWithConfig(
workqueue.DefaultTypedControllerRateLimiter[string](),
workqueue.TypedRateLimitingQueueConfig[string]{
Name: "DeletePod",
DelayingQueue: workqueue.NewTypedDelayingQueue[string](),
},
),
updatePodSecurityQueue: newTypedRateLimitingQueue[string]("UpdatePodSecurity", nil),
podKeyMutex: keymutex.NewHashed(numKeyLocks),
namespacesLister: namespaceInformer.Lister(),
namespacesSynced: namespaceInformer.Informer().HasSynced,
addNamespaceQueue: newTypedRateLimitingQueue[string]("AddNamespace", nil),
nsKeyMutex: keymutex.NewHashed(numKeyLocks),
nodesLister: nodeInformer.Lister(),
nodesSynced: nodeInformer.Informer().HasSynced,
addNodeQueue: newTypedRateLimitingQueue[string]("AddNode", nil),
updateNodeQueue: newTypedRateLimitingQueue[string]("UpdateNode", nil),
deleteNodeQueue: newTypedRateLimitingQueue[string]("DeleteNode", nil),
nodeKeyMutex: keymutex.NewHashed(numKeyLocks),
servicesLister: serviceInformer.Lister(),
serviceSynced: serviceInformer.Informer().HasSynced,
addServiceQueue: newTypedRateLimitingQueue[string]("AddService", nil),
deleteServiceQueue: newTypedRateLimitingQueue[*vpcService]("DeleteService", nil),
updateServiceQueue: newTypedRateLimitingQueue[*updateSvcObject]("UpdateService", nil),
svcKeyMutex: keymutex.NewHashed(numKeyLocks),
endpointsLister: endpointInformer.Lister(),
endpointsSynced: endpointInformer.Informer().HasSynced,
addOrUpdateEndpointQueue: newTypedRateLimitingQueue[string]("UpdateEndpoint", nil),
epKeyMutex: keymutex.NewHashed(numKeyLocks),
deploymentsLister: deploymentInformer.Lister(),
deploymentsSynced: deploymentInformer.Informer().HasSynced,
qosPoliciesLister: qosPolicyInformer.Lister(),
qosPolicySynced: qosPolicyInformer.Informer().HasSynced,
addQoSPolicyQueue: newTypedRateLimitingQueue("AddQoSPolicy", custCrdRateLimiter),
updateQoSPolicyQueue: newTypedRateLimitingQueue("UpdateQoSPolicy", custCrdRateLimiter),
delQoSPolicyQueue: newTypedRateLimitingQueue("DeleteQoSPolicy", custCrdRateLimiter),
configMapsLister: configMapInformer.Lister(),
configMapsSynced: configMapInformer.Informer().HasSynced,
sgKeyMutex: keymutex.NewHashed(numKeyLocks),
sgsLister: sgInformer.Lister(),
sgSynced: sgInformer.Informer().HasSynced,
addOrUpdateSgQueue: newTypedRateLimitingQueue[string]("UpdateSecurityGroup", nil),
delSgQueue: newTypedRateLimitingQueue[string]("DeleteSecurityGroup", nil),
syncSgPortsQueue: newTypedRateLimitingQueue[string]("SyncSecurityGroupPorts", nil),
ovnEipsLister: ovnEipInformer.Lister(),
ovnEipSynced: ovnEipInformer.Informer().HasSynced,
addOvnEipQueue: newTypedRateLimitingQueue("AddOvnEip", custCrdRateLimiter),
updateOvnEipQueue: newTypedRateLimitingQueue("UpdateOvnEip", custCrdRateLimiter),
resetOvnEipQueue: newTypedRateLimitingQueue("ResetOvnEip", custCrdRateLimiter),
delOvnEipQueue: newTypedRateLimitingQueue("DeleteOvnEip", custCrdRateLimiter),
ovnFipsLister: ovnFipInformer.Lister(),
ovnFipSynced: ovnFipInformer.Informer().HasSynced,
addOvnFipQueue: newTypedRateLimitingQueue("AddOvnFip", custCrdRateLimiter),
updateOvnFipQueue: newTypedRateLimitingQueue("UpdateOvnFip", custCrdRateLimiter),
delOvnFipQueue: newTypedRateLimitingQueue("DeleteOvnFip", custCrdRateLimiter),
ovnSnatRulesLister: ovnSnatRuleInformer.Lister(),
ovnSnatRuleSynced: ovnSnatRuleInformer.Informer().HasSynced,
addOvnSnatRuleQueue: newTypedRateLimitingQueue("AddOvnSnatRule", custCrdRateLimiter),
updateOvnSnatRuleQueue: newTypedRateLimitingQueue("UpdateOvnSnatRule", custCrdRateLimiter),
delOvnSnatRuleQueue: newTypedRateLimitingQueue("DeleteOvnSnatRule", custCrdRateLimiter),
ovnDnatRulesLister: ovnDnatRuleInformer.Lister(),
ovnDnatRuleSynced: ovnDnatRuleInformer.Informer().HasSynced,
addOvnDnatRuleQueue: newTypedRateLimitingQueue("AddOvnDnatRule", custCrdRateLimiter),
updateOvnDnatRuleQueue: newTypedRateLimitingQueue("UpdateOvnDnatRule", custCrdRateLimiter),
delOvnDnatRuleQueue: newTypedRateLimitingQueue("DeleteOvnDnatRule", custCrdRateLimiter),
csrLister: csrInformer.Lister(),
csrSynced: csrInformer.Informer().HasSynced,
addOrUpdateCsrQueue: newTypedRateLimitingQueue[string]("AddOrUpdateCSR", custCrdRateLimiter),
vmiMigrationSynced: vmiMigrationInformer.HasSynced,
addOrUpdateVMIMigrationQueue: newTypedRateLimitingQueue[string]("AddOrUpdateVMIMigration", nil),
kubevirtInformerFactory: kubevirtInformerFactory,
recorder: recorder,
informerFactory: informerFactory,
cmInformerFactory: cmInformerFactory,
deployInformerFactory: deployInformerFactory,
kubeovnInformerFactory: kubeovnInformerFactory,
anpInformerFactory: anpInformerFactory,
}
if controller.OVNNbClient, err = ovs.NewOvnNbClient(
config.OvnNbAddr,
config.OvnTimeout,
config.OvsDbConnectTimeout,
config.OvsDbInactivityTimeout,
config.OvsDbConnectMaxRetry,
); err != nil {
util.LogFatalAndExit(err, "failed to create ovn nb client")
}
if controller.OVNSbClient, err = ovs.NewOvnSbClient(
config.OvnSbAddr,
config.OvnTimeout,
config.OvsDbConnectTimeout,
config.OvsDbInactivityTimeout,
config.OvsDbConnectMaxRetry,
); err != nil {
util.LogFatalAndExit(err, "failed to create ovn sb client")
}
if config.EnableLb {
controller.switchLBRuleLister = switchLBRuleInformer.Lister()
controller.switchLBRuleSynced = switchLBRuleInformer.Informer().HasSynced
controller.addSwitchLBRuleQueue = newTypedRateLimitingQueue("AddSwitchLBRule", custCrdRateLimiter)
controller.delSwitchLBRuleQueue = newTypedRateLimitingQueue(
"DeleteSwitchLBRule",
workqueue.NewTypedMaxOfRateLimiter(
workqueue.NewTypedItemExponentialFailureRateLimiter[*SlrInfo](time.Duration(config.CustCrdRetryMinDelay)*time.Second, time.Duration(config.CustCrdRetryMaxDelay)*time.Second),
&workqueue.TypedBucketRateLimiter[*SlrInfo]{Limiter: rate.NewLimiter(rate.Limit(10), 100)},
),
)
controller.updateSwitchLBRuleQueue = newTypedRateLimitingQueue(
"UpdateSwitchLBRule",
workqueue.NewTypedMaxOfRateLimiter(
workqueue.NewTypedItemExponentialFailureRateLimiter[*SlrInfo](time.Duration(config.CustCrdRetryMinDelay)*time.Second, time.Duration(config.CustCrdRetryMaxDelay)*time.Second),
&workqueue.TypedBucketRateLimiter[*SlrInfo]{Limiter: rate.NewLimiter(rate.Limit(10), 100)},
),
)
controller.vpcDNSLister = vpcDNSInformer.Lister()
controller.vpcDNSSynced = vpcDNSInformer.Informer().HasSynced
controller.addOrUpdateVpcDNSQueue = newTypedRateLimitingQueue("AddOrUpdateVpcDns", custCrdRateLimiter)
controller.delVpcDNSQueue = newTypedRateLimitingQueue("DeleteVpcDns", custCrdRateLimiter)
}
if config.EnableNP {
controller.npsLister = npInformer.Lister()
controller.npsSynced = npInformer.Informer().HasSynced
controller.updateNpQueue = newTypedRateLimitingQueue[string]("UpdateNetworkPolicy", nil)
controller.deleteNpQueue = newTypedRateLimitingQueue[string]("DeleteNetworkPolicy", nil)
controller.npKeyMutex = keymutex.NewHashed(numKeyLocks)
}
if config.EnableANP {
controller.anpsLister = anpInformer.Lister()
controller.anpsSynced = anpInformer.Informer().HasSynced
controller.addAnpQueue = newTypedRateLimitingQueue[string]("AddAdminNetworkPolicy", nil)
controller.updateAnpQueue = newTypedRateLimitingQueue[*AdminNetworkPolicyChangedDelta]("UpdateAdminNetworkPolicy", nil)
controller.deleteAnpQueue = newTypedRateLimitingQueue[*v1alpha1.AdminNetworkPolicy]("DeleteAdminNetworkPolicy", nil)
controller.anpKeyMutex = keymutex.NewHashed(numKeyLocks)
controller.banpsLister = banpInformer.Lister()
controller.banpsSynced = banpInformer.Informer().HasSynced
controller.addBanpQueue = newTypedRateLimitingQueue[string]("AddBaseAdminNetworkPolicy", nil)
controller.updateBanpQueue = newTypedRateLimitingQueue[*AdminNetworkPolicyChangedDelta]("UpdateBaseAdminNetworkPolicy", nil)
controller.deleteBanpQueue = newTypedRateLimitingQueue[*v1alpha1.BaselineAdminNetworkPolicy]("DeleteBaseAdminNetworkPolicy", nil)
controller.banpKeyMutex = keymutex.NewHashed(numKeyLocks)
}
defer controller.shutdown()
klog.Info("Starting OVN controller")
// Wait for the caches to be synced before starting workers
controller.informerFactory.Start(ctx.Done())
controller.cmInformerFactory.Start(ctx.Done())
controller.deployInformerFactory.Start(ctx.Done())
controller.kubeovnInformerFactory.Start(ctx.Done())
controller.anpInformerFactory.Start(ctx.Done())
klog.Info("Waiting for informer caches to sync")
cacheSyncs := []cache.InformerSynced{
controller.vpcNatGatewaySynced, controller.vpcEgressGatewaySynced,
controller.vpcSynced, controller.subnetSynced,
controller.ipSynced, controller.virtualIpsSynced, controller.iptablesEipSynced,
controller.iptablesFipSynced, controller.iptablesDnatRuleSynced, controller.iptablesSnatRuleSynced,
controller.vlanSynced, controller.podsSynced, controller.namespacesSynced, controller.nodesSynced,
controller.serviceSynced, controller.endpointsSynced, controller.deploymentsSynced, controller.configMapsSynced,
controller.ovnEipSynced, controller.ovnFipSynced, controller.ovnSnatRuleSynced,
controller.ovnDnatRuleSynced,
}
if controller.config.EnableLb {
cacheSyncs = append(cacheSyncs, controller.switchLBRuleSynced, controller.vpcDNSSynced)
}
if controller.config.EnableNP {
cacheSyncs = append(cacheSyncs, controller.npsSynced)
}
if controller.config.EnableANP {
cacheSyncs = append(cacheSyncs, controller.anpsSynced, controller.banpsSynced)
}
if !cache.WaitForCacheSync(ctx.Done(), cacheSyncs...) {
util.LogFatalAndExit(nil, "failed to wait for caches to sync")
}
if _, err = podInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddPod,
DeleteFunc: controller.enqueueDeletePod,
UpdateFunc: controller.enqueueUpdatePod,
}); err != nil {
util.LogFatalAndExit(err, "failed to add pod event handler")
}
if _, err = namespaceInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddNamespace,
UpdateFunc: controller.enqueueUpdateNamespace,
DeleteFunc: controller.enqueueDeleteNamespace,
}); err != nil {
util.LogFatalAndExit(err, "failed to add namespace event handler")
}
if _, err = nodeInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddNode,
UpdateFunc: controller.enqueueUpdateNode,
DeleteFunc: controller.enqueueDeleteNode,
}); err != nil {
util.LogFatalAndExit(err, "failed to add node event handler")
}
if _, err = serviceInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddService,
DeleteFunc: controller.enqueueDeleteService,
UpdateFunc: controller.enqueueUpdateService,
}); err != nil {
util.LogFatalAndExit(err, "failed to add service event handler")
}
if _, err = endpointInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddEndpoint,
UpdateFunc: controller.enqueueUpdateEndpoint,
}); err != nil {
util.LogFatalAndExit(err, "failed to add endpoint event handler")
}
if _, err = deploymentInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddDeployment,
UpdateFunc: controller.enqueueUpdateDeployment,
}); err != nil {
util.LogFatalAndExit(err, "failed to add deployment event handler")
}
if _, err = vpcInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddVpc,
UpdateFunc: controller.enqueueUpdateVpc,
DeleteFunc: controller.enqueueDelVpc,
}); err != nil {
util.LogFatalAndExit(err, "failed to add vpc event handler")
}
if _, err = vpcNatGatewayInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddVpcNatGw,
UpdateFunc: controller.enqueueUpdateVpcNatGw,
DeleteFunc: controller.enqueueDeleteVpcNatGw,
}); err != nil {
util.LogFatalAndExit(err, "failed to add vpc nat gateway event handler")
}
if _, err = vpcEgressGatewayInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddVpcEgressGateway,
UpdateFunc: controller.enqueueUpdateVpcEgressGateway,
DeleteFunc: controller.enqueueDeleteVpcEgressGateway,
}); err != nil {
util.LogFatalAndExit(err, "failed to add vpc egress gateway event handler")
}
if _, err = subnetInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddSubnet,
UpdateFunc: controller.enqueueUpdateSubnet,
DeleteFunc: controller.enqueueDeleteSubnet,
}); err != nil {
util.LogFatalAndExit(err, "failed to add subnet event handler")
}
if _, err = ippoolInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddIPPool,
UpdateFunc: controller.enqueueUpdateIPPool,
DeleteFunc: controller.enqueueDeleteIPPool,
}); err != nil {
util.LogFatalAndExit(err, "failed to add ippool event handler")
}
if _, err = ipInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddIP,
UpdateFunc: controller.enqueueUpdateIP,
DeleteFunc: controller.enqueueDelIP,
}); err != nil {
util.LogFatalAndExit(err, "failed to add ips event handler")
}
if _, err = vlanInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddVlan,
DeleteFunc: controller.enqueueDelVlan,
UpdateFunc: controller.enqueueUpdateVlan,
}); err != nil {
util.LogFatalAndExit(err, "failed to add vlan event handler")
}
if _, err = sgInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddSg,
DeleteFunc: controller.enqueueDeleteSg,
UpdateFunc: controller.enqueueUpdateSg,
}); err != nil {
util.LogFatalAndExit(err, "failed to add security group event handler")
}
if _, err = virtualIPInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddVirtualIP,
UpdateFunc: controller.enqueueUpdateVirtualIP,
DeleteFunc: controller.enqueueDelVirtualIP,
}); err != nil {
util.LogFatalAndExit(err, "failed to add virtual ip event handler")
}
if _, err = iptablesEipInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddIptablesEip,
UpdateFunc: controller.enqueueUpdateIptablesEip,
DeleteFunc: controller.enqueueDelIptablesEip,
}); err != nil {
util.LogFatalAndExit(err, "failed to add iptables eip event handler")
}
if _, err = iptablesFipInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddIptablesFip,
UpdateFunc: controller.enqueueUpdateIptablesFip,
DeleteFunc: controller.enqueueDelIptablesFip,
}); err != nil {
util.LogFatalAndExit(err, "failed to add iptables fip event handler")
}
if _, err = iptablesDnatRuleInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddIptablesDnatRule,
UpdateFunc: controller.enqueueUpdateIptablesDnatRule,
DeleteFunc: controller.enqueueDelIptablesDnatRule,
}); err != nil {
util.LogFatalAndExit(err, "failed to add iptables dnat event handler")
}
if _, err = iptablesSnatRuleInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddIptablesSnatRule,
UpdateFunc: controller.enqueueUpdateIptablesSnatRule,
DeleteFunc: controller.enqueueDelIptablesSnatRule,
}); err != nil {
util.LogFatalAndExit(err, "failed to add iptables snat rule event handler")
}
if _, err = ovnEipInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddOvnEip,
UpdateFunc: controller.enqueueUpdateOvnEip,
DeleteFunc: controller.enqueueDelOvnEip,
}); err != nil {
util.LogFatalAndExit(err, "failed to add ovn eip event handler")
}
if _, err = ovnFipInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddOvnFip,
UpdateFunc: controller.enqueueUpdateOvnFip,
DeleteFunc: controller.enqueueDelOvnFip,
}); err != nil {
util.LogFatalAndExit(err, "failed to add ovn fip event handler")
}
if _, err = ovnSnatRuleInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddOvnSnatRule,
UpdateFunc: controller.enqueueUpdateOvnSnatRule,
DeleteFunc: controller.enqueueDelOvnSnatRule,
}); err != nil {
util.LogFatalAndExit(err, "failed to add ovn snat rule event handler")
}
if _, err = ovnDnatRuleInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddOvnDnatRule,
UpdateFunc: controller.enqueueUpdateOvnDnatRule,
DeleteFunc: controller.enqueueDelOvnDnatRule,
}); err != nil {
util.LogFatalAndExit(err, "failed to add ovn dnat rule event handler")
}
if _, err = qosPolicyInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddQoSPolicy,
UpdateFunc: controller.enqueueUpdateQoSPolicy,
DeleteFunc: controller.enqueueDelQoSPolicy,
}); err != nil {
util.LogFatalAndExit(err, "failed to add qos policy event handler")
}
if config.EnableLb {
if _, err = switchLBRuleInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddSwitchLBRule,
UpdateFunc: controller.enqueueUpdateSwitchLBRule,
DeleteFunc: controller.enqueueDeleteSwitchLBRule,
}); err != nil {
util.LogFatalAndExit(err, "failed to add switch lb rule event handler")
}
if _, err = vpcDNSInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddVpcDNS,
UpdateFunc: controller.enqueueUpdateVpcDNS,
DeleteFunc: controller.enqueueDeleteVPCDNS,
}); err != nil {
util.LogFatalAndExit(err, "failed to add vpc dns event handler")
}
}
if config.EnableNP {
if _, err = npInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddNp,
UpdateFunc: controller.enqueueUpdateNp,
DeleteFunc: controller.enqueueDeleteNp,
}); err != nil {
util.LogFatalAndExit(err, "failed to add network policy event handler")
}
}
if config.EnableANP {
if _, err = anpInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddAnp,
UpdateFunc: controller.enqueueUpdateAnp,
DeleteFunc: controller.enqueueDeleteAnp,
}); err != nil {
util.LogFatalAndExit(err, "failed to add admin network policy event handler")
}
if _, err = banpInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddBanp,
UpdateFunc: controller.enqueueUpdateBanp,
DeleteFunc: controller.enqueueDeleteBanp,
}); err != nil {
util.LogFatalAndExit(err, "failed to add baseline admin network policy event handler")
}
controller.anpPrioNameMap = make(map[int32]string, 100)
controller.anpNamePrioMap = make(map[string]int32, 100)
}
if config.EnableOVNIPSec {
if _, err = csrInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddCsr,
UpdateFunc: controller.enqueueUpdateCsr,
// no need to add delete func for csr
}); err != nil {
util.LogFatalAndExit(err, "failed to add csr event handler")
}
}
if config.EnableLiveMigrationOptimize {
if _, err = vmiMigrationInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddVMIMigration,
UpdateFunc: controller.enqueueUpdateVMIMigration,
}); err != nil {
util.LogFatalAndExit(err, "failed to add VMI Migration event handler")
}
controller.StartMigrationInformerFactory(ctx, kubevirtInformerFactory)
}
controller.Run(ctx)
}
// Run will set up the event handlers for types we are interested in, as well
// as syncing informer caches and starting workers. It will block until stopCh
// is closed, at which point it will shutdown the workqueue and wait for
// workers to finish processing their current work items.
func (c *Controller) Run(ctx context.Context) {
// The init process can only be placed here if the init process do really affect the normal process of controller, such as Nodes/Pods/Subnets...
// Otherwise, the init process should be placed after all workers have already started working
if err := c.OVNNbClient.SetLsDnatModDlDst(c.config.LsDnatModDlDst); err != nil {
util.LogFatalAndExit(err, "failed to set NB_Global option ls_dnat_mod_dl_dst")
}
if err := c.OVNNbClient.SetUseCtInvMatch(); err != nil {
util.LogFatalAndExit(err, "failed to set NB_Global option use_ct_inv_match to false")
}
if err := c.OVNNbClient.SetLsCtSkipDstLportIPs(c.config.LsCtSkipDstLportIPs); err != nil {
util.LogFatalAndExit(err, "failed to set NB_Global option ls_ct_skip_dst_lport_ips")
}
if err := c.OVNNbClient.SetNodeLocalDNSIP(strings.Join(c.config.NodeLocalDNSIPs, ",")); err != nil {
util.LogFatalAndExit(err, "failed to set NB_Global option node_local_dns_ip")
}
if err := c.OVNNbClient.SetOVNIPSec(c.config.EnableOVNIPSec); err != nil {
util.LogFatalAndExit(err, "failed to set NB_Global ipsec")
}
if err := c.InitOVN(); err != nil {
util.LogFatalAndExit(err, "failed to initialize ovn resources")
}
// sync ip crd before initIPAM since ip crd will be used to restore vm and statefulset pod in initIPAM
if err := c.syncIPCR(); err != nil {
util.LogFatalAndExit(err, "failed to sync crd ips")
}
if err := c.syncFinalizers(); err != nil {
util.LogFatalAndExit(err, "failed to initialize crd finalizers")
}
if err := c.InitIPAM(); err != nil {
util.LogFatalAndExit(err, "failed to initialize ipam")
}
if err := c.syncNodeRoutes(); err != nil {
util.LogFatalAndExit(err, "failed to initialize node routes")
}
if err := c.syncSubnetCR(); err != nil {
util.LogFatalAndExit(err, "failed to sync crd subnets")
}
if err := c.syncVlanCR(); err != nil {
util.LogFatalAndExit(err, "failed to sync crd vlans")
}
if c.config.EnableOVNIPSec {
if err := c.InitDefaultOVNIPsecCA(); err != nil {
util.LogFatalAndExit(err, "failed to init ovn ipsec CA")
}
}
// start workers to do all the network operations
c.startWorkers(ctx)
c.initResourceOnce()
<-ctx.Done()
klog.Info("Shutting down workers")
}
func (c *Controller) shutdown() {
utilruntime.HandleCrash()
c.addOrUpdatePodQueue.ShutDown()