-
Notifications
You must be signed in to change notification settings - Fork 78
/
cloud.go
443 lines (375 loc) · 15.1 KB
/
cloud.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cloud
import (
"context"
"errors"
"fmt"
"os"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/fsx"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
)
const (
// DefaultVolumeSize represents the default size used
// this is the minimum FSx for Lustre FS size
DefaultVolumeSize = 1200
// PollCheckInterval specifies the interval to check if filesystem is ready;
// needs to be shorter than the provisioner timeout
PollCheckInterval = 30 * time.Second
// PollCheckTimeout specifies the time limit for polling DescribeFileSystems
// for a completed create/update operation. FSx for Lustre filesystem
// creation time is around 5 minutes, and update time varies depending on
// target file system values
PollCheckTimeout = 10 * time.Minute
)
// Tags
const (
// VolumeNameTagKey is the key value that refers to the volume's name.
VolumeNameTagKey = "CSIVolumeName"
)
// Set during build time via -ldflags
var driverVersion string
var (
// ErrMultiDisks is an error that is returned when multiple
// disks are found with the same volume name.
ErrMultiFileSystems = errors.New("Multiple filesystems with same ID")
// ErrFsExistsDiffSize is an error that is returned if a filesystem
// exists with a given ID, but a different capacity is requested.
ErrFsExistsDiffSize = errors.New("There is already a disk with same ID and different size")
// ErrNotFound is returned when a resource is not found.
ErrNotFound = errors.New("Resource was not found")
)
// FileSystem represents a FSx for Lustre filesystem
type FileSystem struct {
FileSystemId string
CapacityGiB int64
DnsName string
MountName string
StorageType string
DeploymentType string
PerUnitStorageThroughput int64
}
// FileSystemOptions represents the options to create FSx for Lustre filesystem
type FileSystemOptions struct {
CapacityGiB int64
SubnetId string
SecurityGroupIds []string
AutoImportPolicy string
S3ImportPath string
S3ExportPath string
DeploymentType string
KmsKeyId string
PerUnitStorageThroughput int64
StorageType string
DriveCacheType string
DailyAutomaticBackupStartTime string
AutomaticBackupRetentionDays int64
CopyTagsToBackups bool
DataCompressionType string
WeeklyMaintenanceStartTime string
FileSystemTypeVersion string
ExtraTags []string
}
// FSx abstracts FSx client to facilitate its mocking.
// See https://docs.aws.amazon.com/sdk-for-go/api/service/fsx/ for details
type FSx interface {
CreateFileSystemWithContext(aws.Context, *fsx.CreateFileSystemInput, ...request.Option) (*fsx.CreateFileSystemOutput, error)
UpdateFileSystemWithContext(aws.Context, *fsx.UpdateFileSystemInput, ...request.Option) (*fsx.UpdateFileSystemOutput, error)
DeleteFileSystemWithContext(aws.Context, *fsx.DeleteFileSystemInput, ...request.Option) (*fsx.DeleteFileSystemOutput, error)
DescribeFileSystemsWithContext(aws.Context, *fsx.DescribeFileSystemsInput, ...request.Option) (*fsx.DescribeFileSystemsOutput, error)
}
type Cloud interface {
CreateFileSystem(ctx context.Context, volumeName string, fileSystemOptions *FileSystemOptions) (fs *FileSystem, err error)
ResizeFileSystem(ctx context.Context, fileSystemId string, newSizeGiB int64) (int64, error)
DeleteFileSystem(ctx context.Context, fileSystemId string) (err error)
DescribeFileSystem(ctx context.Context, fileSystemId string) (fs *FileSystem, err error)
WaitForFileSystemAvailable(ctx context.Context, fileSystemId string) error
WaitForFileSystemResize(ctx context.Context, fileSystemId string, resizeGiB int64) error
}
type cloud struct {
region string
fsx FSx
}
// NewCloud returns a new instance of AWS cloud
// It panics if session is invalid
func NewCloud(region string) (Cloud, error) {
awsConfig := &aws.Config{
Region: aws.String(region),
CredentialsChainVerboseErrors: aws.Bool(true),
// Set MaxRetries to a high value. It will be "overwritten" if context deadline comes sooner.
MaxRetries: aws.Int(8),
}
os.Setenv("AWS_EXECUTION_ENV", "aws-fsx-csi-driver-"+driverVersion)
svc := fsx.New(session.Must(session.NewSession(awsConfig)))
return &cloud{
region: region,
fsx: svc,
}, nil
}
func (c *cloud) CreateFileSystem(ctx context.Context, volumeName string, fileSystemOptions *FileSystemOptions) (fs *FileSystem, err error) {
if len(fileSystemOptions.SubnetId) == 0 {
return nil, fmt.Errorf("SubnetId is required")
}
lustreConfiguration := &fsx.CreateFileSystemLustreConfiguration{}
if fileSystemOptions.AutoImportPolicy != "" {
lustreConfiguration.SetAutoImportPolicy(fileSystemOptions.AutoImportPolicy)
}
if fileSystemOptions.S3ImportPath != "" {
lustreConfiguration.SetImportPath(fileSystemOptions.S3ImportPath)
}
if fileSystemOptions.S3ExportPath != "" {
lustreConfiguration.SetExportPath(fileSystemOptions.S3ExportPath)
}
if fileSystemOptions.DeploymentType != "" {
lustreConfiguration.SetDeploymentType(fileSystemOptions.DeploymentType)
}
if fileSystemOptions.DriveCacheType != "" {
lustreConfiguration.SetDriveCacheType(fileSystemOptions.DriveCacheType)
}
if fileSystemOptions.PerUnitStorageThroughput != 0 {
lustreConfiguration.SetPerUnitStorageThroughput(fileSystemOptions.PerUnitStorageThroughput)
}
if fileSystemOptions.AutomaticBackupRetentionDays != 0 {
lustreConfiguration.SetAutomaticBackupRetentionDays(fileSystemOptions.AutomaticBackupRetentionDays)
if fileSystemOptions.DailyAutomaticBackupStartTime != "" {
lustreConfiguration.SetDailyAutomaticBackupStartTime(fileSystemOptions.DailyAutomaticBackupStartTime)
}
}
if fileSystemOptions.CopyTagsToBackups {
lustreConfiguration.SetCopyTagsToBackups(true)
}
if fileSystemOptions.DataCompressionType != "" {
lustreConfiguration.SetDataCompressionType(fileSystemOptions.DataCompressionType)
}
if fileSystemOptions.WeeklyMaintenanceStartTime != "" {
lustreConfiguration.SetWeeklyMaintenanceStartTime(fileSystemOptions.WeeklyMaintenanceStartTime)
}
var tags = []*fsx.Tag{
{
Key: aws.String(VolumeNameTagKey),
Value: aws.String(volumeName),
},
}
for _, extraTag := range fileSystemOptions.ExtraTags {
extraTagSplit := strings.Split(extraTag, "=")
tagKey := extraTagSplit[0]
tagValue := extraTagSplit[1]
tags = append(tags, &fsx.Tag{
Key: aws.String(tagKey),
Value: aws.String(tagValue),
})
}
input := &fsx.CreateFileSystemInput{
ClientRequestToken: aws.String(volumeName),
FileSystemType: aws.String("LUSTRE"),
LustreConfiguration: lustreConfiguration,
StorageCapacity: aws.Int64(fileSystemOptions.CapacityGiB),
SubnetIds: []*string{aws.String(fileSystemOptions.SubnetId)},
SecurityGroupIds: aws.StringSlice(fileSystemOptions.SecurityGroupIds),
Tags: tags,
}
if fileSystemOptions.FileSystemTypeVersion != "" {
input.FileSystemTypeVersion = aws.String(fileSystemOptions.FileSystemTypeVersion)
}
if fileSystemOptions.StorageType != "" {
input.StorageType = aws.String(fileSystemOptions.StorageType)
}
if fileSystemOptions.KmsKeyId != "" {
input.KmsKeyId = aws.String(fileSystemOptions.KmsKeyId)
}
output, err := c.fsx.CreateFileSystemWithContext(ctx, input)
if err != nil {
return nil, fmt.Errorf("CreateFileSystem failed: %v", err)
}
mountName := "fsx"
if output.FileSystem.LustreConfiguration.MountName != nil {
mountName = *output.FileSystem.LustreConfiguration.MountName
}
perUnitStorageThroughput := int64(0)
if output.FileSystem.LustreConfiguration.PerUnitStorageThroughput != nil {
perUnitStorageThroughput = *output.FileSystem.LustreConfiguration.PerUnitStorageThroughput
}
return &FileSystem{
FileSystemId: *output.FileSystem.FileSystemId,
CapacityGiB: *output.FileSystem.StorageCapacity,
DnsName: *output.FileSystem.DNSName,
MountName: mountName,
StorageType: *output.FileSystem.StorageType,
DeploymentType: *output.FileSystem.LustreConfiguration.DeploymentType,
PerUnitStorageThroughput: perUnitStorageThroughput,
}, nil
}
// ResizeFileSystem makes a request to the FSx API to update the storage capacity of the filesystem.
func (c *cloud) ResizeFileSystem(ctx context.Context, fileSystemId string, newSizeGiB int64) (int64, error) {
originalFs, err := c.getFileSystem(ctx, fileSystemId)
if err != nil {
return 0, fmt.Errorf("DescribeFileSystems failed: %v", err)
}
input := &fsx.UpdateFileSystemInput{
FileSystemId: aws.String(fileSystemId),
StorageCapacity: aws.Int64(newSizeGiB),
}
_, err = c.fsx.UpdateFileSystemWithContext(ctx, input)
if err != nil {
if !isBadRequestUpdateInProgress(err) {
return *originalFs.StorageCapacity, fmt.Errorf("UpdateFileSystem failed: %v", err)
}
// If the error is because of an update in progress, check for an existing update with the same target storage
// capacity as the current request. A previous volume expansion request that experienced a timeout could
// have already made an equivalent update request to the FSx API.
_, err = c.getUpdateResizeAdministrativeAction(ctx, fileSystemId, newSizeGiB)
if err != nil {
return *originalFs.StorageCapacity, err
}
}
return newSizeGiB, nil
}
func (c *cloud) DeleteFileSystem(ctx context.Context, fileSystemId string) (err error) {
input := &fsx.DeleteFileSystemInput{
FileSystemId: aws.String(fileSystemId),
}
if _, err = c.fsx.DeleteFileSystemWithContext(ctx, input); err != nil {
if isFileSystemNotFound(err) {
return ErrNotFound
}
return fmt.Errorf("DeleteFileSystem failed: %v", err)
}
return nil
}
func (c *cloud) DescribeFileSystem(ctx context.Context, fileSystemId string) (*FileSystem, error) {
fs, err := c.getFileSystem(ctx, fileSystemId)
if err != nil {
return nil, err
}
mountName := "fsx"
if fs.LustreConfiguration.MountName != nil {
mountName = *fs.LustreConfiguration.MountName
}
perUnitStorageThroughput := int64(0)
if fs.LustreConfiguration.PerUnitStorageThroughput != nil {
perUnitStorageThroughput = *fs.LustreConfiguration.PerUnitStorageThroughput
}
return &FileSystem{
FileSystemId: *fs.FileSystemId,
CapacityGiB: *fs.StorageCapacity,
DnsName: *fs.DNSName,
MountName: mountName,
StorageType: *fs.StorageType,
DeploymentType: *fs.LustreConfiguration.DeploymentType,
PerUnitStorageThroughput: perUnitStorageThroughput,
}, nil
}
func (c *cloud) WaitForFileSystemAvailable(ctx context.Context, fileSystemId string) error {
err := wait.Poll(PollCheckInterval, PollCheckTimeout, func() (done bool, err error) {
fs, err := c.getFileSystem(ctx, fileSystemId)
if err != nil {
return true, err
}
klog.V(2).InfoS("WaitForFileSystemAvailable", "filesystem", fileSystemId, "status", *fs.Lifecycle)
switch *fs.Lifecycle {
case "AVAILABLE":
return true, nil
case "CREATING":
return false, nil
default:
return true, fmt.Errorf("unexpected state for filesystem %s: %q", fileSystemId, *fs.Lifecycle)
}
})
return err
}
// WaitForFileSystemResize polls the FSx API for status of the update operation with the given target storage
// capacity. The polling terminates when the update operation reaches a completed, failed, or unknown state.
func (c *cloud) WaitForFileSystemResize(ctx context.Context, fileSystemId string, resizeGiB int64) error {
err := wait.PollImmediate(PollCheckInterval, PollCheckTimeout, func() (done bool, err error) {
updateAction, err := c.getUpdateResizeAdministrativeAction(ctx, fileSystemId, resizeGiB)
if err != nil {
return true, err
}
klog.V(2).InfoS("WaitForFileSystemResize", "filesystem", fileSystemId, "update status", *updateAction.Status)
switch *updateAction.Status {
case "PENDING", "IN_PROGRESS":
// The resizing workflow has not completed
return false, nil
case "UPDATED_OPTIMIZING", "COMPLETED":
// The resizing workflow has completed and the filesystem is in a usable state
return true, nil
default:
// "FAILURE" is the only remaining AdministrativeAction status
return true, fmt.Errorf("update failed for filesystem %s: %q", fileSystemId, *updateAction.FailureDetails.Message)
}
})
return err
}
func (c *cloud) getFileSystem(ctx context.Context, fileSystemId string) (*fsx.FileSystem, error) {
input := &fsx.DescribeFileSystemsInput{
FileSystemIds: []*string{aws.String(fileSystemId)},
}
output, err := c.fsx.DescribeFileSystemsWithContext(ctx, input)
if err != nil {
return nil, err
}
if len(output.FileSystems) > 1 {
return nil, ErrMultiFileSystems
}
if len(output.FileSystems) == 0 {
return nil, ErrNotFound
}
return output.FileSystems[0], nil
}
// getUpdateResizeAdministrativeAction retrieves the AdministrativeAction associated with a file system update with the
// given target storage capacity, if one exists.
func (c *cloud) getUpdateResizeAdministrativeAction(ctx context.Context, fileSystemId string, resizeGiB int64) (*fsx.AdministrativeAction, error) {
fs, err := c.getFileSystem(ctx, fileSystemId)
if err != nil {
return nil, fmt.Errorf("DescribeFileSystems failed: %v", err)
}
if len(fs.AdministrativeActions) == 0 {
return nil, fmt.Errorf("there is no update on filesystem %s", fileSystemId)
}
// AdministrativeAction items are ordered by newest to oldest start time, so use the first satisfactory target
// storage capacity match
for _, action := range fs.AdministrativeActions {
if *action.AdministrativeActionType == "FILE_SYSTEM_UPDATE" &&
action.TargetFileSystemValues.StorageCapacity != nil &&
*action.TargetFileSystemValues.StorageCapacity == resizeGiB {
return action, nil
}
}
return nil, fmt.Errorf("there is no update with storage capacity of %d GiB on filesystem %s", resizeGiB, fileSystemId)
}
func isFileSystemNotFound(err error) bool {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == fsx.ErrCodeFileSystemNotFound {
return true
}
}
return false
}
// isBadRequestUpdateInProgress identifies an error returned from the FSx API as a BadRequest with an "update already
// in progress" message.
func isBadRequestUpdateInProgress(err error) bool {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == fsx.ErrCodeBadRequest &&
awsErr.Message() == "Unable to perform the storage capacity update. There is an update already in progress." {
return true
}
}
return false
}