-
Notifications
You must be signed in to change notification settings - Fork 807
/
node.go
962 lines (833 loc) · 34.1 KB
/
node.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
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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
/*
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"
"encoding/json"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"time"
csi "github.com/container-storage-interface/spec/lib/go/csi"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud/metadata"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/driver/internal"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/mounter"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/util"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8stypes "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
"k8s.io/kubernetes/pkg/volume"
)
const (
// default file system type to be used when it is not provided.
defaultFsType = FSTypeExt4
// VolumeOperationAlreadyExists is message fmt returned to CO when there is another in-flight call on the given volumeID.
VolumeOperationAlreadyExists = "An operation with the given volume=%q is already in progress"
// sbeDeviceVolumeAttachmentLimit refers to the maximum number of volumes that can be attached to an instance on snow.
sbeDeviceVolumeAttachmentLimit = 10
)
var (
ValidFSTypes = map[string]struct{}{
FSTypeExt3: {},
FSTypeExt4: {},
FSTypeXfs: {},
FSTypeNtfs: {},
}
)
var (
// nodeCaps represents the capability of node service.
nodeCaps = []csi.NodeServiceCapability_RPC_Type{
csi.NodeServiceCapability_RPC_STAGE_UNSTAGE_VOLUME,
csi.NodeServiceCapability_RPC_EXPAND_VOLUME,
csi.NodeServiceCapability_RPC_GET_VOLUME_STATS,
}
// taintRemovalInitialDelay is the initial delay for node taint removal.
taintRemovalInitialDelay = 1 * time.Second
// taintRemovalBackoff is the exponential backoff configuration for node taint removal.
taintRemovalBackoff = wait.Backoff{
Duration: 500 * time.Millisecond,
Factor: 2,
Steps: 10, // Max delay = 0.5 * 2^9 = ~4 minutes
}
)
// NodeService represents the node service of CSI driver.
type NodeService struct {
metadata metadata.MetadataService
mounter mounter.Mounter
inFlight *internal.InFlight
options *Options
csi.UnimplementedNodeServer
}
// NewNodeService creates a new node service.
func NewNodeService(o *Options, md metadata.MetadataService, m mounter.Mounter, k kubernetes.Interface) *NodeService {
if k != nil {
// Remove taint from node to indicate driver startup success
// This is done at the last possible moment to prevent race conditions or false positive removals
time.AfterFunc(taintRemovalInitialDelay, func() {
removeTaintInBackground(k, taintRemovalBackoff, removeNotReadyTaint)
})
}
return &NodeService{
metadata: md,
mounter: m,
inFlight: internal.NewInFlight(),
options: o,
}
}
func (d *NodeService) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) {
klog.V(4).InfoS("NodeStageVolume: called", "args", util.SanitizeRequest(req))
volumeID := req.GetVolumeId()
if len(volumeID) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID not provided")
}
target := req.GetStagingTargetPath()
if len(target) == 0 {
return nil, status.Error(codes.InvalidArgument, "Staging target not provided")
}
volCap := req.GetVolumeCapability()
if volCap == nil {
return nil, status.Error(codes.InvalidArgument, "Volume capability not provided")
}
if !isValidVolumeCapabilities([]*csi.VolumeCapability{volCap}) {
return nil, status.Error(codes.InvalidArgument, "Volume capability not supported")
}
volumeContext := req.GetVolumeContext()
if isValidVolumeContext := isValidVolumeContext(volumeContext); !isValidVolumeContext {
return nil, status.Error(codes.InvalidArgument, "Volume Attribute is not valid")
}
// If the access type is block, do nothing for stage
if _, isAccessTypeBlock := volCap.GetAccessType().(*csi.VolumeCapability_Block); isAccessTypeBlock {
return &csi.NodeStageVolumeResponse{}, nil
}
mountVolume := volCap.GetMount()
if mountVolume == nil {
return nil, status.Error(codes.InvalidArgument, "NodeStageVolume: mount is nil within volume capability")
}
fsType := mountVolume.GetFsType()
if len(fsType) == 0 {
fsType = defaultFsType
}
_, ok := ValidFSTypes[strings.ToLower(fsType)]
if !ok {
return nil, status.Errorf(codes.InvalidArgument, "NodeStageVolume: invalid fstype %s", fsType)
}
context := req.GetVolumeContext()
blockSize, err := recheckFormattingOptionParameter(context, BlockSizeKey, FileSystemConfigs, fsType)
if err != nil {
return nil, err
}
inodeSize, err := recheckFormattingOptionParameter(context, InodeSizeKey, FileSystemConfigs, fsType)
if err != nil {
return nil, err
}
bytesPerInode, err := recheckFormattingOptionParameter(context, BytesPerInodeKey, FileSystemConfigs, fsType)
if err != nil {
return nil, err
}
numInodes, err := recheckFormattingOptionParameter(context, NumberOfInodesKey, FileSystemConfigs, fsType)
if err != nil {
return nil, err
}
ext4BigAlloc, err := recheckFormattingOptionParameter(context, Ext4BigAllocKey, FileSystemConfigs, fsType)
if err != nil {
return nil, err
}
ext4ClusterSize, err := recheckFormattingOptionParameter(context, Ext4ClusterSizeKey, FileSystemConfigs, fsType)
if err != nil {
return nil, err
}
mountOptions := collectMountOptions(fsType, mountVolume.GetMountFlags())
if ok = d.inFlight.Insert(volumeID); !ok {
return nil, status.Errorf(codes.Aborted, VolumeOperationAlreadyExists, volumeID)
}
defer func() {
klog.V(4).InfoS("NodeStageVolume: volume operation finished", "volumeID", volumeID)
d.inFlight.Delete(volumeID)
}()
devicePath, ok := req.GetPublishContext()[DevicePathKey]
if !ok {
return nil, status.Error(codes.InvalidArgument, "Device path not provided")
}
partition := ""
if part, ok := volumeContext[VolumeAttributePartition]; ok {
if part != "0" {
partition = part
} else {
klog.InfoS("NodeStageVolume: invalid partition config, will ignore.", "partition", part)
}
}
source, err := d.mounter.FindDevicePath(devicePath, volumeID, partition, d.metadata.GetRegion())
if err != nil {
return nil, status.Errorf(codes.Internal, "Failed to find device path %s. %v", devicePath, err)
}
klog.V(4).InfoS("NodeStageVolume: find device path", "devicePath", devicePath, "source", source)
exists, err := d.mounter.PathExists(target)
if err != nil {
msg := fmt.Sprintf("failed to check if target %q exists: %v", target, err)
return nil, status.Error(codes.Internal, msg)
}
// When exists is true it means target path was created but device isn't mounted.
// We don't want to do anything in that case and let the operation proceed.
// Otherwise we need to create the target directory.
if !exists {
// If target path does not exist we need to create the directory where volume will be staged
klog.V(4).InfoS("NodeStageVolume: creating target dir", "target", target)
if err = d.mounter.MakeDir(target); err != nil {
msg := fmt.Sprintf("could not create target dir %q: %v", target, err)
return nil, status.Error(codes.Internal, msg)
}
}
// Check if a device is mounted in target directory
device, _, err := d.mounter.GetDeviceNameFromMount(target)
if err != nil {
msg := fmt.Sprintf("failed to check if volume is already mounted: %v", err)
return nil, status.Error(codes.Internal, msg)
}
// This operation (NodeStageVolume) MUST be idempotent.
// If the volume corresponding to the volume_id is already staged to the staging_target_path,
// and is identical to the specified volume_capability the Plugin MUST reply 0 OK.
klog.V(4).InfoS("NodeStageVolume: checking if volume is already staged", "device", device, "source", source, "target", target)
if device == source {
klog.V(4).InfoS("NodeStageVolume: volume already staged", "volumeID", volumeID)
return &csi.NodeStageVolumeResponse{}, nil
}
// FormatAndMount will format only if needed
klog.V(4).InfoS("NodeStageVolume: staging volume", "source", source, "volumeID", volumeID, "target", target, "fstype", fsType)
formatOptions := []string{}
if len(blockSize) > 0 {
if fsType == FSTypeXfs {
blockSize = "size=" + blockSize
}
formatOptions = append(formatOptions, "-b", blockSize)
}
if len(inodeSize) > 0 {
option := "-I"
if fsType == FSTypeXfs {
option, inodeSize = "-i", "size="+inodeSize
}
formatOptions = append(formatOptions, option, inodeSize)
}
if len(bytesPerInode) > 0 {
formatOptions = append(formatOptions, "-i", bytesPerInode)
}
if len(numInodes) > 0 {
formatOptions = append(formatOptions, "-N", numInodes)
}
if ext4BigAlloc == "true" {
formatOptions = append(formatOptions, "-O", "bigalloc")
}
if len(ext4ClusterSize) > 0 {
formatOptions = append(formatOptions, "-C", ext4ClusterSize)
}
if fsType == FSTypeXfs && d.options.LegacyXFSProgs {
formatOptions = append(formatOptions, "-m", "bigtime=0,inobtcount=0,reflink=0")
}
err = d.mounter.FormatAndMountSensitiveWithFormatOptions(source, target, fsType, mountOptions, nil, formatOptions)
if err != nil {
msg := fmt.Sprintf("could not format %q and mount it at %q: %v", source, target, err)
return nil, status.Error(codes.Internal, msg)
}
needResize, err := d.mounter.NeedResize(source, target)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not determine if volume %q (%q) need to be resized: %v", req.GetVolumeId(), source, err)
}
if needResize {
klog.V(2).InfoS("Volume needs resizing", "source", source)
if _, err := d.mounter.Resize(source, target); err != nil {
return nil, status.Errorf(codes.Internal, "Could not resize volume %q (%q): %v", volumeID, source, err)
}
}
klog.V(4).InfoS("NodeStageVolume: successfully staged volume", "source", source, "volumeID", volumeID, "target", target, "fstype", fsType)
return &csi.NodeStageVolumeResponse{}, nil
}
func (d *NodeService) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstageVolumeRequest) (*csi.NodeUnstageVolumeResponse, error) {
klog.V(4).InfoS("NodeUnstageVolume: called", "args", req)
volumeID := req.GetVolumeId()
if len(volumeID) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID not provided")
}
target := req.GetStagingTargetPath()
if len(target) == 0 {
return nil, status.Error(codes.InvalidArgument, "Staging target not provided")
}
if ok := d.inFlight.Insert(volumeID); !ok {
return nil, status.Errorf(codes.Aborted, VolumeOperationAlreadyExists, volumeID)
}
defer func() {
klog.V(4).InfoS("NodeUnStageVolume: volume operation finished", "volumeID", volumeID)
d.inFlight.Delete(volumeID)
}()
// Check if target directory is a mount point. GetDeviceNameFromMount
// given a mnt point, finds the device from /proc/mounts
// returns the device name, reference count, and error code
dev, refCount, err := d.mounter.GetDeviceNameFromMount(target)
if err != nil {
msg := fmt.Sprintf("failed to check if target %q is a mount point: %v", target, err)
return nil, status.Error(codes.Internal, msg)
}
// From the spec: If the volume corresponding to the volume_id
// is not staged to the staging_target_path, the Plugin MUST
// reply 0 OK.
if refCount == 0 {
klog.V(5).InfoS("[Debug] NodeUnstageVolume: target not mounted", "target", target)
return &csi.NodeUnstageVolumeResponse{}, nil
}
if refCount > 1 {
klog.InfoS("NodeUnstageVolume: found references to device mounted at target path", "refCount", refCount, "device", dev, "target", target)
}
klog.V(4).InfoS("NodeUnstageVolume: unmounting", "target", target)
err = d.mounter.Unstage(target)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not unmount target %q: %v", target, err)
}
klog.V(4).InfoS("NodeUnStageVolume: successfully unstaged volume", "volumeID", volumeID, "target", target)
return &csi.NodeUnstageVolumeResponse{}, nil
}
func (d *NodeService) NodeExpandVolume(ctx context.Context, req *csi.NodeExpandVolumeRequest) (*csi.NodeExpandVolumeResponse, error) {
klog.V(4).InfoS("NodeExpandVolume: called", "args", util.SanitizeRequest(req))
volumeID := req.GetVolumeId()
if len(volumeID) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID not provided")
}
volumePath := req.GetVolumePath()
if len(volumePath) == 0 {
return nil, status.Error(codes.InvalidArgument, "volume path must be provided")
}
volumeCapability := req.GetVolumeCapability()
// VolumeCapability is optional, if specified, use that as source of truth
if volumeCapability != nil {
caps := []*csi.VolumeCapability{volumeCapability}
if !isValidVolumeCapabilities(caps) {
return nil, status.Error(codes.InvalidArgument, ("VolumeCapability is invalid"))
}
if blk := volumeCapability.GetBlock(); blk != nil {
// Noop for Block NodeExpandVolume
klog.V(4).InfoS("NodeExpandVolume: called. Since it is a block device, ignoring...", "volumeID", volumeID, "volumePath", volumePath)
return &csi.NodeExpandVolumeResponse{}, nil
}
} else {
// TODO use util.GenericResizeFS
// VolumeCapability is nil, check if volumePath point to a block device
isBlock, err := d.mounter.IsBlockDevice(volumePath)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to determine if volumePath [%v] is a block device: %v", volumePath, err)
}
if isBlock {
// Skip resizing for Block NodeExpandVolume
bcap, err := d.mounter.GetBlockSizeBytes(volumePath)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get block capacity on path %s: %v", req.GetVolumePath(), err)
}
klog.V(4).InfoS("NodeExpandVolume: called, since given volumePath is a block device, ignoring...", "volumeID", volumeID, "volumePath", volumePath)
return &csi.NodeExpandVolumeResponse{CapacityBytes: bcap}, nil
}
}
deviceName, _, err := d.mounter.GetDeviceNameFromMount(volumePath)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get device name from mount %s: %v", volumePath, err)
}
devicePath, err := d.mounter.FindDevicePath(deviceName, volumeID, "", d.metadata.GetRegion())
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to find device path for device name %s for mount %s: %v", deviceName, req.GetVolumePath(), err)
}
// TODO: lock per volume ID to have some idempotency
if _, err = d.mounter.Resize(devicePath, volumePath); err != nil {
return nil, status.Errorf(codes.Internal, "Could not resize volume %q (%q): %v", volumeID, devicePath, err)
}
bcap, err := d.mounter.GetBlockSizeBytes(devicePath)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get block capacity on path %s: %v", req.GetVolumePath(), err)
}
return &csi.NodeExpandVolumeResponse{CapacityBytes: bcap}, nil
}
func (d *NodeService) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) {
klog.V(4).InfoS("NodePublishVolume: called", "args", util.SanitizeRequest(req))
volumeID := req.GetVolumeId()
if len(volumeID) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID not provided")
}
source := req.GetStagingTargetPath()
if len(source) == 0 {
return nil, status.Error(codes.InvalidArgument, "Staging target not provided")
}
target := req.GetTargetPath()
if len(target) == 0 {
return nil, status.Error(codes.InvalidArgument, "Target path not provided")
}
volCap := req.GetVolumeCapability()
if volCap == nil {
return nil, status.Error(codes.InvalidArgument, "Volume capability not provided")
}
if !isValidVolumeCapabilities([]*csi.VolumeCapability{volCap}) {
return nil, status.Error(codes.InvalidArgument, "Volume capability not supported")
}
if ok := d.inFlight.Insert(volumeID); !ok {
return nil, status.Errorf(codes.Aborted, VolumeOperationAlreadyExists, volumeID)
}
defer func() {
klog.V(4).InfoS("NodePublishVolume: volume operation finished", "volumeId", volumeID)
d.inFlight.Delete(volumeID)
}()
mountOptions := []string{"bind"}
if req.GetReadonly() {
mountOptions = append(mountOptions, "ro")
}
switch mode := volCap.GetAccessType().(type) {
case *csi.VolumeCapability_Block:
if err := d.nodePublishVolumeForBlock(req, mountOptions); err != nil {
return nil, err
}
case *csi.VolumeCapability_Mount:
if err := d.nodePublishVolumeForFileSystem(req, mountOptions, mode); err != nil {
return nil, err
}
}
return &csi.NodePublishVolumeResponse{}, nil
}
func (d *NodeService) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) {
klog.V(4).InfoS("NodeUnpublishVolume: called", "args", util.SanitizeRequest(req))
volumeID := req.GetVolumeId()
if len(volumeID) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID not provided")
}
target := req.GetTargetPath()
if len(target) == 0 {
return nil, status.Error(codes.InvalidArgument, "Target path not provided")
}
if ok := d.inFlight.Insert(volumeID); !ok {
return nil, status.Errorf(codes.Aborted, VolumeOperationAlreadyExists, volumeID)
}
defer func() {
klog.V(4).InfoS("NodeUnPublishVolume: volume operation finished", "volumeId", volumeID)
d.inFlight.Delete(volumeID)
}()
klog.V(4).InfoS("NodeUnpublishVolume: unmounting", "target", target)
err := d.mounter.Unpublish(target)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not unmount %q: %v", target, err)
}
return &csi.NodeUnpublishVolumeResponse{}, nil
}
func (d *NodeService) NodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVolumeStatsRequest) (*csi.NodeGetVolumeStatsResponse, error) {
klog.V(4).InfoS("NodeGetVolumeStats: called", "args", req)
if len(req.GetVolumeId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "NodeGetVolumeStats volume ID was empty")
}
if len(req.GetVolumePath()) == 0 {
return nil, status.Error(codes.InvalidArgument, "NodeGetVolumeStats volume path was empty")
}
exists, err := d.mounter.PathExists(req.GetVolumePath())
if err != nil {
return nil, status.Errorf(codes.Internal, "unknown error when stat on %s: %v", req.GetVolumePath(), err)
}
if !exists {
return nil, status.Errorf(codes.NotFound, "path %s does not exist", req.GetVolumePath())
}
isBlock, err := d.mounter.IsBlockDevice(req.GetVolumePath())
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to determine whether %s is block device: %v", req.GetVolumePath(), err)
}
if isBlock {
bcap, blockErr := d.mounter.GetBlockSizeBytes(req.GetVolumePath())
if blockErr != nil {
return nil, status.Errorf(codes.Internal, "failed to get block capacity on path %s: %v", req.GetVolumePath(), blockErr)
}
return &csi.NodeGetVolumeStatsResponse{
Usage: []*csi.VolumeUsage{
{
Unit: csi.VolumeUsage_BYTES,
Total: bcap,
},
},
}, nil
}
metricsProvider := volume.NewMetricsStatFS(req.GetVolumePath())
metrics, err := metricsProvider.GetMetrics()
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get fs info on path %s: %v", req.GetVolumePath(), err)
}
return &csi.NodeGetVolumeStatsResponse{
Usage: []*csi.VolumeUsage{
{
Unit: csi.VolumeUsage_BYTES,
Available: metrics.Available.AsDec().UnscaledBig().Int64(),
Total: metrics.Capacity.AsDec().UnscaledBig().Int64(),
Used: metrics.Used.AsDec().UnscaledBig().Int64(),
},
{
Unit: csi.VolumeUsage_INODES,
Available: metrics.InodesFree.AsDec().UnscaledBig().Int64(),
Total: metrics.Inodes.AsDec().UnscaledBig().Int64(),
Used: metrics.InodesUsed.AsDec().UnscaledBig().Int64(),
},
},
}, nil
}
func (d *NodeService) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetCapabilitiesRequest) (*csi.NodeGetCapabilitiesResponse, error) {
klog.V(4).InfoS("NodeGetCapabilities: called", "args", req)
caps := make([]*csi.NodeServiceCapability, 0, len(nodeCaps))
for _, cap := range nodeCaps {
c := &csi.NodeServiceCapability{
Type: &csi.NodeServiceCapability_Rpc{
Rpc: &csi.NodeServiceCapability_RPC{
Type: cap,
},
},
}
caps = append(caps, c)
}
return &csi.NodeGetCapabilitiesResponse{Capabilities: caps}, nil
}
func (d *NodeService) NodeGetInfo(ctx context.Context, req *csi.NodeGetInfoRequest) (*csi.NodeGetInfoResponse, error) {
klog.V(4).InfoS("NodeGetInfo: called", "args", req)
zone := d.metadata.GetAvailabilityZone()
osType := runtime.GOOS
segments := map[string]string{
ZoneTopologyKey: zone,
WellKnownZoneTopologyKey: zone,
OSTopologyKey: osType,
}
outpostArn := d.metadata.GetOutpostArn()
// to my surprise ARN's string representation is not empty for empty ARN
if len(outpostArn.Resource) > 0 {
segments[AwsRegionKey] = outpostArn.Region
segments[AwsPartitionKey] = outpostArn.Partition
segments[AwsAccountIDKey] = outpostArn.AccountID
segments[AwsOutpostIDKey] = outpostArn.Resource
}
topology := &csi.Topology{Segments: segments}
return &csi.NodeGetInfoResponse{
NodeId: d.metadata.GetInstanceID(),
MaxVolumesPerNode: d.getVolumesLimit(),
AccessibleTopology: topology,
}, nil
}
func (d *NodeService) nodePublishVolumeForBlock(req *csi.NodePublishVolumeRequest, mountOptions []string) error {
target := req.GetTargetPath()
volumeID := req.GetVolumeId()
volumeContext := req.GetVolumeContext()
devicePath, exists := req.GetPublishContext()[DevicePathKey]
if !exists {
return status.Error(codes.InvalidArgument, "Device path not provided")
}
if isValidVolumeContext := isValidVolumeContext(volumeContext); !isValidVolumeContext {
return status.Error(codes.InvalidArgument, "Volume Attribute is invalid")
}
partition := ""
if part, ok := req.GetVolumeContext()[VolumeAttributePartition]; ok {
if part != "0" {
partition = part
} else {
klog.InfoS("NodePublishVolume: invalid partition config, will ignore.", "partition", part)
}
}
source, err := d.mounter.FindDevicePath(devicePath, volumeID, partition, d.metadata.GetRegion())
if err != nil {
return status.Errorf(codes.Internal, "Failed to find device path %s. %v", devicePath, err)
}
klog.V(4).InfoS("NodePublishVolume [block]: find device path", "devicePath", devicePath, "source", source)
globalMountPath := filepath.Dir(target)
// create the global mount path if it is missing
// Path in the form of /var/lib/kubelet/plugins/kubernetes.io/csi/volumeDevices/publish/{volumeName}
exists, err = d.mounter.PathExists(globalMountPath)
if err != nil {
return status.Errorf(codes.Internal, "Could not check if path exists %q: %v", globalMountPath, err)
}
if !exists {
if err = d.mounter.MakeDir(globalMountPath); err != nil {
return status.Errorf(codes.Internal, "Could not create dir %q: %v", globalMountPath, err)
}
}
// Create the mount point as a file since bind mount device node requires it to be a file
// This implementation detail is relied upon by the NVMECollector,
// which discovers block devices by parsing /proc/self/mountinfo. The bind mount
// created here ensures block devices appear in mountinfo even without a filesystem.
klog.V(4).InfoS("NodePublishVolume [block]: making target file", "target", target)
if err = d.mounter.MakeFile(target); err != nil {
if removeErr := os.Remove(target); removeErr != nil {
return status.Errorf(codes.Internal, "Could not remove mount target %q: %v", target, removeErr)
}
return status.Errorf(codes.Internal, "Could not create file %q: %v", target, err)
}
// Checking if the target file is already mounted with a device.
mounted, err := d.isMounted(source, target)
if err != nil {
return status.Errorf(codes.Internal, "Could not check if %q is mounted: %v", target, err)
}
if !mounted {
klog.V(4).InfoS("NodePublishVolume [block]: mounting", "source", source, "target", target)
if err := d.mounter.Mount(source, target, "", mountOptions); err != nil {
if removeErr := os.Remove(target); removeErr != nil {
return status.Errorf(codes.Internal, "Could not remove mount target %q: %v", target, removeErr)
}
return status.Errorf(codes.Internal, "Could not mount %q at %q: %v", source, target, err)
}
} else {
klog.V(4).InfoS("NodePublishVolume [block]: Target path is already mounted", "target", target)
}
return nil
}
// isMounted checks if target is mounted. It does NOT return an error if target
// doesn't exist.
func (d *NodeService) isMounted(_ string, target string) (bool, error) {
/*
Checking if it's a mount point using IsLikelyNotMountPoint. There are three different return values,
1. true, err when the directory does not exist or corrupted.
2. false, nil when the path is already mounted with a device.
3. true, nil when the path is not mounted with any device.
*/
notMnt, err := d.mounter.IsLikelyNotMountPoint(target)
if err != nil && !os.IsNotExist(err) {
// Checking if the path exists and error is related to Corrupted Mount, in that case, the system could unmount and mount.
_, pathErr := d.mounter.PathExists(target)
if pathErr != nil && d.mounter.IsCorruptedMnt(pathErr) {
klog.V(4).InfoS("NodePublishVolume: Target path is a corrupted mount. Trying to unmount.", "target", target)
if mntErr := d.mounter.Unpublish(target); mntErr != nil {
return false, status.Errorf(codes.Internal, "Unable to unmount the target %q : %v", target, mntErr)
}
// After successful unmount, the device is ready to be mounted.
return false, nil
}
return false, status.Errorf(codes.Internal, "Could not check if %q is a mount point: %v, %v", target, err, pathErr)
}
// Do not return os.IsNotExist error. Other errors were handled above. The
// Existence of the target should be checked by the caller explicitly and
// independently because sometimes prior to mount it is expected not to exist
// (in Windows, the target must NOT exist before a symlink is created at it)
// and in others it is an error (in Linux, the target mount directory must
// exist before mount is called on it)
if err != nil && os.IsNotExist(err) {
klog.V(5).InfoS("[Debug] NodePublishVolume: Target path does not exist", "target", target)
return false, nil
}
if !notMnt {
klog.V(4).InfoS("NodePublishVolume: Target path is already mounted", "target", target)
}
return !notMnt, nil
}
func (d *NodeService) nodePublishVolumeForFileSystem(req *csi.NodePublishVolumeRequest, mountOptions []string, mode *csi.VolumeCapability_Mount) error {
target := req.GetTargetPath()
source := req.GetStagingTargetPath()
if m := mode.Mount; m != nil {
for _, f := range m.GetMountFlags() {
if !hasMountOption(mountOptions, f) {
mountOptions = append(mountOptions, f)
}
}
}
if err := d.mounter.PreparePublishTarget(target); err != nil {
return status.Errorf(codes.Internal, "%s", err.Error())
}
// Checking if the target directory is already mounted with a device.
mounted, err := d.isMounted(source, target)
if err != nil {
return status.Errorf(codes.Internal, "Could not check if %q is mounted: %v", target, err)
}
if !mounted {
fsType := mode.Mount.GetFsType()
if len(fsType) == 0 {
fsType = defaultFsType
}
_, ok := ValidFSTypes[strings.ToLower(fsType)]
if !ok {
return status.Errorf(codes.InvalidArgument, "NodePublishVolume: invalid fstype %s", fsType)
}
mountOptions = collectMountOptions(fsType, mountOptions)
klog.V(4).InfoS("NodePublishVolume: mounting", "source", source, "target", target, "mountOptions", mountOptions, "fsType", fsType)
if err := d.mounter.Mount(source, target, fsType, mountOptions); err != nil {
return status.Errorf(codes.Internal, "Could not mount %q at %q: %v", source, target, err)
}
}
return nil
}
// getVolumesLimit returns the limit of volumes that the node supports.
func (d *NodeService) getVolumesLimit() int64 {
if d.options.VolumeAttachLimit >= 0 {
return d.options.VolumeAttachLimit
}
if util.IsSBE(d.metadata.GetRegion()) {
return sbeDeviceVolumeAttachmentLimit
}
instanceType := d.metadata.GetInstanceType()
isNitro := cloud.IsNitroInstanceType(instanceType)
availableAttachments := cloud.GetMaxAttachments(isNitro)
reservedVolumeAttachments := d.options.ReservedVolumeAttachments
if reservedVolumeAttachments == -1 {
reservedVolumeAttachments = d.metadata.GetNumBlockDeviceMappings() + 1 // +1 for the root device
}
dedicatedLimit := cloud.GetDedicatedLimitForInstanceType(instanceType)
maxEBSAttachments, hasMaxVolumeLimit := cloud.GetEBSLimitForInstanceType(instanceType)
if hasMaxVolumeLimit {
availableAttachments = min(maxEBSAttachments, availableAttachments)
}
// For special dedicated limit instance types, the limit is only for EBS volumes
// For (all other) Nitro instances, attachments are shared between EBS volumes, ENIs and NVMe instance stores
if dedicatedLimit != 0 {
availableAttachments = dedicatedLimit
} else if isNitro {
enis := d.metadata.GetNumAttachedENIs()
reservedSlots := cloud.GetReservedSlotsForInstanceType(instanceType)
if hasMaxVolumeLimit {
availableAttachments = availableAttachments - (enis - 1) - reservedSlots
} else {
availableAttachments = availableAttachments - enis - reservedSlots
}
}
availableAttachments -= reservedVolumeAttachments
if availableAttachments <= 0 {
availableAttachments = 1
}
return int64(availableAttachments)
}
// hasMountOption returns a boolean indicating whether the given
// slice already contains a mount option. This is used to prevent
// passing duplicate option to the mount command.
func hasMountOption(options []string, opt string) bool {
for _, o := range options {
if o == opt {
return true
}
}
return false
}
// collectMountOptions returns array of mount options from
// VolumeCapability_MountVolume and special mount options for
// given filesystem.
func collectMountOptions(fsType string, mntFlags []string) []string {
var options []string
for _, opt := range mntFlags {
if !hasMountOption(options, opt) {
options = append(options, opt)
}
}
// By default, xfs does not allow mounting of two volumes with the same filesystem uuid.
// Force ignore this uuid to be able to mount volume + its clone / restored snapshot on the same node.
if fsType == FSTypeXfs {
if !hasMountOption(options, "nouuid") {
options = append(options, "nouuid")
}
}
return options
}
// Struct for JSON patch operations.
type JSONPatch struct {
OP string `json:"op,omitempty"`
Path string `json:"path,omitempty"`
Value interface{} `json:"value"`
}
// removeTaintInBackground is a goroutine that retries removeNotReadyTaint with exponential backoff.
func removeTaintInBackground(k8sClient kubernetes.Interface, backoff wait.Backoff, removalFunc func(kubernetes.Interface) error) {
backoffErr := wait.ExponentialBackoff(backoff, func() (bool, error) {
err := removalFunc(k8sClient)
if err != nil {
klog.ErrorS(err, "Unexpected failure when attempting to remove node taint(s)")
return false, nil
}
return true, nil
})
if backoffErr != nil {
klog.ErrorS(backoffErr, "Retries exhausted, giving up attempting to remove node taint(s)")
}
}
// removeNotReadyTaint removes the taint ebs.csi.aws.com/agent-not-ready from the local node
// This taint can be optionally applied by users to prevent startup race conditions such as
// https://github.com/kubernetes/kubernetes/issues/95911
func removeNotReadyTaint(clientset kubernetes.Interface) error {
nodeName := os.Getenv("CSI_NODE_NAME")
if nodeName == "" {
klog.V(4).InfoS("CSI_NODE_NAME missing, skipping taint removal")
return nil
}
node, err := clientset.CoreV1().Nodes().Get(context.Background(), nodeName, metav1.GetOptions{})
if err != nil {
return err
}
err = checkAllocatable(clientset, nodeName)
if err != nil {
return err
}
var taintsToKeep []corev1.Taint
for _, taint := range node.Spec.Taints {
if taint.Key != AgentNotReadyNodeTaintKey {
taintsToKeep = append(taintsToKeep, taint)
} else {
klog.V(4).InfoS("Queued taint for removal", "key", taint.Key, "effect", taint.Effect)
}
}
if len(taintsToKeep) == len(node.Spec.Taints) {
klog.V(4).InfoS("No taints to remove on node, skipping taint removal")
return nil
}
patchRemoveTaints := []JSONPatch{
{
OP: "test",
Path: "/spec/taints",
Value: node.Spec.Taints,
},
{
OP: "replace",
Path: "/spec/taints",
Value: taintsToKeep,
},
}
patch, err := json.Marshal(patchRemoveTaints)
if err != nil {
return err
}
_, err = clientset.CoreV1().Nodes().Patch(context.Background(), nodeName, k8stypes.JSONPatchType, patch, metav1.PatchOptions{})
if err != nil {
return err
}
klog.InfoS("Removed taint(s) from local node", "node", nodeName)
return nil
}
func checkAllocatable(clientset kubernetes.Interface, nodeName string) error {
csiNode, err := clientset.StorageV1().CSINodes().Get(context.Background(), nodeName, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("isAllocatableSet: failed to get CSINode for %s: %w", nodeName, err)
}
for _, driver := range csiNode.Spec.Drivers {
if driver.Name == DriverName {
if driver.Allocatable != nil && driver.Allocatable.Count != nil {
klog.InfoS("CSINode Allocatable value is set", "nodeName", nodeName, "count", *driver.Allocatable.Count)
return nil
}
return fmt.Errorf("isAllocatableSet: allocatable value not set for driver on node %s", nodeName)
}
}
return fmt.Errorf("isAllocatableSet: driver not found on node %s", nodeName)
}
func recheckFormattingOptionParameter(context map[string]string, key string, fsConfigs map[string]fileSystemConfig, fsType string) (value string, err error) {
v, ok := context[key]
if ok {
// This check is already performed on the controller side
// However, because it is potentially security-sensitive, we redo it here to be safe
if isAlphanumeric := util.StringIsAlphanumeric(v); !isAlphanumeric {
return "", status.Errorf(codes.InvalidArgument, "Invalid %s (aborting!): %v", key, err)
}
// In the case that the default fstype does not support custom sizes we could
// be using an invalid fstype, so recheck that here
if supported := fsConfigs[strings.ToLower(fsType)].isParameterSupported(key); !supported {
return "", status.Errorf(codes.InvalidArgument, "Cannot use %s with fstype %s", key, fsType)
}
}
return v, nil
}