Skip to content

Commit

Permalink
Refactor ENI limit struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Claes Mogren committed Jun 30, 2020
1 parent 2d20308 commit 09eb00b
Show file tree
Hide file tree
Showing 7 changed files with 408 additions and 747 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ generate:
go generate -x ./...
$(MAKE) format

# Generate descriptors for supported ENI configurations.
# Generate limit file go code
generate-limits: GOOS=
generate-limits:
go run pkg/awsutils/gen_vpc_ip_limits.go
go run scripts/gen_vpc_ip_limits.go

# Fetch the CNI plugins
plugins: FETCH_VERSION=0.8.6
Expand Down
37 changes: 23 additions & 14 deletions pkg/awsutils/awsutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ type APIs interface {
// GetPrimaryENI returns the primary ENI
GetPrimaryENI() string

// GetENIipLimit return IP address limit per ENI based on EC2 instance type
GetENIipLimit() (int, error)
// GetENIIPv4Limit return IP address limit per ENI based on EC2 instance type
GetENIIPv4Limit() (int, error)

// GetENILimit returns the number of ENIs that can be attached to an instance
GetENILimit() (int, error)
Expand Down Expand Up @@ -199,6 +199,12 @@ type ENIMetadata struct {
IPv4Addresses []*ec2.NetworkInterfacePrivateIpAddress
}

// InstanceTypeLimits keeps track of limits for an instance type
type InstanceTypeLimits struct {
ENILimit int64
IPv4Limit int64
}

func (eni ENIMetadata) PrimaryIPv4Address() string {
for _, addr := range eni.IPv4Addresses {
if aws.BoolValue(addr.Primary) {
Expand Down Expand Up @@ -1170,19 +1176,19 @@ func (cache *EC2InstanceMetadataCache) AllocIPAddress(eniID string) error {
return nil
}

// GetENIipLimit return IP address limit per ENI based on EC2 instance type
func (cache *EC2InstanceMetadataCache) GetENIipLimit() (int, error) {
ipLimit, ok := InstanceIPsAvailable[cache.instanceType]
// GetENIIPv4Limit return IP address limit per ENI based on EC2 instance type
func (cache *EC2InstanceMetadataCache) GetENIIPv4Limit() (int, error) {
eniLimits, ok := InstanceNetworkingLimits[cache.instanceType]
if !ok {
log.Errorf("Failed to get ENI IP limit due to unknown instance type %s", cache.instanceType)
return 0, errors.New(UnknownInstanceType)
}
return ipLimit - 1, nil
return int(eniLimits.IPv4Limit - 1), nil
}

// GetENILimit returns the number of ENIs can be attached to an instance
func (cache *EC2InstanceMetadataCache) GetENILimit() (int, error) {
eniLimit, ok := InstanceENIsAvailable[cache.instanceType]
eniLimits, ok := InstanceNetworkingLimits[cache.instanceType]
if !ok {
// Fetch from EC2 API
describeInstanceTypesInput := &ec2.DescribeInstanceTypesInput{InstanceTypes: []*string{aws.String(cache.instanceType)}}
Expand All @@ -1194,23 +1200,26 @@ func (cache *EC2InstanceMetadataCache) GetENILimit() (int, error) {
info := output.InstanceTypes[0]
// Ignore any missing values
instanceType := aws.StringValue(info.InstanceType)
eniLimit = int(aws.Int64Value(info.NetworkInfo.MaximumNetworkInterfaces))
ipLimit := int(aws.Int64Value(info.NetworkInfo.Ipv4AddressesPerInterface))
if instanceType != "" && eniLimit > 0 && ipLimit > 0 {
InstanceENIsAvailable[instanceType] = eniLimit
InstanceIPsAvailable[instanceType] = ipLimit
eniLimit := aws.Int64Value(info.NetworkInfo.MaximumNetworkInterfaces)
ipv4Limit := aws.Int64Value(info.NetworkInfo.Ipv4AddressesPerInterface)
if instanceType != "" && eniLimit > 0 && ipv4Limit > 0 {
eniLimits = InstanceTypeLimits{
ENILimit: eniLimit,
IPv4Limit: ipv4Limit,
}
InstanceNetworkingLimits[instanceType] = eniLimits
} else {
return 0, errors.New(fmt.Sprintf("%s: %s", UnknownInstanceType, cache.instanceType))
}
}
return eniLimit, nil
return int(eniLimits.ENILimit), nil
}

// AllocIPAddresses allocates numIPs of IP address on an ENI
func (cache *EC2InstanceMetadataCache) AllocIPAddresses(eniID string, numIPs int) error {
var needIPs = numIPs

ipLimit, err := cache.GetENIipLimit()
ipLimit, err := cache.GetENIIPv4Limit()
if err != nil {
awsUtilsErrInc("UnknownInstanceType", err)
return err
Expand Down
4 changes: 3 additions & 1 deletion pkg/awsutils/awsutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,9 @@ func TestDescribeInstanceTypes(t *testing.T) {
value, err := ins.GetENILimit()
assert.NoError(t, err)
assert.Equal(t, 9, value)
assert.Equal(t, 99, InstanceIPsAvailable[ins.instanceType])
pv4Limit, err := ins.GetENIIPv4Limit()
assert.NoError(t, err)
assert.Equal(t, 98, pv4Limit)
}

func TestAllocIPAddress(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions pkg/awsutils/mocks/awsutils_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 09eb00b

Please sign in to comment.