forked from aws/amazon-vpc-cni-k8s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipamd.go
657 lines (561 loc) · 20.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
// 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 (
"net"
"os"
"strconv"
"strings"
"time"
log "github.com/cihub/seelog"
"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/docker"
"github.com/aws/amazon-vpc-cni-k8s/pkg/k8sapi"
"github.com/aws/amazon-vpc-cni-k8s/pkg/networkutils"
)
// Package ipamd is a long running daemon which manages a warn-pool of available IP addresses.
// It also monitors the size of the pool, dynamically allocate more ENIs when the pool size goes below threshold and
// free them back when the pool size goes above max threshold.
const (
ipPoolMonitorInterval = 5 * time.Second
maxRetryCheckENI = 5
eniAttachTime = 10 * time.Second
defaultWarmENITarget = 1
nodeIPPoolReconcileInterval = 60 * time.Second
maxK8SRetries = 12
retryK8SInterval = 5 * time.Second
)
var (
ipamdErr = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "ipamd_error_count",
Help: "The number of errors encountered in ipamd",
},
[]string{"fn", "error"},
)
ipamdActionsInprogress = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "ipamd_action_inprogress",
Help: "The number of ipamd actions inprogress",
},
[]string{"fn"},
)
enisMax = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "eni_max",
Help: "The maximum number of ENIs that can be attached to the instance",
},
)
ipMax = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "ip_max",
Help: "The maximum number of IP addresses that can be allocated to the instance",
},
)
reconcileCnt = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "reconcile_count",
Help: "The number of times ipamD reconciles on ENIs and IP addresses",
},
[]string{"fn"},
)
prometheusRegistered = false
)
// IPAMContext contains node level control information
type IPAMContext struct {
awsClient awsutils.APIs
dataStore *datastore.DataStore
k8sClient *k8sapi.Controller
networkClient networkutils.NetworkAPIs
currentMaxAddrsPerENI int
maxAddrsPerENI int
// maxENI indicate the maximum number of ENIs can be attached to the instance
// It is initialized to 0 and it is set to current number of ENIs attached
// when ipamD receives AttachmentLimitExceeded error
maxENI int
primaryIP map[string]string
lastNodeIPPoolAction time.Time
}
func prometheusRegister() {
if !prometheusRegistered {
prometheus.MustRegister(ipamdErr)
prometheus.MustRegister(ipamdActionsInprogress)
prometheus.MustRegister(enisMax)
prometheus.MustRegister(ipMax)
prometheus.MustRegister(reconcileCnt)
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.Controller) (*IPAMContext, error) {
prometheusRegister()
c := &IPAMContext{}
c.k8sClient = k8sapiClient
c.networkClient = networkutils.New()
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
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))
maxENIs, err := c.awsClient.GetENILimit()
if err == nil {
enisMax.Set(float64(maxENIs))
}
maxIPs, err := c.awsClient.GetENIipLimit()
if err == nil {
ipMax.Set(float64(maxIPs * int64(maxENIs)))
}
c.primaryIP = make(map[string]string)
enis, err := c.awsClient.GetAttachedENIs()
if err != nil {
log.Error("Failed to retrive 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, &primaryIP)
if err != nil {
log.Error("Failed to setup host network", err)
return errors.Wrap(err, "ipamd init: failed to setup host network")
}
c.dataStore = datastore.NewDataStore()
for _, eni := range enis {
log.Debugf("Discovered ENI %s", eni.ENIID)
err = c.awsClient.AllocAllIPAddress(eni.ENIID)
if err != nil {
ipamdErrInc("nodeInitAllocAllIPAddressFailed", err)
//TODO need to increment ipamd err stats
log.Warn("During ipamd init: error encountered on trying to allocate all available IP addresses", err)
// fall though to add those allocated OK addresses
}
err = c.setupENI(eni.ENIID, eni)
if err != nil {
log.Errorf("Failed to setup eni %s network: %v", eni.ENIID, err)
return errors.Wrapf(err, "Failed to setup eni %v", eni.ENIID)
}
}
usedIPs, err := c.getLocalPodsWithRetry()
if err != nil {
log.Warnf("During ipamd init, failed to get Pod information from Kubelet %v", err)
ipamdErrInc("nodeInitK8SGetLocalPodIPsFailed", err)
// This can happens when L-IPAMD starts before kubelet.
// TODO need to add node health stats here
return nil
}
for _, ip := range usedIPs {
if ip.Container == "" {
log.Infof("Skipping Pod %s, Namespace %s due to no matching container",
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", err)
log.Warnf("During ipamd init, failed to use pod ip %s returned from Kubelet %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.
}
}
return nil
}
func (c *IPAMContext) getLocalPodsWithRetry() ([]k8sapi.K8SPodInfo, error) {
var pods []k8sapi.K8SPodInfo
var err error
for retry := 1; retry <= maxK8SRetries; retry++ {
pods, err = c.k8sClient.GetLocalPods()
if err == nil {
break
}
log.Infof("Not able to get local pods yet (attempt %d/%d): %v", retry, maxK8SRetries, err)
time.Sleep(retryK8SInterval)
}
if pods == nil {
return nil, errors.New("unable to get local pods, giving up")
}
containers, err := docker.GetRunningContainers()
if err != nil {
log.Errorf("Not able to get container info from docker: %v", err)
return nil, errors.Wrapf(err, "not able to get container info from docker")
}
// TODO consider using map
for i, pod := range pods {
// needs to find the container ID
for _, container := range containers {
//e.g. /k8s_POD_worker-hello-5974f49799-q9vct_default_c31721a2-5dfb-11e8-b09c-022ad646a21e_0
k8sName := "/k8s_POD_" + pod.Name + "_" + pod.Namespace + "_" + pod.UID + "_0"
if container.Name == k8sName {
log.Debugf("Found pod(%v)'s container ID: %v ", k8sName, container.ID)
pods[i].Container = container.ID
break
}
}
}
return pods, nil
}
// StartNodeIPPoolManager monitors the IP Pool, add or del them when it is required.
func (c *IPAMContext) StartNodeIPPoolManager() {
for {
time.Sleep(ipPoolMonitorInterval)
c.updateIPPoolIfRequired()
c.nodeIPPoolReconcile(nodeIPPoolReconcileInterval)
}
}
func (c *IPAMContext) updateIPPoolIfRequired() {
c.retryAllocENIIP()
if c.nodeIPPoolTooLow() {
c.increaseIPPool()
} else if c.nodeIPPoolTooHigh() {
c.decreaseIPPool()
}
}
func (c *IPAMContext) retryAllocENIIP() {
ipamdActionsInprogress.WithLabelValues("retryAllocENIIP").Add(float64(1))
defer ipamdActionsInprogress.WithLabelValues("retryAllocENIIP").Sub(float64(1))
maxIPLimit, err := c.awsClient.GetENIipLimit()
if err != nil {
log.Infof("Failed to retrieve ENI IP limit: %v", err)
return
}
eni := c.dataStore.GetENINeedsIP(maxIPLimit)
if eni != nil {
log.Debugf("Attempt again to allocate IP address for eni :%s", eni.Id)
err := c.awsClient.AllocAllIPAddress(eni.Id)
if err != nil {
ipamdErrInc("retryAllocENIIPAllocAllIPAddressFailed", err)
log.Warn("During eni repair: error encountered on allocate IP address", err)
return
}
ec2Addrs, _, err := c.getENIaddresses(eni.Id)
if err != nil {
ipamdErrInc("retryAllocENIIPgetENIaddressesFailed", err)
log.Warn("During eni repair: failed to get ENI ip addresses", err)
return
}
c.lastNodeIPPoolAction = time.Now()
c.addENIaddressesToDataStore(ec2Addrs, eni.Id)
}
}
func (c *IPAMContext) decreaseIPPool() {
ipamdActionsInprogress.WithLabelValues("decreaseIPPool").Add(float64(1))
defer ipamdActionsInprogress.WithLabelValues("decreaseIPPool").Sub(float64(1))
eni, err := c.dataStore.FreeENI()
if err != nil {
ipamdErrInc("decreaseIPPoolFreeENIFailed", err)
log.Errorf("Failed to decrease pool %v", err)
return
}
log.Debugf("Start freeing eni %s", eni)
c.awsClient.FreeENI(eni)
c.lastNodeIPPoolAction = time.Now()
total, used := c.dataStore.GetStats()
log.Debugf("Successfully decreased IP Pool")
logPoolStats(total, used, c.currentMaxAddrsPerENI, c.maxAddrsPerENI)
}
func isAttachmentLimitExceededError(err error) bool {
return strings.Contains(err.Error(), "AttachmentLimitExceeded")
}
func (c *IPAMContext) increaseIPPool() {
log.Debug("Start increasing IP Pool size")
ipamdActionsInprogress.WithLabelValues("increaseIPPool").Add(float64(1))
defer ipamdActionsInprogress.WithLabelValues("increaseIPPool").Sub(float64(1))
maxENIs, err := c.awsClient.GetENILimit()
enisMax.Set(float64(maxENIs))
if err == nil && maxENIs == c.dataStore.GetENIs() {
log.Debugf("Skipping increase IPPOOL due to max ENI already attached to the instance : %d", maxENIs)
return
}
if (c.maxENI > 0) && (c.maxENI == c.dataStore.GetENIs()) {
if c.maxENI < maxENIs {
errString := "desired: " + strconv.FormatInt(int64(maxENIs), 10) + "current: " + strconv.FormatInt(int64(c.maxENI), 10)
ipamdErrInc("unExpectedMaxENIAttached", errors.New(errString))
}
log.Debugf("Skipping increase IPPOOL due to max ENI already attached to the instance : %d", c.maxENI)
return
}
eni, err := c.awsClient.AllocENI()
if err != nil {
log.Errorf("Failed to increase pool size due to not able to allocate ENI %v", err)
if isAttachmentLimitExceededError(err) {
c.maxENI = c.dataStore.GetENIs()
log.Infof("Discovered the instance max ENI allowed is: %d", c.maxENI)
}
// TODO need to add health stats
ipamdErrInc("increaseIPPoolAllocENI", err)
return
}
err = c.awsClient.AllocAllIPAddress(eni)
if err != nil {
log.Warnf("Failed to allocate all available ip addresses on an ENI %v", err)
// continue to proecsses those allocated ip addresses
ipamdErrInc("increaseIPPoolAllocAllIPAddressFailed", err)
}
eniMetadata, err := c.waitENIAttached(eni)
if err != nil {
ipamdErrInc("increaseIPPoolwaitENIAttachedFailed", err)
log.Errorf("Failed to increase pool size: not able to discover attached eni from metadata service %v", err)
return
}
err = c.setupENI(eni, eniMetadata)
if err != nil {
ipamdErrInc("increaseIPPoolsetupENIFailed", err)
log.Errorf("Failed to increase pool size: %v", err)
return
}
c.lastNodeIPPoolAction = time.Now()
total, used := c.dataStore.GetStats()
log.Debugf("Successfully increased IP Pool")
logPoolStats(total, used, c.currentMaxAddrsPerENI, c.maxAddrsPerENI)
}
// setupENI does following:
// 1) add ENI to datastore
// 2) add all ENI's secondary IP addresses to datastore
// 3) setup linux eni related networking stack.
func (c *IPAMContext) setupENI(eni string, eniMetadata awsutils.ENIMetadata) error {
// Have discovered the attached ENI from metadata service
// add eni's IP to IP pool
err := c.dataStore.AddENI(eni, int(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)
}
ec2Addrs, eniPrimaryIP, err := c.getENIaddresses(eni)
if err != nil {
return errors.Wrapf(err, "failed to retrieve eni %s ip addresses", eni)
}
c.currentMaxAddrsPerENI = len(ec2Addrs)
if c.currentMaxAddrsPerENI > c.maxAddrsPerENI {
c.maxAddrsPerENI = c.currentMaxAddrsPerENI
}
if eni != c.awsClient.GetPrimaryENI() {
err = c.networkClient.SetupENINetwork(eniPrimaryIP, eniMetadata.MAC,
int(eniMetadata.DeviceNumber), eniMetadata.SubnetIPv4CIDR)
if err != nil {
return errors.Wrapf(err, "failed to setup 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.AddENIIPv4Address(eni, aws.StringValue(ec2Addr.PrivateIpAddress))
if err != nil && err.Error() != datastore.DuplicateIPError {
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", err)
}
}
return primaryIP
}
// returns all addresses on eni, the primary adderss 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, "fail 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.Wrapf(err, "faind to find eni's primary address for eni %s", eni)
}
func (c *IPAMContext) waitENIAttached(eni string) (awsutils.ENIMetadata, error) {
// wait till eni is showup in the instance meta data 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)
time.Sleep(eniAttachTime)
continue
}
// verify eni is in the returned eni list
for _, returnedENI := range enis {
if eni == returnedENI.ENIID {
return returnedENI, nil
}
}
retry++
if retry > maxRetryCheckENI {
log.Errorf("Unable to discover attached ENI from metadata service")
// TODO need to add health stats
ipamdErrInc("waitENIAttachedMaxRetryExceeded", err)
return awsutils.ENIMetadata{}, errors.New("add eni: not able to retrieve eni from metata service")
}
log.Debugf("Not able to discover attached eni yet (attempt %d/%d)", retry, maxRetryCheckENI)
time.Sleep(eniAttachTime)
}
}
func getWarmENITarget() int {
inputStr, found := os.LookupEnv("WARM_ENI_TARGET")
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, currentMaxAddrsPerENI, maxAddrsPerENI int) {
log.Debugf("IP pool stats: total = %d, used = %d, c.currentMaxAddrsPerENI = %d, c.maxAddrsPerENI = %d",
total, used, currentMaxAddrsPerENI, maxAddrsPerENI)
}
//nodeIPPoolTooLow returns true if IP pool is below low threshhold
func (c *IPAMContext) nodeIPPoolTooLow() bool {
warmENITarget := getWarmENITarget()
total, used := c.dataStore.GetStats()
logPoolStats(total, used, c.currentMaxAddrsPerENI, c.maxAddrsPerENI)
available := total - used
return (available <= c.currentMaxAddrsPerENI*warmENITarget)
}
// NodeIPPoolTooHigh returns true if IP pool is above high threshhold
func (c *IPAMContext) nodeIPPoolTooHigh() bool {
warmENITarget := getWarmENITarget()
total, used := c.dataStore.GetStats()
logPoolStats(total, used, c.currentMaxAddrsPerENI, c.maxAddrsPerENI)
available := total - used
return (available > (warmENITarget+1)*c.currentMaxAddrsPerENI)
}
func ipamdErrInc(fn string, err error) {
ipamdErr.With(prometheus.Labels{"fn": fn, "error": err.Error()}).Inc()
}
func ipamdActionsInprogressSet(fn string, curNum int) {
ipamdActionsInprogress.WithLabelValues(fn).Set(float64(curNum))
}
// nodeIPPoolReconcile reconcile ENI and IP info from metadata service and IP addresses in datastore
func (c *IPAMContext) nodeIPPoolReconcile(interval time.Duration) error {
ipamdActionsInprogress.WithLabelValues("nodeIPPoolReconcile").Add(float64(1))
defer ipamdActionsInprogress.WithLabelValues("nodeIPPoolReconcile").Sub(float64(1))
curTime := time.Now()
last := c.lastNodeIPPoolAction
if curTime.Sub(last) <= interval {
return nil
}
log.Debug("Reconciling ENI/IP pool info...")
attachedENIs, err := c.awsClient.GetAttachedENIs()
if err != nil {
log.Error("ip pool reconcile: Failed to get attached eni info", err.Error())
ipamdErrInc("reconcileFailedGetENIs", err)
return errors.Wrap(err, "ip pool reconcile: failed to get attached eni")
}
curENIs := c.dataStore.GetENIInfos()
// mark phase
for _, attachedENI := range attachedENIs {
eniIPPool, err := c.dataStore.GetENIIPPools(attachedENI.ENIID)
if err == nil {
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 setup eni %s network: %v", attachedENI.ENIID, err)
ipamdErrInc("eniReconcileAdd", err)
//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.DeleteENI(eni)
if err != nil {
log.Errorf("ip pool reconcile: Failed to delete ENI during reconcile: %v", err)
ipamdErrInc("eniReconcileDel", err)
continue
}
reconcileCnt.With(prometheus.Labels{"fn": "eniReconcileDel"}).Inc()
}
log.Debug("Successfully Reconciled ENI/IP pool")
c.lastNodeIPPoolAction = curTime
return nil
}
func (c *IPAMContext) eniIPPoolReconcile(ipPool map[string]*datastore.AddressInfo, attachENI awsutils.ENIMetadata, eni string) error {
for _, localIP := range attachENI.LocalIPv4s {
if localIP == c.primaryIP[eni] {
log.Debugf("Reconcile and skip primary IP %s on eni %s", localIP, eni)
continue
}
err := c.dataStore.AddENIIPv4Address(eni, localIP)
if err != nil && err.Error() == datastore.DuplicateIPError {
log.Debugf("Reconciled IP %s on eni %s", localIP, eni)
// mark action = remove it from eniPool
delete(ipPool, localIP)
continue
}
if err != nil {
log.Errorf("Failed to reconcile IP %s on eni %s", localIP, eni)
ipamdErrInc("ipReconcileAdd", err)
// contine instead of bailout due to one ip
continue
}
reconcileCnt.With(prometheus.Labels{"fn": "eniIPPoolReconcileAdd"}).Inc()
}
// sweep phase, delete remaining IPs
for existingIP := range ipPool {
log.Debugf("Reconcile and delete ip %s on eni %s", existingIP, eni)
err := c.dataStore.DelENIIPv4Address(eni, existingIP)
if err != nil {
log.Errorf("Failed to reconcile and delete IP %s on eni %s, %v", existingIP, eni, err)
ipamdErrInc("ipReconcileDel", err)
// contine instead of bailout due to one ip
continue
}
reconcileCnt.With(prometheus.Labels{"fn": "eniIPPoolReconcileDel"}).Inc()
}
return nil
}