Migrate AMIs to aws-sdk-go-v2

This commit is contained in:
Peter Rifel 2024-04-09 21:35:52 -05:00
parent 3faa28b65e
commit 174da2201b
No known key found for this signature in database
4 changed files with 27 additions and 24 deletions

View File

@ -180,7 +180,7 @@ type AWSCloud interface {
// `ami-...` in which case it is presumed to be an id
// owner/name in which case we find the image with the specified name, owned by owner
// name in which case we find the image with the specified name, with the current owner
ResolveImage(name string) (*ec2.Image, error)
ResolveImage(name string) (*ec2types.Image, error)
// WithTags created a copy of AWSCloud with the specified default-tags bound
WithTags(tags map[string]string) AWSCloud
@ -1994,7 +1994,7 @@ func describeVPC(c AWSCloud, vpcID string) (*ec2types.Vpc, error) {
// `ami-...` in which case it is presumed to be an id
// owner/name in which case we find the image with the specified name, owned by owner
// name in which case we find the image with the specified name, with the current owner
func (c *awsCloudImplementation) ResolveImage(name string) (*ec2.Image, error) {
func (c *awsCloudImplementation) ResolveImage(name string) (*ec2types.Image, error) {
return resolveImage(context.TODO(), c.ssm, c.ec2, name)
}
@ -2009,17 +2009,17 @@ func resolveSSMParameter(ctx context.Context, ssmClient awsinterfaces.SSMAPI, na
return "", fmt.Errorf("failed to get value for SSM parameter: %w", err)
}
return aws.StringValue(response.Parameter.Value), nil
return aws.ToString(response.Parameter.Value), nil
}
func resolveImage(ctx context.Context, ssmClient awsinterfaces.SSMAPI, ec2Client ec2iface.EC2API, name string) (*ec2.Image, error) {
func resolveImage(ctx context.Context, ssmClient awsinterfaces.SSMAPI, ec2Client awsinterfaces.EC2API, name string) (*ec2types.Image, error) {
// TODO: Cache this result during a single execution (we get called multiple times)
klog.V(2).Infof("Calling DescribeImages to resolve name %q", name)
request := &ec2.DescribeImagesInput{}
if strings.HasPrefix(name, "ami-") {
// ami-xxxxxxxx
request.ImageIds = []*string{&name}
request.ImageIds = []string{name}
} else if strings.HasPrefix(name, "ssm:") {
parameter := strings.TrimPrefix(name, "ssm:")
@ -2028,13 +2028,13 @@ func resolveImage(ctx context.Context, ssmClient awsinterfaces.SSMAPI, ec2Client
return nil, err
}
request.ImageIds = []*string{&image}
request.ImageIds = []string{image}
} else {
// Either <imagename> or <owner>/<imagename>
tokens := strings.SplitN(name, "/", 2)
if len(tokens) == 1 {
// self is a well-known value in the DescribeImages call
request.Owners = aws.StringSlice([]string{"self"})
request.Owners = []string{"self"}
request.Filters = append(request.Filters, NewEC2Filter("name", name))
} else if len(tokens) == 2 {
owner := tokens[0]
@ -2055,36 +2055,38 @@ func resolveImage(ctx context.Context, ssmClient awsinterfaces.SSMAPI, ec2Client
owner = WellKnownAccountUbuntu
}
request.Owners = []*string{&owner}
request.Owners = []string{owner}
request.Filters = append(request.Filters, NewEC2Filter("name", tokens[1]))
} else {
return nil, fmt.Errorf("image name specification not recognized: %q", name)
}
}
var image *ec2.Image
err := ec2Client.DescribeImagesPagesWithContext(context.TODO(), request, func(output *ec2.DescribeImagesOutput, b bool) bool {
for _, v := range output.Images {
var image *ec2types.Image
paginator := ec2.NewDescribeImagesPaginator(ec2Client, request)
for paginator.HasMorePages() {
page, err := paginator.NextPage(ctx)
if err != nil {
return nil, fmt.Errorf("error listing images: %v", err)
}
for _, v := range page.Images {
if image == nil {
image = v
image = &v
} else {
itime, _ := time.Parse(time.RFC3339, *image.CreationDate)
vtime, _ := time.Parse(time.RFC3339, *v.CreationDate)
if vtime.After(itime) {
image = v
image = &v
}
}
}
return true
})
if err != nil {
return nil, fmt.Errorf("error listing images: %v", err)
}
if image == nil {
return nil, fmt.Errorf("could not find Image for %q", name)
}
klog.V(4).Infof("Resolved image %q", aws.StringValue(image.ImageId))
klog.V(4).Infof("Resolved image %q", aws.ToString(image.ImageId))
return image, nil
}

View File

@ -221,7 +221,7 @@ func (c *MockAWSCloud) DescribeVPC(vpcID string) (*ec2types.Vpc, error) {
return describeVPC(c, vpcID)
}
func (c *MockAWSCloud) ResolveImage(name string) (*ec2.Image, error) {
func (c *MockAWSCloud) ResolveImage(name string) (*ec2types.Image, error) {
return resolveImage(context.TODO(), c.MockSSM, c.MockEC2, name)
}

View File

@ -28,6 +28,7 @@ When defining a new function:
package cloudup
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
@ -963,8 +964,8 @@ func (tf *TemplateFunctions) GetClusterAutoscalerNodeGroups() map[string]Cluster
func (tf *TemplateFunctions) architectureOfAMI(amiID string) string {
image, _ := tf.cloud.(awsup.AWSCloud).ResolveImage(amiID)
switch *image.Architecture {
case "x86_64":
switch image.Architecture {
case ec2types.ArchitectureValuesX8664:
return "amd64"
}
return "arm64"

View File

@ -22,7 +22,7 @@ import (
"testing"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go/service/ec2"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"k8s.io/kops/cloudmock/aws/mockec2"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/featureflag"
@ -300,13 +300,13 @@ func Test_TemplateFunctions_CloudControllerConfigArgv(t *testing.T) {
func Test_KarpenterInstanceTypes(t *testing.T) {
amiId := "ami-073c8c0760395aab8"
ec2Client := &mockec2.MockEC2{}
ec2Client.Images = append(ec2Client.Images, &ec2.Image{
ec2Client.Images = append(ec2Client.Images, &ec2types.Image{
CreationDate: aws.String("2016-10-21T20:07:19.000Z"),
ImageId: &amiId,
Name: aws.String("focal"),
OwnerId: aws.String(awsup.WellKnownAccountUbuntu),
RootDeviceName: aws.String("/dev/xvda"),
Architecture: aws.String("x86_64"),
Architecture: ec2types.ArchitectureValuesX8664,
})
ig := kops.InstanceGroupSpec{
Image: amiId,