generated from kubernetes/kubernetes-template-project
/
cloud.go
411 lines (346 loc) · 13.6 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
/*
Copyright 2022 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"
"strconv"
"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"
"sigs.k8s.io/aws-file-cache-csi-driver/pkg/util"
)
// DRA configuration parameters
const (
DRAOptionsDataRepositoryPath = "DataRepositoryPath"
DRAOptionsDataRepositorySubdirectories = "DataRepositorySubdirectories"
DRAOptionsFileCachePath = "FileCachePath"
DRAOptionsNFSConfiguration = "NFS"
NFSConfigurationOptionsDnsIps = "DnsIps"
NFSConfigurationOptionsVersion = "Version"
)
// Lustre configuration parameters
const (
LustreConfigOptionsDeploymentType = "DeploymentType"
LustreConfigOptionsMetaDataConfiguration = "MetadataConfiguration"
LustreConfigOptionsPerUnitStorageThroughput = "PerUnitStorageThroughput"
)
// Default values
const (
// DefaultVolumeSize represents the default size used
// this is the minimum Amazon File Cache FC size
DefaultVolumeSize = 1200
DefaultFileSystemType = "LUSTRE"
DefaultFileSystemTypeVersion = "2.12"
DefaultFileCacheDeploymentType = "CACHE_1"
DefaultMetadataStorageCapacity = 2400
DefaultPerUnitStorageThroughput = 1000
// PollCheckInterval specifies the interval to check if file cache is ready;
// needs to be shorter than the provisioner timeout
PollCheckInterval = 30 * time.Second
// PollCheckTimeout specifies the time limit for polling DescribeFileCaches
// for a completed create/update operation.
PollCheckTimeout = 15 * time.Minute
)
// Tags
const (
// VolumeNameTagKey is the key value that refers to the volume's name.
VolumeNameTagKey = "CSIVolumeName"
)
var (
// ErrMultiFileCaches is an error that is returned when multiple
// file caches are found with the same volume name.
ErrMultiFileCaches = errors.New("Multiple filecaches with same ID")
// ErrFcExistsDiffSize is an error that is returned if a file cache
// exists with a given ID, but a different capacity is requested.
ErrFcExistsDiffSize = 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")
)
// FileCache this is mainly for ValidateVolumeCapabilities
type FileCache struct {
FileCacheId string
CapacityGiB int64
DnsName string
MountName string
FileCacheType string
FileCacheTypeVersion string
perUnitStorageThroughput int64
}
type FileCacheOptions struct {
CapacityGiB int64
SubnetId string
SecurityGroupIds []string
DataRepositoryAssociations string
FileCacheType string
FileCacheTypeVersion string
KmsKeyId string
CopyTagsToDataRepositoryAssociations bool
LustreConfiguration []string
WeeklyMaintenanceStartTime string
ExtraTags []string
}
// https://docs.aws.amazon.com/sdk-for-go/api/service/fsx/
type FSx interface {
CreateFileCacheWithContext(aws.Context, *fsx.CreateFileCacheInput, ...request.Option) (*fsx.CreateFileCacheOutput, error)
DeleteFileCacheWithContext(aws.Context, *fsx.DeleteFileCacheInput, ...request.Option) (*fsx.DeleteFileCacheOutput, error)
DescribeFileCachesWithContext(aws.Context, *fsx.DescribeFileCachesInput, ...request.Option) (*fsx.DescribeFileCachesOutput, error)
}
type Cloud interface {
CreateFileCache(ctx context.Context, volumeName string, FileCacheOptions *FileCacheOptions) (fs *FileCache, err error)
DeleteFileCache(ctx context.Context, FileCacheId string) (err error)
DescribeFileCache(ctx context.Context, FileCacheId string) (fs *FileCache, err error)
WaitForFileCacheAvailable(ctx context.Context, FileCacheId string) error
}
type cloud struct {
fsx FSx
}
// NewCloud returns a new instance of AWS cloud
// It panics if session is invalid
func NewCloud(region string) Cloud {
awsConfig := &aws.Config{
Region: aws.String(region),
CredentialsChainVerboseErrors: aws.Bool(true),
}
return &cloud{
fsx: fsx.New(session.Must(session.NewSession(awsConfig))),
}
}
func (c *cloud) CreateFileCache(ctx context.Context, volumeName string, fileCacheOptions *FileCacheOptions) (cache *FileCache, err error) {
if len(fileCacheOptions.SubnetId) == 0 {
return nil, fmt.Errorf("SubnetId is required")
}
if fileCacheOptions.DataRepositoryAssociations == "" {
return nil, fmt.Errorf("At least one DRA is required")
}
var dataRepositoryAssociations []*fsx.FileCacheDataRepositoryAssociation
dras := strings.Fields(fileCacheOptions.DataRepositoryAssociations)
for _, slice := range dras {
draConfiguration := &fsx.FileCacheDataRepositoryAssociation{}
configSlices := util.SplitUnnestedCommas(slice)
configMap := util.MapValues(configSlices)
if dataRepositoryPath, ok := configMap[DRAOptionsDataRepositoryPath]; ok {
draConfiguration.SetDataRepositoryPath(dataRepositoryPath)
}
if dataRepositorySubdirectories, ok := configMap[DRAOptionsDataRepositorySubdirectories]; ok {
subdirectories := strings.Split(dataRepositorySubdirectories[1:len(dataRepositorySubdirectories)-1], ",")
draConfiguration.SetDataRepositorySubdirectories(aws.StringSlice(subdirectories))
}
if fileCachePath, ok := configMap[DRAOptionsFileCachePath]; ok {
draConfiguration.SetFileCachePath(fileCachePath)
}
if nfsConfig, ok := configMap[DRAOptionsNFSConfiguration]; ok {
nfsSlices := util.SplitUnnestedCommas(nfsConfig[1 : len(nfsConfig)-1])
nfsMap := util.MapValues(nfsSlices)
nfsConfiguration := &fsx.FileCacheNFSConfiguration{}
if nfsDnsIps, ok := nfsMap[NFSConfigurationOptionsDnsIps]; ok {
DnsIps := strings.Split(nfsDnsIps[1:len(nfsDnsIps)-1], ",")
nfsConfiguration.SetDnsIps(aws.StringSlice(DnsIps))
}
if nfsVersion, ok := nfsMap[NFSConfigurationOptionsVersion]; ok {
nfsConfiguration.SetVersion(nfsVersion)
}
draConfiguration.SetNFS(nfsConfiguration)
}
dataRepositoryAssociations = append(dataRepositoryAssociations, draConfiguration)
}
var tags = []*fsx.Tag{
{
Key: aws.String(VolumeNameTagKey),
Value: aws.String(volumeName),
},
}
for _, extraTag := range fileCacheOptions.ExtraTags {
extraTagSplit := strings.Split(extraTag, "=")
tagKey := extraTagSplit[0]
tagValue := extraTagSplit[1]
tags = append(tags, &fsx.Tag{
Key: aws.String(tagKey),
Value: aws.String(tagValue),
})
}
fcType := DefaultFileSystemType
if fileCacheOptions.FileCacheType != "" {
fcType = fileCacheOptions.FileCacheType
}
fcTypeVersion := DefaultFileSystemTypeVersion
if fileCacheOptions.FileCacheType != "" {
fcTypeVersion = fileCacheOptions.FileCacheTypeVersion
}
input := &fsx.CreateFileCacheInput{
ClientRequestToken: aws.String(volumeName),
DataRepositoryAssociations: dataRepositoryAssociations,
FileCacheType: aws.String(fcType),
FileCacheTypeVersion: aws.String(fcTypeVersion),
StorageCapacity: aws.Int64(fileCacheOptions.CapacityGiB),
SubnetIds: []*string{aws.String(fileCacheOptions.SubnetId)},
SecurityGroupIds: aws.StringSlice(fileCacheOptions.SecurityGroupIds),
Tags: tags,
}
lustreConfiguration := &fsx.CreateFileCacheLustreConfiguration{}
//map for lustre configuration values
configMap := util.MapValues(fileCacheOptions.LustreConfiguration)
if deploymentType, ok := configMap[LustreConfigOptionsDeploymentType]; ok {
lustreConfiguration.SetDeploymentType(deploymentType)
} else {
lustreConfiguration.SetDeploymentType(DefaultFileCacheDeploymentType)
}
if metadataConfiguration, ok := configMap[LustreConfigOptionsMetaDataConfiguration]; ok {
storageCapacityPair := strings.Split(metadataConfiguration[1:len(metadataConfiguration)-1], "=")
metadataStorageCapacity, err := strconv.ParseInt(storageCapacityPair[1], 10, 64)
if err != nil {
return nil, fmt.Errorf("Invalid input for MetadataConfiguration StorageCapacity: %s", err)
}
metadataConfig := &fsx.FileCacheLustreMetadataConfiguration{StorageCapacity: aws.Int64(metadataStorageCapacity)}
lustreConfiguration.SetMetadataConfiguration(metadataConfig)
} else {
metadataConfig := &fsx.FileCacheLustreMetadataConfiguration{StorageCapacity: aws.Int64(DefaultMetadataStorageCapacity)}
lustreConfiguration.SetMetadataConfiguration(metadataConfig)
}
if perUnitStorageThroughput, ok := configMap[LustreConfigOptionsPerUnitStorageThroughput]; ok {
throughput, err := strconv.ParseInt(perUnitStorageThroughput, 10, 64)
if err != nil {
return nil, fmt.Errorf("Invalid input for MetadataConfiguration PerUnitStorageThroughput: %s", err)
}
lustreConfiguration.SetPerUnitStorageThroughput(throughput)
} else {
lustreConfiguration.SetPerUnitStorageThroughput(DefaultPerUnitStorageThroughput)
}
if fileCacheOptions.WeeklyMaintenanceStartTime != "" {
lustreConfiguration.SetWeeklyMaintenanceStartTime(fileCacheOptions.WeeklyMaintenanceStartTime)
}
input.SetLustreConfiguration(lustreConfiguration)
if fileCacheOptions.KmsKeyId != "" {
input.SetKmsKeyId(fileCacheOptions.KmsKeyId)
}
if fileCacheOptions.CopyTagsToDataRepositoryAssociations {
input.SetCopyTagsToDataRepositoryAssociations(true)
}
klog.V(4).Infof("CreateFileCacheInput: ", input.GoString())
output, err := c.fsx.CreateFileCacheWithContext(ctx, input)
if err != nil {
if isIncompatibleParameter(err) {
return nil, ErrFcExistsDiffSize
}
return nil, fmt.Errorf("CreateFileCache failed: %v", err)
}
if output.FileCache.LustreConfiguration.MountName == nil {
return nil, fmt.Errorf("mountName returned in CreateFileCache is nil")
}
mountName := *output.FileCache.LustreConfiguration.MountName
perUnitStorageThroughput := int64(0)
if output.FileCache.LustreConfiguration.PerUnitStorageThroughput != nil {
perUnitStorageThroughput = *output.FileCache.LustreConfiguration.PerUnitStorageThroughput
}
return &FileCache{
FileCacheId: *output.FileCache.FileCacheId,
CapacityGiB: *output.FileCache.StorageCapacity,
DnsName: *output.FileCache.DNSName,
MountName: mountName,
FileCacheType: *output.FileCache.FileCacheType,
FileCacheTypeVersion: *output.FileCache.FileCacheTypeVersion,
perUnitStorageThroughput: perUnitStorageThroughput,
}, nil
}
func (c *cloud) DeleteFileCache(ctx context.Context, fileCacheId string) (err error) {
input := &fsx.DeleteFileCacheInput{
FileCacheId: aws.String(fileCacheId),
}
if _, err = c.fsx.DeleteFileCacheWithContext(ctx, input); err != nil {
if isFileCacheNotFound(err) {
return ErrNotFound
}
return fmt.Errorf("DeleteFileCache failed: %v", err)
}
return nil
}
func (c *cloud) DescribeFileCache(ctx context.Context, fileCacheId string) (*FileCache, error) {
fc, err := c.getFileCache(ctx, fileCacheId)
if err != nil {
return nil, err
}
if fc.LustreConfiguration.MountName == nil {
return nil, fmt.Errorf("mountName returned in DescribeFileCache is nil")
}
mountName := *fc.LustreConfiguration.MountName
perUnitStorageThroughput := int64(0)
if fc.LustreConfiguration.PerUnitStorageThroughput != nil {
perUnitStorageThroughput = *fc.LustreConfiguration.PerUnitStorageThroughput
}
return &FileCache{
FileCacheId: *fc.FileCacheId,
CapacityGiB: *fc.StorageCapacity,
DnsName: *fc.DNSName,
MountName: mountName,
FileCacheType: *fc.FileCacheType,
FileCacheTypeVersion: *fc.FileCacheTypeVersion,
perUnitStorageThroughput: perUnitStorageThroughput,
}, nil
}
func (c *cloud) WaitForFileCacheAvailable(ctx context.Context, fileCacheId string) error {
err := wait.Poll(PollCheckInterval, PollCheckTimeout, func() (done bool, err error) {
fc, err := c.getFileCache(ctx, fileCacheId)
if err != nil {
return true, err
}
klog.V(2).Infof("WaitForFileCacheAvailable file cache %q status is: %q", fileCacheId, *fc.Lifecycle)
switch *fc.Lifecycle {
case "AVAILABLE":
return true, nil
case "CREATING":
return false, nil
default:
return true, fmt.Errorf("Unexpected state for file cache %s: %q", fileCacheId, *fc.Lifecycle)
}
})
return err
}
func (c *cloud) getFileCache(ctx context.Context, fileCacheId string) (*fsx.FileCache, error) {
input := &fsx.DescribeFileCachesInput{
FileCacheIds: []*string{aws.String(fileCacheId)},
}
output, err := c.fsx.DescribeFileCachesWithContext(ctx, input)
if err != nil {
return nil, err
}
if len(output.FileCaches) == 0 {
return nil, ErrNotFound
}
if len(output.FileCaches) > 1 {
return nil, ErrMultiFileCaches
}
return output.FileCaches[0], nil
}
func isFileCacheNotFound(err error) bool {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == fsx.ErrCodeFileCacheNotFound {
return true
}
}
return false
}
func isIncompatibleParameter(err error) bool {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == fsx.ErrCodeIncompatibleParameterError {
return true
}
}
return false
}