@@ -17,13 +17,15 @@ limitations under the License.
1717package ami
1818
1919import (
20+ "context"
2021 "fmt"
2122 "strconv"
2223 "time"
2324
24- "github.com/aws/aws-sdk-go/aws"
25- "github.com/aws/aws-sdk-go/aws/session"
26- "github.com/aws/aws-sdk-go/service/ec2"
25+ "github.com/aws/aws-sdk-go-v2/aws"
26+ "github.com/aws/aws-sdk-go-v2/config"
27+ "github.com/aws/aws-sdk-go-v2/service/ec2"
28+ "github.com/aws/aws-sdk-go-v2/service/ec2/types"
2729 "github.com/go-logr/logr"
2830 "github.com/pkg/errors"
2931 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -48,14 +50,11 @@ type CopyInput struct {
4850
4951// Copy will create an AWSAMI from a CopyInput.
5052func Copy (input CopyInput ) (* amiv1.AWSAMI , error ) {
51- sourceSession , err := session .NewSessionWithOptions (session.Options {
52- SharedConfigState : session .SharedConfigEnable ,
53- Config : aws.Config {Region : aws .String (input .SourceRegion )},
54- })
53+ sourceCfg , err := config .LoadDefaultConfig (context .TODO (), config .WithRegion (input .SourceRegion ))
5554 if err != nil {
5655 return nil , err
5756 }
58- ec2Client := ec2 .New ( sourceSession )
57+ ec2Client := ec2 .NewFromConfig ( sourceCfg )
5958
6059 image , err := ec2service .DefaultAMILookup (ec2Client , input .OwnerID , input .OperatingSystem , input .KubernetesVersion , ec2service .Amd64ArchitectureTag , "" )
6160 if err != nil {
@@ -64,10 +63,7 @@ func Copy(input CopyInput) (*amiv1.AWSAMI, error) {
6463
6564 var newImageID , newImageName string
6665
67- destSession , err := session .NewSessionWithOptions (session.Options {
68- SharedConfigState : session .SharedConfigEnable ,
69- Config : aws.Config {Region : aws .String (input .DestinationRegion )},
70- })
66+ destCfg , err := config .LoadDefaultConfig (context .TODO (), config .WithRegion (input .DestinationRegion ))
7167 if err != nil {
7268 return nil , err
7369 }
@@ -79,15 +75,15 @@ func Copy(input CopyInput) (*amiv1.AWSAMI, error) {
7975 destinationRegion : input .DestinationRegion ,
8076 encrypted : input .Encrypted ,
8177 kmsKeyID : input .KmsKeyID ,
82- sess : destSession ,
78+ cfg : destCfg ,
8379 log : input .Log ,
8480 })
8581 } else {
8682 newImageName , newImageID , err = copyWithoutSnapshot (copyWithoutSnapshotInput {
8783 sourceRegion : input .SourceRegion ,
8884 image : image ,
8985 dryRun : input .DryRun ,
90- sess : destSession ,
86+ cfg : destCfg ,
9187 log : input .Log ,
9288 })
9389 }
@@ -113,9 +109,7 @@ func Copy(input CopyInput) (*amiv1.AWSAMI, error) {
113109 },
114110 }
115111
116- if err == nil {
117- input .Log .Info ("Completed!" )
118- }
112+ input .Log .Info ("Completed!" )
119113
120114 return & ami , err
121115}
@@ -124,13 +118,13 @@ type copyWithoutSnapshotInput struct {
124118 sourceRegion string
125119 dryRun bool
126120 log logr.Logger
127- sess * session. Session
128- image * ec2 .Image
121+ cfg aws. Config
122+ image * types .Image
129123}
130124
131125func copyWithoutSnapshot (input copyWithoutSnapshotInput ) (string , string , error ) {
132- imgName := aws .StringValue (input .image .Name )
133- ec2Client := ec2 .New (input .sess )
126+ imgName := aws .ToString (input .image .Name )
127+ ec2Client := ec2 .NewFromConfig (input .cfg )
134128 in2 := & ec2.CopyImageInput {
135129 Description : input .image .Description ,
136130 DryRun : aws .Bool (input .dryRun ),
@@ -139,14 +133,14 @@ func copyWithoutSnapshot(input copyWithoutSnapshotInput) (string, string, error)
139133 SourceRegion : aws .String (input .sourceRegion ),
140134 }
141135 log := input .log .WithValues ("imageName" , imgName )
142- log .Info ("Copying the retrieved image" , "imageID" , aws .StringValue (input .image .ImageId ), "ownerID" , aws .StringValue (input .image .OwnerId ))
143- out , err := ec2Client .CopyImage (in2 )
136+ log .Info ("Copying the retrieved image" , "imageID" , aws .ToString (input .image .ImageId ), "ownerID" , aws .ToString (input .image .OwnerId ))
137+ out , err := ec2Client .CopyImage (context . TODO (), in2 )
144138 if err != nil {
145- fmt .Printf ("version %q \n " , out )
139+ fmt .Printf ("version %v \n " , out )
146140 return imgName , "" , err
147141 }
148142
149- return imgName , aws .StringValue (out .ImageId ), nil
143+ return imgName , aws .ToString (out .ImageId ), nil
150144}
151145
152146type copyWithSnapshotInput struct {
@@ -156,12 +150,12 @@ type copyWithSnapshotInput struct {
156150 dryRun bool
157151 encrypted bool
158152 log logr.Logger
159- image * ec2 .Image
160- sess * session. Session
153+ image * types .Image
154+ cfg aws. Config
161155}
162156
163157func copyWithSnapshot (input copyWithSnapshotInput ) (string , string , error ) {
164- ec2Client := ec2 .New (input .sess )
158+ ec2Client := ec2 .NewFromConfig (input .cfg )
165159 imgName := * input .image .Name + util .RandomString (3 ) + strconv .Itoa (int (time .Now ().Unix ()))
166160 log := input .log .WithValues ("imageName" , imgName )
167161
@@ -175,52 +169,57 @@ func copyWithSnapshot(input copyWithSnapshotInput) (string, string, error) {
175169 }
176170
177171 copyInput := & ec2.CopySnapshotInput {
178- Description : input .image .Description ,
179- DestinationRegion : aws .String (input .destinationRegion ),
180- DryRun : aws .Bool (input .dryRun ),
181- Encrypted : aws .Bool (input .encrypted ),
182- SourceRegion : aws .String (input .sourceRegion ),
183- KmsKeyId : kmsKeyIDPtr ,
184- SourceSnapshotId : input .image .BlockDeviceMappings [0 ].Ebs .SnapshotId ,
172+ Description : input .image .Description ,
173+ DryRun : aws .Bool (input .dryRun ),
174+ Encrypted : aws .Bool (input .encrypted ),
175+ SourceRegion : aws .String (input .sourceRegion ),
176+ KmsKeyId : kmsKeyIDPtr ,
177+ SourceSnapshotId : input .image .BlockDeviceMappings [0 ].Ebs .SnapshotId ,
185178 }
186179
187180 // Generate a presigned url from the CopySnapshotInput
188- req , _ := ec2Client .CopySnapshotRequest (copyInput )
189- str , err := req .Presign (15 * time .Minute )
181+ scl := ec2 .NewPresignClient (ec2Client )
182+ str , err := scl .PresignCopySnapshot (context .TODO (), copyInput , ec2 .WithPresignClientFromClientOptions (
183+ func (o * ec2.Options ) {
184+ o .Region = input .destinationRegion
185+ },
186+ ))
190187 if err != nil {
191188 return imgName , "" , errors .Wrap (err , "Failed to generate presigned url" )
192189 }
193- copyInput .PresignedUrl = aws .String (str )
190+ copyInput .PresignedUrl = aws .String (str . URL )
194191
195- out , err := ec2Client .CopySnapshot (copyInput )
192+ out , err := ec2Client .CopySnapshot (context .TODO (), copyInput , func (o * ec2.Options ) {
193+ o .Region = input .destinationRegion
194+ })
196195 if err != nil {
197196 return imgName , "" , errors .Wrap (err , "Failed copying snapshot" )
198197 }
199198 log .Info ("Copying snapshot, this may take a couple of minutes..." ,
200- "sourceSnapshot" , aws .StringValue (input .image .BlockDeviceMappings [0 ].Ebs .SnapshotId ),
201- "destinationSnapshot" , aws .StringValue (out .SnapshotId ),
199+ "sourceSnapshot" , aws .ToString (input .image .BlockDeviceMappings [0 ].Ebs .SnapshotId ),
200+ "destinationSnapshot" , aws .ToString (out .SnapshotId ),
202201 )
203202
204- err = ec2Client . WaitUntilSnapshotCompleted ( & ec2.DescribeSnapshotsInput {
203+ err = ec2 . NewSnapshotCompletedWaiter ( ec2Client ). Wait ( context . TODO (), & ec2.DescribeSnapshotsInput {
205204 DryRun : aws .Bool (input .dryRun ),
206- SnapshotIds : []* string {out .SnapshotId },
207- })
205+ SnapshotIds : []string {aws . ToString ( out .SnapshotId ) },
206+ }, time . Hour * 1 )
208207 if err != nil {
209- return imgName , "" , errors .Wrap (err , fmt .Sprintf ("Failed waiting for encrypted snapshot copy completion: %q\n " , aws .StringValue (out .SnapshotId )))
208+ return imgName , "" , errors .Wrap (err , fmt .Sprintf ("Failed waiting for encrypted snapshot copy completion: %q\n " , aws .ToString (out .SnapshotId )))
210209 }
211210
212- ebsMapping := & ec2 .BlockDeviceMapping {
211+ ebsMapping := types .BlockDeviceMapping {
213212 DeviceName : input .image .BlockDeviceMappings [0 ].DeviceName ,
214- Ebs : & ec2 .EbsBlockDevice {
213+ Ebs : & types .EbsBlockDevice {
215214 SnapshotId : out .SnapshotId ,
216215 },
217216 }
218217
219218 log .Info ("Registering AMI" )
220219
221- registerOut , err := ec2Client .RegisterImage (& ec2.RegisterImageInput {
220+ registerOut , err := ec2Client .RegisterImage (context . TODO (), & ec2.RegisterImageInput {
222221 Architecture : input .image .Architecture ,
223- BlockDeviceMappings : []* ec2 .BlockDeviceMapping {ebsMapping },
222+ BlockDeviceMappings : []types .BlockDeviceMapping {ebsMapping },
224223 Description : input .image .Description ,
225224 DryRun : aws .Bool (input .dryRun ),
226225 EnaSupport : input .image .EnaSupport ,
@@ -229,12 +228,12 @@ func copyWithSnapshot(input copyWithSnapshotInput) (string, string, error) {
229228 RamdiskId : input .image .RamdiskId ,
230229 RootDeviceName : input .image .RootDeviceName ,
231230 SriovNetSupport : input .image .SriovNetSupport ,
232- VirtualizationType : input .image .VirtualizationType ,
231+ VirtualizationType : aws . String ( string ( input .image .VirtualizationType )) ,
233232 })
234233
235234 if err != nil {
236235 return imgName , "" , err
237236 }
238237
239- return imgName , aws .StringValue (registerOut .ImageId ), err
238+ return imgName , aws .ToString (registerOut .ImageId ), err
240239}
0 commit comments