-
Notifications
You must be signed in to change notification settings - Fork 554
/
cloud.go
457 lines (399 loc) · 14.2 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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
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"
"github.com/aws/smithy-go"
"math/rand"
"os"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
"github.com/aws/aws-sdk-go-v2/service/efs"
"github.com/aws/aws-sdk-go-v2/service/efs/types"
"github.com/aws/aws-sdk-go-v2/service/sts"
"k8s.io/klog/v2"
)
const (
AccessDeniedException = "AccessDeniedException"
AccessPointAlreadyExists = "AccessPointAlreadyExists"
PvcNameTagKey = "pvcName"
AccessPointPerFsLimit = 1000
)
var (
ErrNotFound = errors.New("Resource was not found")
ErrAlreadyExists = errors.New("Resource already exists")
ErrAccessDenied = errors.New("Access denied")
)
type FileSystem struct {
FileSystemId string
}
type AccessPoint struct {
AccessPointId string
FileSystemId string
AccessPointRootDir string
// Capacity is used for testing purpose only
// EFS does not consider capacity while provisioning new file systems or access points
CapacityGiB int64
PosixUser *PosixUser
}
type PosixUser struct {
Gid int64
Uid int64
}
type AccessPointOptions struct {
// Capacity is used for testing purpose only.
// EFS does not consider capacity while provisioning new file systems or access points
// Capacity is used to satisfy this test: https://github.com/kubernetes-csi/csi-test/blob/v3.1.1/pkg/sanity/controller.go#L559
CapacityGiB int64
FileSystemId string
Uid int64
Gid int64
DirectoryPerms string
DirectoryPath string
Tags map[string]string
}
type MountTarget struct {
AZName string
AZId string
MountTargetId string
IPAddress string
}
// Efs abstracts efs client(https://docs.aws.amazon.com/sdk-for-go/api/service/efs/)
type Efs interface {
CreateAccessPoint(context.Context, *efs.CreateAccessPointInput, ...func(*efs.Options)) (*efs.CreateAccessPointOutput, error)
DeleteAccessPoint(context.Context, *efs.DeleteAccessPointInput, ...func(*efs.Options)) (*efs.DeleteAccessPointOutput, error)
DescribeAccessPoints(context.Context, *efs.DescribeAccessPointsInput, ...func(*efs.Options)) (*efs.DescribeAccessPointsOutput, error)
DescribeFileSystems(context.Context, *efs.DescribeFileSystemsInput, ...func(*efs.Options)) (*efs.DescribeFileSystemsOutput, error)
DescribeMountTargets(context.Context, *efs.DescribeMountTargetsInput, ...func(*efs.Options)) (*efs.DescribeMountTargetsOutput, error)
}
type Cloud interface {
GetMetadata() MetadataService
CreateAccessPoint(ctx context.Context, clientToken string, accessPointOpts *AccessPointOptions) (accessPoint *AccessPoint, err error)
DeleteAccessPoint(ctx context.Context, accessPointId string) (err error)
DescribeAccessPoint(ctx context.Context, accessPointId string) (accessPoint *AccessPoint, err error)
FindAccessPointByClientToken(ctx context.Context, clientToken, fileSystemId string) (accessPoint *AccessPoint, err error)
ListAccessPoints(ctx context.Context, fileSystemId string) (accessPoints []*AccessPoint, err error)
DescribeFileSystem(ctx context.Context, fileSystemId string) (fs *FileSystem, err error)
DescribeMountTargets(ctx context.Context, fileSystemId, az string) (fs *MountTarget, err error)
}
type cloud struct {
metadata MetadataService
efs Efs
}
// NewCloud returns a new instance of AWS cloud
// It panics if session is invalid
func NewCloud() (Cloud, error) {
return createCloud("")
}
// NewCloudWithRole returns a new instance of AWS cloud after assuming an aws role
// It panics if driver does not have permissions to assume role.
func NewCloudWithRole(awsRoleArn string) (Cloud, error) {
return createCloud(awsRoleArn)
}
func createCloud(awsRoleArn string) (Cloud, error) {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
klog.Warningf("Could not load config: %v", err)
}
svc := imds.NewFromConfig(cfg)
api, err := DefaultKubernetesAPIClient()
if err != nil && !isDriverBootedInECS() {
klog.Warningf("Could not create Kubernetes Client: %v", err)
}
metadataProvider, err := GetNewMetadataProvider(svc, api)
if err != nil {
return nil, fmt.Errorf("error creating MetadataProvider: %v", err)
}
metadata, err := metadataProvider.getMetadata()
if err != nil {
return nil, fmt.Errorf("could not get metadata: %v", err)
}
efs_client := createEfsClient(awsRoleArn, metadata)
klog.V(5).Infof("EFS Client created using the following endpoint: %+v", cfg.BaseEndpoint)
return &cloud{
metadata: metadata,
efs: efs_client,
}, nil
}
func createEfsClient(awsRoleArn string, metadata MetadataService) Efs {
cfg, _ := config.LoadDefaultConfig(context.TODO(), config.WithRegion(metadata.GetRegion()))
if awsRoleArn != "" {
stsClient := sts.NewFromConfig(cfg)
roleProvider := stscreds.NewAssumeRoleProvider(stsClient, awsRoleArn)
cfg.Credentials = aws.NewCredentialsCache(roleProvider)
}
return efs.NewFromConfig(cfg)
}
func (c *cloud) GetMetadata() MetadataService {
return c.metadata
}
func (c *cloud) CreateAccessPoint(ctx context.Context, clientToken string, accessPointOpts *AccessPointOptions) (accessPoint *AccessPoint, err error) {
efsTags := parseEfsTags(accessPointOpts.Tags)
createAPInput := &efs.CreateAccessPointInput{
ClientToken: &clientToken,
FileSystemId: &accessPointOpts.FileSystemId,
PosixUser: &types.PosixUser{
Gid: &accessPointOpts.Gid,
Uid: &accessPointOpts.Uid,
},
RootDirectory: &types.RootDirectory{
CreationInfo: &types.CreationInfo{
OwnerGid: &accessPointOpts.Gid,
OwnerUid: &accessPointOpts.Uid,
Permissions: &accessPointOpts.DirectoryPerms,
},
Path: &accessPointOpts.DirectoryPath,
},
Tags: efsTags,
}
klog.V(5).Infof("Calling Create AP with input: %+v", *createAPInput)
res, err := c.efs.CreateAccessPoint(ctx, createAPInput)
if err != nil {
if isAccessDenied(err) {
return nil, ErrAccessDenied
}
return nil, fmt.Errorf("Failed to create access point: %v", err)
}
klog.V(5).Infof("Create AP response : %+v", res)
return &AccessPoint{
AccessPointId: *res.AccessPointId,
FileSystemId: *res.FileSystemId,
CapacityGiB: accessPointOpts.CapacityGiB,
}, nil
}
func (c *cloud) DeleteAccessPoint(ctx context.Context, accessPointId string) (err error) {
deleteAccessPointInput := &efs.DeleteAccessPointInput{AccessPointId: &accessPointId}
_, err = c.efs.DeleteAccessPoint(ctx, deleteAccessPointInput)
if err != nil {
if isAccessDenied(err) {
return ErrAccessDenied
}
if isAccessPointNotFound(err) {
return ErrNotFound
}
return fmt.Errorf("Failed to delete access point: %v, error: %v", accessPointId, err)
}
return nil
}
func (c *cloud) DescribeAccessPoint(ctx context.Context, accessPointId string) (accessPoint *AccessPoint, err error) {
describeAPInput := &efs.DescribeAccessPointsInput{
AccessPointId: &accessPointId,
}
res, err := c.efs.DescribeAccessPoints(ctx, describeAPInput)
if err != nil {
if isAccessDenied(err) {
return nil, ErrAccessDenied
}
if isAccessPointNotFound(err) {
return nil, ErrNotFound
}
return nil, fmt.Errorf("Describe Access Point failed: %v", err)
}
accessPoints := res.AccessPoints
if len(accessPoints) == 0 || len(accessPoints) > 1 {
return nil, fmt.Errorf("DescribeAccessPoint failed. Expected exactly 1 access point in DescribeAccessPoint result. However, recevied %d access points", len(accessPoints))
}
return &AccessPoint{
AccessPointId: *accessPoints[0].AccessPointId,
FileSystemId: *accessPoints[0].FileSystemId,
AccessPointRootDir: *accessPoints[0].RootDirectory.Path,
}, nil
}
func (c *cloud) FindAccessPointByClientToken(ctx context.Context, clientToken, fileSystemId string) (accessPoint *AccessPoint, err error) {
klog.V(5).Infof("Filesystem ID to find AP : %+v", fileSystemId)
klog.V(2).Infof("ClientToken to find AP : %s", clientToken)
describeAPInput := &efs.DescribeAccessPointsInput{
FileSystemId: &fileSystemId,
MaxResults: aws.Int32(AccessPointPerFsLimit),
}
res, err := c.efs.DescribeAccessPoints(ctx, describeAPInput)
if err != nil {
if isAccessDenied(err) {
return nil, ErrAccessDenied
}
if isFileSystemNotFound(err) {
return nil, ErrNotFound
}
err = fmt.Errorf("failed to list Access Points of efs = %s : %v", fileSystemId, err)
return
}
for _, ap := range res.AccessPoints {
// check if AP exists with same client token
if *ap.ClientToken == clientToken {
return &AccessPoint{
AccessPointId: *ap.AccessPointId,
FileSystemId: *ap.FileSystemId,
AccessPointRootDir: *ap.RootDirectory.Path,
}, nil
}
}
klog.V(2).Infof("Access point does not exist")
return nil, nil
}
func (c *cloud) ListAccessPoints(ctx context.Context, fileSystemId string) (accessPoints []*AccessPoint, err error) {
describeAPInput := &efs.DescribeAccessPointsInput{
FileSystemId: &fileSystemId,
MaxResults: aws.Int32(AccessPointPerFsLimit),
}
res, err := c.efs.DescribeAccessPoints(ctx, describeAPInput)
if err != nil {
if isAccessDenied(err) {
return nil, ErrAccessDenied
}
if isFileSystemNotFound(err) {
return nil, ErrNotFound
}
err = fmt.Errorf("List Access Points failed: %v", err)
return
}
var posixUser *PosixUser
for _, accessPointDescription := range res.AccessPoints {
if accessPointDescription.PosixUser != nil {
posixUser = &PosixUser{
Gid: *accessPointDescription.PosixUser.Gid,
Uid: *accessPointDescription.PosixUser.Gid,
}
} else {
posixUser = nil
}
accessPoint := &AccessPoint{
AccessPointId: *accessPointDescription.AccessPointId,
FileSystemId: *accessPointDescription.FileSystemId,
PosixUser: posixUser,
}
accessPoints = append(accessPoints, accessPoint)
}
return
}
func (c *cloud) DescribeFileSystem(ctx context.Context, fileSystemId string) (fs *FileSystem, err error) {
describeFsInput := &efs.DescribeFileSystemsInput{FileSystemId: &fileSystemId}
klog.V(5).Infof("Calling DescribeFileSystems with input: %+v", *describeFsInput)
res, err := c.efs.DescribeFileSystems(ctx, describeFsInput)
if err != nil {
if isAccessDenied(err) {
return nil, ErrAccessDenied
}
if isFileSystemNotFound(err) {
return nil, ErrNotFound
}
return nil, fmt.Errorf("Describe File System failed: %v", err)
}
fileSystems := res.FileSystems
if len(fileSystems) == 0 || len(fileSystems) > 1 {
return nil, fmt.Errorf("DescribeFileSystem failed. Expected exactly 1 file system in DescribeFileSystem result. However, recevied %d file systems", len(fileSystems))
}
return &FileSystem{
FileSystemId: *res.FileSystems[0].FileSystemId,
}, nil
}
func (c *cloud) DescribeMountTargets(ctx context.Context, fileSystemId, azName string) (fs *MountTarget, err error) {
describeMtInput := &efs.DescribeMountTargetsInput{FileSystemId: &fileSystemId}
klog.V(5).Infof("Calling DescribeMountTargets with input: %+v", *describeMtInput)
res, err := c.efs.DescribeMountTargets(ctx, describeMtInput)
if err != nil {
if isAccessDenied(err) {
return nil, ErrAccessDenied
}
if isFileSystemNotFound(err) {
return nil, ErrNotFound
}
return nil, fmt.Errorf("Describe Mount Targets failed: %v", err)
}
mountTargets := res.MountTargets
if len(mountTargets) == 0 {
return nil, fmt.Errorf("Cannot find mount targets for file system %v. Please create mount targets for file system.", fileSystemId)
}
availableMountTargets := getAvailableMountTargets(mountTargets)
if len(availableMountTargets) == 0 {
return nil, fmt.Errorf("No mount target for file system %v is in available state. Please retry in 5 minutes.", fileSystemId)
}
var mountTarget *types.MountTargetDescription
if azName != "" {
mountTarget = getMountTargetForAz(availableMountTargets, azName)
}
// Pick random Mount target from available mount target if azName is not provided.
// Or if there is no mount target matching azName
if mountTarget == nil {
klog.Infof("Picking a random mount target from available mount target")
rand.Seed(time.Now().Unix())
mountTarget = &availableMountTargets[rand.Intn(len(availableMountTargets))]
}
return &MountTarget{
AZName: *mountTarget.AvailabilityZoneName,
AZId: *mountTarget.AvailabilityZoneId,
MountTargetId: *mountTarget.MountTargetId,
IPAddress: *mountTarget.IpAddress,
}, nil
}
func isFileSystemNotFound(err error) bool {
var FileSystemNotFoundErr *types.FileSystemNotFound
if errors.As(err, &FileSystemNotFoundErr) {
return true
}
return false
}
func isAccessPointNotFound(err error) bool {
var AccessPointNotFoundErr *types.AccessPointNotFound
if errors.As(err, &AccessPointNotFoundErr) {
return true
}
return false
}
func isAccessDenied(err error) bool {
var apiErr smithy.APIError
if errors.As(err, &apiErr) {
if apiErr.ErrorCode() == AccessDeniedException {
return true
}
}
return false
}
func isDriverBootedInECS() bool {
ecsContainerMetadataUri := os.Getenv(taskMetadataV4EnvName)
return ecsContainerMetadataUri != ""
}
func parseEfsTags(tagMap map[string]string) []types.Tag {
efsTags := []types.Tag{}
for k, v := range tagMap {
key := k
value := v
efsTags = append(efsTags, types.Tag{
Key: &key,
Value: &value,
})
}
return efsTags
}
func getAvailableMountTargets(mountTargets []types.MountTargetDescription) []types.MountTargetDescription {
availableMountTargets := []types.MountTargetDescription{}
for _, mt := range mountTargets {
if mt.LifeCycleState == "available" {
availableMountTargets = append(availableMountTargets, mt)
}
}
return availableMountTargets
}
func getMountTargetForAz(mountTargets []types.MountTargetDescription, azName string) *types.MountTargetDescription {
for _, mt := range mountTargets {
if *mt.AvailabilityZoneName == azName {
return &mt
}
}
klog.Infof("There is no mount target match %v", azName)
return nil
}