github.com/openshift/installer@v1.4.17/pkg/infrastructure/aws/clusterapi/ami.go (about) 1 package clusterapi 2 3 import ( 4 "context" 5 "fmt" 6 "strings" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/service/ec2" 10 "github.com/sirupsen/logrus" 11 12 "github.com/openshift/installer/pkg/asset/installconfig" 13 "github.com/openshift/installer/pkg/asset/rhcos" 14 ) 15 16 // copyAMIToRegion copies the AMI to the region configured in the installConfig if needed. 17 func copyAMIToRegion(ctx context.Context, installConfig *installconfig.InstallConfig, infraID string, rhcosImage *rhcos.Image) (string, error) { 18 osImage := strings.SplitN(rhcosImage.ControlPlane, ",", 2) 19 amiID, amiRegion := osImage[0], osImage[1] 20 21 logrus.Infof("Copying AMI %s to region %s", amiID, installConfig.AWS.Region) 22 23 session, err := installConfig.AWS.Session(ctx) 24 if err != nil { 25 return "", fmt.Errorf("failed to get AWS session: %w", err) 26 } 27 client := ec2.New(session) 28 29 res, err := client.CopyImageWithContext(ctx, &ec2.CopyImageInput{ 30 Name: aws.String(fmt.Sprintf("%s-master", infraID)), 31 ClientToken: aws.String(infraID), 32 Description: aws.String("Created by Openshift Installer"), 33 SourceImageId: aws.String(amiID), 34 SourceRegion: aws.String(amiRegion), 35 Encrypted: aws.Bool(true), 36 }) 37 if err != nil { 38 return "", err 39 } 40 41 name := fmt.Sprintf("%s-ami-%s", infraID, installConfig.AWS.Region) 42 amiTags := make([]*ec2.Tag, 0, len(installConfig.Config.AWS.UserTags)+4) 43 for k, v := range installConfig.Config.AWS.UserTags { 44 amiTags = append(amiTags, &ec2.Tag{ 45 Key: aws.String(k), 46 Value: aws.String(v), 47 }) 48 } 49 for k, v := range map[string]string{ 50 "Name": name, 51 "sourceAMI": amiID, 52 "sourceRegion": amiRegion, 53 fmt.Sprintf("kubernetes.io/cluster/%s", infraID): "owned", 54 } { 55 amiTags = append(amiTags, &ec2.Tag{ 56 Key: aws.String(k), 57 Value: aws.String(v), 58 }) 59 } 60 61 _, err = client.CreateTagsWithContext(ctx, &ec2.CreateTagsInput{ 62 Resources: []*string{res.ImageId}, 63 Tags: amiTags, 64 }) 65 if err != nil { 66 return "", fmt.Errorf("failed to tag AMI copy (%s): %w", name, err) 67 } 68 69 return aws.StringValue(res.ImageId), nil 70 }