Skip to content

Commit

Permalink
aws: allow the user to customize their AMI name
Browse files Browse the repository at this point in the history
In the frontend a new field is allowing user to setup customized names
for their images (see
osbuild/image-builder-frontend#1136).

In order to allow the customization to effectively take place, this
commit is slightly changing how EC2 images are activated.

Before, two identical informations where communicated, the `Name` tag
and the AMI Name. Both being set to an identical generated value
starting with `composer-api-*`.

The maintenance service is using the `Name` tag value to filter out
which images are generated by composer by looking for the ones starting
with `composer-api-*`.

However, Tags can't be shared. Which means that no matter what is the
value we choose to put int the `Name` tag, the service user won't get
access to it.

Which means that the custom name only needs to be set in the AMI Name.
Meaning that the logic around the `Name` tag stays identical. Therefore
eliminating the need to update the maintenance service.

AMI Names must be unique. If a user is creating twice the same
image with the same name, AWS would refuse the upload. To avoid getting
the user blocked by this composer is appending a UUID to the custom
name, therefore making it most probably unique.

Lastly, tags are set both to the AMI and the snapshot, for consistency,
the same tags are sent for both of them.
  • Loading branch information
lavocatt committed Jan 15, 2024
1 parent 6735e74 commit ec07fd1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions internal/cloud/awscloud/awscloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func (a *AWS) Register(name, bucket, key string, shareWith []string, rpmArch str
Tags: []*ec2.Tag{
{
Key: aws.String("Name"),
Value: aws.String(name),
Value: aws.String(key),
},
},
},
Expand Down Expand Up @@ -332,7 +332,7 @@ func (a *AWS) Register(name, bucket, key string, shareWith []string, rpmArch str
Tags: []*ec2.Tag{
{
Key: aws.String("Name"),
Value: aws.String(name),
Value: aws.String(key),
},
},
},
Expand Down
13 changes: 7 additions & 6 deletions internal/cloudapi/v2/imagerequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ func newAWSTarget(options UploadOptions, imageType distro.ImageType) (*target.Ta
return nil, HTTPError(ErrorJSONUnMarshallingError)
}

// 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.
key := fmt.Sprintf("composer-api-%s", uuid.New().String())
// For service maintenance, images are discovered by the "Name:composer-api-*" tag filter. The `Name` tag differs
// from the AMI Name. The later being user customizable.
imageUUID := uuid.New()
key := fmt.Sprintf("composer-api-%s", imageUUID.String())

var amiBootMode *string
switch imageType.BootMode() {
Expand All @@ -69,8 +68,10 @@ func newAWSTarget(options UploadOptions, imageType distro.ImageType) (*target.Ta
ShareWithAccounts: awsUploadOptions.ShareWithAccounts,
BootMode: amiBootMode,
})
// The user can specify a custom image name with the SnapshotName field in the request. AWS will reject duplicated
// names. To increase the likelihood of acceptance, a UUID is appended to the custom name.
if awsUploadOptions.SnapshotName != nil {
t.ImageName = *awsUploadOptions.SnapshotName
t.ImageName = fmt.Sprintf("%s-%s", *awsUploadOptions.SnapshotName, imageUUID.String())
} else {
t.ImageName = key
}
Expand Down
2 changes: 1 addition & 1 deletion test/cases/api/aws.sh
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function checkUploadStatusOptions() {
function verify() {
$AWS_CMD ec2 describe-images \
--owners self \
--filters Name=name,Values="$AWS_SNAPSHOT_NAME" \
--filters Name=name,Values="$AWS_SNAPSHOT_NAME-*" \
> "$WORKDIR/ami.json"

AMI_IMAGE_ID=$(jq -r '.Images[].ImageId' "$WORKDIR/ami.json")
Expand Down

0 comments on commit ec07fd1

Please sign in to comment.