Skip to content

Commit 1bf9f7a

Browse files
committed
add changes from review
2 parents cdecbbb + 08ef8ed commit 1bf9f7a

4 files changed

Lines changed: 18 additions & 28 deletions

File tree

cmd/aws-k8s-agent/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ func _main() int {
144144
}
145145
// Measure node initialization duration
146146
IPAMDNodeInitStartTime := time.Now()
147-
ipamContext, err := ipamd.New(k8sClient, withApiServer)
147+
ctx := context.Background()
148+
ipamContext, err := ipamd.New(ctx, k8sClient, withApiServer)
148149
IPAMDNodeInitDuration := time.Since(IPAMDNodeInitStartTime).Seconds()
149150

150151
if err != nil {
@@ -164,7 +165,7 @@ func _main() int {
164165
}
165166

166167
// Pool manager
167-
go ipamContext.StartNodeIPPoolManager(context.Background())
168+
go ipamContext.StartNodeIPPoolManager(ctx)
168169

169170
if !utils.GetBoolAsStringEnvVar(envDisableMetrics, false) {
170171
// Prometheus metrics

pkg/awsutils/awsutils.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,7 @@ func (i instrumentedIMDS) GetMetadataWithContext(ctx context.Context, p string)
408408
}
409409

410410
// New creates an EC2InstanceMetadataCache
411-
func New(useSubnetDiscovery, useCustomNetworking, disableLeakedENICleanup, v4Enabled, v6Enabled bool) (*EC2InstanceMetadataCache, error) {
412-
// ctx is passed to initWithEC2Metadata func to cancel spawned go-routines when tests are run
413-
ctx := context.Background()
414-
411+
func New(ctx context.Context, useSubnetDiscovery, useCustomNetworking, disableLeakedENICleanup, v4Enabled, v6Enabled bool) (*EC2InstanceMetadataCache, error) {
415412
awsconfig, err := awssession.New(ctx)
416413
if err != nil {
417414
return nil, errors.Wrap(err, "failed to create aws session")
@@ -449,7 +446,7 @@ func New(useSubnetDiscovery, useCustomNetworking, disableLeakedENICleanup, v4Ena
449446

450447
// Clean up leaked ENIs in the background
451448
if !disableLeakedENICleanup {
452-
go wait.Forever(func() { cache.cleanUpLeakedENIs(context.Background()) }, time.Hour)
449+
go wait.Forever(func() { cache.cleanUpLeakedENIs(ctx) }, time.Hour)
453450
}
454451
return cache, nil
455452
}
@@ -602,10 +599,7 @@ func (cache *EC2InstanceMetadataCache) getFilteredENIs(ctx context.Context, stor
602599
isSecondarySubnet := cache.isENIInSecondarySubnet(ctx, eniID)
603600

604601
// Filter based on subnet type
605-
if onlySecondarySubnets && !isSecondarySubnet {
606-
continue
607-
} else if !onlySecondarySubnets && isSecondarySubnet {
608-
// When we want primary subnet ENIs, skip secondary subnet ENIs
602+
if onlySecondarySubnets != isSecondarySubnet {
609603
continue
610604
}
611605

@@ -676,8 +670,7 @@ func (cache *EC2InstanceMetadataCache) RefreshCustomSGIDs(ctx context.Context, d
676670
sgIDs, err := cache.discoverCustomSecurityGroups(ctx)
677671
if err != nil {
678672
awsAPIErrInc("DiscoverCustomSecurityGroups", err)
679-
log.Warnf("Failed to discover custom security groups: %v", err)
680-
log.Info("Falling back to using primary security groups for ENIs in secondary subnets")
673+
log.Warnf("Failed to discover custom security groups: %v. Falling back to using primary security groups for ENIs in secondary subnets", err)
681674

682675
// Clear custom security groups cache to trigger fallback behavior
683676
cache.customSecurityGroups.Set([]string{})

pkg/ipamd/ipamd.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,8 @@ func (c *IPAMContext) inInsufficientCidrCoolingPeriod() bool {
394394

395395
// New retrieves IP address usage information from Instance MetaData service and Kubelet
396396
// then initializes IP address pool data store
397-
func New(k8sClient client.Client, withApiServer bool) (*IPAMContext, error) {
397+
func New(ctx context.Context, k8sClient client.Client, withApiServer bool) (*IPAMContext, error) {
398398
prometheusRegister()
399-
ctx := context.Background()
400399
c := &IPAMContext{}
401400
c.k8sClient = k8sClient
402401
c.networkClient = networkutils.New()
@@ -407,7 +406,7 @@ func New(k8sClient client.Client, withApiServer bool) (*IPAMContext, error) {
407406
c.enableIPv4 = isIPv4Enabled()
408407
c.enableIPv6 = isIPv6Enabled()
409408
c.disableENIProvisioning = disableENIProvisioning()
410-
client, err := awsutils.New(c.useSubnetDiscovery, c.useCustomNetworking, disableLeakedENICleanup(), c.enableIPv4, c.enableIPv6)
409+
client, err := awsutils.New(ctx, c.useSubnetDiscovery, c.useCustomNetworking, disableLeakedENICleanup(), c.enableIPv4, c.enableIPv6)
411410
if err != nil {
412411
return nil, errors.Wrap(err, "ipamd: can not initialize with AWS SDK interface")
413412
}
@@ -446,16 +445,15 @@ func New(k8sClient client.Client, withApiServer bool) (*IPAMContext, error) {
446445
c.myNodeName = os.Getenv(envNodeName)
447446
c.withApiServer = withApiServer
448447

449-
if err := c.nodeInit(); err != nil {
448+
if err := c.nodeInit(ctx); err != nil {
450449
return nil, err
451450
}
452451
return c, nil
453452
}
454453

455-
func (c *IPAMContext) nodeInit() error {
454+
func (c *IPAMContext) nodeInit(ctx context.Context) error {
456455
prometheusmetrics.IpamdActionsInprogress.WithLabelValues("nodeInit").Add(float64(1))
457456
defer prometheusmetrics.IpamdActionsInprogress.WithLabelValues("nodeInit").Sub(float64(1))
458-
ctx := context.TODO()
459457
log.Debugf("Start node init")
460458

461459
if err := c.initENIAndIPLimits(); err != nil {
@@ -582,10 +580,10 @@ func (c *IPAMContext) nodeInit() error {
582580
// Refresh security groups and VPC CIDR blocks in the background
583581
// Ignoring errors since we will retry in 30s
584582
go wait.Forever(func() {
585-
c.awsClient.RefreshSGIDs(context.Background(), primaryENIMac, c.dataStoreAccess)
583+
c.awsClient.RefreshSGIDs(ctx, primaryENIMac, c.dataStoreAccess)
586584
// Also refresh custom security groups for secondary subnets
587585
if c.useSubnetDiscovery {
588-
c.awsClient.RefreshCustomSGIDs(context.Background(), c.dataStoreAccess)
586+
c.awsClient.RefreshCustomSGIDs(ctx, c.dataStoreAccess)
589587
}
590588
}, 30*time.Second)
591589
}
@@ -2519,8 +2517,6 @@ func (c *IPAMContext) cleanupExcludedENI(ctx context.Context, eniID string) {
25192517

25202518
// checkAndHandleAllENIExclusion checks all existing ENIs (primary and secondary) across all network cards for subnet exclusion
25212519
func (c *IPAMContext) checkAndHandleAllENIExclusion(ctx context.Context) {
2522-
log.Debugf("checkAndHandleAllENIExclusion: checking all existing ENIs for subnet exclusion")
2523-
25242520
// Iterate over all datastores (one per network card)
25252521
for _, ds := range c.dataStoreAccess.DataStores {
25262522
// Get ENI information using the public method

pkg/ipamd/ipamd_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func TestNodeInit(t *testing.T) {
222222
// Add IPs
223223
m.awsutils.EXPECT().AllocIPAddresses(gomock.Any(), gomock.Any(), gomock.Any())
224224
os.Setenv("MY_NODE_NAME", myNodeName)
225-
err := mockContext.nodeInit()
225+
err := mockContext.nodeInit(context.TODO())
226226
assert.NoError(t, err)
227227
}
228228

@@ -324,7 +324,7 @@ func TestNodeInitwithPDenabledIPv4Mode(t *testing.T) {
324324
m.k8sClient.Create(ctx, &fakeNode)
325325

326326
os.Setenv("MY_NODE_NAME", myNodeName)
327-
err := mockContext.nodeInit()
327+
err := mockContext.nodeInit(context.TODO())
328328
assert.NoError(t, err)
329329
}
330330

@@ -413,7 +413,7 @@ func TestNodeInitwithPDenabledIPv6Mode(t *testing.T) {
413413
m.k8sClient.Create(ctx, &fakeNode)
414414
os.Setenv("MY_NODE_NAME", myNodeName)
415415

416-
err := mockContext.nodeInit()
416+
err := mockContext.nodeInit(context.TODO())
417417
assert.NoError(t, err)
418418
}
419419

@@ -2968,7 +2968,7 @@ func TestNodeInitPrimarySubnetExclusionWithExistingPodIPs(t *testing.T) {
29682968
// Add IPs
29692969
m.awsutils.EXPECT().AllocIPAddresses(gomock.Any(), gomock.Any(), gomock.Any()).Return(&ec2.AssignPrivateIpAddressesOutput{}, nil)
29702970
os.Setenv("MY_NODE_NAME", myNodeName)
2971-
err := mockContext.nodeInit()
2971+
err := mockContext.nodeInit(context.TODO())
29722972
assert.NoError(t, err)
29732973

29742974
// Verify that primary ENI exclusion is now always respected
@@ -3111,7 +3111,7 @@ func TestNodeInitPrimarySubnetExclusionWithoutExistingPodIPs(t *testing.T) {
31113111
// Add IPs
31123112
m.awsutils.EXPECT().AllocIPAddresses(gomock.Any(), gomock.Any(), gomock.Any()).Return(&ec2.AssignPrivateIpAddressesOutput{}, nil)
31133113
os.Setenv("MY_NODE_NAME", myNodeName)
3114-
err := mockContext.nodeInit()
3114+
err := mockContext.nodeInit(context.TODO())
31153115
assert.NoError(t, err)
31163116

31173117
// Verify that primary ENI remains excluded due to no existing pod IPs

0 commit comments

Comments
 (0)