-
Notifications
You must be signed in to change notification settings - Fork 456
/
Copy pathvpc_nat_gw_eip.go
885 lines (818 loc) · 25.3 KB
/
vpc_nat_gw_eip.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
package controller
import (
"context"
"errors"
"fmt"
"net"
"strings"
"time"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
"github.com/kubeovn/kube-ovn/pkg/ovs"
"github.com/kubeovn/kube-ovn/pkg/util"
)
func (c *Controller) enqueueAddIptablesEip(obj interface{}) {
key := cache.MetaObjectToName(obj.(*kubeovnv1.IptablesEIP)).String()
klog.Infof("enqueue add iptables eip %s", key)
c.addIptablesEipQueue.Add(key)
}
func (c *Controller) enqueueUpdateIptablesEip(oldObj, newObj interface{}) {
oldEip := oldObj.(*kubeovnv1.IptablesEIP)
newEip := newObj.(*kubeovnv1.IptablesEIP)
if !newEip.DeletionTimestamp.IsZero() ||
oldEip.Status.Redo != newEip.Status.Redo ||
oldEip.Spec.QoSPolicy != newEip.Spec.QoSPolicy {
key := cache.MetaObjectToName(newEip).String()
klog.Infof("enqueue update iptables eip %s", key)
c.updateIptablesEipQueue.Add(key)
}
externalNetwork := util.GetExternalNetwork(newEip.Spec.ExternalSubnet)
c.updateSubnetStatusQueue.Add(externalNetwork)
}
func (c *Controller) enqueueDelIptablesEip(obj interface{}) {
eip := obj.(*kubeovnv1.IptablesEIP)
key := cache.MetaObjectToName(eip).String()
klog.Infof("enqueue del iptables eip %s", key)
c.delIptablesEipQueue.Add(key)
externalNetwork := util.GetExternalNetwork(eip.Spec.ExternalSubnet)
c.updateSubnetStatusQueue.Add(externalNetwork)
}
func (c *Controller) handleAddIptablesEip(key string) error {
cachedEip, err := c.iptablesEipsLister.Get(key)
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
klog.Error(err)
return err
}
if vpcNatEnabled != "true" {
return errors.New("iptables nat gw not enable")
}
c.vpcNatGwKeyMutex.LockKey(key)
defer func() { _ = c.vpcNatGwKeyMutex.UnlockKey(key) }()
klog.Infof("handle add iptables eip %s", key)
if cachedEip.Status.Ready && cachedEip.Status.IP != "" {
// already ok
return nil
}
subnets, err := c.subnetsLister.List(labels.Everything())
if err != nil {
klog.Errorf("failed to list subnets: %v", err)
return err
}
nadName := util.GetExternalNetwork(cachedEip.Spec.ExternalSubnet)
subnet, err := c.findSubnetByNetworkAttachmentDefinition(c.config.PodNamespace, nadName, subnets)
if err != nil {
klog.Error(err)
return err
}
// v6 ip address can not use upper case
if util.ContainsUppercase(cachedEip.Spec.V6ip) {
err := fmt.Errorf("eip %s v6 ip address %s can not contain upper case", cachedEip.Name, cachedEip.Spec.V6ip)
klog.Error(err)
return err
}
// make sure vpc nat gw pod is ready before eip allocation
if _, err := c.getNatGwPod(cachedEip.Spec.NatGwDp); err != nil {
klog.Error(err)
return err
}
var v4ip, v6ip, mac string
portName := ovs.PodNameToPortName(cachedEip.Name, cachedEip.Namespace, subnet.Spec.Provider)
if cachedEip.Spec.V4ip != "" {
if v4ip, v6ip, mac, err = c.acquireStaticEip(cachedEip.Name, cachedEip.Namespace, portName, cachedEip.Spec.V4ip, subnet.Name); err != nil {
klog.Errorf("failed to acquire static eip, err: %v", err)
return err
}
} else {
// Random allocate
if v4ip, v6ip, mac, err = c.acquireEip(cachedEip.Name, cachedEip.Namespace, portName, subnet.Name); err != nil {
klog.Errorf("failed to allocate eip, err: %v", err)
return err
}
}
eipV4Cidr, _ := util.SplitStringIP(subnet.Spec.CIDRBlock)
if v4ip == "" || eipV4Cidr == "" {
err = fmt.Errorf("subnet %s does not support ipv4", subnet.Name)
klog.Error(err)
return err
}
addrV4, err := util.GetIPAddrWithMask(v4ip, eipV4Cidr)
if err != nil {
err = fmt.Errorf("failed to get eip %s with mask by cidr %s: %w", v4ip, eipV4Cidr, err)
klog.Error(err)
return err
}
if err = c.createEipInPod(cachedEip.Spec.NatGwDp, addrV4); err != nil {
klog.Errorf("failed to create eip '%s' in pod, %v", key, err)
return err
}
if cachedEip.Spec.QoSPolicy != "" {
if err = c.addEipQoS(cachedEip, v4ip); err != nil {
klog.Errorf("failed to add qos '%s' in pod, %v", key, err)
return err
}
}
if err = c.createOrUpdateEipCR(key, v4ip, v6ip, mac, cachedEip.Spec.NatGwDp, cachedEip.Spec.QoSPolicy, subnet.Name); err != nil {
klog.Errorf("failed to update eip %s, %v", key, err)
return err
}
return nil
}
func (c *Controller) handleResetIptablesEip(key string) error {
if _, err := c.iptablesEipsLister.Get(key); err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
klog.Error(err)
return err
}
klog.Infof("handle reset eip %s", key)
if err := c.patchEipLabel(key); err != nil {
klog.Errorf("failed to patch label for eip %s, %v", key, err)
return err
}
if err := c.patchEipStatus(key, "", "", "", true); err != nil {
klog.Errorf("failed to reset nat for eip %s, %v", key, err)
return err
}
return nil
}
func (c *Controller) handleUpdateIptablesEip(key string) error {
cachedEip, err := c.iptablesEipsLister.Get(key)
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
klog.Error(err)
return err
}
c.vpcNatGwKeyMutex.LockKey(key)
defer func() { _ = c.vpcNatGwKeyMutex.UnlockKey(key) }()
klog.Infof("handle update iptables eip %s", key)
subnets, err := c.subnetsLister.List(labels.Everything())
if err != nil {
klog.Errorf("failed to list subnets: %v", err)
return err
}
nadName := util.GetExternalNetwork(cachedEip.Spec.ExternalSubnet)
subnet, err := c.findSubnetByNetworkAttachmentDefinition(c.config.PodNamespace, nadName, subnets)
if err != nil {
klog.Error(err)
return err
}
v4Cidr, _ := util.SplitStringIP(subnet.Spec.CIDRBlock)
if v4Cidr == "" {
err = fmt.Errorf("subnet %s does not support ipv4", subnet.Name)
klog.Error(err)
return err
}
if !cachedEip.DeletionTimestamp.IsZero() {
klog.Infof("clean eip %q in pod", key)
if vpcNatEnabled == "true" {
v4ipCidr, err := util.GetIPAddrWithMask(cachedEip.Status.IP, v4Cidr)
if err != nil {
err = fmt.Errorf("failed to get eip %s with mask by cidr %s: %w", cachedEip.Status.IP, v4Cidr, err)
klog.Error(err)
return err
}
if err = c.deleteEipInPod(cachedEip.Spec.NatGwDp, v4ipCidr); err != nil {
klog.Errorf("failed to clean eip '%s' in pod, %v", key, err)
return err
}
}
if cachedEip.Status.QoSPolicy != "" {
if err = c.delEipQoS(cachedEip, cachedEip.Status.IP); err != nil {
klog.Errorf("failed to del qos '%s' in pod, %v", key, err)
return err
}
}
if err = c.handleDelIptablesEipFinalizer(key); err != nil {
klog.Errorf("failed to handle del finalizer for eip %s, %v", key, err)
return err
}
c.ipam.ReleaseAddressByPod(key, cachedEip.Spec.ExternalSubnet)
return nil
}
klog.Infof("handle update eip %s", key)
// v6 ip address can not use upper case
if util.ContainsUppercase(cachedEip.Spec.V6ip) {
err := fmt.Errorf("eip %s v6 ip address %s can not contain upper case", cachedEip.Name, cachedEip.Spec.V6ip)
klog.Error(err)
return err
}
// eip change ip
if c.eipChangeIP(cachedEip) {
err := fmt.Errorf("not support eip change ip, old ip '%s', new ip '%s'", cachedEip.Status.IP, cachedEip.Spec.V4ip)
klog.Error(err)
return err
}
// make sure vpc nat enabled
if vpcNatEnabled != "true" {
err := errors.New("iptables nat gw not enable")
klog.Error(err)
return err
}
// update qos
if cachedEip.Status.QoSPolicy != cachedEip.Spec.QoSPolicy {
if cachedEip.Status.QoSPolicy != "" {
if err = c.delEipQoS(cachedEip, cachedEip.Status.IP); err != nil {
klog.Errorf("failed to del qos '%s' in pod, %v", key, err)
return err
}
}
if cachedEip.Spec.QoSPolicy != "" {
if err = c.addEipQoS(cachedEip, cachedEip.Status.IP); err != nil {
klog.Errorf("failed to add qos '%s' in pod, %v", key, err)
return err
}
}
if err = c.patchEipLabel(key); err != nil {
klog.Errorf("failed to label qos in eip, %v", err)
return err
}
if err = c.patchEipQoSStatus(key, cachedEip.Spec.QoSPolicy); err != nil {
klog.Errorf("failed to patch status for eip %s, %v", key, err)
return err
}
}
// redo
if !cachedEip.Status.Ready &&
cachedEip.Status.Redo != "" &&
cachedEip.Status.IP != "" &&
cachedEip.DeletionTimestamp.IsZero() {
gwPod, err := c.getNatGwPod(cachedEip.Spec.NatGwDp)
if err != nil {
klog.Error(err)
return err
}
// compare gw pod started time with eip redo time. if redo time before gw pod started. redo again
eipRedo, _ := time.ParseInLocation("2006-01-02T15:04:05", cachedEip.Status.Redo, time.Local)
if cachedEip.Status.Ready && cachedEip.Status.IP != "" && gwPod.Status.ContainerStatuses[0].State.Running.StartedAt.Before(&metav1.Time{Time: eipRedo}) {
// already ok
klog.V(3).Infof("eip %s already ok", key)
return nil
}
addrV4, err := util.GetIPAddrWithMask(cachedEip.Status.IP, v4Cidr)
if err != nil {
err = fmt.Errorf("failed to get eip %s with mask by cidr %s: %w", cachedEip.Status.IP, v4Cidr, err)
klog.Error(err)
return err
}
if err = c.createEipInPod(cachedEip.Spec.NatGwDp, addrV4); err != nil {
klog.Errorf("failed to create eip, %v", err)
return err
}
if cachedEip.Spec.QoSPolicy != "" {
if err = c.addEipQoS(cachedEip, cachedEip.Status.IP); err != nil {
klog.Errorf("failed to add qos '%s' in pod, %v", key, err)
return err
}
}
if err = c.patchEipStatus(key, "", "", cachedEip.Spec.QoSPolicy, true); err != nil {
klog.Errorf("failed to patch status for eip %s, %v", key, err)
return err
}
}
if err = c.handleAddIptablesEipFinalizer(key); err != nil {
klog.Errorf("failed to handle add finalizer for eip, %v", err)
return err
}
return nil
}
func (c *Controller) handleDelIptablesEip(key string) error {
klog.Infof("handle delete iptables eip %s", key)
return nil
}
func (c *Controller) GetEip(eipName string) (*kubeovnv1.IptablesEIP, error) {
cachedEip, err := c.iptablesEipsLister.Get(eipName)
if err != nil {
klog.Errorf("failed to get eip %s, %v", eipName, err)
return nil, err
}
if cachedEip.Status.IP == "" {
return nil, fmt.Errorf("eip '%s' is not ready, has no v4ip", eipName)
}
eip := cachedEip.DeepCopy()
return eip, nil
}
func (c *Controller) createEipInPod(dp, addrV4 string) error {
gwPod, err := c.getNatGwPod(dp)
if err != nil {
klog.Error(err)
return err
}
return c.execNatGwRules(gwPod, natGwEipAdd, []string{addrV4})
}
func (c *Controller) deleteEipInPod(dp, v4Cidr string) error {
gwPod, err := c.getNatGwPod(dp)
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
klog.Error(err)
return err
}
var delRules []string
rule := v4Cidr
delRules = append(delRules, rule)
if err = c.execNatGwRules(gwPod, natGwEipDel, delRules); err != nil {
klog.Error(err)
return err
}
return nil
}
func (c *Controller) addOrUpdateEIPBandwidthLimitRules(eip *kubeovnv1.IptablesEIP, v4ip string, rules kubeovnv1.QoSPolicyBandwidthLimitRules) error {
var err error
for _, rule := range rules {
if err = c.addEipQoSInPod(eip.Spec.NatGwDp, v4ip, rule.Direction, rule.Priority, rule.RateMax, rule.BurstMax); err != nil {
klog.Errorf("failed to set ingress eip '%s' qos in pod, %v", eip.Name, err)
return err
}
}
return nil
}
// add tc rule for eip in nat gw pod
func (c *Controller) addEipQoS(eip *kubeovnv1.IptablesEIP, v4ip string) error {
var err error
qosPolicy, err := c.qosPoliciesLister.Get(eip.Spec.QoSPolicy)
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
klog.Errorf("failed to get qos policy %s, %v", eip.Spec.QoSPolicy, err)
return err
}
if !qosPolicy.Status.Shared {
eips, err := c.iptablesEipsLister.List(
labels.SelectorFromSet(labels.Set{util.QoSLabel: qosPolicy.Name}))
if err != nil {
klog.Errorf("failed to get eip list, %v", err)
return err
}
if len(eips) != 0 {
if eips[0].Name != eip.Name {
err := fmt.Errorf("not support unshared qos policy %s to related to multiple eip", eip.Spec.QoSPolicy)
klog.Error(err)
return err
}
}
}
return c.addOrUpdateEIPBandwidthLimitRules(eip, v4ip, qosPolicy.Status.BandwidthLimitRules)
}
func (c *Controller) delEIPBandtithLimitRules(eip *kubeovnv1.IptablesEIP, v4ip string, rules kubeovnv1.QoSPolicyBandwidthLimitRules) error {
var err error
for _, rule := range rules {
// del qos
if err = c.delEipQoSInPod(eip.Spec.NatGwDp, v4ip, rule.Direction); err != nil {
klog.Errorf("failed to del egress eip '%s' qos in pod, %v", eip.Name, err)
return err
}
}
return nil
}
// del tc rule for eip in nat gw pod
func (c *Controller) delEipQoS(eip *kubeovnv1.IptablesEIP, v4ip string) error {
var err error
qosPolicy, err := c.qosPoliciesLister.Get(eip.Status.QoSPolicy)
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
klog.Errorf("failed to get qos policy %s, %v", eip.Status.QoSPolicy, err)
return err
}
return c.delEIPBandtithLimitRules(eip, v4ip, qosPolicy.Status.BandwidthLimitRules)
}
func (c *Controller) addEipQoSInPod(
dp, v4ip string, direction kubeovnv1.QoSPolicyRuleDirection, priority int, rate string,
burst string,
) error {
var operation string
gwPod, err := c.getNatGwPod(dp)
if err != nil {
klog.Error(err)
return err
}
var addRules []string
rule := fmt.Sprintf("%s,%d,%s,%s", v4ip, priority, rate, burst)
addRules = append(addRules, rule)
switch direction {
case kubeovnv1.QoSDirectionIngress:
operation = natGwEipIngressQoSAdd
case kubeovnv1.QoSDirectionEgress:
operation = natGwEipEgressQoSAdd
}
return c.execNatGwRules(gwPod, operation, addRules)
}
func (c *Controller) delEipQoSInPod(dp, v4ip string, direction kubeovnv1.QoSPolicyRuleDirection) error {
var operation string
gwPod, err := c.getNatGwPod(dp)
if err != nil {
klog.Error(err)
return err
}
var delRules []string
delRules = append(delRules, v4ip)
switch direction {
case kubeovnv1.QoSDirectionIngress:
operation = natGwEipIngressQoSDel
case kubeovnv1.QoSDirectionEgress:
operation = natGwEipEgressQoSDel
}
return c.execNatGwRules(gwPod, operation, delRules)
}
func (c *Controller) acquireStaticEip(name, _, nicName, ip, externalSubnet string) (string, string, string, error) {
checkConflict := true
var v4ip, v6ip, mac string
var err error
for _, ipStr := range strings.Split(ip, ",") {
if net.ParseIP(ipStr) == nil {
return "", "", "", fmt.Errorf("failed to parse eip ip %s", ipStr)
}
}
if v4ip, v6ip, mac, err = c.ipam.GetStaticAddress(name, nicName, ip, nil, externalSubnet, checkConflict); err != nil {
klog.Errorf("failed to get static ip %v, mac %v, subnet %v, err %v", ip, mac, externalSubnet, err)
return "", "", "", err
}
return v4ip, v6ip, mac, nil
}
func (c *Controller) acquireEip(name, _, nicName, externalSubnet string) (string, string, string, error) {
var skippedAddrs []string
for {
ipv4, ipv6, mac, err := c.ipam.GetRandomAddress(name, nicName, nil, externalSubnet, "", skippedAddrs, true)
if err != nil {
klog.Error(err)
return "", "", "", err
}
ipv4OK, ipv6OK, err := c.validatePodIP(name, externalSubnet, ipv4, ipv6)
if err != nil {
klog.Error(err)
return "", "", "", err
}
if ipv4OK && ipv6OK {
return ipv4, ipv6, mac, nil
}
if !ipv4OK {
skippedAddrs = append(skippedAddrs, ipv4)
}
if !ipv6OK {
skippedAddrs = append(skippedAddrs, ipv6)
}
}
}
func (c *Controller) eipChangeIP(eip *kubeovnv1.IptablesEIP) bool {
if eip.Status.IP == "" {
// eip created but not ready
return false
}
if eip.Status.IP != eip.Spec.V4ip {
return true
}
return false
}
func (c *Controller) GetGwBySubnet(name string) (string, string, error) {
subnet, err := c.subnetsLister.Get(name)
if err != nil {
err = fmt.Errorf("faile to get subnet %q: %w", name, err)
klog.Error(err)
return "", "", err
}
v4, v6 := util.SplitStringIP(subnet.Spec.Gateway)
return v4, v6, nil
}
func (c *Controller) createOrUpdateEipCR(key, v4ip, v6ip, mac, natGwDp, qos, externalNet string) error {
needCreate := false
cachedEip, err := c.iptablesEipsLister.Get(key)
if err != nil {
if k8serrors.IsNotFound(err) {
needCreate = true
} else {
klog.Errorf("failed to get eip %s, %v", key, err)
return err
}
}
externalNetwork := util.GetExternalNetwork(cachedEip.Spec.ExternalSubnet)
if needCreate {
klog.V(3).Infof("create eip cr %s", key)
_, err := c.config.KubeOvnClient.KubeovnV1().IptablesEIPs().Create(context.Background(), &kubeovnv1.IptablesEIP{
ObjectMeta: metav1.ObjectMeta{
Name: key,
Labels: map[string]string{
util.SubnetNameLabel: externalNet,
util.EipV4IpLabel: v4ip,
util.VpcNatGatewayNameLabel: natGwDp,
},
},
Spec: kubeovnv1.IptablesEIPSpec{
V4ip: v4ip,
V6ip: v6ip,
MacAddress: mac,
NatGwDp: natGwDp,
},
}, metav1.CreateOptions{})
if err != nil {
errMsg := fmt.Errorf("failed to create eip crd %s, %w", key, err)
klog.Error(errMsg)
return errMsg
}
} else {
eip := cachedEip.DeepCopy()
if v4ip != "" {
klog.V(3).Infof("update eip cr %s", key)
eip.Spec.V4ip = v4ip
eip.Spec.V6ip = v6ip
eip.Spec.NatGwDp = natGwDp
eip.Spec.MacAddress = mac
if _, err := c.config.KubeOvnClient.KubeovnV1().IptablesEIPs().Update(context.Background(), eip, metav1.UpdateOptions{}); err != nil {
errMsg := fmt.Errorf("failed to update eip crd %s, %w", key, err)
klog.Error(errMsg)
return errMsg
}
if eip.Status.IP == "" {
// eip is ip holder, not support change ip
eip.Status.IP = v4ip
// TODO:// ipv6
}
eip.Status.Ready = true
eip.Status.QoSPolicy = qos
bytes, err := eip.Status.Bytes()
if err != nil {
klog.Error(err)
return err
}
if _, err = c.config.KubeOvnClient.KubeovnV1().IptablesEIPs().Patch(context.Background(), key, types.MergePatchType,
bytes, metav1.PatchOptions{}, "status"); err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
klog.Errorf("failed to patch eip %s, %v", eip.Name, err)
return err
}
}
var needUpdateLabel bool
var op string
if len(eip.Labels) == 0 {
op = "add"
eip.Labels = map[string]string{
util.SubnetNameLabel: externalNetwork,
util.VpcNatGatewayNameLabel: natGwDp,
util.EipV4IpLabel: v4ip,
}
needUpdateLabel = true
} else if eip.Labels[util.SubnetNameLabel] != externalNetwork {
op = "replace"
eip.Labels[util.SubnetNameLabel] = externalNetwork
eip.Labels[util.VpcNatGatewayNameLabel] = natGwDp
needUpdateLabel = true
}
if eip.Spec.QoSPolicy != "" && eip.Labels[util.QoSLabel] != eip.Spec.QoSPolicy {
eip.Labels[util.QoSLabel] = eip.Spec.QoSPolicy
needUpdateLabel = true
}
if needUpdateLabel {
if err := c.updateIptableLabels(eip.Name, op, "eip", eip.Labels); err != nil {
klog.Errorf("failed to update eip %s labels, %v", eip.Name, err)
return err
}
}
if err = c.handleAddIptablesEipFinalizer(key); err != nil {
klog.Errorf("failed to handle add finalizer for eip, %v", err)
return err
}
}
return nil
}
func (c *Controller) syncIptablesEipFinalizer(cl client.Client) error {
// migrate depreciated finalizer to new finalizer
eips := &kubeovnv1.IptablesEIPList{}
return migrateFinalizers(cl, eips, func(i int) (client.Object, client.Object) {
if i < 0 || i >= len(eips.Items) {
return nil, nil
}
return eips.Items[i].DeepCopy(), eips.Items[i].DeepCopy()
})
}
func (c *Controller) handleAddIptablesEipFinalizer(key string) error {
cachedIptablesEip, err := c.iptablesEipsLister.Get(key)
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
klog.Error(err)
return err
}
if !cachedIptablesEip.DeletionTimestamp.IsZero() || len(cachedIptablesEip.GetFinalizers()) != 0 {
return nil
}
newIptablesEip := cachedIptablesEip.DeepCopy()
controllerutil.AddFinalizer(newIptablesEip, util.KubeOVNControllerFinalizer)
patch, err := util.GenerateMergePatchPayload(cachedIptablesEip, newIptablesEip)
if err != nil {
klog.Errorf("failed to generate patch payload for iptables eip '%s', %v", cachedIptablesEip.Name, err)
return err
}
if _, err := c.config.KubeOvnClient.KubeovnV1().IptablesEIPs().Patch(context.Background(), cachedIptablesEip.Name,
types.MergePatchType, patch, metav1.PatchOptions{}, ""); err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
klog.Errorf("failed to add finalizer for iptables eip '%s', %v", cachedIptablesEip.Name, err)
return err
}
return nil
}
func (c *Controller) handleDelIptablesEipFinalizer(key string) error {
cachedIptablesEip, err := c.iptablesEipsLister.Get(key)
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
klog.Error(err)
return err
}
if len(cachedIptablesEip.GetFinalizers()) == 0 {
return nil
}
newIptablesEip := cachedIptablesEip.DeepCopy()
controllerutil.RemoveFinalizer(newIptablesEip, util.KubeOVNControllerFinalizer)
patch, err := util.GenerateMergePatchPayload(cachedIptablesEip, newIptablesEip)
if err != nil {
klog.Errorf("failed to generate patch payload for iptables eip '%s', %v", cachedIptablesEip.Name, err)
return err
}
if _, err := c.config.KubeOvnClient.KubeovnV1().IptablesEIPs().Patch(context.Background(), cachedIptablesEip.Name,
types.MergePatchType, patch, metav1.PatchOptions{}, ""); err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
klog.Errorf("failed to remove finalizer from iptables eip '%s', %v", cachedIptablesEip.Name, err)
return err
}
return nil
}
func (c *Controller) patchEipQoSStatus(key, qos string) error {
var changed bool
oriEip, err := c.iptablesEipsLister.Get(key)
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
klog.Error(err)
return err
}
eip := oriEip.DeepCopy()
// update status.qosPolicy
if eip.Status.QoSPolicy != qos {
eip.Status.QoSPolicy = qos
changed = true
}
if changed {
bytes, err := eip.Status.Bytes()
if err != nil {
klog.Error(err)
return err
}
if _, err = c.config.KubeOvnClient.KubeovnV1().IptablesEIPs().Patch(context.Background(), key, types.MergePatchType,
bytes, metav1.PatchOptions{}, "status"); err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
klog.Errorf("failed to patch eip %s, %v", eip.Name, err)
return err
}
}
return nil
}
func (c *Controller) getIptablesEipNat(eipV4IP string) (string, error) {
nats := make([]string, 0, 3)
selector := labels.SelectorFromSet(labels.Set{util.EipV4IpLabel: eipV4IP})
dnats, err := c.iptablesDnatRulesLister.List(selector)
if err != nil {
klog.Errorf("failed to get dnats, %v", err)
return "", err
}
if len(dnats) != 0 {
nats = append(nats, util.DnatUsingEip)
}
fips, err := c.iptablesFipsLister.List(selector)
if err != nil {
klog.Errorf("failed to get fips, %v", err)
return "", err
}
if len(fips) != 0 {
nats = append(nats, util.FipUsingEip)
}
snats, err := c.iptablesSnatRulesLister.List(selector)
if err != nil {
klog.Errorf("failed to get snats, %v", err)
return "", err
}
if len(snats) != 0 {
nats = append(nats, util.SnatUsingEip)
}
nat := strings.Join(nats, ",")
return nat, nil
}
func (c *Controller) patchEipStatus(key, v4ip, redo, qos string, ready bool) error {
oriEip, err := c.iptablesEipsLister.Get(key)
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
klog.Error(err)
return err
}
eip := oriEip.DeepCopy()
var changed bool
if eip.Status.Ready != ready {
eip.Status.Ready = ready
changed = true
}
if redo != "" && eip.Status.Redo != redo {
eip.Status.Redo = redo
changed = true
}
if ready && v4ip != "" && eip.Status.IP != v4ip {
eip.Status.IP = v4ip
changed = true
}
nat, err := c.getIptablesEipNat(oriEip.Spec.V4ip)
if err != nil {
err := errors.New("failed to get eip nat")
klog.Error(err)
return err
}
// nat record all kinds of nat rules using this eip
klog.V(3).Infof("nat of eip %s is %s", eip.Name, nat)
if eip.Status.Nat != nat {
eip.Status.Nat = nat
changed = true
}
if qos != "" && eip.Status.QoSPolicy != qos {
eip.Status.QoSPolicy = qos
changed = true
}
if changed {
bytes, err := eip.Status.Bytes()
if err != nil {
klog.Error(err)
return err
}
if _, err = c.config.KubeOvnClient.KubeovnV1().IptablesEIPs().Patch(context.Background(), key, types.MergePatchType,
bytes, metav1.PatchOptions{}, "status"); err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
klog.Errorf("failed to patch eip %s, %v", eip.Name, err)
return err
}
}
return nil
}
func (c *Controller) patchEipLabel(eipName string) error {
oriEip, err := c.iptablesEipsLister.Get(eipName)
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
klog.Error(err)
return err
}
externalNetwork := util.GetExternalNetwork(oriEip.Spec.ExternalSubnet)
eip := oriEip.DeepCopy()
var needUpdateLabel bool
var op string
if len(eip.Labels) == 0 {
op = "add"
needUpdateLabel = true
eip.Labels = map[string]string{
util.SubnetNameLabel: externalNetwork,
util.VpcNatGatewayNameLabel: eip.Spec.NatGwDp,
util.QoSLabel: eip.Spec.QoSPolicy,
util.EipV4IpLabel: eip.Spec.V4ip,
}
} else if eip.Labels[util.VpcNatGatewayNameLabel] != eip.Spec.NatGwDp || eip.Labels[util.QoSLabel] != eip.Spec.QoSPolicy {
op = "replace"
needUpdateLabel = true
eip.Labels[util.SubnetNameLabel] = externalNetwork
eip.Labels[util.VpcNatGatewayNameLabel] = eip.Spec.NatGwDp
eip.Labels[util.QoSLabel] = eip.Spec.QoSPolicy
eip.Labels[util.EipV4IpLabel] = eip.Spec.V4ip
}
if needUpdateLabel {
if err := c.updateIptableLabels(eip.Name, op, "eip", eip.Labels); err != nil {
klog.Errorf("failed to update label of eip %s, %v", eip.Name, err)
return err
}
}
return err
}