internal/awscloud: use AWS.DeleteEC2Image() from osbuild/images

Signed-off-by: Tomáš Hozza <thozza@redhat.com>
This commit is contained in:
Tomáš Hozza 2025-08-11 13:56:57 +02:00 committed by Tomáš Hozza
parent e7d8431489
commit 3db1f88f84
5 changed files with 2 additions and 80 deletions

View file

@ -9,6 +9,7 @@ import (
"sync"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"golang.org/x/sync/semaphore"
@ -96,7 +97,7 @@ func AWSCleanup(maxConcurrentRequests int, dryRun bool, accessKeyID, accessKey s
defer sem.Release(1)
defer wg.Done()
err := a.RemoveSnapshotAndDeregisterImage(&images[i])
err := a.DeleteEC2Image(aws.ToString(images[i].ImageId))
if err != nil {
log.Printf("Cleanup for image %s in region %s failed: %v", *images[i].ImageId, region, err)
}

View file

@ -35,12 +35,10 @@ type EC2 interface {
// Images
CopyImage(context.Context, *ec2.CopyImageInput, ...func(*ec2.Options)) (*ec2.CopyImageOutput, error)
DeregisterImage(context.Context, *ec2.DeregisterImageInput, ...func(*ec2.Options)) (*ec2.DeregisterImageOutput, error)
DescribeImages(context.Context, *ec2.DescribeImagesInput, ...func(*ec2.Options)) (*ec2.DescribeImagesOutput, error)
ModifyImageAttribute(context.Context, *ec2.ModifyImageAttributeInput, ...func(*ec2.Options)) (*ec2.ModifyImageAttributeOutput, error)
// Snapshots
DeleteSnapshot(context.Context, *ec2.DeleteSnapshotInput, ...func(*ec2.Options)) (*ec2.DeleteSnapshotOutput, error)
DescribeImportSnapshotTasks(context.Context, *ec2.DescribeImportSnapshotTasksInput, ...func(*ec2.Options)) (*ec2.DescribeImportSnapshotTasksOutput, error)
ModifySnapshotAttribute(context.Context, *ec2.ModifySnapshotAttributeInput, ...func(*ec2.Options)) (*ec2.ModifySnapshotAttributeOutput, error)

View file

@ -8,7 +8,6 @@ import (
"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
@ -32,41 +31,6 @@ func (a *AWS) DescribeImagesByTag(tagKey, tagValue string) ([]ec2types.Image, er
return imgs.Images, 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
}
func (a *AWS) describeInstancesByKeyValue(key, value string) ([]ec2types.Reservation, error) {
res, err := a.ec2.DescribeInstances(
context.Background(),

View file

@ -1,31 +0,0 @@
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)
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"])
}

View file

@ -246,11 +246,6 @@ func (m *ec2mock) CopyImage(ctx context.Context, input *ec2.CopyImageInput, optf
}, nil
}
func (m *ec2mock) DeregisterImage(ctx context.Context, input *ec2.DeregisterImageInput, optfns ...func(*ec2.Options)) (*ec2.DeregisterImageOutput, error) {
m.calledFn["DeregisterImage"] += 1
return nil, nil
}
func (m *ec2mock) DescribeImages(ctx context.Context, input *ec2.DescribeImagesInput, optfns ...func(*ec2.Options)) (*ec2.DescribeImagesOutput, error) {
m.calledFn["DescribeImages"] += 1
return &ec2.DescribeImagesOutput{
@ -275,11 +270,6 @@ func (m *ec2mock) ModifyImageAttribute(ctx context.Context, input *ec2.ModifyIma
return nil, nil
}
func (m *ec2mock) DeleteSnapshot(ctx context.Context, input *ec2.DeleteSnapshotInput, optfns ...func(*ec2.Options)) (*ec2.DeleteSnapshotOutput, error) {
m.calledFn["DeleteSnapshot"] += 1
return nil, nil
}
func (m *ec2mock) DescribeImportSnapshotTasks(ctx context.Context, input *ec2.DescribeImportSnapshotTasksInput, optfns ...func(*ec2.Options)) (*ec2.DescribeImportSnapshotTasksOutput, error) {
m.calledFn["DescribeImportSnapshotTasks"] += 1
return &ec2.DescribeImportSnapshotTasksOutput{