From 8fc91d1c6d98bb468a4ac43a52c540930ec1c225 Mon Sep 17 00:00:00 2001 From: Sanne Raymaekers Date: Mon, 21 Oct 2024 16:11:32 +0200 Subject: [PATCH] cloud/aws: move maintenance calls to separate file --- internal/cloud/awscloud/awscloud.go | 53 ----------------- internal/cloud/awscloud/awscloud_test.go | 22 ------- internal/cloud/awscloud/maintenance.go | 64 +++++++++++++++++++++ internal/cloud/awscloud/maintenance_test.go | 31 ++++++++++ 4 files changed, 95 insertions(+), 75 deletions(-) create mode 100644 internal/cloud/awscloud/maintenance.go create mode 100644 internal/cloud/awscloud/maintenance_test.go diff --git a/internal/cloud/awscloud/awscloud.go b/internal/cloud/awscloud/awscloud.go index 4a398e3f6..dbf4c952d 100644 --- a/internal/cloud/awscloud/awscloud.go +++ b/internal/cloud/awscloud/awscloud.go @@ -534,59 +534,6 @@ func (a *AWS) shareSnapshot(snapshotId string, userIds []string) error { return nil } -func (a *AWS) RemoveSnapshotAndDeregisterImage(image *ec2types.Image) error { - if image == nil { - return fmt.Errorf("image is nil") - } - - var snapshots []*string - for _, bdm := range image.BlockDeviceMappings { - snapshots = append(snapshots, bdm.Ebs.SnapshotId) - } - - _, err := a.ec2.DeregisterImage( - context.Background(), - &ec2.DeregisterImageInput{ - ImageId: image.ImageId, - }, - ) - if err != nil { - return err - } - - for _, s := range snapshots { - _, err = a.ec2.DeleteSnapshot( - context.Background(), - &ec2.DeleteSnapshotInput{ - SnapshotId: s, - }, - ) - if err != nil { - // TODO return err? - logrus.Warn("Unable to remove snapshot", s) - } - } - return err -} - -// For service maintenance images are discovered by the "Name:composer-api-*" tag filter. Currently -// all image names in the service are generated, so they're guaranteed to be unique as well. If -// users are ever allowed to name their images, an extra tag should be added. -func (a *AWS) DescribeImagesByTag(tagKey, tagValue string) ([]ec2types.Image, error) { - imgs, err := a.ec2.DescribeImages( - context.Background(), - &ec2.DescribeImagesInput{ - Filters: []ec2types.Filter{ - { - Name: aws.String(fmt.Sprintf("tag:%s", tagKey)), - Values: []string{tagValue}, - }, - }, - }, - ) - return imgs.Images, err -} - func (a *AWS) S3ObjectPresignedURL(bucket, objectKey string) (string, error) { logrus.Infof("[AWS] 📋 Generating Presigned URL for S3 object %s/%s", bucket, objectKey) diff --git a/internal/cloud/awscloud/awscloud_test.go b/internal/cloud/awscloud/awscloud_test.go index 59ebb725c..1e3bb3695 100644 --- a/internal/cloud/awscloud/awscloud_test.go +++ b/internal/cloud/awscloud/awscloud_test.go @@ -5,7 +5,6 @@ import ( "path/filepath" "testing" - ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types" "github.com/stretchr/testify/require" "github.com/osbuild/osbuild-composer/internal/cloud/awscloud" @@ -78,27 +77,6 @@ func TestEC2CopyImage(t *testing.T) { require.Equal(t, 2, m.calledFn["CreateTags"]) } -func TestEC2RemoveSnapshotAndDeregisterImage(t *testing.T) { - m := newEc2Mock(t) - aws := awscloud.NewForTest(m, nil, &s3mock{t, "bucket", "object-key"}, nil, nil) - require.NotNil(t, aws) - - err := aws.RemoveSnapshotAndDeregisterImage(&ec2types.Image{ - ImageId: &m.imageId, - State: ec2types.ImageStateAvailable, - BlockDeviceMappings: []ec2types.BlockDeviceMapping{ - { - Ebs: &ec2types.EbsBlockDevice{ - SnapshotId: &m.snapshotId, - }, - }, - }, - }) - require.NoError(t, err) - require.Equal(t, 1, m.calledFn["DeleteSnapshot"]) - require.Equal(t, 1, m.calledFn["DeregisterImage"]) -} - func TestEC2Regions(t *testing.T) { m := newEc2Mock(t) aws := awscloud.NewForTest(m, nil, &s3mock{t, "bucket", "object-key"}, nil, nil) diff --git a/internal/cloud/awscloud/maintenance.go b/internal/cloud/awscloud/maintenance.go new file mode 100644 index 000000000..a6f0acbe5 --- /dev/null +++ b/internal/cloud/awscloud/maintenance.go @@ -0,0 +1,64 @@ +package awscloud + +import ( + "context" + "fmt" + + "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" + "github.com/sirupsen/logrus" +) + +// For service maintenance images are discovered by the "Name:composer-api-*" tag filter. Currently +// all image names in the service are generated, so they're guaranteed to be unique as well. If +// users are ever allowed to name their images, an extra tag should be added. +func (a *AWS) DescribeImagesByTag(tagKey, tagValue string) ([]ec2types.Image, error) { + imgs, err := a.ec2.DescribeImages( + context.Background(), + &ec2.DescribeImagesInput{ + Filters: []ec2types.Filter{ + { + Name: aws.String(fmt.Sprintf("tag:%s", tagKey)), + Values: []string{tagValue}, + }, + }, + }, + ) + return imgs.Images, err +} + +func (a *AWS) RemoveSnapshotAndDeregisterImage(image *ec2types.Image) error { + if image == nil { + return fmt.Errorf("image is nil") + } + + var snapshots []*string + for _, bdm := range image.BlockDeviceMappings { + snapshots = append(snapshots, bdm.Ebs.SnapshotId) + } + + _, err := a.ec2.DeregisterImage( + context.Background(), + &ec2.DeregisterImageInput{ + ImageId: image.ImageId, + }, + ) + if err != nil { + return err + } + + for _, s := range snapshots { + _, err = a.ec2.DeleteSnapshot( + context.Background(), + &ec2.DeleteSnapshotInput{ + SnapshotId: s, + }, + ) + if err != nil { + // TODO return err? + logrus.Warn("Unable to remove snapshot", s) + } + } + return err +} diff --git a/internal/cloud/awscloud/maintenance_test.go b/internal/cloud/awscloud/maintenance_test.go new file mode 100644 index 000000000..3f53723d9 --- /dev/null +++ b/internal/cloud/awscloud/maintenance_test.go @@ -0,0 +1,31 @@ +package awscloud_test + +import ( + "testing" + + ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types" + "github.com/stretchr/testify/require" + + "github.com/osbuild/osbuild-composer/internal/cloud/awscloud" +) + +func TestEC2RemoveSnapshotAndDeregisterImage(t *testing.T) { + m := newEc2Mock(t) + aws := awscloud.NewForTest(m, nil, &s3mock{t, "bucket", "object-key"}, nil, nil) + require.NotNil(t, aws) + + err := aws.RemoveSnapshotAndDeregisterImage(&ec2types.Image{ + ImageId: &m.imageId, + State: ec2types.ImageStateAvailable, + BlockDeviceMappings: []ec2types.BlockDeviceMapping{ + { + Ebs: &ec2types.EbsBlockDevice{ + SnapshotId: &m.snapshotId, + }, + }, + }, + }) + require.NoError(t, err) + require.Equal(t, 1, m.calledFn["DeleteSnapshot"]) + require.Equal(t, 1, m.calledFn["DeregisterImage"]) +}