Skip to content

Commit

Permalink
pkg aws ec2 api: describe network interfaces v2
Browse files Browse the repository at this point in the history
  • Loading branch information
dshehbaj committed Jan 30, 2025
1 parent 273e659 commit 668f83d
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 36 deletions.

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

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

17 changes: 10 additions & 7 deletions pkg/aws/ec2/api/eni_cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import (
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/exp/slices"

ec2v2 "github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"

ec2Errors "github.com/aws/amazon-vpc-resource-controller-k8s/pkg/aws/errors"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
Expand Down Expand Up @@ -114,24 +117,24 @@ func (e *ENICleaner) cleanUpAvailableENIs() {
vpccniAvailableCount := 0
leakedENICount := 0

describeNetworkInterfaceIp := &ec2.DescribeNetworkInterfacesInput{
Filters: []*ec2.Filter{
describeNetworkInterfaceIp := &ec2v2.DescribeNetworkInterfacesInput{
Filters: []types.Filter{
{
Name: aws.String("status"),
Values: []*string{aws.String(ec2.NetworkInterfaceStatusAvailable)},
Values: []string{ec2.NetworkInterfaceStatusAvailable},
},
{
Name: aws.String("tag:" + e.clusterNameTagKey),
Values: []*string{aws.String(config.ClusterNameTagValue)},
Values: []string{config.ClusterNameTagValue},
},
{
Name: aws.String("tag:" + config.NetworkInterfaceOwnerTagKey),
Values: aws.StringSlice([]string{config.NetworkInterfaceOwnerTagValue,
Values: ([]string{config.NetworkInterfaceOwnerTagValue,
config.NetworkInterfaceOwnerVPCCNITagValue}),
},
{
Name: aws.String("vpc-id"),
Values: []*string{aws.String(e.VPCID)},
Values: []string{(e.VPCID)},
},
},
}
Expand All @@ -148,7 +151,7 @@ func (e *ENICleaner) cleanUpAvailableENIs() {
for _, networkInterface := range describeNetworkInterfaceOp.NetworkInterfaces {
if _, exists := e.availableENIs[*networkInterface.NetworkInterfaceId]; exists {
// Increment promethues metrics for number of leaked ENIs cleaned up
if tagIdx := slices.IndexFunc(networkInterface.TagSet, func(tag *ec2.Tag) bool {
if tagIdx := slices.IndexFunc(networkInterface.TagSet, func(tag types.Tag) bool {
return *tag.Key == config.NetworkInterfaceOwnerTagKey
}); tagIdx != -1 {
switch *networkInterface.TagSet[tagIdx].Value {
Expand Down
28 changes: 14 additions & 14 deletions pkg/aws/ec2/api/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ type EC2APIHelper interface {
ipResourceCount *config.IPResourceCount, interfaceType *string) (*ec2.NetworkInterface, error)
DeleteNetworkInterface(interfaceId *string) error
GetSubnet(subnetId *string) (*types.Subnet, error)
GetBranchNetworkInterface(trunkID, subnetID *string) ([]*ec2.NetworkInterface, error)
GetBranchNetworkInterface(trunkID, subnetID *string) ([]types.NetworkInterface, error)
GetInstanceNetworkInterface(instanceId *string) ([]types.InstanceNetworkInterface, error)
DescribeNetworkInterfaces(nwInterfaceIds []*string) ([]*ec2.NetworkInterface, error)
DescribeNetworkInterfaces(nwInterfaceIds []string) ([]types.NetworkInterface, error)
DescribeTrunkInterfaceAssociation(trunkInterfaceId *string) ([]types.TrunkInterfaceAssociation, error)
CreateAndAttachNetworkInterface(instanceId *string, subnetId *string, securityGroups []string, tags []*ec2.Tag, deviceIndex *int64,
description *string, interfaceType *string, ipResourceCount *config.IPResourceCount) (*ec2.NetworkInterface, error)
Expand Down Expand Up @@ -233,8 +233,8 @@ func (h *ec2APIHelper) GetInstanceNetworkInterface(instanceId *string) ([]types.
}

// DescribeNetworkInterfaces returns the network interface details of the given network interface ids
func (h *ec2APIHelper) DescribeNetworkInterfaces(nwInterfaceIds []*string) ([]*ec2.NetworkInterface, error) {
describeNetworkInterfacesInput := &ec2.DescribeNetworkInterfacesInput{
func (h *ec2APIHelper) DescribeNetworkInterfaces(nwInterfaceIds []string) ([]types.NetworkInterface, error) {
describeNetworkInterfacesInput := &ec2v2.DescribeNetworkInterfacesInput{
NetworkInterfaceIds: nwInterfaceIds,
}
describeNetworkInterfaceOutput, err := h.ec2Wrapper.DescribeNetworkInterfaces(describeNetworkInterfacesInput)
Expand Down Expand Up @@ -416,10 +416,10 @@ func (h *ec2APIHelper) WaitForNetworkInterfaceStatusChange(networkInterfaceId *s
}
return false
}, func() error {
interfaces, err := h.DescribeNetworkInterfaces([]*string{networkInterfaceId})
interfaces, err := h.DescribeNetworkInterfaces([]string{*networkInterfaceId})
if err == nil && len(interfaces) == 1 {
attachment := interfaces[0].Attachment
if attachment != nil && attachment.Status != nil && *attachment.Status == desiredStatus {
if attachment != nil && string(attachment.Status) == desiredStatus {
return nil
} else {
return ErrRetryAttachmentStatusCheck
Expand Down Expand Up @@ -490,7 +490,7 @@ func (h *ec2APIHelper) AssignIPv4ResourcesAndWaitTillReady(eniID string, resourc
return false
}, func() error {
// Describe the network interface on which the new IP or prefixes are assigned
interfaces, err := h.DescribeNetworkInterfaces([]*string{&eniID})
interfaces, err := h.DescribeNetworkInterfaces([]string{eniID})
// Re-initialize the slice so that we don't add IP resources multiple times
assignedResources = []string{}

Expand Down Expand Up @@ -567,20 +567,20 @@ func (h *ec2APIHelper) UnassignIPv4Resources(eniID string, resourceType config.R
return err
}

func (h *ec2APIHelper) GetBranchNetworkInterface(trunkID, subnetID *string) ([]*ec2.NetworkInterface, error) {
filters := []*ec2.Filter{
func (h *ec2APIHelper) GetBranchNetworkInterface(trunkID, subnetID *string) ([]types.NetworkInterface, error) {
filters := []types.Filter{
{
Name: aws.String("tag:" + config.TrunkENIIDTag),
Values: []*string{trunkID},
Values: []string{*trunkID},
},
{
Name: aws.String("subnet-id"),
Values: []*string{subnetID},
Values: []string{*subnetID},
},
}

describeNetworkInterfacesInput := &ec2.DescribeNetworkInterfacesInput{Filters: filters}
var nwInterfaces []*ec2.NetworkInterface
describeNetworkInterfacesInput := &ec2v2.DescribeNetworkInterfacesInput{Filters: filters}
var nwInterfaces []types.NetworkInterface
for {
describeNetworkInterfaceOutput, err := h.ec2Wrapper.DescribeNetworkInterfaces(describeNetworkInterfacesInput)
if err != nil {
Expand All @@ -596,7 +596,7 @@ func (h *ec2APIHelper) GetBranchNetworkInterface(trunkID, subnetID *string) ([]*
// One or more interface associated with the trunk, return the result
for _, nwInterface := range describeNetworkInterfaceOutput.NetworkInterfaces {
// Only attach the required details to avoid consuming extra memory
nwInterfaces = append(nwInterfaces, &ec2.NetworkInterface{
nwInterfaces = append(nwInterfaces, types.NetworkInterface{
NetworkInterfaceId: nwInterface.NetworkInterfaceId,
TagSet: nwInterface.TagSet,
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/aws/ec2/api/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ func TestEc2APIHelper_DescribeNetworkInterfaces(t *testing.T) {
mockWrapper.EXPECT().DescribeNetworkInterfaces(describeNetworkInterfaceInputUsingInterfaceId).
Return(describeNetworkInterfaceOutputUsingInterfaceId, nil)

nwInterfaces, err := ec2ApiHelper.DescribeNetworkInterfaces([]*string{&branchInterfaceId, &branchInterfaceId2})
nwInterfaces, err := ec2ApiHelper.DescribeNetworkInterfaces([]string{branchInterfaceId, branchInterfaceId2})
assert.NoError(t, err)
assert.Equal(t, 2, len(nwInterfaces))
}
Expand All @@ -697,7 +697,7 @@ func TestEc2APIHelper_DescribeNetworkInterfaces_Error(t *testing.T) {
mockWrapper.EXPECT().DescribeNetworkInterfaces(describeNetworkInterfaceInputUsingInterfaceId).
Return(nil, mockError)

_, err := ec2ApiHelper.DescribeNetworkInterfaces([]*string{&branchInterfaceId, &branchInterfaceId2})
_, err := ec2ApiHelper.DescribeNetworkInterfaces([]string{branchInterfaceId, branchInterfaceId2})
assert.Error(t, mockError, err)
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/aws/ec2/api/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type EC2Wrapper interface {
DeleteNetworkInterface(input *ec2.DeleteNetworkInterfaceInput) (*ec2.DeleteNetworkInterfaceOutput, error)
AssignPrivateIPAddresses(input *ec2.AssignPrivateIpAddressesInput) (*ec2.AssignPrivateIpAddressesOutput, error)
UnassignPrivateIPAddresses(input *ec2.UnassignPrivateIpAddressesInput) (*ec2.UnassignPrivateIpAddressesOutput, error)
DescribeNetworkInterfaces(input *ec2.DescribeNetworkInterfacesInput) (*ec2.DescribeNetworkInterfacesOutput, error)
DescribeNetworkInterfaces(input *ec2v2.DescribeNetworkInterfacesInput) (*ec2v2.DescribeNetworkInterfacesOutput, error)
CreateTags(input *ec2.CreateTagsInput) (*ec2.CreateTagsOutput, error)
DescribeSubnets(input *ec2v2.DescribeSubnetsInput) (*ec2v2.DescribeSubnetsOutput, error)
AssociateTrunkInterface(input *ec2.AssociateTrunkInterfaceInput) (*ec2.AssociateTrunkInterfaceOutput, error)
Expand Down Expand Up @@ -646,10 +646,10 @@ func (e *ec2Wrapper) DetachNetworkInterface(input *ec2.DetachNetworkInterfaceInp
return detachNetworkInterfaceOutput, err
}

func (e *ec2Wrapper) DescribeNetworkInterfaces(input *ec2.DescribeNetworkInterfacesInput) (*ec2.DescribeNetworkInterfacesOutput, error) {
func (e *ec2Wrapper) DescribeNetworkInterfaces(input *ec2v2.DescribeNetworkInterfacesInput) (*ec2v2.DescribeNetworkInterfacesOutput, error) {
start := time.Now()
describeNetworkInterfacesOutput, err := e.userServiceClient.DescribeNetworkInterfaces(input)
ec2APICallLatencies.WithLabelValues("describe_network_interface").Observe(timeSinceMs(start))
output, err := e.userServiceClientV2.DescribeNetworkInterfaces(context.Background(), input)
ec2APICallLatencies.WithLabelValues("describe_network_interfaces").Observe(timeSinceMs(start))

// Metric updates
ec2APICallCnt.Inc()
Expand All @@ -660,7 +660,7 @@ func (e *ec2Wrapper) DescribeNetworkInterfaces(input *ec2.DescribeNetworkInterfa
ec2DescribeNetworkInterfaceAPIErrCnt.Inc()
}

return describeNetworkInterfacesOutput, err
return output, err
}

func (e *ec2Wrapper) AssignPrivateIPAddresses(input *ec2.AssignPrivateIpAddressesInput) (*ec2.AssignPrivateIpAddressesOutput, error) {
Expand Down
5 changes: 3 additions & 2 deletions pkg/provider/branch/trunk/trunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/aws/vpc"
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/config"
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/provider/branch/cooldown"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
awsEC2Types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/samber/lo"

Expand Down Expand Up @@ -286,7 +287,7 @@ func (t *trunkENI) InitTrunk(instance ec2.EC2Instance, podList []v1.Pod) error {
}

// Convert the list of interfaces to a set
associatedBranchInterfaces := make(map[string]*awsEC2.NetworkInterface)
associatedBranchInterfaces := make(map[string]awsEC2Types.NetworkInterface)
for _, branchInterface := range branchInterfaces {
associatedBranchInterfaces[*branchInterface.NetworkInterfaceId] = branchInterface
}
Expand Down Expand Up @@ -687,7 +688,7 @@ func (t *trunkENI) freeVlanId(vlanId int) {
t.usedVlanIds[vlanId] = false
}

func (t *trunkENI) getVlanIdFromTag(tags []*awsEC2.Tag) (int, error) {
func (t *trunkENI) getVlanIdFromTag(tags []types.Tag) (int, error) {

for _, tag := range tags {
if *tag.Key == config.VLandIDTag {
Expand Down

0 comments on commit 668f83d

Please sign in to comment.