@@ -17,13 +17,15 @@ limitations under the License.
17
17
package ami
18
18
19
19
import (
20
+ "context"
20
21
"fmt"
21
22
"strconv"
22
23
"time"
23
24
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"
27
29
"github.com/go-logr/logr"
28
30
"github.com/pkg/errors"
29
31
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -48,14 +50,11 @@ type CopyInput struct {
48
50
49
51
// Copy will create an AWSAMI from a CopyInput.
50
52
func 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 ))
55
54
if err != nil {
56
55
return nil , err
57
56
}
58
- ec2Client := ec2 .New ( sourceSession )
57
+ ec2Client := ec2 .NewFromConfig ( sourceCfg )
59
58
60
59
image , err := ec2service .DefaultAMILookup (ec2Client , input .OwnerID , input .OperatingSystem , input .KubernetesVersion , ec2service .Amd64ArchitectureTag , "" )
61
60
if err != nil {
@@ -64,10 +63,7 @@ func Copy(input CopyInput) (*amiv1.AWSAMI, error) {
64
63
65
64
var newImageID , newImageName string
66
65
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 ))
71
67
if err != nil {
72
68
return nil , err
73
69
}
@@ -79,15 +75,15 @@ func Copy(input CopyInput) (*amiv1.AWSAMI, error) {
79
75
destinationRegion : input .DestinationRegion ,
80
76
encrypted : input .Encrypted ,
81
77
kmsKeyID : input .KmsKeyID ,
82
- sess : destSession ,
78
+ cfg : destCfg ,
83
79
log : input .Log ,
84
80
})
85
81
} else {
86
82
newImageName , newImageID , err = copyWithoutSnapshot (copyWithoutSnapshotInput {
87
83
sourceRegion : input .SourceRegion ,
88
84
image : image ,
89
85
dryRun : input .DryRun ,
90
- sess : destSession ,
86
+ cfg : destCfg ,
91
87
log : input .Log ,
92
88
})
93
89
}
@@ -113,9 +109,7 @@ func Copy(input CopyInput) (*amiv1.AWSAMI, error) {
113
109
},
114
110
}
115
111
116
- if err == nil {
117
- input .Log .Info ("Completed!" )
118
- }
112
+ input .Log .Info ("Completed!" )
119
113
120
114
return & ami , err
121
115
}
@@ -124,13 +118,13 @@ type copyWithoutSnapshotInput struct {
124
118
sourceRegion string
125
119
dryRun bool
126
120
log logr.Logger
127
- sess * session. Session
128
- image * ec2 .Image
121
+ cfg aws. Config
122
+ image * types .Image
129
123
}
130
124
131
125
func 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 )
134
128
in2 := & ec2.CopyImageInput {
135
129
Description : input .image .Description ,
136
130
DryRun : aws .Bool (input .dryRun ),
@@ -139,14 +133,14 @@ func copyWithoutSnapshot(input copyWithoutSnapshotInput) (string, string, error)
139
133
SourceRegion : aws .String (input .sourceRegion ),
140
134
}
141
135
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 )
144
138
if err != nil {
145
- fmt .Printf ("version %q \n " , out )
139
+ fmt .Printf ("version %v \n " , out )
146
140
return imgName , "" , err
147
141
}
148
142
149
- return imgName , aws .StringValue (out .ImageId ), nil
143
+ return imgName , aws .ToString (out .ImageId ), nil
150
144
}
151
145
152
146
type copyWithSnapshotInput struct {
@@ -156,12 +150,12 @@ type copyWithSnapshotInput struct {
156
150
dryRun bool
157
151
encrypted bool
158
152
log logr.Logger
159
- image * ec2 .Image
160
- sess * session. Session
153
+ image * types .Image
154
+ cfg aws. Config
161
155
}
162
156
163
157
func copyWithSnapshot (input copyWithSnapshotInput ) (string , string , error ) {
164
- ec2Client := ec2 .New (input .sess )
158
+ ec2Client := ec2 .NewFromConfig (input .cfg )
165
159
imgName := * input .image .Name + util .RandomString (3 ) + strconv .Itoa (int (time .Now ().Unix ()))
166
160
log := input .log .WithValues ("imageName" , imgName )
167
161
@@ -175,52 +169,57 @@ func copyWithSnapshot(input copyWithSnapshotInput) (string, string, error) {
175
169
}
176
170
177
171
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 ,
185
178
}
186
179
187
180
// 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
+ ))
190
187
if err != nil {
191
188
return imgName , "" , errors .Wrap (err , "Failed to generate presigned url" )
192
189
}
193
- copyInput .PresignedUrl = aws .String (str )
190
+ copyInput .PresignedUrl = aws .String (str . URL )
194
191
195
- out , err := ec2Client .CopySnapshot (copyInput )
192
+ out , err := ec2Client .CopySnapshot (context .TODO (), copyInput , func (o * ec2.Options ) {
193
+ o .Region = input .destinationRegion
194
+ })
196
195
if err != nil {
197
196
return imgName , "" , errors .Wrap (err , "Failed copying snapshot" )
198
197
}
199
198
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 ),
202
201
)
203
202
204
- err = ec2Client . WaitUntilSnapshotCompleted ( & ec2.DescribeSnapshotsInput {
203
+ err = ec2 . NewSnapshotCompletedWaiter ( ec2Client ). Wait ( context . TODO (), & ec2.DescribeSnapshotsInput {
205
204
DryRun : aws .Bool (input .dryRun ),
206
- SnapshotIds : []* string {out .SnapshotId },
207
- })
205
+ SnapshotIds : []string {aws . ToString ( out .SnapshotId ) },
206
+ }, time . Hour * 1 )
208
207
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 )))
210
209
}
211
210
212
- ebsMapping := & ec2 .BlockDeviceMapping {
211
+ ebsMapping := types .BlockDeviceMapping {
213
212
DeviceName : input .image .BlockDeviceMappings [0 ].DeviceName ,
214
- Ebs : & ec2 .EbsBlockDevice {
213
+ Ebs : & types .EbsBlockDevice {
215
214
SnapshotId : out .SnapshotId ,
216
215
},
217
216
}
218
217
219
218
log .Info ("Registering AMI" )
220
219
221
- registerOut , err := ec2Client .RegisterImage (& ec2.RegisterImageInput {
220
+ registerOut , err := ec2Client .RegisterImage (context . TODO (), & ec2.RegisterImageInput {
222
221
Architecture : input .image .Architecture ,
223
- BlockDeviceMappings : []* ec2 .BlockDeviceMapping {ebsMapping },
222
+ BlockDeviceMappings : []types .BlockDeviceMapping {ebsMapping },
224
223
Description : input .image .Description ,
225
224
DryRun : aws .Bool (input .dryRun ),
226
225
EnaSupport : input .image .EnaSupport ,
@@ -229,12 +228,12 @@ func copyWithSnapshot(input copyWithSnapshotInput) (string, string, error) {
229
228
RamdiskId : input .image .RamdiskId ,
230
229
RootDeviceName : input .image .RootDeviceName ,
231
230
SriovNetSupport : input .image .SriovNetSupport ,
232
- VirtualizationType : input .image .VirtualizationType ,
231
+ VirtualizationType : aws . String ( string ( input .image .VirtualizationType )) ,
233
232
})
234
233
235
234
if err != nil {
236
235
return imgName , "" , err
237
236
}
238
237
239
- return imgName , aws .StringValue (registerOut .ImageId ), err
238
+ return imgName , aws .ToString (registerOut .ImageId ), err
240
239
}
0 commit comments