Skip to content

Commit

Permalink
Add context and user agents to EC2 requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Claes Mogren authored and mogren committed May 19, 2020
1 parent 301c2a6 commit d8ca7e9
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 129 deletions.
38 changes: 20 additions & 18 deletions pkg/awsutils/awsutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/aws/amazon-vpc-cni-k8s/pkg/utils/retry"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"k8s.io/apimachinery/pkg/util/sets"
Expand Down Expand Up @@ -79,6 +80,8 @@ var (
// ErrNoNetworkInterfaces occurs when
// DesribeNetworkInterfaces(eniID) returns no network interfaces
ErrNoNetworkInterfaces = errors.New("No network interfaces found for ENI")
// Custom user agent
userAgent = request.WithAppendUserAgent("amazon-vpc-cni-k8s")
)

var log = logger.Get()
Expand Down Expand Up @@ -549,7 +552,7 @@ func (cache *EC2InstanceMetadataCache) awsGetFreeDeviceNumber() (int, error) {
}

start := time.Now()
result, err := cache.ec2SVC.DescribeInstances(input)
result, err := cache.ec2SVC.DescribeInstancesWithContext(context.Background(), input, userAgent)
awsAPILatency.WithLabelValues("DescribeInstances", fmt.Sprint(err != nil)).Observe(msSince(start))
if err != nil {
awsAPIErrInc("DescribeInstances", err)
Expand Down Expand Up @@ -615,7 +618,7 @@ func (cache *EC2InstanceMetadataCache) AllocENI(useCustomCfg bool, sg []*string,
}

start := time.Now()
_, err = cache.ec2SVC.ModifyNetworkInterfaceAttribute(attributeInput)
_, err = cache.ec2SVC.ModifyNetworkInterfaceAttributeWithContext(context.Background(), attributeInput, userAgent)
awsAPILatency.WithLabelValues("ModifyNetworkInterfaceAttribute", fmt.Sprint(err != nil)).Observe(msSince(start))
if err != nil {
awsAPIErrInc("ModifyNetworkInterfaceAttribute", err)
Expand Down Expand Up @@ -644,7 +647,7 @@ func (cache *EC2InstanceMetadataCache) attachENI(eniID string) (string, error) {
NetworkInterfaceId: aws.String(eniID),
}
start := time.Now()
attachOutput, err := cache.ec2SVC.AttachNetworkInterface(attachInput)
attachOutput, err := cache.ec2SVC.AttachNetworkInterfaceWithContext(context.Background(), attachInput, userAgent)
awsAPILatency.WithLabelValues("AttachNetworkInterface", fmt.Sprint(err != nil)).Observe(msSince(start))
if err != nil {
awsAPIErrInc("AttachNetworkInterface", err)
Expand Down Expand Up @@ -676,7 +679,7 @@ func (cache *EC2InstanceMetadataCache) createENI(useCustomCfg bool, sg []*string
}
log.Infof("Creating ENI with security groups: %v in subnet: %s", sgs, *input.SubnetId)
start := time.Now()
result, err := cache.ec2SVC.CreateNetworkInterface(input)
result, err := cache.ec2SVC.CreateNetworkInterfaceWithContext(context.Background(), input, userAgent)
awsAPILatency.WithLabelValues("CreateNetworkInterface", fmt.Sprint(err != nil)).Observe(msSince(start))
if err != nil {
awsAPIErrInc("CreateNetworkInterface", err)
Expand Down Expand Up @@ -729,7 +732,7 @@ func (cache *EC2InstanceMetadataCache) tagENI(eniID string, maxBackoffDelay time

_ = retry.RetryNWithBackoff(retry.NewSimpleBackoff(500*time.Millisecond, maxBackoffDelay, 0.3, 2), 5, func() error {
start := time.Now()
_, err := cache.ec2SVC.CreateTags(input)
_, err := cache.ec2SVC.CreateTagsWithContext(context.Background(), input, userAgent)
awsAPILatency.WithLabelValues("CreateTags", fmt.Sprint(err != nil)).Observe(msSince(start))
if err != nil {
awsAPIErrInc("CreateTags", err)
Expand Down Expand Up @@ -819,7 +822,7 @@ func (cache *EC2InstanceMetadataCache) freeENI(eniName string, sleepDelayAfterDe
// Retry detaching the ENI from the instance
err = retry.RetryNWithBackoff(retry.NewSimpleBackoff(time.Millisecond*200, maxBackoffDelay, 0.15, 2.0), maxENIDeleteRetries, func() error {
start := time.Now()
_, ec2Err := cache.ec2SVC.DetachNetworkInterface(detachInput)
_, ec2Err := cache.ec2SVC.DetachNetworkInterfaceWithContext(context.Background(), detachInput, userAgent)
awsAPILatency.WithLabelValues("DetachNetworkInterface", fmt.Sprint(ec2Err != nil)).Observe(msSince(start))
if ec2Err != nil {
awsAPIErrInc("DetachNetworkInterface", ec2Err)
Expand Down Expand Up @@ -854,7 +857,7 @@ func (cache *EC2InstanceMetadataCache) getENIAttachmentID(eniID string) (*string
input := &ec2.DescribeNetworkInterfacesInput{NetworkInterfaceIds: eniIds}

start := time.Now()
result, err := cache.ec2SVC.DescribeNetworkInterfaces(input)
result, err := cache.ec2SVC.DescribeNetworkInterfacesWithContext(context.Background(), input, userAgent)
awsAPILatency.WithLabelValues("DescribeNetworkInterfaces", fmt.Sprint(err != nil)).Observe(msSince(start))
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
Expand Down Expand Up @@ -889,7 +892,7 @@ func (cache *EC2InstanceMetadataCache) deleteENI(eniName string, maxBackoffDelay
}
err := retry.RetryNWithBackoff(retry.NewSimpleBackoff(time.Millisecond*500, maxBackoffDelay, 0.15, 2.0), maxENIDeleteRetries, func() error {
start := time.Now()
_, ec2Err := cache.ec2SVC.DeleteNetworkInterface(deleteInput)
_, ec2Err := cache.ec2SVC.DeleteNetworkInterfaceWithContext(context.Background(), deleteInput, userAgent)
awsAPILatency.WithLabelValues("DeleteNetworkInterface", fmt.Sprint(ec2Err != nil)).Observe(msSince(start))
if ec2Err != nil {
if aerr, ok := ec2Err.(awserr.Error); ok {
Expand All @@ -916,7 +919,7 @@ func (cache *EC2InstanceMetadataCache) GetIPv4sFromEC2(eniID string) (addrList [
input := &ec2.DescribeNetworkInterfacesInput{NetworkInterfaceIds: eniIds}

start := time.Now()
result, err := cache.ec2SVC.DescribeNetworkInterfaces(input)
result, err := cache.ec2SVC.DescribeNetworkInterfacesWithContext(context.Background(), input, userAgent)
awsAPILatency.WithLabelValues("DescribeNetworkInterfaces", fmt.Sprint(err != nil)).Observe(msSince(start))
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
Expand Down Expand Up @@ -955,7 +958,7 @@ func (cache *EC2InstanceMetadataCache) DescribeAllENIs() ([]ENIMetadata, map[str
input := &ec2.DescribeNetworkInterfacesInput{NetworkInterfaceIds: aws.StringSlice(eniIDs)}

start := time.Now()
ec2Response, err := cache.ec2SVC.DescribeNetworkInterfaces(input)
ec2Response, err := cache.ec2SVC.DescribeNetworkInterfacesWithContext(context.Background(), input, userAgent)
awsAPILatency.WithLabelValues("DescribeNetworkInterfaces", fmt.Sprint(err != nil)).Observe(msSince(start))
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
Expand Down Expand Up @@ -1036,7 +1039,7 @@ func (cache *EC2InstanceMetadataCache) AllocIPAddress(eniID string) error {
}

start := time.Now()
output, err := cache.ec2SVC.AssignPrivateIpAddresses(input)
output, err := cache.ec2SVC.AssignPrivateIpAddressesWithContext(context.Background(), input, userAgent)
awsAPILatency.WithLabelValues("AssignPrivateIpAddresses", fmt.Sprint(err != nil)).Observe(msSince(start))
if err != nil {
awsAPIErrInc("AssignPrivateIpAddresses", err)
Expand Down Expand Up @@ -1064,7 +1067,7 @@ func (cache *EC2InstanceMetadataCache) GetENILimit() (int, error) {
if !ok {
// Fetch from EC2 API
describeInstanceTypesInput := &ec2.DescribeInstanceTypesInput{InstanceTypes: []*string{aws.String(cache.instanceType)}}
output, err := cache.ec2SVC.DescribeInstanceTypes(describeInstanceTypesInput)
output, err := cache.ec2SVC.DescribeInstanceTypesWithContext(context.Background(), describeInstanceTypesInput, userAgent)
if err != nil || len(output.InstanceTypes) != 1 {
log.Errorf("", err)
return 0, errors.New(fmt.Sprintf("Failed calling DescribeInstanceTypes for `%s`: %v", cache.instanceType, err))
Expand Down Expand Up @@ -1110,7 +1113,7 @@ func (cache *EC2InstanceMetadataCache) AllocIPAddresses(eniID string, numIPs int
}

start := time.Now()
output, err := cache.ec2SVC.AssignPrivateIpAddresses(input)
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)
Expand All @@ -1129,7 +1132,6 @@ func (cache *EC2InstanceMetadataCache) AllocIPAddresses(eniID string, numIPs int

// DeallocIPAddresses allocates numIPs of IP address on an ENI
func (cache *EC2InstanceMetadataCache) DeallocIPAddresses(eniID string, ips []string) error {
ctx := context.Background()
log.Infof("Trying to unassign the following IPs %s from ENI %s", ips, eniID)
var ipsInput []*string
for _, ip := range ips {
Expand All @@ -1142,10 +1144,10 @@ func (cache *EC2InstanceMetadataCache) DeallocIPAddresses(eniID string, ips []st
}

start := time.Now()
_, err := cache.ec2SVC.UnassignPrivateIpAddressesWithContext(ctx, input)
awsAPILatency.WithLabelValues("UnassignPrivateIpAddressesWithContext", fmt.Sprint(err != nil)).Observe(msSince(start))
_, err := cache.ec2SVC.UnassignPrivateIpAddressesWithContext(context.Background(), input, userAgent)
awsAPILatency.WithLabelValues("UnassignPrivateIpAddresses", fmt.Sprint(err != nil)).Observe(msSince(start))
if err != nil {
awsAPIErrInc("UnassignPrivateIpAddressesWithContext", err)
awsAPIErrInc("UnassignPrivateIpAddresses", err)
log.Errorf("Failed to deallocate a private IP address %v", err)
return errors.Wrap(err, fmt.Sprintf("deallocate IP addresses: failed to deallocate private IP addresses: %s", ips))
}
Expand Down Expand Up @@ -1196,7 +1198,7 @@ func (cache *EC2InstanceMetadataCache) getFilteredListOfNetworkInterfaces() ([]*
input := &ec2.DescribeNetworkInterfacesInput{
Filters: []*ec2.Filter{tagFilter, statusFilter},
}
result, err := cache.ec2SVC.DescribeNetworkInterfaces(input)
result, err := cache.ec2SVC.DescribeNetworkInterfacesWithContext(context.Background(), input, userAgent)
if err != nil {
return nil, errors.Wrap(err, "awsutils: unable to obtain filtered list of network interfaces")
}
Expand Down
Loading

0 comments on commit d8ca7e9

Please sign in to comment.