Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added retryNbackoff for tagENI method #626

Merged
merged 1 commit into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions pkg/awsutils/awsutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,15 +675,17 @@ func (cache *EC2InstanceMetadataCache) tagENI(eniID string) {
Tags: tags,
}

start := time.Now()
_, err := cache.ec2SVC.CreateTags(input)
awsAPILatency.WithLabelValues("CreateTags", fmt.Sprint(err != nil)).Observe(msSince(start))
if err != nil {
awsAPIErrInc("CreateTags", err)
log.Warnf("Failed to tag the newly created ENI %s: %v", eniID, err)
} else {
_ = retry.RetryNWithBackoff(retry.NewSimpleBackoff(time.Second, time.Minute, 0.3, 2), 5, func() error {
start := time.Now()
_, err := cache.ec2SVC.CreateTags(input)
awsAPILatency.WithLabelValues("CreateTags", fmt.Sprint(err != nil)).Observe(msSince(start))
if err != nil {
awsAPIErrInc("CreateTags", err)
return log.Warnf("Failed to tag the newly created ENI %s: %v", eniID, err)
}
log.Debugf("Successfully tagged ENI: %s", eniID)
}
return nil
})
}

//containsAttachmentLimitExceededError returns whether exceeds instance's ENI limit
Expand Down Expand Up @@ -758,7 +760,7 @@ func (cache *EC2InstanceMetadataCache) freeENI(eniName string, maxBackoffDelay t
}

// It does take awhile for EC2 to detach ENI from instance, so we wait 2s before trying the delete.
time.Sleep(time.Second*2)
time.Sleep(time.Second * 2)
err = cache.deleteENI(eniName, maxBackoffDelay)
if err != nil {
awsUtilsErrInc("FreeENIDeleteErr", err)
Expand Down
29 changes: 29 additions & 0 deletions pkg/awsutils/awsutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,35 @@ func TestDescribeENI(t *testing.T) {
}
}

func TestTagEni(t *testing.T) {
ctrl, mockMetadata, mockEC2 := setup(t)
defer ctrl.Finish()
mockMetadata.EXPECT().GetMetadata(metadataAZ).Return(az, nil)
mockMetadata.EXPECT().GetMetadata(metadataLocalIP).Return(localIP, nil)
mockMetadata.EXPECT().GetMetadata(metadataInstanceID).Return(instanceID, nil)
mockMetadata.EXPECT().GetMetadata(metadataInstanceType).Return(instanceType, nil)
mockMetadata.EXPECT().GetMetadata(metadataMAC).Return(primaryMAC, nil)
mockMetadata.EXPECT().GetMetadata(metadataMACPath).Return(primaryMAC, nil)
mockMetadata.EXPECT().GetMetadata(metadataMACPath+primaryMAC+metadataDeviceNum).Return("1", nil)
mockMetadata.EXPECT().GetMetadata(metadataMACPath+primaryMAC+metadataOwnerID).Return("1234", nil)
mockMetadata.EXPECT().GetMetadata(metadataMACPath+primaryMAC+metadataInterface).Return(primaryMAC, nil)
mockMetadata.EXPECT().GetMetadata(metadataMACPath+primaryMAC+metadataSGs).Return(sgs, nil)
mockMetadata.EXPECT().GetMetadata(metadataMACPath+primaryMAC+metadataSubnetID).Return(subnetID, nil)
mockMetadata.EXPECT().GetMetadata(metadataMACPath+primaryMAC+metadataVPCcidr).Return(vpcCIDR, nil)
mockMetadata.EXPECT().GetMetadata(metadataMACPath+primaryMAC+metadataVPCcidrs).Return(vpcCIDR, nil)

ins := &EC2InstanceMetadataCache{ec2Metadata: mockMetadata, ec2SVC: mockEC2}
err := ins.initWithEC2Metadata()
assert.NoError(t, err)
mockEC2.EXPECT().CreateTags(gomock.Any()).Return(nil, errors.New("Tagging Failed"))
mockEC2.EXPECT().CreateTags(gomock.Any()).Return(nil, errors.New("Tagging Failed"))
mockEC2.EXPECT().CreateTags(gomock.Any()).Return(nil, errors.New("Tagging Failed"))
mockEC2.EXPECT().CreateTags(gomock.Any()).Return(nil, errors.New("Tagging Failed"))
mockEC2.EXPECT().CreateTags(gomock.Any()).Return(nil, nil)
ins.tagENI(eniID)
assert.NoError(t, err)
}

func TestAllocENI(t *testing.T) {
ctrl, _, mockEC2 := setup(t)
defer ctrl.Finish()
Expand Down