Skip to content

Commit

Permalink
Use the AWS API to retrieve instance network limits (#78)
Browse files Browse the repository at this point in the history
* Dynamically get instance network limits

* Propagate errors due to network limits
  • Loading branch information
lbernail authored Jan 31, 2020
1 parent 154265b commit 25af6fc
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 289 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ true.
"ec2:DetachNetworkInterface"
"ec2:DeleteNetworkInterface"
"ec2:ModifyNetworkInterfaceAttribute"
"ec2:DescribeInstanceTypes"
"ec2:DescribeVpcs"
"ec2:DescribeVpcPeeringConnections"

Expand Down
13 changes: 10 additions & 3 deletions aws/allocate.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ func (c *allocateClient) AllocateIPsOn(intf Interface, batchSize int64) ([]*Allo
NetworkInterfaceId: &intf.ID,
}

available := int64(c.aws.ENILimits().IPv4 - len(intf.IPv4s))
limits, err := c.aws.ENILimits()
if err != nil {
return nil, err
}
available := limits.IPv4 - int64(len(intf.IPv4s))

// If there are fewer IPs left than the batch size, request all the remaining IPs
// batch size 0 conventionally means "request the limit"
Expand Down Expand Up @@ -102,14 +106,17 @@ func (c *allocateClient) AllocateIPsFirstAvailableAtIndex(index int, batchSize i
if err != nil {
return nil, err
}
limits := c.aws.ENILimits()
limits, err := c.aws.ENILimits()
if err != nil {
return nil, err
}

var candidates []Interface
for _, intf := range interfaces {
if intf.Number < index {
continue
}
if len(intf.IPv4s) < limits.IPv4 {
if int64(len(intf.IPv4s)) < limits.IPv4 {
candidates = append(candidates, intf)
}
}
Expand Down
13 changes: 10 additions & 3 deletions aws/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ func (c *interfaceClient) NewInterfaceOnSubnetAtIndex(index int, secGrps []strin
createReq.SetSubnetId(subnet.ID)

// Subtract 1 to Account for primary IP
ipv4Limit := int64(c.aws.ENILimits().IPv4) - 1
limits, err := c.aws.ENILimits()
if err != nil {
return nil, err
}
ipv4Limit := limits.IPv4 - 1

// batch size 0 conventionally means "request the limit"
if ipBatchSize == 0 || ipBatchSize > ipv4Limit {
Expand Down Expand Up @@ -166,8 +170,11 @@ func (c *interfaceClient) NewInterface(secGrps []string, requiredTags map[string
return nil, err
}

limits := c.aws.ENILimits()
if len(existingInterfaces) >= limits.Adapters {
limits, err := c.aws.ENILimits()
if err != nil {
return nil, err
}
if int64(len(existingInterfaces)) >= limits.Adapters {
return nil, fmt.Errorf("too many adapters on this instance already")
}

Expand Down
Loading

0 comments on commit 25af6fc

Please sign in to comment.