-
Notifications
You must be signed in to change notification settings - Fork 149
/
eni-multi-ip.go
1181 lines (1055 loc) · 31.1 KB
/
eni-multi-ip.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 daemon
import (
"context"
"fmt"
"net"
"sort"
"strings"
"sync"
"time"
"github.com/AliyunContainerService/terway/deviceplugin"
"github.com/AliyunContainerService/terway/pkg/aliyun"
apiErr "github.com/AliyunContainerService/terway/pkg/aliyun/client/errors"
terwayIP "github.com/AliyunContainerService/terway/pkg/ip"
"github.com/AliyunContainerService/terway/pkg/ipam"
"github.com/AliyunContainerService/terway/pkg/logger"
"github.com/AliyunContainerService/terway/pkg/metric"
"github.com/AliyunContainerService/terway/pkg/pool"
"github.com/AliyunContainerService/terway/pkg/tracing"
"github.com/AliyunContainerService/terway/pkg/utils"
"github.com/AliyunContainerService/terway/types"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
corev1 "k8s.io/api/core/v1"
)
type AllocCtx struct {
Trace []Trace
}
func (a *AllocCtx) String() string {
var s []string
for _, t := range a.Trace {
s = append(s, t.Msg)
}
return strings.Join(s, "\n")
}
type Trace struct {
Msg string
}
var eniIPLog = logger.DefaultLogger
const (
maxEniOperating = 3
maxIPBacklog = 10
)
const (
eniIPAllocInhibitTimeout = 10 * time.Minute
typeNameENIIP = "eniip"
poolNameENIIP = "eniip"
factoryNameENIIP = "eniip"
tracingKeyENIMaxIP = "eni_max_ip"
tracingKeyENICount = "eni_count"
tracingKeySecondaryIPCount = "secondary_ip_count"
commandAudit = "audit"
)
const timeFormat = "2006-01-02 15:04:05"
type eniIPFactory struct {
name string
enableTrunk bool
trunkOnEni string
eniFactory *eniFactory
enis []*ENI
maxENI chan struct{}
eniMaxIP int
eniOperChan chan struct{}
ipResultChan chan *ENIIP
sync.RWMutex
// metrics
metricENICount prometheus.Gauge
disableSecurityGroupCheck bool
ipFamily *types.IPFamily
}
// ENIIP the secondary ip of eni
type ENIIP struct {
*types.ENIIP
err error
}
// ENI to hold ENI's secondary config
type ENI struct {
lock sync.Mutex
*types.ENI
ips []*ENIIP
pending int
ipBacklog chan struct{}
ecs ipam.API
done chan struct{}
// Unix timestamp to mark when this ENI can allocate Pod IP.
ipAllocInhibitExpireAt time.Time
}
func (e *ENI) getIPCountLocked() int {
return e.pending + len(e.ips)
}
// eni ip allocator
func (e *ENI) allocateWorker(resultChan chan<- *ENIIP) {
for {
toAllocate := 0
select {
case <-e.done:
return
case <-e.ipBacklog:
toAllocate = 1
}
// wait 300ms for aggregation the cni request
time.Sleep(300 * time.Millisecond)
popAll:
for {
select {
case <-e.ipBacklog:
toAllocate++
default:
break popAll
}
if toAllocate >= maxIPBacklog {
break
}
}
eniIPLog.Debugf("allocate %v ips for eni", toAllocate)
v4, v6, err := e.ecs.AssignNIPsForENI(context.Background(), e.ENI.ID, e.ENI.MAC, toAllocate)
eniIPLog.Debugf("allocated ips for eni: eni = %+v, v4 = %+v,v6 = %+v, err = %v", e.ENI, v4, v6, err)
if err != nil {
eniIPLog.Errorf("error allocate ips for eni: %v", err)
metric.ENIIPFactoryIPAllocCount.WithLabelValues(e.MAC, metric.ENIIPAllocActionFail).Add(float64(toAllocate))
for i := 0; i < toAllocate; i++ {
resultChan <- &ENIIP{
ENIIP: &types.ENIIP{
ENI: e.ENI,
},
err: errors.Errorf("error assign ip for ENI: %v", err),
}
}
} else {
metric.ENIIPFactoryIPAllocCount.WithLabelValues(e.MAC, metric.ENIIPAllocActionSucceed).Add(float64(toAllocate))
for _, ip := range types.MergeIPs(v4, v6) {
resultChan <- &ENIIP{
ENIIP: &types.ENIIP{
ENI: e.ENI,
IPSet: ip,
},
err: nil,
}
}
}
}
}
func (f *eniIPFactory) getEnis(ctx *AllocCtx) ([]*ENI, error) {
var (
enis []*ENI
pendingEnis []*ENI
)
enisLen := len(f.enis)
// If there is only one switch, then no need for ordering.
if enisLen <= 1 {
return f.enis, nil
}
// pre sort eni by ip count to balance ip allocation on enis
sort.Slice(f.enis, func(i, j int) bool {
return f.enis[i].getIPCountLocked() < f.enis[j].getIPCountLocked()
})
// If VSwitchSelectionPolicy is ordered, then call f.eniFactory.GetVSwitches() API to get a switch slice
// in descending order per each switch's available IP count.
vSwitches, err := f.eniFactory.GetVSwitches()
eniIPLog.Infof("adjusted vswitch slice: %+v, original eni slice: %s", vSwitches, func(enis []*ENI) string {
var vsw []string
for i := 0; i < len(enis); i++ {
if enis[i] == nil || enis[i].ENI == nil {
continue
}
vsw = append(vsw, enis[i].VSwitchID)
}
return strings.Join(vsw, ",")
}(f.enis))
if err != nil {
eniIPLog.Errorf("error to get vswitch slice: %v, instead use original eni slice in eniIPFactory: %v", err, f.enis)
return f.enis, err
}
for _, vswitch := range vSwitches {
for _, eni := range f.enis {
if eni.ENI == nil {
pendingEnis = append(pendingEnis, eni)
continue
}
if vswitch == eni.VSwitchID {
enis = append(enis, eni)
}
}
}
enis = append(enis, pendingEnis...)
if len(enis) == 0 && len(f.enis) != 0 {
ctx.Trace = append(ctx.Trace, Trace{
Msg: "current eni have vSwitch not as expected, please check eni-config",
})
}
return enis, nil
}
func (f *eniIPFactory) submit(ctx *AllocCtx) error {
f.Lock()
defer f.Unlock()
var enis []*ENI
enis, _ = f.getEnis(ctx)
for _, eni := range enis {
eniIPLog.Infof("check existing eni: %+v", eni)
eni.lock.Lock()
now := time.Now()
if eni.ENI != nil {
eniIPLog.Infof("check if the current eni is in the time window for IP allocation inhibition: "+
"eni = %+v, vsw= %s, now = %s, expireAt = %s", eni, eni.VSwitchID, now.Format(timeFormat), eni.ipAllocInhibitExpireAt.Format(timeFormat))
}
// if the current eni has been inhibited for Pod IP allocation, then skip current eni.
if now.Before(eni.ipAllocInhibitExpireAt) && eni.ENI != nil {
eni.lock.Unlock()
// vsw have insufficient ip
ctx.Trace = append(ctx.Trace, Trace{Msg: fmt.Sprintf("eni %s vSwitch %s have insufficient IP, next check %s", eni.ID, eni.VSwitchID, eni.ipAllocInhibitExpireAt)})
eniIPLog.Debugf("skip IP allocation: eni = %+v, vsw = %s", eni, eni.VSwitchID)
continue
}
eniIPLog.Debugf("check if the current eni will reach eni IP quota with new pending IP added: "+
"eni = %+v, eni.pending = %d, len(eni.ips) = %d, eni.MaxIPs = %d", eni, eni.pending, len(eni.ips), f.eniMaxIP)
if eni.getIPCountLocked() < f.eniMaxIP {
select {
case eni.ipBacklog <- struct{}{}:
default:
eni.lock.Unlock()
continue
}
eniIPLog.Debugf("submit eniip to the eni backlog: %+v", eni)
eni.pending++
eni.lock.Unlock()
return nil
}
eni.lock.Unlock()
}
return errors.Errorf("trigger ENIIP throttle, max operating concurrent: %v", maxIPBacklog)
}
func (f *eniIPFactory) popResult() (ip *types.ENIIP, err error) {
result := <-f.ipResultChan
eniIPLog.Debugf("pop result from resultChan: %+v", result)
if result.ENIIP == nil || result.err != nil {
// There are two error cases:
// Error Case 1. The ENI-associated VSwitch has no available IP for Pod IP allocation.
// Error Case 2. The IP number allocated has reached ENI quota.
f.Lock()
defer f.Unlock()
if result.ENIIP != nil && result.ENI != nil {
for _, eni := range f.enis {
// eni is not ready
if eni == nil {
continue
}
if eni.MAC == result.ENI.MAC {
eni.pending--
// if an error message with InvalidVSwitchIDIPNotEnough returned, then mark the ENI as IP allocation inhibited.
if strings.Contains(result.err.Error(), apiErr.InvalidVSwitchIDIPNotEnough) {
eni.ipAllocInhibitExpireAt = time.Now().Add(eniIPAllocInhibitTimeout)
eniIPLog.Infof("eni's associated vswitch %s has no available IP, set eni ipAllocInhibitExpireAt = %s",
eni.VSwitchID, eni.ipAllocInhibitExpireAt.Format(timeFormat))
}
}
}
}
return nil, errors.Errorf("error allocate ip from eni: %v", result.err)
}
f.Lock()
defer f.Unlock()
for _, eni := range f.enis {
if eni.ENI != nil && eni.MAC == result.ENI.MAC {
eni.pending--
eni.lock.Lock()
eni.ips = append(eni.ips, result)
eni.lock.Unlock()
metric.ENIIPFactoryIPCount.WithLabelValues(f.name, eni.MAC, fmt.Sprint(f.eniMaxIP)).Inc()
return result.ENIIP, nil
}
}
return nil, errors.Errorf("unexpected eni ip allocated: %v", result)
}
func (f *eniIPFactory) Create(count int) ([]types.NetworkResource, error) {
ctx := &AllocCtx{}
var (
ipResult []types.NetworkResource
err error
waiting int
)
defer func() {
if len(ipResult) == 0 {
eniIPLog.Debugf("create result: %v, error: %v", ipResult, err)
} else {
for _, ip := range ipResult {
eniIPLog.Debugf("create result nil: %+v, error: %v", ip, err)
}
}
}()
// find for available ENIs and submit for ip allocation
for ; waiting < count; waiting++ {
err = f.submit(ctx)
if err != nil {
break
}
}
// there are (count - waiting) ips still wait for allocation
// create a new ENI with initial ip count (count - waiting) as initENIIPCount
// initENIIPCount can't be greater than eniMaxIP and maxIPBacklog
initENIIPCount := count - waiting
if initENIIPCount > f.eniMaxIP {
initENIIPCount = f.eniMaxIP
}
if initENIIPCount > maxIPBacklog {
initENIIPCount = maxIPBacklog
}
if initENIIPCount > 0 {
eniIPLog.Debugf("create eni async, ip count: %+v", initENIIPCount)
_, err = f.createENIAsync(initENIIPCount)
if err == nil {
waiting += initENIIPCount
} else {
eniIPLog.Errorf("error create eni async: %+v", err)
}
}
// no ip has been created
if waiting == 0 {
return ipResult, errors.Errorf("error submit ip create request: %v,%s", err, ctx.String())
}
var ip *types.ENIIP
for ; waiting > 0; waiting-- { // receive allocate result
ip, err = f.popResult()
if err != nil {
eniIPLog.Errorf("error allocate ip address: %+v", err)
} else {
ipResult = append(ipResult, ip)
}
}
if len(ipResult) == 0 {
return ipResult, errors.Errorf("error allocate ip address: %v", err)
}
return ipResult, nil
}
func (f *eniIPFactory) Dispose(res types.NetworkResource) (err error) {
defer func() {
eniIPLog.Debugf("dispose result: %v, error: %v", res.GetResourceID(), err != nil)
}()
ip := res.(*types.ENIIP)
if ip.ENI == nil {
return nil
}
var (
eni *ENI
eniip *ENIIP
)
f.RLock()
for _, e := range f.enis {
if ip.ENI.ID == e.ID {
eni = e
e.lock.Lock()
for _, eip := range e.ips {
if eip.IPSet.String() == ip.IPSet.String() {
eniip = eip
}
}
e.lock.Unlock()
}
}
f.RUnlock()
if eni == nil || eniip == nil {
return fmt.Errorf("invalid resource to dispose")
}
eni.lock.Lock()
if len(eni.ips) == 1 {
if f.enableTrunk && eni.Trunk {
eni.lock.Unlock()
return fmt.Errorf("trunk ENI %+v will not dispose", eni.ID)
}
if eni.pending > 0 {
eni.lock.Unlock()
return fmt.Errorf("ENI have pending ips to be allocate")
}
// block ip allocate
eni.pending = f.eniMaxIP
eni.lock.Unlock()
f.Lock()
for i, e := range f.enis {
if ip.ENI.ID == e.ID {
close(eni.done)
f.enis[len(f.enis)-1], f.enis[i] = f.enis[i], f.enis[len(f.enis)-1]
f.enis = f.enis[:len(f.enis)-1]
break
}
}
f.metricENICount.Dec()
f.Unlock()
err = f.destroyENICompartment(ip.ENI)
if err != nil {
return fmt.Errorf("error destroy eni compartment before disposing: %v", err)
}
f.eniOperChan <- struct{}{}
// only remain ENI main ip address, release the ENI interface
err = f.eniFactory.Dispose(ip.ENI)
<-f.eniOperChan
if err != nil {
return fmt.Errorf("error dispose ENI for eniip, %v", err)
}
<-f.maxENI
return nil
}
eni.lock.Unlock()
// main ip of ENI, raise put_it_back error
if ip.ENI.PrimaryIP.IPv4.Equal(ip.IPSet.IPv4) {
// if in dual-stack and have no ipv6 address may need add one ip for it
return fmt.Errorf("ip to be release is primary ip of ENI")
}
var v4, v6 []net.IP
if ip.IPSet.IPv4 != nil {
v4 = append(v4, ip.IPSet.IPv4)
}
if ip.IPSet.IPv6 != nil {
v6 = append(v6, ip.IPSet.IPv6)
}
err = f.eniFactory.ecs.UnAssignIPsForENI(context.Background(), ip.ENI.ID, ip.ENI.MAC, v4, v6)
if err != nil {
return fmt.Errorf("error unassign eniip, %v", err)
}
eni.lock.Lock()
for i, e := range eni.ips {
if e.IPSet.IPv4.Equal(eniip.IPSet.IPv4) {
eni.ips[len(eni.ips)-1], eni.ips[i] = eni.ips[i], eni.ips[len(eni.ips)-1]
eni.ips = eni.ips[:len(eni.ips)-1]
break
}
}
eni.lock.Unlock()
metric.ENIIPFactoryIPCount.WithLabelValues(f.name, eni.MAC, fmt.Sprint(f.eniMaxIP)).Dec()
return nil
}
// Check resource in remote
func (f *eniIPFactory) Check(res types.NetworkResource) error {
eniIP, ok := res.(*types.ENIIP)
if !ok {
return fmt.Errorf("unsupported type %T", res)
}
ipv4, ipv6, err := f.eniFactory.ecs.GetENIIPs(context.Background(), eniIP.ENI.MAC)
if err != nil {
return err
}
if utils.IsWindowsOS() {
// NB(thxCode): don't assign the primary IP of the assistant eni.
ipv4, ipv6 = dropPrimaryIP(eniIP.ENI, ipv4, ipv6)
}
if eniIP.IPSet.IPv4 != nil {
if !terwayIP.IPsIntersect([]net.IP{eniIP.IPSet.IPv4}, ipv4) {
return apiErr.ErrNotFound
}
}
if eniIP.IPSet.IPv6 != nil {
if !terwayIP.IPsIntersect([]net.IP{eniIP.IPSet.IPv6}, ipv6) {
return apiErr.ErrNotFound
}
}
return nil
}
// ListResource load all eni info from metadata
func (f *eniIPFactory) ListResource() (map[string]types.NetworkResource, error) {
f.RLock()
defer f.RUnlock()
// list all resource status in our pool
mapping := make(map[string]types.NetworkResource)
var inUseENIIPs []*types.ENIIP
for _, e := range f.enis {
e.lock.Lock()
for _, eniIP := range e.ips {
inUseENIIPs = append(inUseENIIPs, eniIP.ENIIP)
}
e.lock.Unlock()
}
ctx := context.Background()
macs, err := f.eniFactory.ecs.GetSecondaryENIMACs(ctx)
if err != nil {
return nil, err
}
for _, mac := range macs {
// get secondary ips from one mac
ipv4s, ipv6s, err := f.eniFactory.ecs.GetENIIPs(ctx, mac)
if err != nil {
if errors.Is(err, apiErr.ErrNotFound) {
continue
}
return nil, err
}
ipv4Set := terwayIP.ToIPMap(ipv4s)
ipv6Set := terwayIP.ToIPMap(ipv6s)
for _, eniIP := range inUseENIIPs {
if eniIP.ENI.MAC != mac {
continue
}
var v4, v6 net.IP
if eniIP.IPSet.IPv4 != nil {
_, ok := ipv4Set[eniIP.IPSet.IPv4.String()]
if ok {
v4 = eniIP.IPSet.IPv4
}
}
if eniIP.IPSet.IPv6 != nil {
_, ok := ipv6Set[eniIP.IPSet.IPv6.String()]
if ok {
v6 = eniIP.IPSet.IPv6
}
}
tmp := types.ENIIP{
ENI: &types.ENI{
MAC: mac,
},
IPSet: types.IPSet{
IPv4: v4,
IPv6: v6,
},
}
mapping[tmp.GetResourceID()] = &tmp
}
}
return mapping, nil
}
func (f *eniIPFactory) Reconcile() {
// check security group
err := f.eniFactory.ecs.CheckEniSecurityGroup(context.Background(), f.eniFactory.securityGroups)
if err != nil {
_ = tracing.RecordNodeEvent(corev1.EventTypeWarning, "ResourceInvalid", fmt.Sprintf("eni has misconfiged security group. %s", err.Error()))
}
}
func (f *eniIPFactory) initialENI(eni *ENI, ipCount int) {
if utils.IsWindowsOS() {
// NB(thxCode): create eni with one more IP in windows at initialization.
ipCount++
}
rawEni, err := f.eniFactory.CreateWithIPCount(ipCount, false)
var ipv4s []net.IP
var ipv6s []net.IP
// eni operate finished
<-f.eniOperChan
if err != nil || len(rawEni) != 1 {
// create eni failed, put quota back
<-f.maxENI
} else {
var ok bool
eni.ENI, ok = rawEni[0].(*types.ENI)
if !ok {
err = fmt.Errorf("error get type ENI from factory, got: %+v, rollback it", rawEni)
eniIPLog.Error(err)
errDispose := f.eniFactory.Dispose(rawEni[0])
if errDispose != nil {
eniIPLog.Errorf("rollback %+v failed", rawEni)
}
<-f.maxENI
} else {
ipv4s, ipv6s, err = f.eniFactory.ecs.GetENIIPs(context.Background(), eni.MAC)
if err != nil {
eniIPLog.Errorf("error get eni secondary address: %+v, rollback it", err)
errDispose := f.eniFactory.Dispose(rawEni[0])
if errDispose != nil {
eniIPLog.Errorf("rollback %+v failed", rawEni)
}
<-f.maxENI
}
if f.ipFamily.IPv4 && f.ipFamily.IPv6 {
if len(ipv4s) != len(ipv6s) {
eniIPLog.Errorf("error get eni secondary address: ipv4 ipv6 length is not equal, rollback it")
errDispose := f.eniFactory.Dispose(rawEni[0])
if errDispose != nil {
eniIPLog.Errorf("rollback %+v failed", rawEni)
}
<-f.maxENI
}
}
err = f.setupENICompartment(eni.ENI)
if err != nil {
eniIPLog.Errorf("error setup eni compartment after initialization: %v", err)
errDispose := f.eniFactory.Dispose(rawEni[0])
if errDispose != nil {
eniIPLog.Errorf("rollback %+v failed", rawEni)
}
<-f.maxENI
}
}
}
eniIPLog.Debugf("eni initial finished: %+v, err: %+v", eni, err)
if err != nil {
eni.lock.Lock()
//failed all pending on this initial eni
for i := 0; i < eni.pending; i++ {
f.ipResultChan <- &ENIIP{
ENIIP: &types.ENIIP{
ENI: nil,
},
err: fmt.Errorf("error initial ENI: %w", err),
}
}
// disable eni for submit
eni.pending = f.eniMaxIP
eni.lock.Unlock()
// remove from eni list
f.Lock()
for i, e := range f.enis {
if e == eni {
f.enis[len(f.enis)-1], f.enis[i] = f.enis[i], f.enis[len(f.enis)-1]
f.enis = f.enis[:len(f.enis)-1]
break
}
}
f.metricENICount.Dec()
f.Unlock()
return
}
eni.lock.Lock()
if utils.IsWindowsOS() {
// NB(thxCode): don't assign the primary IP of the assistant eni.
ipv4s, ipv6s = dropPrimaryIP(eni.ENI, ipv4s, ipv6s)
}
eniIPLog.Infof("allocate status on async eni: %+v, pending: %v, ips: %v, backlog: %v",
eni, eni.pending, ipv4s, len(eni.ipBacklog))
for _, ipSet := range types.MergeIPs(ipv4s, ipv6s) {
eniIP := &types.ENIIP{
ENI: eni.ENI,
IPSet: ipSet,
}
f.ipResultChan <- &ENIIP{
ENIIP: eniIP,
err: nil,
}
}
eni.lock.Unlock()
go eni.allocateWorker(f.ipResultChan)
}
func (f *eniIPFactory) createENIAsync(initIPs int) (*ENI, error) {
eni := &ENI{
ENI: nil,
ips: make([]*ENIIP, 0),
pending: initIPs,
ipBacklog: make(chan struct{}, maxIPBacklog),
ecs: f.eniFactory.ecs,
done: make(chan struct{}, 1),
}
select {
case f.maxENI <- struct{}{}:
select {
case f.eniOperChan <- struct{}{}:
default:
<-f.maxENI
return nil, fmt.Errorf("trigger ENI throttle, max operating concurrent: %v", maxEniOperating)
}
go f.initialENI(eni, eni.pending)
default:
return nil, fmt.Errorf("max ENI exceeded")
}
f.Lock()
f.enis = append(f.enis, eni)
f.Unlock()
// metric
f.metricENICount.Inc()
return eni, nil
}
func (f *eniIPFactory) Config() []tracing.MapKeyValueEntry {
config := []tracing.MapKeyValueEntry{
{Key: tracingKeyName, Value: f.name},
{Key: tracingKeyENIMaxIP, Value: fmt.Sprint(f.eniMaxIP)},
}
return config
}
func (f *eniIPFactory) Trace() []tracing.MapKeyValueEntry {
var trace []tracing.MapKeyValueEntry
secIPCount := 0
trace = append(trace, tracing.MapKeyValueEntry{Key: tracingKeyENICount, Value: fmt.Sprint(len(f.enis))})
trace = append(trace, tracing.MapKeyValueEntry{Key: tracingKeySecondaryIPCount, Value: ""}) // placeholder
for _, v := range f.enis {
secIPCount += len(v.ips)
trace = append(trace, tracing.MapKeyValueEntry{
Key: fmt.Sprintf("eni/%s/pending", v.MAC),
Value: fmt.Sprint(v.pending),
})
var secIPs []string
for _, v := range v.ips {
secIPs = append(secIPs, v.IPSet.String())
}
trace = append(trace, tracing.MapKeyValueEntry{
Key: fmt.Sprintf("eni/%s/secondary_ips", v.MAC),
Value: strings.Join(secIPs, " "),
})
trace = append(trace, tracing.MapKeyValueEntry{
Key: fmt.Sprintf("eni/%s/ip_alloc_inhibit_expire_at", v.MAC),
Value: v.ipAllocInhibitExpireAt.Format(timeFormat),
})
}
trace[1].Value = fmt.Sprint(secIPCount)
return trace
}
func (f *eniIPFactory) Execute(cmd string, _ []string, message chan<- string) {
switch cmd {
case commandAudit: // check account
f.checkAccount(message)
case commandMapping:
mapping, err := f.ListResource()
message <- fmt.Sprintf("mapping: %v, err: %s\n", mapping, err)
default:
message <- "can't recognize command\n"
}
close(message)
}
func (f *eniIPFactory) checkAccount(message chan<- string) {
// get ENIs via Aliyun API
message <- "fetching attached ENIs from aliyun\n"
ctx := context.Background()
enis, err := f.eniFactory.ecs.GetAttachedENIs(ctx, false)
if err != nil {
message <- fmt.Sprintf("error while fetching from remote: %s\n", err.Error())
return
}
message <- fmt.Sprintf("%d enis fetched\n", len(enis))
f.Lock()
// check local enis count
if len(f.enis) != len(enis) {
message <- fmt.Sprintf("remote eni count not equal to local: remote %d, local: %d\n", len(enis), len(f.enis))
}
// diff
diffMap := make(map[string]*ENI)
for _, v := range f.enis {
diffMap[v.ID] = v
}
f.Unlock()
// range for remote enis
for _, v := range enis {
message <- fmt.Sprintf("checking eni %s(%s)\n", v.ID, v.MAC)
eni, ok := diffMap[v.ID]
if !ok {
message <- fmt.Sprintf("remote eni %s(%s) not found on local machine\n", v.ID, v.MAC)
continue
}
// diff secondary ips
remoteIPs, _, err := f.eniFactory.ecs.GetENIIPs(ctx, v.MAC)
if err != nil {
message <- fmt.Sprintf("error while fetching ips: %s", err.Error())
return
}
if utils.IsWindowsOS() {
// NB(thxCode): don't assign the primary IP of the assistant eni.
remoteIPs, _ = dropPrimaryIP(v, remoteIPs, nil)
}
diffIPMap := make(map[string]struct{})
for _, ip := range eni.ips {
diffIPMap[ip.IPSet.IPv4.String()] = struct{}{}
}
// range for remote ips
for _, ip := range remoteIPs {
_, ok := diffIPMap[ip.String()]
if !ok {
message <- fmt.Sprintf("ip %s attached to eni %s(%s) not found on local machine\n", ip.String(), v.ID, v.MAC)
}
}
}
message <- "done.\n"
}
type eniIPResourceManager struct {
trunkENI *types.ENI
pool pool.ObjectPool
}
func newENIIPResourceManager(poolConfig *types.PoolConfig, ecs ipam.API, k8s Kubernetes, allocatedResources map[string]resourceManagerInitItem, ipFamily *types.IPFamily) (ResourceManager, error) {
eniFactory, err := newENIFactory(poolConfig, ecs)
if err != nil {
return nil, fmt.Errorf("error get ENI factory for eniip factory, %w", err)
}
factory := &eniIPFactory{
name: factoryNameENIIP,
eniFactory: eniFactory,
enableTrunk: poolConfig.EnableENITrunking,
enis: []*ENI{},
eniOperChan: make(chan struct{}, maxEniOperating),
ipResultChan: make(chan *ENIIP, maxIPBacklog),
ipFamily: ipFamily,
}
var capacity, maxEni, memberENIPod, adapters int
if !poolConfig.DisableDevicePlugin {
limit, err := aliyun.GetLimit(ecs, aliyun.GetInstanceMeta().InstanceType)
if err != nil {
return nil, fmt.Errorf("error get max eni for eniip factory, %w", err)
}
maxEni = limit.Adapters
maxEni = int(float64(maxEni)*poolConfig.EniCapRatio) + poolConfig.EniCapShift - 1
ipPerENI := limit.IPv4PerAdapter
if utils.IsWindowsOS() {
// NB(thxCode): don't assign the primary IP of one assistant eni.
ipPerENI--
}
factory.eniMaxIP = ipPerENI
if poolConfig.MaxENI != 0 && poolConfig.MaxENI < maxEni {
maxEni = poolConfig.MaxENI
}
capacity = maxEni * ipPerENI
if capacity < 0 {
capacity = 0
}
memberENIPod = limit.MemberAdapterLimit
if memberENIPod < 0 {
memberENIPod = 0
}
factory.maxENI = make(chan struct{}, maxEni)
if poolConfig.MinENI != 0 {
poolConfig.MinPoolSize = poolConfig.MinENI * ipPerENI
}
if poolConfig.MinPoolSize > capacity {
eniIPLog.Infof("min pool size bigger than node capacity, set min pool size to capacity")
poolConfig.MinPoolSize = capacity
}
if poolConfig.MaxPoolSize > capacity {
eniIPLog.Infof("max pool size bigger than node capacity, set max pool size to capacity")
poolConfig.MaxPoolSize = capacity
}
if poolConfig.MinPoolSize > poolConfig.MaxPoolSize {
eniIPLog.Warnf("min_pool_size bigger: %v than max_pool_size: %v, set max_pool_size to the min_pool_size",
poolConfig.MinPoolSize, poolConfig.MaxPoolSize)
poolConfig.MaxPoolSize = poolConfig.MinPoolSize
}
adapters = limit.Adapters
} else {
capacity = 1
maxEni = 1
memberENIPod = 0
adapters = 0
}
if poolConfig.WaitTrunkENI {
logger.DefaultLogger.Infof("waitting trunk eni ready")
factory.trunkOnEni, err = k8s.WaitTrunkReady()
if err != nil {
return nil, err
}
logger.DefaultLogger.Infof("trunk eni found %s", factory.trunkOnEni)
}
// eniip factory metrics
factory.metricENICount = metric.ENIIPFactoryENICount.WithLabelValues(factory.name, fmt.Sprint(maxEni))
var trunkENI *types.ENI
poolCfg := pool.Config{
Name: poolNameENIIP,
Type: typeNameENIIP,
MaxIdle: poolConfig.MaxPoolSize,
MinIdle: poolConfig.MinPoolSize,
Factory: factory,
Capacity: capacity,
Initializer: func(holder pool.ResourceHolder) error {
ctx := context.Background()
// not use main ENI for ENI multiple ip allocate
enis, err := ecs.GetAttachedENIs(ctx, false)
if err != nil {
return fmt.Errorf("error get attach ENI on pool init, %w", err)
}
if factory.enableTrunk && memberENIPod > 0 {
for _, eni := range enis {
if eni.Trunk {
factory.trunkOnEni = eni.ID
}
if eni.ID == factory.trunkOnEni {
trunkENI = eni
}
}
if factory.trunkOnEni == "" && len(enis) < adapters {
trunkENIRes, err := factory.eniFactory.CreateWithIPCount(1, true)
if err != nil {
return errors.Wrapf(err, "error init trunk eni")
}
trunkENI, _ = trunkENIRes[0].(*types.ENI)
factory.trunkOnEni = trunkENI.ID
enis = append(enis, trunkENI)
}
}
for _, eni := range enis {
ipv4s, ipv6s, err := ecs.GetENIIPs(ctx, eni.MAC)
if err != nil {
return fmt.Errorf("error get ENI's ip on pool init, %w", err)
}
err = factory.setupENICompartment(eni)
if err != nil {
// NB(thxCode): an unbinding eni stuck and then block starting,
// we just ignore this kind of error.
if strings.Contains(err.Error(), "no interface with given MAC") {
continue
}
return errors.Wrap(err, "error setup eni compartment")
}
if utils.IsWindowsOS() {
// NB(thxCode): don't assign the primary IP of one assistant eni.
ipv4s, ipv6s = dropPrimaryIP(eni, ipv4s, ipv6s)
}
poolENI := &ENI{
ENI: eni,
ips: []*ENIIP{},
ecs: ecs,
ipBacklog: make(chan struct{}, maxIPBacklog),
done: make(chan struct{}, 1),
}
factory.enis = append(factory.enis, poolENI)
factory.metricENICount.Inc()
if ipFamily.IPv4 && !ipFamily.IPv6 {
for _, ip := range ipv4s {
eniIP := &types.ENIIP{