Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

awscloud: add import role parameter to AMI registration #1013

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions cmd/boot-aws/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,19 @@ func newClientFromArgs(flags *pflag.FlagSet) (*awscloud.AWS, error) {
return awscloud.New(region, keyID, secretKey, sessionToken)
}

// getOptionalStringFlag returns the value of a string flag if it's set, or nil
// if it's not set.
func getOptionalStringFlag(flags *pflag.FlagSet, name string) (*string, error) {
value, err := flags.GetString(name)
if err != nil {
return nil, err
}
if value == "" {
return nil, nil
}
return &value, nil
}

func doSetup(a *awscloud.AWS, filename string, flags *pflag.FlagSet, res *resources) error {
username, err := flags.GetString("username")
if err != nil {
Expand Down Expand Up @@ -185,10 +198,13 @@ func doSetup(a *awscloud.AWS, filename string, flags *pflag.FlagSet, res *resour

fmt.Printf("file uploaded to %s\n", aws.StringValue(&uploadOutput.Location))

var bootModePtr *string
if bootMode, err := flags.GetString("boot-mode"); bootMode != "" {
bootModePtr = &bootMode
} else if err != nil {
bootMode, err := getOptionalStringFlag(flags, "boot-mode")
if err != nil {
return err
}

importRole, err := getOptionalStringFlag(flags, "import-role")
if err != nil {
return err
}

Expand All @@ -202,7 +218,7 @@ func doSetup(a *awscloud.AWS, filename string, flags *pflag.FlagSet, res *resour
return err
}

ami, snapshot, err := a.Register(imageName, bucketName, keyName, nil, arch, bootModePtr)
ami, snapshot, err := a.Register(imageName, bucketName, keyName, nil, arch, bootMode, importRole)
if err != nil {
return fmt.Errorf("Register(): %s", err.Error())
}
Expand Down Expand Up @@ -474,6 +490,7 @@ func setupCLI() *cobra.Command {
rootFlags.String("ami-name", "", "AMI name")
rootFlags.String("arch", "", "arch (x86_64 or aarch64)")
rootFlags.String("boot-mode", "", "boot mode (legacy-bios, uefi, uefi-preferred)")
rootFlags.String("import-role", "", "name of the import role to be used (default is determined by the AWS API, it's usually 'vmimport')")
rootFlags.String("username", "", "name of the user to create on the system")
rootFlags.String("ssh-pubkey", "", "path to user's public ssh key")
rootFlags.String("ssh-privkey", "", "path to user's private ssh key")
Expand Down
5 changes: 4 additions & 1 deletion pkg/cloud/awscloud/awscloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,10 @@ func WaitUntilImportSnapshotTaskCompletedWithContext(c *ec2.EC2, ctx aws.Context
// 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.
// The caller can also specify the name of the role used to do the import.
// If nil is given, the default one from the SDK is used (vmimport).
// Returns the image ID and the snapshot ID.
func (a *AWS) Register(name, bucket, key string, shareWith []string, rpmArch string, bootMode *string) (*string, *string, error) {
func (a *AWS) Register(name, bucket, key string, shareWith []string, rpmArch string, bootMode, importRole *string) (*string, *string, error) {
rpmArchToEC2Arch := map[string]string{
"x86_64": "x86_64",
"aarch64": "arm64",
Expand Down Expand Up @@ -269,6 +271,7 @@ func (a *AWS) Register(name, bucket, key string, shareWith []string, rpmArch str
S3Key: aws.String(key),
},
},
RoleName: importRole,
},
)
if err != nil {
Expand Down
Loading