Skip to content

Commit

Permalink
Wait for ENI and secondary IPs
Browse files Browse the repository at this point in the history
When a newly created ENI gets attached, we should wait for the
secondary IPv4 addresses to be assigned to the ENI before continuing.
  • Loading branch information
Claes Mogren committed Aug 17, 2020
1 parent 2d84610 commit 64f2853
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
7 changes: 4 additions & 3 deletions pkg/awsutils/awsutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1224,12 +1224,13 @@ func (cache *EC2InstanceMetadataCache) AllocIPAddresses(eniID string, numIPs int
output, err := cache.ec2SVC.AssignPrivateIpAddressesWithContext(context.Background(), input, userAgent)
awsAPILatency.WithLabelValues("AssignPrivateIpAddresses", fmt.Sprint(err != nil)).Observe(msSince(start))
if err != nil {
log.Errorf("Failed to allocate a private IP addresses on ENI %v: %v", eniID, err)
awsAPIErrInc("AssignPrivateIpAddresses", err)
if containsPrivateIPAddressLimitExceededError(err) {
log.Debug("AssignPrivateIpAddresses returned PrivateIpAddressLimitExceeded")
log.Debug("AssignPrivateIpAddresses returned PrivateIpAddressLimitExceeded, but this is usually not true." +
"Returning nil, since we will check again by calling EC2 to get what addresses were assigned to the ENI.")
return nil
}
log.Errorf("Failed to allocate a private IP addresses on ENI %v: %v", eniID, err)
awsAPIErrInc("AssignPrivateIpAddresses", err)
return errors.Wrap(err, "allocate IP address: failed to allocate a private IP address")
}
if output != nil {
Expand Down
15 changes: 12 additions & 3 deletions pkg/ipamd/ipamd.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ func (c *IPAMContext) tryAllocateENI() error {
ipamdErrInc("increaseIPPoolAllocIPAddressesFailed")
}

eniMetadata, err := c.waitENIAttached(eni)
eniMetadata, err := c.waitENIAttached(eni, ipsToAllocate)
if err != nil {
ipamdErrInc("increaseIPPoolwaitENIAttachedFailed")
log.Errorf("Failed to increase pool size: Unable to discover attached ENI from metadata service %v", err)
Expand Down Expand Up @@ -802,7 +802,7 @@ func (c *IPAMContext) addENIaddressesToDataStore(ec2Addrs []*ec2.NetworkInterfac
return primaryIP
}

func (c *IPAMContext) waitENIAttached(eni string) (awsutils.ENIMetadata, error) {
func (c *IPAMContext) waitENIAttached(eni string, ipsToAllocate int) (awsutils.ENIMetadata, error) {
// Wait until the ENI shows up in the instance metadata service
retry := 0
for {
Expand All @@ -813,7 +813,16 @@ func (c *IPAMContext) waitENIAttached(eni string) (awsutils.ENIMetadata, error)
// Verify that the ENI we are waiting for is in the returned list
for _, returnedENI := range enis {
if eni == returnedENI.ENIID {
return returnedENI, nil
// Check number of Secondary IPs attached by comparing to allocate
eniIPCount := len(returnedENI.IPv4Addresses)
if eniIPCount <= 1 {
log.Debug("No secondary IPv4 addresses available yet")
continue
}
if eniIPCount == ipsToAllocate {
return returnedENI, nil
}
// TODO: If we have at least 1 Secondary IP, return without an error
}
}
log.Debugf("Not able to find the right ENI yet (attempt %d/%d)", retry, maxRetryCheckENI)
Expand Down

0 comments on commit 64f2853

Please sign in to comment.