AWS: allow specifying the AMI boot mode when registering the image

When the AMI is being registered from a snapshot, the caller can
optionally specify the boot mode of the AMI. If no boot mode is
specified, then the default behavior is to use the boot type of the
instance that is launched from the AMI.

The default behavior (no boot type specified) is preserved after this
change.

Signed-off-by: Tomáš Hozza <thozza@redhat.com>
This commit is contained in:
Tomáš Hozza 2023-05-16 11:45:07 +02:00 committed by Achilleas Koutsou
parent 594778a230
commit e13f0a1ae2
4 changed files with 15 additions and 4 deletions

View file

@ -50,7 +50,7 @@ func main() {
if shareWith != "" {
share = append(share, shareWith)
}
ami, err := a.Register(imageName, bucketName, keyName, share, arch)
ami, err := a.Register(imageName, bucketName, keyName, share, arch, nil)
if err != nil {
println(err.Error())
return

View file

@ -511,7 +511,7 @@ func (impl *OSBuildJobImpl) Run(job worker.Job) error {
break
}
ami, err := a.Register(jobTarget.ImageName, bucket, targetOptions.Key, targetOptions.ShareWithAccounts, common.CurrentArch())
ami, err := a.Register(jobTarget.ImageName, bucket, targetOptions.Key, targetOptions.ShareWithAccounts, common.CurrentArch(), nil)
if err != nil {
targetResult.TargetError = clienterrors.WorkerClientError(clienterrors.ErrorImportingImage, err.Error(), nil)
break

View file

@ -99,7 +99,7 @@ func UploadImageToAWS(c *awsCredentials, imagePath string, imageName string) err
if err != nil {
return fmt.Errorf("cannot upload the image: %v", err)
}
_, err = uploader.Register(imageName, c.Bucket, imageName, nil, common.CurrentArch())
_, err = uploader.Register(imageName, c.Bucket, imageName, nil, common.CurrentArch(), nil)
if err != nil {
return fmt.Errorf("cannot register the image: %v", err)
}

View file

@ -15,6 +15,7 @@ import (
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/sirupsen/logrus"
"golang.org/x/exp/slices"
)
type AWS struct {
@ -208,7 +209,10 @@ func WaitUntilImportSnapshotTaskCompletedWithContext(c *ec2.EC2, ctx aws.Context
// Register is a function that imports a snapshot, waits for the snapshot to
// fully import, tags the snapshot, cleans up the image in S3, and registers
// an AMI in AWS.
func (a *AWS) Register(name, bucket, key string, shareWith []string, rpmArch string) (*string, error) {
// The caller can optionally specify the boot mode of the AMI. If the boot
// mode is not specified, then the instances launched from this AMI use the
// default boot mode value of the instance type.
func (a *AWS) Register(name, bucket, key string, shareWith []string, rpmArch string, bootMode *string) (*string, error) {
rpmArchToEC2Arch := map[string]string{
"x86_64": "x86_64",
"aarch64": "arm64",
@ -219,6 +223,12 @@ func (a *AWS) Register(name, bucket, key string, shareWith []string, rpmArch str
return nil, fmt.Errorf("ec2 doesn't support the following arch: %s", rpmArch)
}
if bootMode != nil {
if !slices.Contains(ec2.BootModeValues_Values(), *bootMode) {
return nil, fmt.Errorf("ec2 doesn't support the following boot mode: %s", *bootMode)
}
}
logrus.Infof("[AWS] 📥 Importing snapshot from image: %s/%s", bucket, key)
snapshotDescription := fmt.Sprintf("Image Builder AWS Import of %s", name)
importTaskOutput, err := a.ec2.ImportSnapshot(
@ -294,6 +304,7 @@ func (a *AWS) Register(name, bucket, key string, shareWith []string, rpmArch str
registerOutput, err := a.ec2.RegisterImage(
&ec2.RegisterImageInput{
Architecture: aws.String(ec2Arch),
BootMode: bootMode,
VirtualizationType: aws.String("hvm"),
Name: aws.String(name),
RootDeviceName: aws.String("/dev/sda1"),