forked from kubernetes-sigs/aws-efs-csi-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.go
More file actions
758 lines (663 loc) · 28 KB
/
controller.go
File metadata and controls
758 lines (663 loc) · 28 KB
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
/*
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 driver
import (
"context"
"crypto/sha256"
"fmt"
"os"
"path"
"sort"
"strconv"
"strings"
"time"
"github.com/google/uuid"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/kubernetes-sigs/aws-efs-csi-driver/pkg/cloud"
"github.com/kubernetes-sigs/aws-efs-csi-driver/pkg/util"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/klog/v2"
)
const (
AccessPointMode = "efs-ap"
AzName = "az"
BasePath = "basePath"
DefaultGidMin = int64(50000)
DefaultGidMax = DefaultGidMin + cloud.AccessPointPerFsLimit
DefaultTagKey = "efs.csi.aws.com/cluster"
DefaultTagValue = "true"
DirectoryPerms = "directoryPerms"
EnsureUniqueDirectory = "ensureUniqueDirectory"
FsId = "fileSystemId"
Gid = "gid"
GidMin = "gidRangeStart"
GidMax = "gidRangeEnd"
MountTargetIp = "mounttargetip"
ProvisioningMode = "provisioningMode"
PvName = "csi.storage.k8s.io/pv/name"
PvcName = "csi.storage.k8s.io/pvc/name"
PvcNamespace = "csi.storage.k8s.io/pvc/namespace"
RoleArn = "awsRoleArn"
SubPathPattern = "subPathPattern"
TempMountPathPrefix = "/var/lib/csi/pv"
Uid = "uid"
ReuseAccessPointKey = "reuseAccessPoint"
PvcNameKey = "csi.storage.k8s.io/pvc/name"
CrossAccount = "crossaccount"
ApLockWaitTimeSec = 3
)
var (
// controllerCaps represents the capability of controller service
controllerCaps = []csi.ControllerServiceCapability_RPC_Type{
csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME,
}
// subPathPatternComponents shows the elements that we allow to be in the construction of the root directory
// of the access point, as well as the values we need to extract them from the Volume Parameters.
subPathPatternComponents = map[string]string{
".PVC.name": PvcName,
".PVC.namespace": PvcNamespace,
".PV.name": PvName,
}
)
func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
klog.V(4).Infof("CreateVolume: called with args %+v", util.SanitizeRequest(*req))
var reuseAccessPoint bool
var err error
volumeParams := req.GetParameters()
volName := req.GetName()
clientToken := volName
// if true, then use sha256 hash of pvcName as clientToken instead of PVC Id
// This allows users to reconnect to the same AP from different k8s cluster
if reuseAccessPointStr, ok := volumeParams[ReuseAccessPointKey]; ok {
reuseAccessPoint, err = strconv.ParseBool(reuseAccessPointStr)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "Invalid value for reuseAccessPoint parameter")
}
if reuseAccessPoint {
clientToken = get64LenHash(volumeParams[PvcNameKey])
klog.V(5).Infof("Client token : %s", clientToken)
}
}
if volName == "" {
return nil, status.Error(codes.InvalidArgument, "Volume name not provided")
}
// Volume size is required to match PV to PVC by k8s.
// Volume size is not consumed by EFS for any purposes.
volSize := req.GetCapacityRange().GetRequiredBytes()
volCaps := req.GetVolumeCapabilities()
if len(volCaps) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume capabilities not provided")
}
if err := d.isValidVolumeCapabilities(volCaps); err != nil {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("Volume capabilities not supported: %s", err))
}
if err := d.validateFStype(volCaps); err != nil {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("Volume fstype not supported: %s", err))
}
var (
azName string
basePath string
gid int64
gidMin int64
gidMax int64
localCloud cloud.Cloud
provisioningMode string
roleArn string
uid int64
crossAccountDNSEnabled bool
)
//Parse parameters
if value, ok := volumeParams[ProvisioningMode]; ok {
provisioningMode = value
//TODO: Add FS provisioning mode check when implemented
if provisioningMode != AccessPointMode {
errStr := "Provisioning mode " + provisioningMode + " is not supported. Only Access point provisioning: 'efs-ap' is supported"
return nil, status.Error(codes.InvalidArgument, errStr)
}
} else {
return nil, status.Errorf(codes.InvalidArgument, "Missing %v parameter", ProvisioningMode)
}
accessPointsOptions := &cloud.AccessPointOptions{
CapacityGiB: volSize,
}
if value, ok := volumeParams[FsId]; ok {
if strings.TrimSpace(value) == "" {
return nil, status.Errorf(codes.InvalidArgument, "Parameter %v cannot be empty", FsId)
}
accessPointsOptions.FileSystemId = value
} else {
return nil, status.Errorf(codes.InvalidArgument, "Missing %v parameter", FsId)
}
localCloud, roleArn, crossAccountDNSEnabled, err = getCloud(req.GetSecrets(), d)
if err != nil {
return nil, err
}
var accessPoint *cloud.AccessPoint
//if reuseAccessPoint is true, check for AP with same Root Directory exists in efs
// if found reuse that AP
if reuseAccessPoint {
existingAP, err := localCloud.FindAccessPointByClientToken(ctx, clientToken, accessPointsOptions.FileSystemId)
if err != nil {
return nil, fmt.Errorf("failed to find access point: %v", err)
}
if existingAP != nil {
//AP path already exists
klog.V(2).Infof("Existing AccessPoint found : %+v", existingAP)
accessPoint = &cloud.AccessPoint{
AccessPointId: existingAP.AccessPointId,
FileSystemId: existingAP.FileSystemId,
CapacityGiB: accessPointsOptions.CapacityGiB,
}
// Take the lock to prevent this access point from being deleted while creating volume
if d.lockManager.lockMutex(accessPoint.AccessPointId, ApLockWaitTimeSec*time.Second) {
defer d.lockManager.unlockMutex(accessPoint.AccessPointId)
} else {
return nil, status.Errorf(codes.Internal, "Could not take the lock for existing access point: %v", accessPoint.AccessPointId)
}
}
}
if accessPoint == nil {
// Create tags
tags := map[string]string{
DefaultTagKey: DefaultTagValue,
}
// Append input tags to default tag
if len(d.tags) != 0 {
for k, v := range d.tags {
tags[k] = v
}
}
accessPointsOptions.Tags = tags
uid = -1
if value, ok := volumeParams[Uid]; ok {
uid, err = strconv.ParseInt(value, 10, 64)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "Failed to parse invalid %v: %v", Uid, err)
}
if uid < 0 {
return nil, status.Errorf(codes.InvalidArgument, "%v must be greater or equal than 0", Uid)
}
}
gid = -1
if value, ok := volumeParams[Gid]; ok {
gid, err = strconv.ParseInt(value, 10, 64)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "Failed to parse invalid %v: %v", Gid, err)
}
if gid < 0 {
return nil, status.Errorf(codes.InvalidArgument, "%v must be greater or equal than 0", Gid)
}
}
if value, ok := volumeParams[GidMin]; ok {
gidMin, err = strconv.ParseInt(value, 10, 64)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "Failed to parse invalid %v: %v", GidMin, err)
}
if gidMin <= 0 {
return nil, status.Errorf(codes.InvalidArgument, "%v must be greater than 0", GidMin)
}
}
if value, ok := volumeParams[GidMax]; ok {
// Ensure GID min is provided with GID max
if gidMin == 0 {
return nil, status.Errorf(codes.InvalidArgument, "Missing %v parameter", GidMin)
}
gidMax, err = strconv.ParseInt(value, 10, 64)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "Failed to parse invalid %v: %v", GidMax, err)
}
if gidMax <= gidMin {
return nil, status.Errorf(codes.InvalidArgument, "%v must be greater than %v", GidMax, GidMin)
}
} else {
// Ensure GID max is provided with GID min
if gidMin != 0 {
return nil, status.Errorf(codes.InvalidArgument, "Missing %v parameter", GidMax)
}
}
// Assign default GID ranges if not provided
if gidMin == 0 && gidMax == 0 {
gidMin = DefaultGidMin
gidMax = DefaultGidMax
}
if value, ok := volumeParams[DirectoryPerms]; ok {
accessPointsOptions.DirectoryPerms = value
}
// Storage class parameter `az` will be used to fetch preferred mount target for cross account mount.
// If the `az` storage class parameter is not provided, a random mount target will be picked for mounting.
// This storage class parameter different from `az` mount option provided by efs-utils https://github.com/aws/efs-utils/blob/v1.31.1/src/mount_efs/__init__.py#L195
// The `az` mount option provided by efs-utils is used for cross az mount or to provide az of efs one zone file system mount within the same aws-account.
// To make use of the `az` mount option, add it under storage class's `mountOptions` section. https://kubernetes.io/docs/concepts/storage/storage-classes/#mount-options
if value, ok := volumeParams[AzName]; ok {
azName = value
}
// Check if file system exists. Describe FS or List APs handle appropriate error codes
// With dynamic uid/gid provisioning we can save a call to describe FS, as list APs fails if FS ID does not exist
var accessPoints []*cloud.AccessPoint
if uid == -1 || gid == -1 {
accessPoints, err = localCloud.ListAccessPoints(ctx, accessPointsOptions.FileSystemId)
} else {
_, err = localCloud.DescribeFileSystem(ctx, accessPointsOptions.FileSystemId)
}
if err != nil {
if err == cloud.ErrAccessDenied {
return nil, status.Errorf(codes.Unauthenticated, "Access Denied. Please ensure you have the right AWS permissions: %v", err)
}
if err == cloud.ErrNotFound {
return nil, status.Errorf(codes.InvalidArgument, "File System does not exist: %v", err)
}
return nil, status.Errorf(codes.Internal, "Failed to fetch Access Points or Describe File System: %v", err)
}
var allocatedGid int64
if uid == -1 || gid == -1 {
allocatedGid, err = d.gidAllocator.getNextGid(accessPointsOptions.FileSystemId, accessPoints, gidMin, gidMax)
if err != nil {
return nil, err
}
}
if uid == -1 {
uid = allocatedGid
}
if gid == -1 {
gid = allocatedGid
}
if value, ok := volumeParams[BasePath]; ok {
basePath = value
}
rootDirName := volName
// Check if a custom structure should be imposed on the access point directory
if value, ok := volumeParams[SubPathPattern]; ok {
// Try and construct the root directory and check it only contains supported components
val, err := interpolateRootDirectoryName(value, volumeParams)
if err == nil {
klog.Infof("Using user-specified structure for access point directory.")
rootDirName = val
if value, ok := volumeParams[EnsureUniqueDirectory]; ok {
if ensureUniqueDirectory, err := strconv.ParseBool(value); !ensureUniqueDirectory && err == nil {
klog.Infof("Not appending PVC UID to path.")
} else {
klog.Infof("Appending PVC UID to path.")
rootDirName = fmt.Sprintf("%s-%s", val, uuid.New().String())
}
} else {
klog.Infof("Appending PVC UID to path.")
rootDirName = fmt.Sprintf("%s-%s", val, uuid.New().String())
}
} else {
return nil, err
}
} else {
klog.Infof("Using PV name for access point directory.")
}
rootDir := path.Join("/", basePath, rootDirName)
if ok, err := validateEfsPathRequirements(rootDir); !ok {
return nil, err
}
klog.Infof("Using %v as the access point directory.", rootDir)
accessPointsOptions.Uid = uid
accessPointsOptions.Gid = gid
accessPointsOptions.DirectoryPath = rootDir
accessPoint, err = localCloud.CreateAccessPoint(ctx, clientToken, accessPointsOptions)
if err != nil {
if err == cloud.ErrAccessDenied {
return nil, status.Errorf(codes.Unauthenticated, "Access Denied. Please ensure you have the right AWS permissions: %v", err)
} else if err == cloud.ErrAlreadyExists {
klog.V(4).Infof("Access point already exists for client token %s. Retrieving existing access point details.", clientToken)
existingAccessPoint, err := localCloud.FindAccessPointByClientToken(ctx, clientToken, accessPointsOptions.FileSystemId)
if err != nil {
return nil, fmt.Errorf("Error attempting to retrieve existing access point for client token %s: %v", clientToken, err)
}
if existingAccessPoint == nil {
return nil, fmt.Errorf("No access point for client token %s was returned: %v", clientToken, err)
}
err = validateExistingAccessPoint(existingAccessPoint, basePath, gidMin, gidMax)
if err != nil {
return nil, status.Errorf(codes.AlreadyExists, "Invalid existing access point: %v", err)
}
accessPoint = existingAccessPoint
} else {
return nil, status.Errorf(codes.Internal, "Failed to create Access point in File System %v : %v", accessPointsOptions.FileSystemId, err)
}
}
// Lock on the new access point to prevent accidental deletion before creation is done
if d.lockManager.lockMutex(accessPoint.AccessPointId, ApLockWaitTimeSec*time.Second) {
defer d.lockManager.unlockMutex(accessPoint.AccessPointId)
} else {
return nil, status.Errorf(codes.Internal, "Could not take the lock after creating access point: %v", accessPoint.AccessPointId)
}
}
volContext := map[string]string{}
// Enable cross-account dns resolution or fetch mount target Ip for cross-account mount
if roleArn != "" {
if crossAccountDNSEnabled {
// This option indicates the customer would like to use DNS to resolve
// the cross-account mount target ip address (in order to mount to
// the same AZ-ID as the client instance); mounttargetip should
// not be used as a mount option in this case.
volContext[CrossAccount] = strconv.FormatBool(true)
} else {
mountTarget, err := localCloud.DescribeMountTargets(ctx, accessPointsOptions.FileSystemId, azName)
if err != nil {
klog.Warningf("Failed to describe mount targets for file system %v. Skip using `mounttargetip` mount option: %v", accessPointsOptions.FileSystemId, err)
} else if azName == "multi" {
var mountTargets []string
for _, mt := range mountTarget {
mountTargets = append(mountTargets, fmt.Sprintf("%s:%s", mt.AZId, mt.IPAddress))
}
volContext[MountTargetIp] = strings.Join(mountTargets, ",")
} else {
volContext[MountTargetIp] = mountTarget[0].IPAddress
}
}
}
return &csi.CreateVolumeResponse{
Volume: &csi.Volume{
CapacityBytes: volSize,
VolumeId: accessPointsOptions.FileSystemId + "::" + accessPoint.AccessPointId,
VolumeContext: volContext,
},
}, nil
}
func (d *Driver) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest) (*csi.DeleteVolumeResponse, error) {
var (
localCloud cloud.Cloud
roleArn string
crossAccountDNSEnabled bool
err error
)
localCloud, roleArn, crossAccountDNSEnabled, err = getCloud(req.GetSecrets(), d)
if err != nil {
return nil, err
}
klog.V(4).Infof("DeleteVolume: called with args %+v", util.SanitizeRequest(*req))
volId := req.GetVolumeId()
if volId == "" {
return nil, status.Error(codes.InvalidArgument, "Volume ID not provided")
}
fileSystemId, _, accessPointId, err := parseVolumeId(volId)
if err != nil {
//Returning success for an invalid volume ID. See here - https://github.com/kubernetes-csi/csi-test/blame/5deb83d58fea909b2895731d43e32400380aae3c/pkg/sanity/controller.go#L733
klog.V(5).Infof("DeleteVolume: Failed to parse volumeID: %v, err: %v, returning success", volId, err)
return &csi.DeleteVolumeResponse{}, nil
}
if accessPointId == "" {
klog.V(5).Infof("DeleteVolume: No Access Point for volume %v, returning success", volId)
return &csi.DeleteVolumeResponse{}, nil
}
// Lock on the access point ID to ensure a retry won't race with the in-progress deletion
if d.lockManager.lockMutex(accessPointId, ApLockWaitTimeSec*time.Second) {
defer d.lockManager.unlockMutex(accessPointId)
} else {
return nil, status.Errorf(codes.Internal, "Could not take the lock to delete access point: %v", accessPointId)
}
//TODO: Add Delete File System when FS provisioning is implemented
// Delete access point root directory if delete-access-point-root-dir is set.
if d.deleteAccessPointRootDir {
fsRoot := TempMountPathPrefix + "/" + accessPointId
deleteCompleted := false
// Ensure the volume is cleaned up properly in case of an incomplete deletion
defer func() {
if !deleteCompleted {
// Check if the FS is still mounted
isNotMounted, err := d.mounter.IsLikelyNotMountPoint(fsRoot)
if err != nil {
return // Skip cleanup, we can't verify mount status
}
if !isNotMounted {
if err := d.mounter.Unmount(fsRoot); err != nil {
klog.Warningf("Failed to unmount %v: %v", fsRoot, err)
return // Don't remove any data if the unmount fails
}
}
// Only try folder removal if the unmount succeeded or wasn't mounted
// If the directory already doesn't exist it will be treated as success
if err := os.Remove(fsRoot); err != nil && !os.IsNotExist(err) {
klog.Warningf("Failed to remove %v: %v", fsRoot, err)
}
}
}()
// Check if Access point exists.
// If access point exists, retrieve its root directory and delete it/
accessPoint, err := localCloud.DescribeAccessPoint(ctx, accessPointId)
if err != nil {
if err == cloud.ErrAccessDenied {
return nil, status.Errorf(codes.Unauthenticated, "Access Denied. Please ensure you have the right AWS permissions: %v", err)
}
if err == cloud.ErrNotFound {
klog.V(5).Infof("DeleteVolume: Access Point %v not found, returning success", accessPointId)
return &csi.DeleteVolumeResponse{}, nil
}
return nil, status.Errorf(codes.Internal, "Could not get describe Access Point: %v , error: %v", accessPointId, err)
}
//Mount File System at it root and delete access point root directory
mountOptions := []string{"tls", "iam"}
if roleArn != "" {
if crossAccountDNSEnabled {
// Connect via dns rather than mounttargetip
mountOptions = append(mountOptions, CrossAccount)
} else {
mountTarget, err := localCloud.DescribeMountTargets(ctx, fileSystemId, "")
if err == nil {
// TODO optimize multi zone
mountOptions = append(mountOptions, MountTargetIp+"="+mountTarget[0].IPAddress)
} else {
klog.Warningf("Failed to describe mount targets for file system %v. Skip using `mounttargetip` mount option: %v", fileSystemId, err)
}
}
}
// Create the target directory, This won't fail if it already exists
if err := d.mounter.MakeDir(fsRoot); err != nil {
return nil, status.Errorf(codes.Internal, "Could not create dir %q: %v", fsRoot, err)
}
// Only attempt to mount the target filesystem if its not already mounted
isNotMounted, err := d.mounter.IsLikelyNotMountPoint(fsRoot)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not check if %q is mounted: %v", fsRoot, err)
}
if isNotMounted {
if err := d.mounter.Mount(fileSystemId, fsRoot, "efs", mountOptions); err != nil {
return nil, status.Errorf(codes.Internal, "Could not mount %q at %q: %v", fileSystemId, fsRoot, err)
}
}
// Before removing, ensure the removal path exists and is a directory
apRootPath := fsRoot + accessPoint.AccessPointRootDir
if pathInfo, err := d.mounter.Stat(apRootPath); err == nil && !os.IsNotExist(err) && pathInfo.IsDir() {
err = os.RemoveAll(apRootPath)
}
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not delete access point root directory %q: %v", accessPoint.AccessPointRootDir, err)
}
err = d.mounter.Unmount(fsRoot)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not unmount %q: %v", fsRoot, err)
}
err = os.Remove(fsRoot)
if err != nil && !os.IsNotExist(err) {
return nil, status.Errorf(codes.Internal, "Could not delete %q: %v", fsRoot, err)
}
//Mark the delete as complete, Nothing needs cleanup in the deferred function
deleteCompleted = true
}
// Delete access point
if err = localCloud.DeleteAccessPoint(ctx, accessPointId); err != nil {
if err == cloud.ErrAccessDenied {
return nil, status.Errorf(codes.Unauthenticated, "Access Denied. Please ensure you have the right AWS permissions: %v", err)
}
if err == cloud.ErrNotFound {
klog.V(5).Infof("DeleteVolume: Access Point not found, returning success")
return &csi.DeleteVolumeResponse{}, nil
}
return nil, status.Errorf(codes.Internal, "Failed to Delete volume %v: %v", volId, err)
}
return &csi.DeleteVolumeResponse{}, nil
}
func (d *Driver) ControllerPublishVolume(ctx context.Context, req *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
func (d *Driver) ControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
func (d *Driver) ValidateVolumeCapabilities(ctx context.Context, req *csi.ValidateVolumeCapabilitiesRequest) (*csi.ValidateVolumeCapabilitiesResponse, error) {
klog.V(4).Infof("ValidateVolumeCapabilities: called with args %+v", util.SanitizeRequest(*req))
volId := req.GetVolumeId()
if volId == "" {
return nil, status.Error(codes.InvalidArgument, "Volume ID not provided")
}
volCaps := req.GetVolumeCapabilities()
if len(volCaps) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume capabilities not provided")
}
_, _, _, err := parseVolumeId(volId)
if err != nil {
return nil, status.Errorf(codes.NotFound, "Volume not found, err: %v", err)
}
var confirmed *csi.ValidateVolumeCapabilitiesResponse_Confirmed
if err := d.isValidVolumeCapabilities(volCaps); err == nil {
confirmed = &csi.ValidateVolumeCapabilitiesResponse_Confirmed{VolumeCapabilities: volCaps}
}
return &csi.ValidateVolumeCapabilitiesResponse{
Confirmed: confirmed,
}, nil
}
func (d *Driver) ListVolumes(ctx context.Context, req *csi.ListVolumesRequest) (*csi.ListVolumesResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
func (d *Driver) GetCapacity(ctx context.Context, req *csi.GetCapacityRequest) (*csi.GetCapacityResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
func (d *Driver) ControllerGetCapabilities(ctx context.Context, req *csi.ControllerGetCapabilitiesRequest) (*csi.ControllerGetCapabilitiesResponse, error) {
klog.V(4).Infof("ControllerGetCapabilities: called with args %+v", util.SanitizeRequest(*req))
var caps []*csi.ControllerServiceCapability
for _, cap := range controllerCaps {
c := &csi.ControllerServiceCapability{
Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{
Type: cap,
},
},
}
caps = append(caps, c)
}
return &csi.ControllerGetCapabilitiesResponse{Capabilities: caps}, nil
}
func (d *Driver) CreateSnapshot(ctx context.Context, req *csi.CreateSnapshotRequest) (*csi.CreateSnapshotResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
func (d *Driver) DeleteSnapshot(ctx context.Context, req *csi.DeleteSnapshotRequest) (*csi.DeleteSnapshotResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
func (d *Driver) ListSnapshots(ctx context.Context, req *csi.ListSnapshotsRequest) (*csi.ListSnapshotsResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
func (d *Driver) ControllerExpandVolume(ctx context.Context, req *csi.ControllerExpandVolumeRequest) (*csi.ControllerExpandVolumeResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
func (d *Driver) ControllerGetVolume(ctx context.Context, req *csi.ControllerGetVolumeRequest) (*csi.ControllerGetVolumeResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
func (d *Driver) ControllerModifyVolume(ctx context.Context, req *csi.ControllerModifyVolumeRequest) (*csi.ControllerModifyVolumeResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
func getCloud(secrets map[string]string, driver *Driver) (cloud.Cloud, string, bool, error) {
var localCloud cloud.Cloud
var roleArn string
var crossAccountDNSEnabled bool
var err error
// Fetch aws role ARN for cross account mount from CSI secrets. Link to CSI secrets below
// https://kubernetes-csi.github.io/docs/secrets-and-credentials.html#csi-operation-secrets
if value, ok := secrets[RoleArn]; ok {
roleArn = value
}
if value, ok := secrets[CrossAccount]; ok {
crossAccountDNSEnabled, err = strconv.ParseBool(value)
if err != nil {
return nil, "", false, status.Error(codes.InvalidArgument, "crossaccount parameter must have boolean value.")
}
} else {
crossAccountDNSEnabled = false
}
if roleArn != "" {
localCloud, err = cloud.NewCloudWithRole(roleArn, driver.adaptiveRetryMode)
if err != nil {
return nil, "", false, status.Errorf(codes.Unauthenticated, "Unable to initialize aws cloud: %v. Please verify role has the correct AWS permissions for cross account mount", err)
}
} else {
localCloud = driver.cloud
}
return localCloud, roleArn, crossAccountDNSEnabled, nil
}
func interpolateRootDirectoryName(rootDirectoryPath string, volumeParams map[string]string) (string, error) {
r := strings.NewReplacer(createListOfVariableSubstitutions(volumeParams)...)
result := r.Replace(rootDirectoryPath)
// Check if any templating characters still exist
if strings.Contains(result, "${") || strings.Contains(result, "}") {
return "", status.Errorf(codes.InvalidArgument,
"Path specified \"%v\" contains invalid elements. Can only contain %v", rootDirectoryPath,
getSupportedComponentNames())
}
return result, nil
}
func createListOfVariableSubstitutions(volumeParams map[string]string) []string {
variableSubstitutions := make([]string, 2*len(subPathPatternComponents))
i := 0
for key, volumeParamsKey := range subPathPatternComponents {
variableSubstitutions[i] = "${" + key + "}"
variableSubstitutions[i+1] = volumeParams[volumeParamsKey]
i += 2
}
return variableSubstitutions
}
func getSupportedComponentNames() []string {
keys := make([]string, len(subPathPatternComponents))
i := 0
for key := range subPathPatternComponents {
keys[i] = key
i++
}
sort.Strings(keys)
return keys
}
func validateEfsPathRequirements(proposedPath string) (bool, error) {
if len(proposedPath) > 100 {
// Check the proposed path is 100 characters or fewer
return false, status.Errorf(codes.InvalidArgument, "Proposed path '%s' exceeds EFS limit of 100 characters", proposedPath)
} else if strings.Count(proposedPath, "/") > 5 {
// Check the proposed path contains at most 4 subdirectories
return false, status.Errorf(codes.InvalidArgument, "Proposed path '%s' EFS limit of 4 subdirectories", proposedPath)
} else {
return true, nil
}
}
func get64LenHash(text string) string {
h := sha256.New()
h.Write([]byte(text))
return fmt.Sprintf("%x", h.Sum(nil))
}
func validateExistingAccessPoint(existingAccessPoint *cloud.AccessPoint, basePath string, gidMin int64, gidMax int64) error {
normalizedBasePath := strings.TrimPrefix(basePath, "/")
normalizedAccessPointPath := strings.TrimPrefix(existingAccessPoint.AccessPointRootDir, "/")
if !strings.HasPrefix(normalizedAccessPointPath, normalizedBasePath) {
return fmt.Errorf("Access point found but has different base path than what's specified in storage class")
}
if existingAccessPoint.PosixUser == nil {
return fmt.Errorf("Access point found but PosixUser is nil")
}
if existingAccessPoint.PosixUser.Gid < gidMin || existingAccessPoint.PosixUser.Gid > gidMax {
return fmt.Errorf("Access point found but its GID is outside the specified range")
}
if existingAccessPoint.PosixUser.Uid < gidMin || existingAccessPoint.PosixUser.Uid > gidMax {
return fmt.Errorf("Access point found but its UID is outside the specified range")
}
return nil
}