-
Notifications
You must be signed in to change notification settings - Fork 753
/
Copy pathipamd.go
1135 lines (995 loc) · 37.8 KB
/
ipamd.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
// Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package ipamd
import (
"fmt"
"net"
"os"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
log "github.com/cihub/seelog"
set "github.com/deckarep/golang-set"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/amazon-vpc-cni-k8s/ipamd/datastore"
"github.com/aws/amazon-vpc-cni-k8s/pkg/awsutils"
"github.com/aws/amazon-vpc-cni-k8s/pkg/eniconfig"
"github.com/aws/amazon-vpc-cni-k8s/pkg/k8sapi"
"github.com/aws/amazon-vpc-cni-k8s/pkg/networkutils"
)
// The package ipamd is a long running daemon which manages a warm pool of available IP addresses.
// It also monitors the size of the pool, dynamically allocates more ENIs when the pool size goes below
// the minimum threshold and frees them back when the pool size goes above max threshold.
const (
ipPoolMonitorInterval = 5 * time.Second
maxRetryCheckENI = 5
eniAttachTime = 10 * time.Second
nodeIPPoolReconcileInterval = 60 * time.Second
decreaseIPPoolInterval = 30 * time.Second
maxK8SRetries = 5
retryK8SInterval = 3 * time.Second
// ipReconcileCooldown is the amount of time that an IP address must wait until it can be added to the data store
// during reconciliation after being discovered on the EC2 instance metadata.
ipReconcileCooldown = 60 * time.Second
// This environment variable is used to specify the desired number of free IPs always available in the "warm pool".
// When it is not set, ipamd defaults to use all available IPs per ENI for that instance type.
// For example, for a m4.4xlarge node,
// If WARM-IP-TARGET is set to 1, and there are 9 pods running on the node, ipamd will try
// to make the "warm pool" have 10 IP addresses with 9 being assigned to pods and 1 free IP.
//
// If "WARM-IP-TARGET is not set, it will default to 30 (which the maximum number of IPs per ENI).
// If there are 9 pods running on the node, ipamd will try to make the "warm pool" have 39 IPs with 9 being
// assigned to pods and 30 free IPs.
envWarmIPTarget = "WARM_IP_TARGET"
noWarmIPTarget = 0
// This environment variable is used to specify the desired minimum number of total IPs.
// When it is not set, ipamd defaults to 0.
// For example, for a m4.4xlarge node,
// If WARM_IP_TARGET is set to 1 and MINIMUM_IP_TARGET is set to 12, and there are 9 pods running on the node,
// ipamd will make the "warm pool" have 12 IP addresses with 9 being assigned to pods and 3 free IPs.
//
// If "MINIMUM_IP_TARGET is not set, it will default to 0, which causes WARM_IP_TARGET settings to be the
// only settings considered.
envMinimumIPTarget = "MINIMUM_IP_TARGET"
noMinimumIPTarget = 0
// This environment is used to specify the desired number of free ENIs along with all of its IP addresses
// always available in "warm pool".
// When it is not set, it is default to 1.
//
// when "WARM-IP-TARGET" is defined, ipamd will use behavior defined for "WARM-IP-TARGET".
//
// For example, for a m4.4xlarge node
// If WARM_ENI_TARGET is set to 2, and there are 9 pods running on the node, ipamd will try to
// make the "warm pool" to have 2 extra ENIs and its IP addresses, in other words, 90 IP addresses
// with 9 IPs assigned to pods and 81 free IPs.
//
// If "WARM_ENI_TARGET" is not set, it defaults to 1, so if there are 9 pods running on the node,
// ipamd will try to make the "warm pool" have 1 extra ENI, in other words, 60 IPs with 9 already
// being assigned to pods and 51 free IPs.
envWarmENITarget = "WARM_ENI_TARGET"
defaultWarmENITarget = 1
// This environment variable is used to specify the maximum number of ENIs that will be allocated.
// When it is not set or less than 1, the default is to use the maximum available for the instance type.
//
// The maximum number of ENIs is in any case limited to the amount allowed for the instance type.
envMaxENI = "MAX_ENI"
defaultMaxENI = -1
// This environment is used to specify whether Pods need to use a security group and subnet defined in an ENIConfig CRD.
// When it is NOT set or set to false, ipamd will use primary interface security group and subnet for Pod network.
envCustomNetworkCfg = "AWS_VPC_K8S_CNI_CUSTOM_NETWORK_CFG"
)
var (
ipamdErr = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "awscni_ipamd_error_count",
Help: "The number of errors encountered in ipamd",
},
[]string{"fn"},
)
ipamdActionsInprogress = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "awscni_ipamd_action_inprogress",
Help: "The number of ipamd actions in progress",
},
[]string{"fn"},
)
enisMax = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "awscni_eni_max",
Help: "The maximum number of ENIs that can be attached to the instance",
},
)
ipMax = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "awscni_ip_max",
Help: "The maximum number of IP addresses that can be allocated to the instance",
},
)
reconcileCnt = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "awscni_reconcile_count",
Help: "The number of times ipamd reconciles on ENIs and IP addresses",
},
[]string{"fn"},
)
addIPCnt = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "awscni_add_ip_req_count",
Help: "The number of add IP address request",
},
)
delIPCnt = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "awscni_del_ip_req_count",
Help: "The number of delete IP address request",
},
[]string{"reason"},
)
prometheusRegistered = false
)
// IPAMContext contains node level control information
type IPAMContext struct {
awsClient awsutils.APIs
dataStore *datastore.DataStore
k8sClient k8sapi.K8SAPIs
useCustomNetworking bool
eniConfig eniconfig.ENIConfig
networkClient networkutils.NetworkAPIs
maxIPsPerENI int
maxENI int
warmENITarget int
warmIPTarget int
minimumIPTarget int
primaryIP map[string]string
lastNodeIPPoolAction time.Time
lastDecreaseIPPool time.Time
// reconcileCooldownCache keeps timestamps of the last time an IP address was unassigned from an ENI,
// so that we don't reconcile and add it back too quickly if IMDS lags behind reality.
reconcileCooldownCache ReconcileCooldownCache
terminating int32 // Flag to warn that the pod is about to shut down.
}
// Keep track of recently freed IPs to avoid reading stale EC2 metadata
type ReconcileCooldownCache struct {
cache map[string]time.Time
lock sync.RWMutex
}
// Add sets a timestamp for the list of IPs added that says how long they are not to be put back in the data store.
func (r *ReconcileCooldownCache) Add(ips []string) {
r.lock.Lock()
defer r.lock.Unlock()
expiry := time.Now().Add(ipReconcileCooldown)
for _, ip := range ips {
r.cache[ip] = expiry
}
}
// Remove removes an IP from the cooldown cache.
func (r *ReconcileCooldownCache) Remove(ip string) {
r.lock.Lock()
defer r.lock.Unlock()
log.Debugf("Removing %s from cooldown cache.", ip)
delete(r.cache, ip)
}
// RecentlyFreed checks if this IP was recently freed.
func (r *ReconcileCooldownCache) RecentlyFreed(ip string) (found, recentlyFreed bool) {
r.lock.Lock()
defer r.lock.Unlock()
now := time.Now()
if expiry, ok := r.cache[ip]; ok {
log.Debugf("Checking if IP %s has been recently freed. Cooldown expires at: %s. (Cooldown: %v)", ip, expiry, now.Sub(expiry) < 0)
return true, now.Sub(expiry) < 0
}
return false, false
}
func prometheusRegister() {
if !prometheusRegistered {
prometheus.MustRegister(ipamdErr)
prometheus.MustRegister(ipamdActionsInprogress)
prometheus.MustRegister(enisMax)
prometheus.MustRegister(ipMax)
prometheus.MustRegister(reconcileCnt)
prometheus.MustRegister(addIPCnt)
prometheus.MustRegister(delIPCnt)
prometheusRegistered = true
}
}
// New retrieves IP address usage information from Instance MetaData service and Kubelet
// then initializes IP address pool data store
func New(k8sapiClient k8sapi.K8SAPIs, eniConfig *eniconfig.ENIConfigController) (*IPAMContext, error) {
prometheusRegister()
c := &IPAMContext{}
c.k8sClient = k8sapiClient
c.networkClient = networkutils.New()
c.eniConfig = eniConfig
client, err := awsutils.New()
if err != nil {
log.Errorf("Failed to initialize awsutil interface %v", err)
return nil, errors.Wrap(err, "ipamd: can not initialize with AWS SDK interface")
}
c.awsClient = client
c.primaryIP = make(map[string]string)
c.reconcileCooldownCache.cache = make(map[string]time.Time)
c.warmENITarget = getWarmENITarget()
c.warmIPTarget = getWarmIPTarget()
c.minimumIPTarget = getMinimumIPTarget()
c.useCustomNetworking = UseCustomNetworkCfg()
err = c.nodeInit()
if err != nil {
return nil, err
}
return c, nil
}
//TODO need to break this function down(comments from CR)
func (c *IPAMContext) nodeInit() error {
ipamdActionsInprogress.WithLabelValues("nodeInit").Add(float64(1))
defer ipamdActionsInprogress.WithLabelValues("nodeInit").Sub(float64(1))
var err error
log.Debugf("Start node init")
c.maxENI, err = c.getMaxENI()
if err != nil {
log.Error("Failed to get ENI limit")
return err
}
enisMax.Set(float64(c.maxENI))
c.maxIPsPerENI, err = c.awsClient.GetENIipLimit()
if err != nil {
log.Error("Failed to get IPs per ENI limit")
return err
}
ipMax.Set(float64(c.maxIPsPerENI * c.maxENI))
enis, err := c.awsClient.GetAttachedENIs()
if err != nil {
log.Error("Failed to retrieve ENI info")
return errors.New("ipamd init: failed to retrieve attached ENIs info")
}
_, vpcCIDR, err := net.ParseCIDR(c.awsClient.GetVPCIPv4CIDR())
if err != nil {
log.Error("Failed to parse VPC IPv4 CIDR", err.Error())
return errors.Wrap(err, "ipamd init: failed to retrieve VPC CIDR")
}
primaryIP := net.ParseIP(c.awsClient.GetLocalIPv4())
err = c.networkClient.SetupHostNetwork(vpcCIDR, c.awsClient.GetVPCIPv4CIDRs(), c.awsClient.GetPrimaryENImac(), &primaryIP)
if err != nil {
log.Error("Failed to set up host network", err)
return errors.Wrap(err, "ipamd init: failed to set up host network")
}
c.dataStore = datastore.NewDataStore()
for _, eni := range enis {
log.Debugf("Discovered ENI %s, trying to set it up", eni.ENIID)
// Retry ENI sync
retry := 0
for {
retry++
err = c.setupENI(eni.ENIID, eni)
if retry > maxRetryCheckENI {
log.Errorf("Unable to discover attached IPs for ENI from metadata service")
ipamdErrInc("waitENIAttachedMaxRetryExceeded")
break
}
if err != nil {
log.Warnf("Error trying to set up ENI %s: %v", eni.ENIID, err)
if strings.Contains(err.Error(), "setupENINetwork: failed to find the link which uses MAC address") {
// If we can't find the matching link for this MAC address, there is no point in retrying for this ENI.
log.Errorf("Unable to match link for this ENI, going to the next one.")
break
}
log.Debugf("Unable to discover IPs for this ENI yet (attempt %d/%d)", retry, maxRetryCheckENI)
time.Sleep(eniAttachTime)
continue
}
log.Infof("ENI %s set up.", eni.ENIID)
break
}
}
localPods, err := c.getLocalPodsWithRetry()
log.Debugf("getLocalPodsWithRetry() found %d local pods", len(localPods))
if err != nil {
log.Warnf("During ipamd init, failed to get Pod information from Kubernetes API Server %v", err)
ipamdErrInc("nodeInitK8SGetLocalPodIPsFailed")
// This can happens when L-IPAMD starts before kubelet.
// TODO need to add node health stats here
return errors.Wrap(err, "failed to get running pods!")
}
rules, err := c.networkClient.GetRuleList()
if err != nil {
log.Errorf("During ipamd init: failed to retrieve IP rule list %v", err)
return nil
}
for _, ip := range localPods {
if ip.Container == "" {
log.Infof("Skipping Pod %s, Namespace %s, due to no matching container", ip.Name, ip.Namespace)
continue
}
if ip.IP == "" {
log.Infof("Skipping Pod %s, Namespace %s, due to no IP", ip.Name, ip.Namespace)
continue
}
log.Infof("Recovered AddNetwork for Pod %s, Namespace %s, Container %s", ip.Name, ip.Namespace, ip.Container)
_, _, err = c.dataStore.AssignPodIPv4Address(ip)
if err != nil {
ipamdErrInc("nodeInitAssignPodIPv4AddressFailed")
log.Warnf("During ipamd init, failed to use pod IP %s returned from Kubernetes API Server %v", ip.IP, err)
// TODO continue, but need to add node health stats here
// TODO need to feed this to controller on the health of pod and node
// This is a bug among kubelet/cni-plugin/l-ipamd/ec2-metadata that this particular pod is using an non existent ip address.
// Here we choose to continue instead of returning error and EXIT out L-IPAMD(exit L-IPAMD will make whole node out)
// The plan(TODO) is to feed this info back to controller and let controller cleanup this pod from this node.
}
// Update ip rules in case there is a change in VPC CIDRs, AWS_VPC_K8S_CNI_EXTERNALSNAT setting
srcIPNet := net.IPNet{IP: net.ParseIP(ip.IP), Mask: net.IPv4Mask(255, 255, 255, 255)}
vpcCIDRs := c.awsClient.GetVPCIPv4CIDRs()
var pbVPCcidrs []string
for _, cidr := range vpcCIDRs {
pbVPCcidrs = append(pbVPCcidrs, *cidr)
}
err = c.networkClient.UpdateRuleListBySrc(rules, srcIPNet, pbVPCcidrs, !c.networkClient.UseExternalSNAT())
if err != nil {
log.Errorf("UpdateRuleListBySrc in nodeInit() failed for IP %s: %v", ip.IP, err)
}
}
// For a new node, attach IPs
increasedPool, err := c.tryAssignIPs()
if err == nil && increasedPool {
c.updateLastNodeIPPoolAction()
}
return err
}
func (c *IPAMContext) getLocalPodsWithRetry() ([]*k8sapi.K8SPodInfo, error) {
var pods []*k8sapi.K8SPodInfo
var err error
for retry := 1; retry <= maxK8SRetries; retry++ {
pods, err = c.k8sClient.K8SGetLocalPodIPs()
if err == nil {
// Check for pods with no IP since the API server might not have the latest state of the node.
allPodsHaveAnIP := true
for _, pod := range pods {
if pod.IP == "" {
log.Infof("Pod %s, Namespace %s, has no IP", pod.Name, pod.Namespace)
allPodsHaveAnIP = false
}
}
if allPodsHaveAnIP {
break
}
log.Warnf("Not all pods have an IP, trying again in %v seconds.", retryK8SInterval.Seconds())
}
log.Infof("Not able to get local pods yet (attempt %d/%d): %v", retry, maxK8SRetries, err)
time.Sleep(retryK8SInterval)
}
if err != nil {
return nil, errors.Wrap(err, "no pods because apiserver not running.")
}
if pods == nil {
return nil, nil
}
return pods, nil
}
// StartNodeIPPoolManager monitors the IP pool, add or del them when it is required.
func (c *IPAMContext) StartNodeIPPoolManager() {
sleepDuration := ipPoolMonitorInterval / 2
for {
time.Sleep(sleepDuration)
c.updateIPPoolIfRequired()
time.Sleep(sleepDuration)
c.nodeIPPoolReconcile(nodeIPPoolReconcileInterval)
}
}
func (c *IPAMContext) updateIPPoolIfRequired() {
if c.nodeIPPoolTooLow() {
c.increaseIPPool()
} else if c.nodeIPPoolTooHigh() {
c.decreaseIPPool(decreaseIPPoolInterval)
}
if c.shouldRemoveExtraENIs() {
c.tryFreeENI()
}
}
// decreaseIPPool runs every `interval` and attempts to return unused ENIs and IPs
func (c *IPAMContext) decreaseIPPool(interval time.Duration) {
ipamdActionsInprogress.WithLabelValues("decreaseIPPool").Add(float64(1))
defer ipamdActionsInprogress.WithLabelValues("decreaseIPPool").Sub(float64(1))
now := time.Now()
timeSinceLast := now.Sub(c.lastDecreaseIPPool)
if timeSinceLast <= interval {
log.Debugf("Skipping decrease IP pool because time since last %v <= %v", timeSinceLast, interval)
return
}
log.Debugf("Starting to decrease IP pool")
c.tryUnassignIPsFromAll()
c.lastDecreaseIPPool = now
c.lastNodeIPPoolAction = now
total, used := c.dataStore.GetStats()
log.Debugf("Successfully decreased IP pool")
logPoolStats(total, used, c.maxIPsPerENI)
}
// tryFreeENI always tries to free one ENI
func (c *IPAMContext) tryFreeENI() {
if c.isTerminating() {
log.Debug("AWS CNI is terminating, not detaching any ENIs")
return
}
eni := c.dataStore.RemoveUnusedENIFromStore(c.warmIPTarget, c.minimumIPTarget)
if eni == "" {
return
}
log.Debugf("Start freeing ENI %s", eni)
err := c.awsClient.FreeENI(eni)
if err != nil {
ipamdErrInc("decreaseIPPoolFreeENIFailed")
log.Errorf("Failed to free ENI %s, err: %v", eni, err)
return
}
}
// tryUnassignIPsFromAll determines if there are IPs to free when we have extra IPs beyond the target and warmIPTargetDefined
// is enabled, deallocate extra IP addresses
func (c *IPAMContext) tryUnassignIPsFromAll() {
if _, over, warmIPTargetDefined := c.ipTargetState(); warmIPTargetDefined && over > 0 {
eniInfos := c.dataStore.GetENIInfos()
for eniID := range eniInfos.ENIIPPools {
ips, err := c.findFreeableIPs(eniID)
if err != nil {
log.Errorf("Error finding unassigned IPs: %s", err)
return
}
if len(ips) == 0 {
continue
}
// Delete IPs from datastore
var deletedIPs []string
for _, toDelete := range ips {
err := c.dataStore.DelIPv4AddressFromStore(eniID, toDelete)
if err != nil {
log.Warnf("Failed to delete IP %s on ENI %s from datastore: %s", toDelete, eniID, err)
ipamdErrInc("decreaseIPPool")
continue
} else {
deletedIPs = append(deletedIPs, toDelete)
}
}
// Deallocate IPs from the instance if they aren't used by pods.
if err := c.awsClient.DeallocIPAddresses(eniID, deletedIPs); err != nil {
log.Warnf("Failed to decrease IP pool by removing IPs %v from ENI %s: %s", deletedIPs, eniID, err)
} else {
log.Debugf("Successfully decreased IP pool by removing IPs %v from ENI %s", deletedIPs, eniID)
}
// Track the last time we unassigned IPs from an ENI. We won't reconcile any IPs in this cache
// for at least ipReconcileCooldown
c.reconcileCooldownCache.Add(deletedIPs)
}
}
}
// findFreeableIPs finds and returns IPs that are not assigned to Pods but are attached
// to ENIs on the node.
func (c *IPAMContext) findFreeableIPs(eni string) ([]string, error) {
podIPInfos := c.dataStore.GetPodInfos()
usedIPs := set.NewSet()
allocatedIPs := set.NewSet()
// Get IPs that are currently in use by pods
for _, pod := range *podIPInfos {
usedIPs.Add(pod.IP)
}
// Get IPs that are currently attached to the instance
eniInfos := c.dataStore.GetENIInfos()
eniIPPools := eniInfos.ENIIPPools
pool, ok := eniIPPools[eni]
if !ok {
return nil, fmt.Errorf("error finding available IPs: eni %s does not exist", eni)
}
for _, ip := range pool.IPv4Addresses {
allocatedIPs.Add(ip.Address)
}
availableIPs := allocatedIPs.Difference(usedIPs).ToSlice()
var freeableIPs []string
// Free the number of ips `over` the warm IP target, unless `over` is greater than the number of available IPs on
// this ENI. In that case we should only free the number of available IPs.
_, over, _ := c.ipTargetState()
numFreeable := min(over, len(availableIPs))
for _, ip := range availableIPs[:numFreeable] {
freeableIPs = append(freeableIPs, ip.(string))
}
return freeableIPs, nil
}
func (c *IPAMContext) increaseIPPool() {
log.Debug("Starting to increase IP pool size")
ipamdActionsInprogress.WithLabelValues("increaseIPPool").Add(float64(1))
defer ipamdActionsInprogress.WithLabelValues("increaseIPPool").Sub(float64(1))
short, _, warmIPTargetDefined := c.ipTargetState()
if warmIPTargetDefined && short == 0 {
log.Debugf("Skipping increase IP pool, warm IP target reached")
return
}
if c.isTerminating() {
log.Debug("AWS CNI is terminating, will not try to attach any new IPs or ENIs right now")
return
}
// Try to add more IPs to existing ENIs first.
increasedPool, err := c.tryAssignIPs()
if err != nil {
log.Errorf(err.Error())
}
if increasedPool {
c.updateLastNodeIPPoolAction()
} else {
// If we did not add an IP, try to add an ENI instead.
if c.dataStore.GetENIs() < c.maxENI {
if err = c.tryAllocateENI(); err == nil {
c.updateLastNodeIPPoolAction()
}
} else {
log.Debugf("Skipping ENI allocation as the instance's max ENI limit of %d is already reached", c.maxENI)
}
}
}
func (c *IPAMContext) updateLastNodeIPPoolAction() {
c.lastNodeIPPoolAction = time.Now()
total, used := c.dataStore.GetStats()
log.Debugf("Successfully increased IP pool, total: %d, used: %d", total, used)
logPoolStats(total, used, c.maxIPsPerENI)
}
func (c *IPAMContext) tryAllocateENI() error {
var securityGroups []*string
var subnet string
if c.useCustomNetworking {
eniCfg, err := c.eniConfig.MyENIConfig()
if err != nil {
log.Errorf("Failed to get pod ENI config")
return err
}
log.Infof("ipamd: using custom network config: %v, %s", eniCfg.SecurityGroups, eniCfg.Subnet)
for _, sgID := range eniCfg.SecurityGroups {
log.Debugf("Found security-group id: %s", sgID)
securityGroups = append(securityGroups, aws.String(sgID))
}
subnet = eniCfg.Subnet
}
eni, err := c.awsClient.AllocENI(c.useCustomNetworking, securityGroups, subnet)
if err != nil {
log.Errorf("Failed to increase pool size due to not able to allocate ENI %v", err)
ipamdErrInc("increaseIPPoolAllocENI")
return err
}
ipsToAllocate := c.maxIPsPerENI
short, _, warmIPTargetDefined := c.ipTargetState()
if warmIPTargetDefined {
ipsToAllocate = short
}
err = c.awsClient.AllocIPAddresses(eni, ipsToAllocate)
if err != nil {
log.Warnf("Failed to allocate %d IP addresses on an ENI: %v", ipsToAllocate, err)
// Continue to process the allocated IP addresses
ipamdErrInc("increaseIPPoolAllocIPAddressesFailed")
}
eniMetadata, err := c.waitENIAttached(eni)
if err != nil {
ipamdErrInc("increaseIPPoolwaitENIAttachedFailed")
log.Errorf("Failed to increase pool size: Unable to discover attached ENI from metadata service %v", err)
return err
}
err = c.setupENI(eni, eniMetadata)
if err != nil {
ipamdErrInc("increaseIPPoolsetupENIFailed")
log.Errorf("Failed to increase pool size: %v", err)
return err
}
return nil
}
// For an ENI, try to fill in missing IPs on an existing ENI
func (c *IPAMContext) tryAssignIPs() (increasedPool bool, err error) {
// If WARM_IP_TARGET is set, only proceed if we are short of target
short, _, warmIPTargetDefined := c.ipTargetState()
if warmIPTargetDefined && short == 0 {
return false, nil
}
// Find an ENI where we can add more IPs
eni := c.dataStore.GetENINeedsIP(c.maxIPsPerENI, c.useCustomNetworking)
if eni != nil && len(eni.IPv4Addresses) < c.maxIPsPerENI {
currentNumberOfAllocatedIPs := len(eni.IPv4Addresses)
// Try to allocate all available IPs for this ENI
err = c.awsClient.AllocIPAddresses(eni.ID, c.maxIPsPerENI-currentNumberOfAllocatedIPs)
if err != nil {
log.Warnf("failed to allocate all available IP addresses on ENI %s, err: %v", eni.ID, err)
// Try to just get one more IP
err = c.awsClient.AllocIPAddresses(eni.ID, 1)
if err != nil {
ipamdErrInc("increaseIPPoolAllocIPAddressesFailed")
return false, errors.Wrap(err, fmt.Sprintf("failed to allocate one IP addresses on ENI %s, err: %v", eni.ID, err))
}
}
ec2Addrs, _, err := c.getENIaddresses(eni.ID)
if err != nil {
ipamdErrInc("increaseIPPoolGetENIaddressesFailed")
return true, errors.Wrap(err, "failed to get ENI IP addresses during IP allocation")
}
c.addENIaddressesToDataStore(ec2Addrs, eni.ID)
return true, nil
}
return false, nil
}
// setupENI does following:
// 1) add ENI to datastore
// 2) set up linux ENI related networking stack.
// 3) add all ENI's secondary IP addresses to datastore
func (c *IPAMContext) setupENI(eni string, eniMetadata awsutils.ENIMetadata) error {
ec2Addrs, eniPrimaryIP, err := c.getENIaddresses(eni)
if err != nil {
return errors.Wrapf(err, "failed to retrieve ENI %s IP addresses", eni)
}
// Add the ENI to the datastore
err = c.dataStore.AddENI(eni, eniMetadata.DeviceNumber, eni == c.awsClient.GetPrimaryENI())
if err != nil && err.Error() != datastore.DuplicatedENIError {
return errors.Wrapf(err, "failed to add ENI %s to data store", eni)
}
// For secondary ENIs, set up the network
if eni != c.awsClient.GetPrimaryENI() {
err = c.networkClient.SetupENINetwork(eniPrimaryIP, eniMetadata.MAC, eniMetadata.DeviceNumber, eniMetadata.SubnetIPv4CIDR)
if err != nil {
log.Errorf("Failed to set up networking for ENI %s", eni)
return errors.Wrapf(err, "failed to set up ENI %s network", eni)
}
}
c.primaryIP[eni] = c.addENIaddressesToDataStore(ec2Addrs, eni)
return nil
}
// return primary ip address on the interface
func (c *IPAMContext) addENIaddressesToDataStore(ec2Addrs []*ec2.NetworkInterfacePrivateIpAddress, eni string) string {
var primaryIP string
for _, ec2Addr := range ec2Addrs {
if aws.BoolValue(ec2Addr.Primary) {
primaryIP = aws.StringValue(ec2Addr.PrivateIpAddress)
continue
}
err := c.dataStore.AddIPv4AddressToStore(eni, aws.StringValue(ec2Addr.PrivateIpAddress))
if err != nil && err.Error() != datastore.IPAlreadyInStoreError {
log.Warnf("Failed to increase IP pool, failed to add IP %s to data store", ec2Addr.PrivateIpAddress)
// continue to add next address
// TODO need to add health stats for err
ipamdErrInc("addENIaddressesToDataStoreAddENIIPv4AddressFailed")
}
}
return primaryIP
}
// returns all addresses on ENI, the primary address on ENI, error
func (c *IPAMContext) getENIaddresses(eni string) ([]*ec2.NetworkInterfacePrivateIpAddress, string, error) {
ec2Addrs, _, err := c.awsClient.DescribeENI(eni)
if err != nil {
return nil, "", errors.Wrapf(err, "failed to find ENI addresses for ENI %s", eni)
}
for _, ec2Addr := range ec2Addrs {
if aws.BoolValue(ec2Addr.Primary) {
eniPrimaryIP := aws.StringValue(ec2Addr.PrivateIpAddress)
return ec2Addrs, eniPrimaryIP, nil
}
}
return nil, "", errors.Errorf("failed to find the ENI's primary address for ENI %s", eni)
}
func (c *IPAMContext) waitENIAttached(eni string) (awsutils.ENIMetadata, error) {
// Wait until the ENI shows up in the instance metadata service
retry := 0
for {
enis, err := c.awsClient.GetAttachedENIs()
if err != nil {
log.Warnf("Failed to increase pool, error trying to discover attached ENIs: %v ", err)
} else {
// Verify that the ENI we are waiting for is in the returned list
for _, returnedENI := range enis {
if eni == returnedENI.ENIID {
return returnedENI, nil
}
}
log.Debugf("Not able to find the right ENI yet (attempt %d/%d)", retry, maxRetryCheckENI)
}
retry++
if retry > maxRetryCheckENI {
ipamdErrInc("waitENIAttachedMaxRetryExceeded")
return awsutils.ENIMetadata{}, errors.New("waitENIAttached: giving up trying to retrieve ENIs from metadata service")
}
log.Debugf("Not able to discover attached ENIs yet (attempt %d/%d)", retry, maxRetryCheckENI)
time.Sleep(eniAttachTime)
}
}
// getMaxENI returns the maximum number of ENIs to attach to this instance. This is calculated as the lesser of
// the limit for the instance type and the value configured via the MAX_ENI environment variable. If the value of
// the environment variable is 0 or less, it will be ignored and the maximum for the instance is returned.
func (c *IPAMContext) getMaxENI() (int, error) {
instanceMaxENI, err := c.awsClient.GetENILimit()
if err != nil {
return 0, err
}
inputStr, found := os.LookupEnv(envMaxENI)
envMax := defaultMaxENI
if found {
if input, err := strconv.Atoi(inputStr); err == nil && input >= 1 {
log.Debugf("Using MAX_ENI %v", input)
envMax = input
}
}
if envMax >= 1 && envMax < instanceMaxENI {
return envMax, nil
}
return instanceMaxENI, nil
}
func getWarmENITarget() int {
inputStr, found := os.LookupEnv(envWarmENITarget)
if !found {
return defaultWarmENITarget
}
if input, err := strconv.Atoi(inputStr); err == nil {
if input < 0 {
return defaultWarmENITarget
}
log.Debugf("Using WARM_ENI_TARGET %v", input)
return input
}
return defaultWarmENITarget
}
func logPoolStats(total, used, maxAddrsPerENI int) {
log.Debugf("IP pool stats: total = %d, used = %d, c.maxIPsPerENI = %d",
total, used, maxAddrsPerENI)
}
// nodeIPPoolTooLow returns true if IP pool is below low threshold
func (c *IPAMContext) nodeIPPoolTooLow() bool {
short, _, warmIPTargetDefined := c.ipTargetState()
if warmIPTargetDefined {
return short > 0
}
total, used := c.dataStore.GetStats()
logPoolStats(total, used, c.maxIPsPerENI)
available := total - used
poolTooLow := available < c.maxIPsPerENI*c.warmENITarget || (c.warmENITarget == 0 && available == 0)
if poolTooLow {
log.Tracef("IP pool is too low: available (%d) < ENI target (%d) * addrsPerENI (%d)", available, c.warmENITarget, c.maxIPsPerENI)
} else {
log.Tracef("IP pool is NOT too low: available (%d) >= ENI target (%d) * addrsPerENI (%d)", available, c.warmENITarget, c.maxIPsPerENI)
}
return poolTooLow
}
// nodeIPPoolTooHigh returns true if IP pool is above high threshold
func (c *IPAMContext) nodeIPPoolTooHigh() bool {
_, over, warmIPTargetDefined := c.ipTargetState()
if warmIPTargetDefined {
return over > 0
}
// We only ever report the pool being too high if WARM_IP_TARGET is set
return false
}
// shouldRemoveExtraENIs returns true if we should attempt to find an ENI to free. When WARM_IP_TARGET is set, we
// always check and do verification in getDeletableENI()
func (c *IPAMContext) shouldRemoveExtraENIs() bool {
_, _, warmIPTargetDefined := c.ipTargetState()
if warmIPTargetDefined {
return true
}
total, used := c.dataStore.GetStats()
logPoolStats(total, used, c.maxIPsPerENI)
available := total - used
// We need the +1 to make sure we are not going below the WARM_ENI_TARGET.
shouldRemoveExtra := available >= (c.warmENITarget+1)*c.maxIPsPerENI
if shouldRemoveExtra {
log.Tracef("It might be possible to remove extra ENIs because available (%d) >= (ENI target (%d) + 1) * addrsPerENI (%d): ", available, c.warmENITarget, c.maxIPsPerENI)
} else {
log.Tracef("Its NOT possible to remove extra ENIs because available (%d) < (ENI target (%d) + 1) * addrsPerENI (%d): ", available, c.warmENITarget, c.maxIPsPerENI)
}
return shouldRemoveExtra
}
func ipamdErrInc(fn string) {
ipamdErr.With(prometheus.Labels{"fn": fn}).Inc()
}
// nodeIPPoolReconcile reconcile ENI and IP info from metadata service and IP addresses in datastore
func (c *IPAMContext) nodeIPPoolReconcile(interval time.Duration) {
ipamdActionsInprogress.WithLabelValues("nodeIPPoolReconcile").Add(float64(1))
defer ipamdActionsInprogress.WithLabelValues("nodeIPPoolReconcile").Sub(float64(1))
curTime := time.Now()
timeSinceLast := curTime.Sub(c.lastNodeIPPoolAction)
if timeSinceLast <= interval {
log.Debugf("nodeIPPoolReconcile: skipping because time since last %v <= %v", timeSinceLast, interval)
return
}
log.Debug("Reconciling ENI/IP pool info...")
attachedENIs, err := c.awsClient.GetAttachedENIs()
if err != nil {
log.Errorf("IP pool reconcile: Failed to get attached ENI info: %v", err.Error())
ipamdErrInc("reconcileFailedGetENIs")
return
}
curENIs := c.dataStore.GetENIInfos()
// Mark phase
for _, attachedENI := range attachedENIs {
eniIPPool, err := c.dataStore.GetENIIPPools(attachedENI.ENIID)
if err == nil {
// If the attached ENI is in the data store
log.Debugf("Reconcile existing ENI %s IP pool", attachedENI.ENIID)
// Reconcile IP pool
c.eniIPPoolReconcile(eniIPPool, attachedENI, attachedENI.ENIID)
// Mark action, remove this ENI from curENIs list
delete(curENIs.ENIIPPools, attachedENI.ENIID)
continue
}
// Add new ENI
log.Debugf("Reconcile and add a new ENI %s", attachedENI)
err = c.setupENI(attachedENI.ENIID, attachedENI)
if err != nil {
log.Errorf("IP pool reconcile: Failed to set up ENI %s network: %v", attachedENI.ENIID, err)
ipamdErrInc("eniReconcileAdd")
// Continue if having trouble with ONLY 1 ENI, instead of bailout here?
continue
}
reconcileCnt.With(prometheus.Labels{"fn": "eniReconcileAdd"}).Inc()
}
// Sweep phase: since the marked ENI have been removed, the remaining ones needs to be sweeped
for eni := range curENIs.ENIIPPools {
log.Infof("Reconcile and delete detached ENI %s", eni)
err = c.dataStore.RemoveENIFromDataStore(eni)
if err != nil {
log.Errorf("IP pool reconcile: Failed to delete ENI during reconcile: %v", err)
ipamdErrInc("eniReconcileDel")
continue
}
reconcileCnt.With(prometheus.Labels{"fn": "eniReconcileDel"}).Inc()
}
log.Debug("Successfully Reconciled ENI/IP pool")
c.lastNodeIPPoolAction = curTime
}
func (c *IPAMContext) eniIPPoolReconcile(ipPool map[string]*datastore.AddressInfo, attachedENI awsutils.ENIMetadata, eni string) {
for _, localIP := range attachedENI.LocalIPv4s {
if localIP == c.primaryIP[eni] {
log.Debugf("Reconcile and skip primary IP %s on ENI %s", localIP, eni)
continue
}
// Check if this IP was recently freed
found, recentlyFreed := c.reconcileCooldownCache.RecentlyFreed(localIP)
if found {
if recentlyFreed {
log.Debugf("Reconcile skipping IP %s on ENI %s because it was recently unassigned from the ENI.", localIP, eni)
continue
} else {
log.Debugf("This IP was recently freed, but is out of cooldown. We need to verify with EC2 control plane.")
// Call EC2 to verify
ec2Addresses, _, err := c.getENIaddresses(eni)
if err != nil {
log.Error("Failed to fetch ENI IP addresses!")
continue
} else {
// Verify that the IP really belongs to this ENI
isReallyAttachedToENI := false
for _, ec2Addr := range ec2Addresses {
if localIP == aws.StringValue(ec2Addr.PrivateIpAddress) {
isReallyAttachedToENI = true
log.Debugf("Verified that IP %s is attached to ENI %s", localIP, eni)
break
}
}
if isReallyAttachedToENI {
c.reconcileCooldownCache.Remove(localIP)
} else {
log.Warnf("Skipping IP %s on ENI %s because it does not belong to this ENI!.", localIP, eni)
continue
}
}
}