forked from AliyunContainerService/terway
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpool.go
1396 lines (1192 loc) · 38 KB
/
pool.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 node
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/netip"
"reflect"
"sync"
"sync/atomic"
"time"
"github.com/go-logr/logr"
"github.com/samber/lo"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"golang.org/x/time/rate"
corev1 "k8s.io/api/core/v1"
k8sErr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
k8stypes "k8s.io/apimachinery/pkg/types"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/metrics"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
"github.com/AliyunContainerService/terway/deviceplugin"
aliyunClient "github.com/AliyunContainerService/terway/pkg/aliyun/client"
apiErr "github.com/AliyunContainerService/terway/pkg/aliyun/client/errors"
networkv1beta1 "github.com/AliyunContainerService/terway/pkg/apis/network.alibabacloud.com/v1beta1"
"github.com/AliyunContainerService/terway/pkg/backoff"
register "github.com/AliyunContainerService/terway/pkg/controller"
"github.com/AliyunContainerService/terway/pkg/utils"
"github.com/AliyunContainerService/terway/pkg/vswitch"
"github.com/AliyunContainerService/terway/types"
)
const (
ControllerName = "multi-ip-node"
finalizer = "network.alibabacloud.com/node-controller"
batchSize = 10
)
var EventCh = make(chan event.GenericEvent, 1)
func init() {
register.Add(ControllerName, func(mgr manager.Manager, ctrlCtx *register.ControllerCtx) error {
fullSyncPeriod, err := time.ParseDuration(ctrlCtx.Config.MultiIPNodeSyncPeriod)
if err != nil {
return err
}
gcPeriod, err := time.ParseDuration(ctrlCtx.Config.MultiIPGCPeriod)
if err != nil {
return err
}
minSyncPeriod, err := time.ParseDuration(ctrlCtx.Config.MultiIPMinSyncPeriodOnFailure)
if err != nil {
return err
}
maxSyncPeriod, err := time.ParseDuration(ctrlCtx.Config.MultiIPMaxSyncPeriodOnFailure)
if err != nil {
return err
}
// metric and tracer
metrics.Registry.MustRegister(ResourcePoolTotal)
tracer := ctrlCtx.TracerProvider.Tracer(ControllerName)
ctrl, err := controller.New(ControllerName, mgr, controller.Options{
MaxConcurrentReconciles: ctrlCtx.Config.MultiIPNodeMaxConcurrent,
Reconciler: &ReconcileNode{
client: mgr.GetClient(),
scheme: mgr.GetScheme(),
record: mgr.GetEventRecorderFor(ControllerName),
aliyun: ctrlCtx.AliyunClient,
vswpool: ctrlCtx.VSwitchPool,
fullSyncNodePeriod: fullSyncPeriod,
gcPeriod: gcPeriod,
tracer: tracer,
},
RateLimiter: workqueue.NewMaxOfRateLimiter(
workqueue.NewItemExponentialFailureRateLimiter(minSyncPeriod, maxSyncPeriod),
&workqueue.BucketRateLimiter{Limiter: rate.NewLimiter(rate.Limit(500), 1000)}),
LogConstructor: func(request *reconcile.Request) logr.Logger {
log := mgr.GetLogger()
if request != nil {
log = log.WithValues("name", request.Name)
}
return log
},
})
if err != nil {
return err
}
return ctrl.Watch(
&source.Channel{Source: EventCh},
&handler.EnqueueRequestForObject{},
)
}, false)
}
var _ reconcile.Reconciler = &ReconcileNode{}
type NodeStatus struct {
NeedSyncOpenAPI *atomic.Bool
StatusChanged *atomic.Bool
LastGCTime time.Time
LastReconcileTime time.Time
}
type ReconcileNode struct {
client client.Client
scheme *runtime.Scheme
record record.EventRecorder
aliyun register.Interface
vswpool *vswitch.SwitchPool
cache sync.Map
fullSyncNodePeriod time.Duration
gcPeriod time.Duration
tracer trace.Tracer
}
type ctxMetaKey struct{}
func MetaCtx(ctx context.Context) *NodeStatus {
if metadata := ctx.Value(ctxMetaKey{}); metadata != nil {
if m, ok := metadata.(*NodeStatus); ok {
return m
}
}
// return nil to avoid mistake
return nil
}
func (n *ReconcileNode) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
ctx, span := n.tracer.Start(ctx, "reconcile", trace.WithAttributes(attribute.String("node", request.Name)))
defer span.End()
// write trace
var l logr.Logger
if span.SpanContext().IsValid() {
traceID := span.SpanContext().TraceID().String()
spanID := span.SpanContext().SpanID().String()
l = logf.FromContext(ctx, "traceID", traceID, "spanID", spanID)
ctx = logf.IntoContext(ctx, l)
} else {
l = logf.FromContext(ctx)
}
l.V(2).Info("reconcile node")
node := &networkv1beta1.Node{}
err := n.client.Get(ctx, client.ObjectKey{Name: request.Name}, node)
defer func() {
if err != nil {
span.SetStatus(codes.Error, "")
} else {
span.SetStatus(codes.Ok, "")
}
}()
if err != nil {
if k8sErr.IsNotFound(err) {
// special for vk nodes, we don't need to do anything
n.cache.Delete(request.Name)
return reconcile.Result{}, nil
}
return reconcile.Result{}, err
}
if !node.DeletionTimestamp.IsZero() {
patch := client.MergeFrom(node.DeepCopy())
changed := controllerutil.RemoveFinalizer(node, finalizer)
n.cache.Delete(request.Name)
if changed {
err = n.client.Patch(ctx, node, patch)
}
return reconcile.Result{}, err
}
// check if daemon has ready
if node.Spec.ENISpec == nil ||
node.Spec.Pool == nil {
return reconcile.Result{}, nil
}
if types.NodeExclusiveENIMode(node.Labels) == types.ExclusiveENIOnly {
return reconcile.Result{}, nil
}
ctx = logf.IntoContext(ctx, l.WithValues("rv", node.ResourceVersion))
var nodeStatus *NodeStatus
prev, ok := n.cache.Load(node.Name)
if !ok {
nodeStatus = &NodeStatus{
NeedSyncOpenAPI: &atomic.Bool{},
StatusChanged: &atomic.Bool{},
}
} else {
nodeStatus = prev.(*NodeStatus)
}
if nodeStatus.LastReconcileTime.Add(1 * time.Second).After(time.Now()) {
return reconcile.Result{RequeueAfter: 1 * time.Second}, nil
}
// write into ctx for latter use
// this should be thread safe to access NodeStatus
ctx = context.WithValue(ctx, ctxMetaKey{}, nodeStatus)
defer func() {
nodeStatus.LastReconcileTime = time.Now()
n.cache.Store(node.Name, nodeStatus)
}()
beforeStatus, err := runtime.DefaultUnstructuredConverter.ToUnstructured(node.Status.DeepCopy())
if err != nil {
return reconcile.Result{}, err
}
podRequests, err := n.getPods(ctx, node)
if err != nil {
return reconcile.Result{}, err
}
// on first startup, only node with pods on it, need to do a full sync
// for legacy , we need to handle trunk eni
if len(podRequests) != 0 && len(node.Status.NetworkInterfaces) == 0 {
span.AddEvent("newNodeForceSyncOpenAPI")
nodeStatus.NeedSyncOpenAPI.Store(true)
} else {
now := metav1.Now()
if node.Status.NextSyncOpenAPITime.Before(&now) {
span.AddEvent("ttlReachedSyncOpenAPI")
nodeStatus.NeedSyncOpenAPI.Store(true)
}
}
err = n.syncWithAPI(ctx, node)
if err != nil {
return reconcile.Result{}, err
}
syncErr := n.syncPods(ctx, podRequests, node)
if syncErr != nil {
l.Error(syncErr, "syncPods error")
}
afterStatus, err := runtime.DefaultUnstructuredConverter.ToUnstructured(node.Status.DeepCopy())
if err != nil {
return reconcile.Result{}, err
}
if !reflect.DeepEqual(beforeStatus, afterStatus) {
span.AddEvent("update node cr")
err = n.client.Status().Update(ctx, node)
if err != nil && nodeStatus.StatusChanged.CompareAndSwap(true, false) {
nodeStatus.NeedSyncOpenAPI.Store(true)
}
return reconcile.Result{RequeueAfter: 1 * time.Second}, err
}
return reconcile.Result{}, syncErr
}
// syncWithAPI will sync all eni from openAPI. Need to re-sync with local pods.
func (n *ReconcileNode) syncWithAPI(ctx context.Context, node *networkv1beta1.Node) error {
if !MetaCtx(ctx).NeedSyncOpenAPI.Load() {
return nil
}
ctx, span := n.tracer.Start(ctx, "syncWithAPI")
defer span.End()
l := logf.FromContext(ctx).WithName("syncWithAPI")
SyncOpenAPITotal.WithLabelValues(node.Name).Inc()
// all eni attached to this instance is take into count. (exclude member eni)
enis, err := n.aliyun.DescribeNetworkInterface(ctx, "", nil, node.Spec.NodeMetadata.InstanceID, "", "", node.Spec.ENISpec.TagFilter)
if err != nil {
return err
}
// ignore primary eni
enis = lo.Filter(enis, func(item *aliyunClient.NetworkInterface, index int) bool {
return item.Type != aliyunClient.ENITypePrimary
})
eniIDMap := map[string]struct{}{}
// add ip
for _, item := range enis {
log := l.WithValues("eni", item.NetworkInterfaceID, "status", item.Status)
eniIDMap[item.NetworkInterfaceID] = struct{}{}
remote := newENIFromAPI(item)
crENI, ok := node.Status.NetworkInterfaces[item.NetworkInterfaceID]
if !ok {
log.Info("sync eni with remote, new eni added")
// new eni, need add to local
if node.Status.NetworkInterfaces == nil {
node.Status.NetworkInterfaces = make(map[string]*networkv1beta1.NetworkInterface)
}
// we need cidr info
vsw, err := n.vswpool.GetByID(ctx, n.aliyun, remote.VSwitchID)
if err != nil {
return err
}
remote.IPv4CIDR = vsw.IPv4CIDR
remote.IPv6CIDR = vsw.IPv6CIDR
node.Status.NetworkInterfaces[item.NetworkInterfaceID] = remote
} else {
// exist record
// only ip is updated
mergeIPMap(log, remote.IPv4, crENI.IPv4)
mergeIPMap(log, remote.IPv6, crENI.IPv6)
// nb(l1b0k): use Deleting status in cr for eni we don't wanted
if crENI.Status != aliyunClient.ENIStatusDeleting {
crENI.Status = remote.Status
}
}
}
// del eni (those eni is not attached on ecs)
for id := range node.Status.NetworkInterfaces {
if _, ok := eniIDMap[id]; !ok {
// as the eni is not attached, so just delete it
if node.Status.NetworkInterfaces[id].NetworkInterfaceType == networkv1beta1.ENITypeSecondary {
l.Info("delete eni not found in remote, but in cr", "eni", id)
err = n.aliyun.DeleteNetworkInterface(ctx, id)
}
if err != nil {
l.Error(err, "eni not found on remote, delete eni error", "eni", id)
} else {
delete(node.Status.NetworkInterfaces, id)
}
}
}
// change the ts
node.Status.NextSyncOpenAPITime = metav1.NewTime(time.Now().Add(wait.Jitter(n.fullSyncNodePeriod, 1)))
node.Status.LastSyncOpenAPITime = metav1.Now()
MetaCtx(ctx).NeedSyncOpenAPI.Store(false)
return nil
}
func (n *ReconcileNode) getPods(ctx context.Context, node *networkv1beta1.Node) (map[string]*PodRequest, error) {
ctx, span := n.tracer.Start(ctx, "getPods")
defer span.End()
pods := &corev1.PodList{}
err := n.client.List(ctx, pods, client.MatchingFields{
"spec.nodeName": node.Name,
})
if err != nil {
return nil, err
}
podsMapper := make(map[string]*PodRequest, len(pods.Items))
for _, pod := range pods.Items {
if pod.Spec.HostNetwork ||
types.PodUseENI(&pod) ||
utils.PodSandboxExited(&pod) {
continue
}
requireERDMA := false
if node.Spec.ENISpec.EnableERDMA {
lo.ForEach(append(pod.Spec.InitContainers, pod.Spec.Containers...), func(item corev1.Container, index int) {
if res, ok := item.Resources.Limits[deviceplugin.ERDMAResName]; ok && !res.IsZero() {
requireERDMA = true
}
})
}
ips := []string{pod.Status.PodIP}
for _, v := range pod.Status.PodIPs {
ips = append(ips, v.IP)
}
ipv4, ipv6, err := podIPs(ips)
if err != nil {
return nil, err
}
podsMapper[pod.Namespace+"/"+pod.Name] = &PodRequest{
PodUID: string(pod.UID),
RequireIPv4: node.Spec.ENISpec.EnableIPv4,
RequireIPv6: node.Spec.ENISpec.EnableIPv6,
RequireERDMA: requireERDMA,
IPv4: ipv4,
IPv6: ipv6,
}
}
return podsMapper, nil
}
// syncPods build a resource mapping to each pod
func (n *ReconcileNode) syncPods(ctx context.Context, podsMapper map[string]*PodRequest, node *networkv1beta1.Node) error {
ctx, span := n.tracer.Start(ctx, "syncPods")
defer span.End()
l := logf.FromContext(ctx)
ipv4Map, ipv6Map := buildIPMap(podsMapper, node.Status.NetworkInterfaces)
// 1. delete unwanted
releasePodNotFound(l, podsMapper, ipv4Map, ipv6Map)
// 2. assign ip from local pool
unSucceedPods := assignIPFromLocalPool(l, podsMapper, ipv4Map, ipv6Map, node.Spec.ENISpec.EnableERDMA)
// 3. if there is no enough ip, try to allocate from api
err := n.addIP(ctx, unSucceedPods, node)
// 4. after all is assigned , we can re-allocate ip
ipv4Map, ipv6Map = buildIPMap(podsMapper, node.Status.NetworkInterfaces)
_ = assignIPFromLocalPool(l, podsMapper, ipv4Map, ipv6Map, node.Spec.ENISpec.EnableERDMA)
if err == nil {
err = n.gc(ctx, node)
}
return err
}
// releasePodNotFound release ip if there is no pod found
func releasePodNotFound(log logr.Logger, podsMapper map[string]*PodRequest, ipMapper ...map[string]*EniIP) {
for _, ipMap := range ipMapper {
for _, v := range ipMap {
if v.IP.PodID == "" {
continue
}
info, ok := podsMapper[v.IP.PodID]
if ok {
v.IP.PodUID = info.PodUID
continue
}
log.Info("pod released", "pod", v.IP.PodID, "ip", v.IP.IP)
v.IP.PodID = ""
v.IP.PodUID = ""
}
}
}
// DO NOT assign pod ip if pod already has one
func assignIPFromLocalPool(log logr.Logger, podsMapper map[string]*PodRequest, ipv4Map, ipv6Map map[string]*EniIP, enableEDRMA bool) map[string]*PodRequest {
pendingPods := lo.PickBy(podsMapper, func(key string, value *PodRequest) bool {
if value.RequireIPv4 && value.ipv4Ref == nil {
return true
}
if value.RequireIPv6 && value.ipv6Ref == nil {
return true
}
return value.IPv6 == "" && value.IPv4 == ""
})
unSucceedPods := map[string]*PodRequest{}
// handle exist pod ip
for podID, info := range pendingPods {
if info.RequireIPv4 && info.ipv4Ref == nil {
if info.IPv4 != "" {
// for take over case , pod has ip already, we can only assign to previous eni
eniIP, ok := ipv4Map[info.IPv4]
if ok && (eniIP.IP.PodID == "" || eniIP.IP.PodID == podID) {
info.ipv4Ref = eniIP
eniIP.IP.PodID = podID
eniIP.IP.PodUID = info.PodUID
log.Info("assign ip (from pod status)", "pod", podID, "ip", eniIP.IP, "eni", eniIP.NetworkInterface.ID)
}
}
}
if info.RequireIPv6 && info.ipv6Ref == nil {
if info.IPv6 != "" {
// for take over case , pod has ip already, we can only assign to previous eni
eniIP, ok := ipv6Map[info.IPv6]
if ok && (eniIP.IP.PodID == "" || eniIP.IP.PodID == podID) {
info.ipv6Ref = eniIP
eniIP.IP.PodID = podID
eniIP.IP.PodUID = info.PodUID
log.Info("assign ip (from pod status)", "pod", podID, "ip", eniIP.IP, "eni", eniIP.NetworkInterface.ID)
}
}
}
}
// only pending pods is handled
for podID, info := range pendingPods {
// choose eni first ...
if info.RequireIPv4 && info.ipv4Ref == nil {
if info.IPv4 == "" {
for _, v := range ipv4Map {
if v.NetworkInterface.Status != aliyunClient.ENIStatusInUse {
continue
}
// schedule to erdma card
if info.RequireERDMA &&
v.NetworkInterface.NetworkInterfaceTrafficMode != networkv1beta1.NetworkInterfaceTrafficModeHighPerformance {
continue
}
// do not schedule to erdma if node has erdma enable
if !info.RequireERDMA &&
enableEDRMA &&
v.NetworkInterface.NetworkInterfaceTrafficMode == networkv1beta1.NetworkInterfaceTrafficModeHighPerformance {
continue
}
if v.IP.Status == networkv1beta1.IPStatusValid && v.IP.PodID == "" {
info.ipv4Ref = &EniIP{
NetworkInterface: v.NetworkInterface,
IP: v.IP,
}
v.IP.PodID = podID
v.IP.PodUID = info.PodUID
log.Info("assign ip", "pod", podID, "ip", v.IP.IP, "eni", v.NetworkInterface.ID)
break
}
}
}
// not found
if info.ipv4Ref == nil {
unSucceedPods[podID] = info
continue
}
}
if info.RequireIPv6 && info.ipv6Ref == nil {
if info.IPv6 == "" {
for _, v := range ipv6Map {
if v.NetworkInterface.Status != aliyunClient.ENIStatusInUse {
continue
}
if info.RequireERDMA &&
v.NetworkInterface.NetworkInterfaceTrafficMode != networkv1beta1.NetworkInterfaceTrafficModeHighPerformance {
continue
}
if !info.RequireERDMA &&
enableEDRMA &&
v.NetworkInterface.NetworkInterfaceTrafficMode == networkv1beta1.NetworkInterfaceTrafficModeHighPerformance {
continue
}
if info.ipv4Ref != nil && v.NetworkInterface.ID != info.ipv4Ref.NetworkInterface.ID {
// we have chosen the eni
continue
}
if v.IP.Status == networkv1beta1.IPStatusValid && v.IP.PodID == "" {
info.ipv6Ref = &EniIP{
NetworkInterface: v.NetworkInterface,
IP: v.IP,
}
v.IP.PodID = podID
v.IP.PodUID = info.PodUID
log.Info("assign ip", "pod", podID, "ip", v.IP.IP, "eni", v.NetworkInterface.ID)
break
}
}
}
if info.ipv6Ref == nil {
if info.IPv4 == "" && info.ipv4Ref != nil {
log.Info("failed to get ipv6 addr, roll back ipv4", "pod", podID, "ip", info.ipv4Ref.IP)
info.ipv4Ref.IP.PodID = ""
info.ipv4Ref.IP.PodUID = ""
info.ipv4Ref = nil
}
unSucceedPods[podID] = info
continue
}
}
}
if len(unSucceedPods) > 0 {
log.Info("unSucceedPods pods", "pods", unSucceedPods)
}
return unSucceedPods
}
// addIP is called when there is no enough ip for current pods
// for cases, eni is attaching, we need to wait
func (n *ReconcileNode) addIP(ctx context.Context, unSucceedPods map[string]*PodRequest, node *networkv1beta1.Node) error {
ctx, span := n.tracer.Start(ctx, "addIP")
defer span.End()
normalPods := lo.PickBy(unSucceedPods, func(key string, value *PodRequest) bool {
return !value.RequireERDMA
})
rdmaPods := lo.PickBy(unSucceedPods, func(key string, value *PodRequest) bool {
return value.RequireERDMA
})
// before create eni , we need to check the quota
options := getEniOptions(node)
// handle trunk/secondary eni
assignEniWithOptions(node, len(normalPods)+node.Spec.Pool.MinPoolSize, options, func(option *eniOptions) bool {
return n.validateENI(ctx, option, []eniTypeKey{secondaryKey, trunkKey})
})
assignEniWithOptions(node, len(rdmaPods), options, func(option *eniOptions) bool {
return n.validateENI(ctx, option, []eniTypeKey{rdmaKey})
})
err := n.allocateFromOptions(ctx, node, options)
// update node condition based on eni status
updateNodeCondition(ctx, n.client, node.Name, options)
updateCrCondition(options)
if err != nil {
n.record.Event(node, corev1.EventTypeWarning, "AllocIPFailed", err.Error())
}
// the err is kept
return err
}
// updateCrCondition record openAPI error to cr
func updateCrCondition(options []*eniOptions) {
lo.ForEach(options, func(item *eniOptions, index int) {
if item.eniRef == nil {
return
}
if len(item.errors) == 0 {
item.eniRef.Conditions = nil
return
}
if item.eniRef.Conditions == nil {
item.eniRef.Conditions = make(map[string]networkv1beta1.Condition)
}
var ipNotEnoughErr []error
var otherErr []error
for _, err := range item.errors {
if isIPNotEnough(err) {
ipNotEnoughErr = append(ipNotEnoughErr, err)
} else {
otherErr = append(otherErr, err)
}
}
if len(ipNotEnoughErr) > 0 {
prev, ok := item.eniRef.Conditions[ConditionInsufficientIP]
if !ok {
item.eniRef.Conditions[ConditionInsufficientIP] = networkv1beta1.Condition{
ObservedTime: metav1.Now(),
Message: fmt.Sprintf("%s ip is not enough", item.eniRef.VSwitchID),
}
} else {
if prev.ObservedTime.Add(5 * time.Minute).Before(time.Now()) {
item.eniRef.Conditions[ConditionInsufficientIP] = networkv1beta1.Condition{
ObservedTime: metav1.Now(),
Message: fmt.Sprintf("%s ip is not enough", item.eniRef.VSwitchID)}
}
}
}
if len(otherErr) > 0 {
str := utilerrors.NewAggregate(otherErr).Error()
item.eniRef.Conditions[ConditionOperationErr] = networkv1beta1.Condition{
ObservedTime: metav1.Now(),
Message: str[:min(len(str), 256)],
}
}
})
}
func updateNodeCondition(ctx context.Context, c client.Client, nodeName string, options []*eniOptions) {
l := logf.FromContext(ctx)
k8sNode := &corev1.Node{}
err := c.Get(ctx, client.ObjectKey{Name: nodeName}, k8sNode)
if err != nil {
if k8sErr.IsNotFound(err) {
return
}
l.Error(err, "get node failed", "node", nodeName)
return
}
hasIPLeft := false
hasAllocatableEND := false
// here we don't check the pod type is met
lo.ForEach(options, func(item *eniOptions, index int) {
if hasIPLeft || hasAllocatableEND {
return
}
if item.eniRef != nil {
for _, v := range item.eniRef.IPv4 {
if v != nil {
if v.Status == networkv1beta1.IPStatusValid && v.PodID == "" {
hasIPLeft = true
break
}
}
}
}
// 1. eni is full
if item.isFull {
return
}
// 2. ip not enough
if lo.ContainsBy(item.errors, func(err error) bool {
return isIPNotEnough(err)
}) {
return
}
hasAllocatableEND = true
})
status := corev1.ConditionTrue
reason := types.IPResSufficientReason
message := "node has sufficient IP"
if !hasIPLeft && !hasAllocatableEND {
status = corev1.ConditionFalse
reason = types.IPResInsufficientReason
message = "node has insufficient IP"
}
transitionTime := metav1.Now()
for _, cond := range k8sNode.Status.Conditions {
if cond.Type == types.SufficientIPCondition && cond.Status == status &&
cond.Reason == reason && cond.Message == message {
if cond.LastHeartbeatTime.Add(5 * time.Minute).After(time.Now()) {
// refresh condition period 5min
return
}
transitionTime = cond.LastTransitionTime
}
}
now := metav1.Now()
ipResCondition := corev1.NodeCondition{
Type: types.SufficientIPCondition,
Status: status,
LastHeartbeatTime: now,
LastTransitionTime: transitionTime,
Reason: reason,
Message: message,
}
raw, err := json.Marshal(&[]corev1.NodeCondition{ipResCondition})
if err != nil {
l.Error(err, "marshal failed")
return
}
patch := []byte(fmt.Sprintf(`{"status":{"conditions":%s}}`, raw))
err = c.Status().Patch(ctx, k8sNode, client.RawPatch(k8stypes.StrategicMergePatchType, patch))
if err != nil {
l.Error(err, "patch node failed", "node", nodeName, "patch", patch)
}
}
func (n *ReconcileNode) validateENI(ctx context.Context, option *eniOptions, eniTypes []eniTypeKey) bool {
if !lo.Contains(eniTypes, option.eniTypeKey) {
return false
}
if option.eniRef != nil {
vsw, err := n.vswpool.GetByID(ctx, n.aliyun, option.eniRef.VSwitchID)
if err != nil {
logf.FromContext(ctx).Error(err, "failed to get vsw")
return false
}
if vsw.AvailableIPCount <= 0 {
option.errors = append(option.errors, vswitch.ErrNoAvailableVSwitch)
return false
}
return true
}
return true
}
// assignEniWithOptions determine how many ip should be added to this eni.
// In dual stack, ip on eni is automatically balanced.
func assignEniWithOptions(node *networkv1beta1.Node, toAdd int, options []*eniOptions, filterFunc func(option *eniOptions) bool) {
eniSpec := node.Spec.ENISpec
toAddIPv4, toAddIPv6 := 0, 0
if eniSpec.EnableIPv4 {
toAddIPv4 = toAdd
}
if eniSpec.EnableIPv6 {
toAddIPv6 = toAdd
}
// already ordered the eni
for _, option := range options {
if !filterFunc(option) {
continue
}
if option.eniRef != nil {
// exist eni
if toAddIPv4 > 0 {
toAddIPv4 -= len(getAllocatable(option.eniRef.IPv4))
if toAddIPv4 > 0 {
leftQuota := node.Spec.NodeCap.IPv4PerAdapter - len(option.eniRef.IPv4)
if leftQuota > 0 {
option.addIPv4N = min(leftQuota, toAddIPv4, batchSize)
toAddIPv4 -= option.addIPv4N
} else {
option.isFull = true
}
}
}
if toAddIPv6 > 0 {
// exclude the already assigned count, call this before quota check
toAddIPv6 -= len(getAllocatable(option.eniRef.IPv6))
if toAddIPv6 > 0 {
leftQuota := node.Spec.NodeCap.IPv6PerAdapter - len(option.eniRef.IPv6)
if leftQuota > 0 {
option.addIPv6N = min(leftQuota, toAddIPv6, batchSize)
toAddIPv6 -= option.addIPv6N
} else {
option.isFull = true
}
}
}
} else {
// for new eni
// for trunk , just create it
if option.eniTypeKey == trunkKey {
if toAddIPv4 <= 0 {
toAddIPv4 = 1
}
if eniSpec.EnableIPv6 && toAddIPv6 <= 0 {
toAddIPv6 = 1
}
}
if toAddIPv4 > 0 {
option.addIPv4N = min(node.Spec.NodeCap.IPv4PerAdapter, toAddIPv4, batchSize)
toAddIPv4 -= option.addIPv4N
}
if toAddIPv6 > 0 {
option.addIPv6N = min(node.Spec.NodeCap.IPv6PerAdapter, toAddIPv6, batchSize)
toAddIPv6 -= option.addIPv6N
}
}
}
}
// allocateFromOptions add ip or create eni, based on the options
func (n *ReconcileNode) allocateFromOptions(ctx context.Context, node *networkv1beta1.Node, options []*eniOptions) error {
ctx, span := n.tracer.Start(ctx, "allocateFromOptions")
defer span.End()
wg := wait.Group{}
lock := sync.Mutex{}
var errs []error
for _, option := range options {
if option.addIPv6N <= 0 && option.addIPv4N <= 0 {
continue
}
opt := option
// start a gr for each eni
wg.StartWithContext(ctx, func(ctx context.Context) {
var err error
if opt.eniRef == nil {
err = n.createENI(ctx, node, opt)
} else {
// for exists enis
err = n.assignIP(ctx, opt)
}
if err != nil {
opt.errors = append(opt.errors, err)
if apiErr.ErrorCodeIs(err, apiErr.ErrEniPerInstanceLimitExceeded, apiErr.ErrIPv4CountExceeded, apiErr.ErrIPv6CountExceeded) {
MetaCtx(ctx).NeedSyncOpenAPI.Store(true)
}
lock.Lock()
errs = append(errs, err)
lock.Unlock()
}
})
}
wg.Wait()
return utilerrors.NewAggregate(errs)
}
// gc follow those steps:
// 1. clean up deleting status eni and ip
// 2. mark unwanted eni/ip status to deleting
func (n *ReconcileNode) gc(ctx context.Context, node *networkv1beta1.Node) error {
ctx, span := n.tracer.Start(ctx, "gc")
defer span.End()
// 1. clean up deleting status eni and ip
err := n.handleStatus(ctx, node)
if err != nil {
return err
}
// 2. sort eni and find the victim
return n.adjustPool(ctx, node)
}
func (n *ReconcileNode) handleStatus(ctx context.Context, node *networkv1beta1.Node) error {
ctx, span := n.tracer.Start(ctx, "handleStatus")
defer span.End()
l := logf.FromContext(ctx).WithName("handleStatus")
// 1. clean up deleting status eni and ip
for _, eni := range node.Status.NetworkInterfaces {
// we use ENIStatusDeleting as the eni is not needed, so we always need to detach it and then delete it
log := l.WithValues("eni", eni.ID, "status", eni.Status)
switch eni.Status {
case aliyunClient.ENIStatusDeleting, aliyunClient.ENIStatusDetaching:
err := n.aliyun.DetachNetworkInterface(ctx, eni.ID, node.Spec.NodeMetadata.InstanceID, "")
if err != nil {
log.Error(err, "run gc failed")
continue
}
_, err = n.aliyun.WaitForNetworkInterface(ctx, eni.ID, aliyunClient.ENIStatusAvailable, backoff.Backoff(backoff.WaitENIStatus), true)
if err != nil {
if !errors.Is(err, apiErr.ErrNotFound) {
log.Error(err, "run gc failed")
continue
}
}
// wait eni detached
err = n.aliyun.DeleteNetworkInterface(ctx, eni.ID)
if err != nil {
log.Error(err, "run gc failed")
continue
}
MetaCtx(ctx).StatusChanged.Store(true)
log.Info("run gc succeed, eni removed", "eni", eni.ID)
// remove from status
delete(node.Status.NetworkInterfaces, eni.ID)
case aliyunClient.ENIStatusInUse:
var waitTime time.Duration
ips := make([]netip.Addr, 0)
for _, ip := range eni.IPv4 {