Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate EC2 to aws-sdk-go-v2 #16460

Merged
merged 14 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 6 additions & 7 deletions cloudmock/aws/mockautoscaling/ec2shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,29 @@ import (

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/autoscaling"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"k8s.io/kops/util/pkg/awsinterfaces"
)

type ec2Shim struct {
ec2iface.EC2API
awsinterfaces.EC2API
mockAutoscaling *MockAutoscaling
}

func (m *MockAutoscaling) GetEC2Shim(e ec2iface.EC2API) ec2iface.EC2API {
func (m *MockAutoscaling) GetEC2Shim(e awsinterfaces.EC2API) awsinterfaces.EC2API {
return &ec2Shim{
EC2API: e,
mockAutoscaling: m,
}
}

func (e *ec2Shim) TerminateInstances(input *ec2.TerminateInstancesInput) (*ec2.TerminateInstancesOutput, error) {
ctx := context.TODO()
func (e *ec2Shim) TerminateInstances(ctx context.Context, input *ec2.TerminateInstancesInput, optFns ...func(*ec2.Options)) (*ec2.TerminateInstancesOutput, error) {
if input.DryRun != nil && *input.DryRun {
return &ec2.TerminateInstancesOutput{}, nil
}
for _, id := range input.InstanceIds {
request := &autoscaling.TerminateInstanceInAutoScalingGroupInput{
InstanceId: id,
InstanceId: aws.String(id),
ShouldDecrementDesiredCapacity: aws.Bool(false),
}
if _, err := e.mockAutoscaling.TerminateInstanceInAutoScalingGroup(ctx, request); err != nil {
Expand Down
83 changes: 18 additions & 65 deletions cloudmock/aws/mockec2/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,17 @@ limitations under the License.
package mockec2

import (
"context"
"encoding/binary"
"fmt"
"net"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"k8s.io/klog/v2"
)

func (m *MockEC2) AllocateAddressRequest(*ec2.AllocateAddressInput) (*request.Request, *ec2.AllocateAddressOutput) {
panic("Not implemented")
}

func (m *MockEC2) AllocateAddressWithContext(aws.Context, *ec2.AllocateAddressInput, ...request.Option) (*ec2.AllocateAddressOutput, error) {
panic("Not implemented")
}

func (m *MockEC2) AllocateAddressWithId(request *ec2.AllocateAddressInput, id string) (*ec2.AllocateAddressOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
Expand All @@ -50,10 +43,10 @@ func (m *MockEC2) AllocateAddressWithId(request *ec2.AllocateAddressInput, id st
binary.BigEndian.PutUint32(publicIP, v)
}

tags := tagSpecificationsToTags(request.TagSpecifications, ec2.ResourceTypeElasticIp)
address := &ec2.Address{
tags := tagSpecificationsToTags(request.TagSpecifications, ec2types.ResourceTypeElasticIp)
address := &ec2types.Address{
AllocationId: s(id),
Domain: s("vpc"),
Domain: ec2types.DomainTypeVpc,
PublicIp: s(publicIP.String()),
Tags: tags,
}
Expand All @@ -62,7 +55,7 @@ func (m *MockEC2) AllocateAddressWithId(request *ec2.AllocateAddressInput, id st
}

if m.Addresses == nil {
m.Addresses = make(map[string]*ec2.Address)
m.Addresses = make(map[string]*ec2types.Address)
}
m.Addresses[id] = address
m.addTags(id, tags...)
Expand All @@ -75,54 +68,22 @@ func (m *MockEC2) AllocateAddressWithId(request *ec2.AllocateAddressInput, id st
return response, nil
}

func (m *MockEC2) AllocateAddress(request *ec2.AllocateAddressInput) (*ec2.AllocateAddressOutput, error) {
func (m *MockEC2) AllocateAddress(ctx context.Context, request *ec2.AllocateAddressInput, optFns ...func(*ec2.Options)) (*ec2.AllocateAddressOutput, error) {
klog.Infof("AllocateAddress: %v", request)
id := m.allocateId("eipalloc")
return m.AllocateAddressWithId(request, id)
}

func (m *MockEC2) AssignPrivateIpAddressesRequest(*ec2.AssignPrivateIpAddressesInput) (*request.Request, *ec2.AssignPrivateIpAddressesOutput) {
panic("Not implemented")
}

func (m *MockEC2) AssignPrivateIpAddressesWithContext(aws.Context, *ec2.AssignPrivateIpAddressesInput, ...request.Option) (*ec2.AssignPrivateIpAddressesOutput, error) {
panic("Not implemented")
}

func (m *MockEC2) AssignPrivateIpAddresses(*ec2.AssignPrivateIpAddressesInput) (*ec2.AssignPrivateIpAddressesOutput, error) {
panic("Not implemented")
}

func (m *MockEC2) AssociateAddressRequest(*ec2.AssociateAddressInput) (*request.Request, *ec2.AssociateAddressOutput) {
panic("Not implemented")
}

func (m *MockEC2) AssociateAddressWithContext(aws.Context, *ec2.AssociateAddressInput, ...request.Option) (*ec2.AssociateAddressOutput, error) {
panic("Not implemented")
}

func (m *MockEC2) AssociateAddress(*ec2.AssociateAddressInput) (*ec2.AssociateAddressOutput, error) {
panic("Not implemented")
}

func (m *MockEC2) DescribeAddressesRequest(*ec2.DescribeAddressesInput) (*request.Request, *ec2.DescribeAddressesOutput) {
panic("Not implemented")
}

func (m *MockEC2) DescribeAddressesWithContext(aws.Context, *ec2.DescribeAddressesInput, ...request.Option) (*ec2.DescribeAddressesOutput, error) {
panic("Not implemented")
}

func (m *MockEC2) DescribeAddresses(request *ec2.DescribeAddressesInput) (*ec2.DescribeAddressesOutput, error) {
func (m *MockEC2) DescribeAddresses(ctx context.Context, request *ec2.DescribeAddressesInput, optFns ...func(*ec2.Options)) (*ec2.DescribeAddressesOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

klog.Infof("DescribeAddresses: %v", request)

var addresses []*ec2.Address
var addresses []ec2types.Address

if len(request.AllocationIds) != 0 {
request.Filters = append(request.Filters, &ec2.Filter{Name: s("allocation-id"), Values: request.AllocationIds})
request.Filters = append(request.Filters, ec2types.Filter{Name: s("allocation-id"), Values: request.AllocationIds})
}
for _, address := range m.Addresses {
allFiltersMatch := true
Expand All @@ -132,14 +93,14 @@ func (m *MockEC2) DescribeAddresses(request *ec2.DescribeAddressesInput) (*ec2.D

case "allocation-id":
for _, v := range filter.Values {
if *address.AllocationId == *v {
if *address.AllocationId == v {
match = true
}
}

case "public-ip":
for _, v := range filter.Values {
if *address.PublicIp == *v {
if *address.PublicIp == v {
match = true
}
}
Expand All @@ -159,8 +120,8 @@ func (m *MockEC2) DescribeAddresses(request *ec2.DescribeAddressesInput) (*ec2.D
}

copy := *address
copy.Tags = m.getTags(ec2.ResourceTypeElasticIp, *address.AllocationId)
addresses = append(addresses, &copy)
copy.Tags = m.getTags(ec2types.ResourceTypeElasticIp, *address.AllocationId)
addresses = append(addresses, copy)
}

response := &ec2.DescribeAddressesOutput{
Expand All @@ -170,21 +131,13 @@ func (m *MockEC2) DescribeAddresses(request *ec2.DescribeAddressesInput) (*ec2.D
return response, nil
}

func (m *MockEC2) ReleaseAddressRequest(*ec2.ReleaseAddressInput) (*request.Request, *ec2.ReleaseAddressOutput) {
panic("Not implemented")
}

func (m *MockEC2) ReleaseAddressWithContext(aws.Context, *ec2.ReleaseAddressInput, ...request.Option) (*ec2.ReleaseAddressOutput, error) {
panic("Not implemented")
}

func (m *MockEC2) ReleaseAddress(request *ec2.ReleaseAddressInput) (*ec2.ReleaseAddressOutput, error) {
func (m *MockEC2) ReleaseAddress(ctx context.Context, request *ec2.ReleaseAddressInput, optFns ...func(*ec2.Options)) (*ec2.ReleaseAddressOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

klog.Infof("ReleaseAddress: %v", request)

id := aws.StringValue(request.AllocationId)
id := aws.ToString(request.AllocationId)
o := m.Addresses[id]
if o == nil {
return nil, fmt.Errorf("Address %q not found", id)
Expand Down
38 changes: 19 additions & 19 deletions cloudmock/aws/mockec2/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,59 +20,59 @@ import (
"fmt"
"sync"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/aws/aws-sdk-go-v2/aws"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"k8s.io/kops/util/pkg/awsinterfaces"
)

type MockEC2 struct {
// Stub out interface
ec2iface.EC2API
awsinterfaces.EC2API

mutex sync.Mutex

addressNumber int
Addresses map[string]*ec2.Address
Addresses map[string]*ec2types.Address

RouteTables map[string]*ec2.RouteTable
RouteTables map[string]*ec2types.RouteTable

DhcpOptions map[string]*ec2.DhcpOptions
DhcpOptions map[string]*ec2types.DhcpOptions

Images []*ec2.Image
Images []*ec2types.Image

securityGroupNumber int
SecurityGroups map[string]*ec2.SecurityGroup
SecurityGroupRules map[string]*ec2.SecurityGroupRule
SecurityGroups map[string]*ec2types.SecurityGroup
SecurityGroupRules map[string]*ec2types.SecurityGroupRule

subnets map[string]*subnetInfo

Volumes map[string]*ec2.Volume
Volumes map[string]*ec2types.Volume

KeyPairs map[string]*ec2.KeyPairInfo
KeyPairs map[string]*ec2types.KeyPairInfo

Tags []*ec2.TagDescription
Tags []*ec2types.TagDescription

Vpcs map[string]*vpcInfo

InternetGateways map[string]*ec2.InternetGateway
EgressOnlyInternetGateways map[string]*ec2.EgressOnlyInternetGateway
InternetGateways map[string]*ec2types.InternetGateway
EgressOnlyInternetGateways map[string]*ec2types.EgressOnlyInternetGateway

launchTemplateNumber int
LaunchTemplates map[string]*launchTemplateInfo

NatGateways map[string]*ec2.NatGateway
NatGateways map[string]*ec2types.NatGateway

idsMutex sync.Mutex
ids map[string]*idAllocator
}

var _ ec2iface.EC2API = &MockEC2{}
var _ awsinterfaces.EC2API = &MockEC2{}

func (m *MockEC2) All() map[string]interface{} {
all := make(map[string]interface{})

for _, o := range m.Addresses {
all[aws.StringValue(o.AllocationId)] = o
all[aws.ToString(o.AllocationId)] = o
}
for id, o := range m.RouteTables {
all[id] = o
Expand All @@ -81,7 +81,7 @@ func (m *MockEC2) All() map[string]interface{} {
all[id] = o
}
for _, o := range m.Images {
all[aws.StringValue(o.ImageId)] = o
all[aws.ToString(o.ImageId)] = o
}
for id, o := range m.SecurityGroups {
all[id] = o
Expand Down
2 changes: 1 addition & 1 deletion cloudmock/aws/mockec2/convenience.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

package mockec2

import "github.com/aws/aws-sdk-go/aws"
import "github.com/aws/aws-sdk-go-v2/aws"

// s is a helper that builds a *string from a string value
func s(v string) *string {
Expand Down