-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathnode.go
More file actions
911 lines (782 loc) · 31.6 KB
/
Copy pathnode.go
File metadata and controls
911 lines (782 loc) · 31.6 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
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
/**
* Copyright 2019 IBM Corp.
*
* 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"
"fmt"
"path"
"reflect"
"strings"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/ibm/ibm-block-csi-driver/node/goid_info"
"github.com/ibm/ibm-block-csi-driver/node/logger"
"github.com/ibm/ibm-block-csi-driver/node/pkg/driver/device_connectivity"
"github.com/ibm/ibm-block-csi-driver/node/pkg/driver/executer"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
mount "k8s.io/mount-utils"
)
var (
nodeCaps = []csi.NodeServiceCapability_RPC_Type{
csi.NodeServiceCapability_RPC_STAGE_UNSTAGE_VOLUME,
csi.NodeServiceCapability_RPC_EXPAND_VOLUME,
csi.NodeServiceCapability_RPC_GET_VOLUME_STATS,
}
// volumeCaps represents how the volume could be accessed.
// It is SINGLE_NODE_WRITER since EBS volume could only be
// attached to a single node at any given time.
volumeCaps = []csi.VolumeCapability_AccessMode{
{
Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
},
{
Mode: csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER,
},
}
defaultFSType = "ext4"
StageInfoFilename = ".stageInfo.json"
NvmeFullPath = "/host/etc/nvme/hostnqn"
IscsiFullPath = "/host/etc/iscsi/initiatorname.iscsi"
)
const (
FCPath = "/sys/class/fc_host"
FCPortPath = "/sys/class/fc_host/host*/port_name"
MaxNodeIdLength = 256
)
//go:generate mockgen -destination=../../mocks/mock_NodeMounter.go -package=mocks github.com/ibm/ibm-block-csi-driver/node/pkg/driver NodeMounter
type NodeMounter interface {
mount.Interface
FormatAndMount(source string, target string, fstype string, options []string) error
GetDiskFormat(disk string) (string, error)
}
// nodeService represents the node service of CSI driver
type NodeService struct {
Mounter NodeMounter
ConfigYaml ConfigFile
Hostname string
NodeUtils NodeUtilsInterface
executer executer.ExecuterInterface
VolumeIdLocksMap SyncLockInterface
OsDeviceConnectivityMapping map[string]device_connectivity.OsDeviceConnectivityInterface
OsDeviceConnectivityHelper device_connectivity.OsDeviceConnectivityHelperScsiGenericInterface
}
type VolumeStatistics struct {
AvailableBytes, TotalBytes, UsedBytes int64
AvailableInodes, TotalInodes, UsedInodes int64
}
// newNodeService creates a new node service
// it panics if failed to create the service
func NewNodeService(configYaml ConfigFile, hostname string, nodeUtils NodeUtilsInterface,
OsDeviceConnectivityMapping map[string]device_connectivity.OsDeviceConnectivityInterface,
osDeviceConnectivityHelper device_connectivity.OsDeviceConnectivityHelperScsiGenericInterface,
executer executer.ExecuterInterface, mounter NodeMounter, syncLock SyncLockInterface) NodeService {
return NodeService{
ConfigYaml: configYaml,
Hostname: hostname,
NodeUtils: nodeUtils,
executer: executer,
OsDeviceConnectivityMapping: OsDeviceConnectivityMapping,
OsDeviceConnectivityHelper: osDeviceConnectivityHelper,
Mounter: mounter,
VolumeIdLocksMap: syncLock,
}
}
func (d *NodeService) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) {
defer logger.Exit(logger.Enter(req))
err := d.nodeStageVolumeRequestValidation(req)
if err != nil {
switch err.(type) {
case *RequestValidationError:
return nil, status.Error(codes.InvalidArgument, err.Error())
default:
return nil, status.Error(codes.Internal, err.Error())
}
}
connectivityType, lun, ipsByArrayInitiator, err := d.NodeUtils.GetInfoFromPublishContext(req.PublishContext)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
volumeID := req.VolumeId
err = d.VolumeIdLocksMap.AddVolumeAndLunLock(volumeID, lun, "NodeStageVolume")
if err != nil {
logger.Errorf("Another operation is being performed on volume : {%s}", volumeID)
return nil, status.Error(codes.Aborted, err.Error())
}
defer d.VolumeIdLocksMap.RemoveVolumeAndLunLock(volumeID, lun, "NodeStageVolume")
arrayInitiators := d.NodeUtils.GetArrayInitiators(ipsByArrayInitiator)
osDeviceConnectivity, ok := d.OsDeviceConnectivityMapping[connectivityType]
if !ok {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("Wrong connectivity type %s", connectivityType))
}
osDeviceConnectivity.EnsureLogin(ipsByArrayInitiator)
err = d.OsDeviceConnectivityHelper.RemoveGhostDevice(lun)
if err != nil {
return nil, status.Error(codes.Aborted, err.Error())
}
err = osDeviceConnectivity.RescanDevices(lun, arrayInitiators)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
err = d.OsDeviceConnectivityHelper.RemoveGhostDevice(lun)
if err != nil {
logger.Debugf("Failed to clean ghost device for lun %d", lun)
// we can swallow the error here, since it's just for cleanliness
}
volumeUuid := d.NodeUtils.GetVolumeUuid(volumeID)
mpathDevice, err := osDeviceConnectivity.GetMpathDevice(volumeUuid)
logger.Debugf("Discovered device : {%v}", mpathDevice)
if err != nil {
logger.Errorf("Error while discovering the device : {%v}", err.Error())
return nil, status.Error(codes.Internal, err.Error())
}
volumeCap := req.GetVolumeCapability()
switch volumeCap.GetAccessType().(type) {
case *csi.VolumeCapability_Block:
logger.Debugf("NodeStageVolume Finished: multipath device [%s] is ready to be mounted by NodePublishVolume API", mpathDevice)
return &csi.NodeStageVolumeResponse{}, nil
}
baseDevice := path.Base(mpathDevice)
sysDevices, err := d.NodeUtils.GetSysDevicesFromMpath(baseDevice)
if err != nil {
logger.Errorf("Error while trying to get sys devices : {%v}", err.Error())
return nil, status.Error(codes.Internal, err.Error())
}
err = osDeviceConnectivity.ValidateLun(lun, sysDevices)
if err != nil {
logger.Errorf("Error while trying to validate lun : {%v}", err.Error())
return nil, status.Error(codes.Internal, err.Error())
}
existingFormat, err := d.Mounter.GetDiskFormat(mpathDevice)
if err != nil {
logger.Errorf("Could not determine if disk {%v} is formatted, error: %v", mpathDevice, err)
return nil, status.Error(codes.Internal, err.Error())
}
requestedFsType := volumeCap.GetMount().FsType
fsTypeForMount, err := d.resolveFsTypeForMount(requestedFsType, existingFormat)
if err != nil {
logger.Errorf("Error while resolving type of filesystem to mount : {%v}", err.Error())
return nil, err
}
stagingPath := req.GetStagingTargetPath() // e.g in k8s /var/lib/kubelet/plugins/kubernetes.io/csi/pv/pvc-21967c74-b456-11e9-b93e-005056a45d5f/globalmount
stagingPathWithHostPrefix := d.NodeUtils.GetPodPath(stagingPath)
// check if already mounted
isMounted, err := d.isTargetMounted(stagingPathWithHostPrefix, true)
if err != nil {
logger.Debugf("Existing mount check failed {%v}", err.Error())
return nil, err
}
if isMounted { // idempotent case
return &csi.NodeStageVolumeResponse{}, nil
}
err = d.formatAndMount(mpathDevice, stagingPath, fsTypeForMount, existingFormat)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
logger.Debugf("NodeStageVolume Finished: staging path [%s] is ready to be mounted by NodePublishVolume API", stagingPath)
return &csi.NodeStageVolumeResponse{}, nil
}
func isValidConnectivity(connectivityTypes Connectivity_type, connectivityType string) bool {
connectivityReflect := reflect.ValueOf(&connectivityTypes).Elem()
for i := 0; i < connectivityReflect.NumField(); i++ {
if connectivityReflect.Field(i).Interface() == connectivityType {
return true
}
}
return false
}
func (d *NodeService) nodeStageVolumeRequestValidation(req *csi.NodeStageVolumeRequest) error {
volumeID := req.GetVolumeId()
if len(volumeID) == 0 {
return &RequestValidationError{"Volume ID not provided"}
}
stagingPath := req.GetStagingTargetPath()
if len(stagingPath) == 0 {
return &RequestValidationError{"Staging path not provided"}
}
stagingPathWithHostPrefix := d.NodeUtils.GetPodPath(stagingPath)
isStagingPathExists := d.NodeUtils.IsPathExists(stagingPathWithHostPrefix)
if !isStagingPathExists {
return &RequestValidationError{fmt.Sprintf("Staging path [%s] does not exist", stagingPathWithHostPrefix)}
}
volCap := req.GetVolumeCapability()
if volCap == nil {
return &RequestValidationError{"Volume capability not provided"}
}
if !isValidVolumeCapabilitiesAccessMode([]*csi.VolumeCapability{volCap}) {
return &RequestValidationError{"Volume capability AccessMode not supported"}
}
// If the access type is not mount and not block, should never happen
switch volCap.GetAccessType().(type) {
case *csi.VolumeCapability_Mount:
case *csi.VolumeCapability_Block:
default:
return &RequestValidationError{"Volume Access Type is not supported"}
}
connectivityType, lun, ipsByArrayInitiator, err := d.NodeUtils.GetInfoFromPublishContext(req.PublishContext)
if err != nil {
return &RequestValidationError{fmt.Sprintf("Fail to parse PublishContext %v with err = %v", req.PublishContext, err)}
}
supportedConnectivityTypes := d.ConfigYaml.Connectivity_type
if !isValidConnectivity(supportedConnectivityTypes, connectivityType) {
return &RequestValidationError{fmt.Sprintf("PublishContext with wrong connectivity type %s. Supported connectivities %v", connectivityType, supportedConnectivityTypes)}
}
if lun < 0 {
return &RequestValidationError{fmt.Sprintf("PublishContext with wrong lun id %d.", lun)}
}
if connectivityType != d.ConfigYaml.Connectivity_type.Nvme_over_fc {
if len(ipsByArrayInitiator) == 0 {
return &RequestValidationError{fmt.Sprintf("PublishContext with wrong arrayInitiators %v.",
ipsByArrayInitiator)}
}
}
if connectivityType == d.ConfigYaml.Connectivity_type.Iscsi {
isAnyIpFound := false
for arrayInitiator := range ipsByArrayInitiator {
if _, ok := req.PublishContext[arrayInitiator]; ok {
isAnyIpFound = true
break
}
}
if !isAnyIpFound {
return &RequestValidationError{fmt.Sprintf("PublishContext with no iscsi target IP %v.",
req.PublishContext)}
}
}
return nil
}
func (d *NodeService) resolveFsTypeForMount(requestedFsType string, existingFormat string) (string, error) {
fsTypeForMount := requestedFsType
if requestedFsType == "" {
if existingFormat == "" {
fsTypeForMount = defaultFSType
} else {
fsTypeForMount = existingFormat
}
} else if existingFormat != "" {
if requestedFsType != existingFormat {
return "", status.Errorf(codes.AlreadyExists, "Requested fs_type {%v} but found {%v}", requestedFsType, existingFormat)
}
}
return fsTypeForMount, nil
}
func (d *NodeService) formatAndMount(mpathDevice string, stagingPath string, fsTypeForMount string, existingFormat string) error {
if existingFormat == "" {
d.NodeUtils.FormatDevice(mpathDevice, fsTypeForMount)
}
var mountOptions []string
if fsTypeForMount == "xfs" {
mountOptions = append(mountOptions, "nouuid")
}
logger.Debugf("Mount the device with fs_type = {%v} (Create filesystem if needed)", fsTypeForMount)
return d.Mounter.FormatAndMount(mpathDevice, stagingPath, fsTypeForMount, mountOptions) // Passing without /host because k8s mounter uses mount\mkfs\fsck
}
func (d *NodeService) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstageVolumeRequest) (*csi.NodeUnstageVolumeResponse, error) {
defer logger.Exit(logger.Enter(req))
volumeID := req.GetVolumeId()
if len(volumeID) == 0 {
logger.Errorf("Volume ID not provided")
return nil, status.Error(codes.InvalidArgument, "Volume ID not provided")
}
err := d.VolumeIdLocksMap.AddVolumeLock(volumeID, "NodeUnstageVolume")
if err != nil {
logger.Errorf("Another operation is being performed on volume : {%s}", volumeID)
return nil, status.Error(codes.Aborted, err.Error())
}
defer d.VolumeIdLocksMap.RemoveVolumeLock(volumeID, "NodeUnstageVolume")
stagingTargetPath := req.GetStagingTargetPath()
if len(stagingTargetPath) == 0 {
logger.Errorf("Staging target not provided")
return nil, status.Error(codes.InvalidArgument, "Staging target not provided")
}
stagingPathWithHostPrefix := d.NodeUtils.GetPodPath(stagingTargetPath)
logger.Debugf("Check if staging path {%s} is mounted", stagingPathWithHostPrefix)
isNotMounted, err := d.NodeUtils.IsNotMountPoint(stagingPathWithHostPrefix)
if err != nil {
logger.Warningf("Failed to check if (%s), is mounted", stagingPathWithHostPrefix)
return nil, status.Error(codes.Internal, err.Error())
}
if !isNotMounted {
err = d.Mounter.Unmount(stagingTargetPath)
if err != nil {
logger.Errorf("Unmount failed. Target : %q, err : %v", stagingTargetPath, err.Error())
return nil, status.Error(codes.Internal, err.Error())
}
}
volumeUuid := d.NodeUtils.GetVolumeUuid(volumeID)
mpathDevice, err := d.OsDeviceConnectivityHelper.GetMpathDevice(volumeUuid)
if err != nil {
switch err.(type) {
case *device_connectivity.MultipathDeviceNotFoundForVolumeError:
return &csi.NodeUnstageVolumeResponse{}, nil
default:
logger.Errorf("Error while discovering the device : {%v}", err.Error())
return nil, status.Error(codes.Internal, err.Error())
}
}
logger.Debugf("Discovered device : {%v}", mpathDevice)
baseDevice := path.Base(mpathDevice)
sysDevices, err := d.NodeUtils.GetSysDevicesFromMpath(baseDevice)
if err != nil {
logger.Errorf("Error while trying to get sys devices : {%v}", err.Error())
return nil, status.Error(codes.Internal, err.Error())
}
isNvme, err := d.NodeUtils.IsNvmeBaseDevice(baseDevice)
if err != nil {
logger.Warningf("Failed to check if device %s is NVMe, assuming non-NVMe: %v", baseDevice, err)
isNvme = false
}
if !isNvme {
err = d.OsDeviceConnectivityHelper.FlushMultipathDevice(baseDevice)
if err != nil {
return nil, status.Errorf(codes.Internal, "Multipath -f command failed with error: %v", err)
}
err = d.OsDeviceConnectivityHelper.RemovePhysicalDevice(sysDevices)
if err != nil {
return nil, status.Errorf(codes.Internal, "Remove scsi device failed with error: %v", err)
}
} else {
logger.Infof("Device %s is NVMe: skipping multipath -f and SCSI device cleanup", baseDevice)
}
stageInfoPath := path.Join(stagingTargetPath, StageInfoFilename)
if d.NodeUtils.StageInfoFileIsExist(stageInfoPath) {
if err := d.NodeUtils.ClearStageInfoFile(stageInfoPath); err != nil {
return nil, status.Errorf(codes.Internal, "Fail to clear the stage info file: error %v", err)
}
}
logger.Debugf("NodeUnStageVolume Finished: multipath device removed from host")
return &csi.NodeUnstageVolumeResponse{}, nil
}
func (d *NodeService) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) {
defer logger.Exit(logger.Enter(req))
code, err := d.nodePublishVolumeRequestValidation(req)
if err != nil {
switch err.(type) {
case *RequestValidationError:
return nil, status.Error(code, err.Error())
default:
return nil, status.Error(codes.Internal, err.Error())
}
}
volumeID := req.GetVolumeId()
err = d.VolumeIdLocksMap.AddVolumeLock(volumeID, "NodePublishVolume")
if err != nil {
logger.Errorf("Another operation is being perfomed on volume : {%s}", volumeID)
return nil, status.Error(codes.Aborted, err.Error())
}
defer d.VolumeIdLocksMap.RemoveVolumeLock(volumeID, "NodePublishVolume")
// checking if the node staging path was mounted into
stagingPath := req.GetStagingTargetPath()
targetPath := req.GetTargetPath()
targetPathWithHostPrefix := d.NodeUtils.GetPodPath(targetPath)
logger.Debugf("stagingPath : {%v}, targetPath : {%v}", stagingPath, targetPath)
// if the device is not mounted then we are mounting it.
volumeCap := req.GetVolumeCapability()
isFSVolume := true
switch volumeCap.GetAccessType().(type) {
case *csi.VolumeCapability_Block:
isFSVolume = false
}
if isFSVolume {
stagingPathWithHostPrefix := d.NodeUtils.GetPodPath(stagingPath)
isStagingNotMounted, err := d.NodeUtils.IsNotMountPoint(stagingPathWithHostPrefix)
if err != nil {
logger.Errorf("Existing mount check failed {%v}", err.Error())
return nil, err
}
if isStagingNotMounted {
return nil, status.Errorf(codes.InvalidArgument, "Staging path %v is not a mount point", stagingPath)
}
}
isTargetPathExists := d.NodeUtils.IsPathExists(targetPathWithHostPrefix)
if isTargetPathExists {
// check if already mounted
isTargetMounted, err := d.isTargetMounted(targetPathWithHostPrefix, isFSVolume)
if err != nil {
logger.Debugf("Existing mount check failed {%v}", err.Error())
return nil, err
}
if isTargetMounted { // idempotent case
return &csi.NodePublishVolumeResponse{}, nil
}
} else {
logger.Debugf("Target path does not exist. Creating : {%v}", targetPathWithHostPrefix)
if isFSVolume {
err = d.NodeUtils.MakeDir(targetPathWithHostPrefix)
} else {
err = d.NodeUtils.MakeFile(targetPathWithHostPrefix)
}
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not create %q: %v", targetPathWithHostPrefix, err.Error())
}
}
if isFSVolume {
fsType := volumeCap.GetMount().FsType
err = d.publishFileSystemVolume(stagingPath, targetPath, fsType)
} else {
volumeUuid := d.NodeUtils.GetVolumeUuid(volumeID)
mpathDevice, err := d.OsDeviceConnectivityHelper.GetMpathDevice(volumeUuid)
if err != nil {
logger.Errorf("Error while discovering the device : {%v}", err.Error())
return nil, status.Error(codes.Internal, err.Error())
}
logger.Debugf("Discovered device : {%v}", mpathDevice)
err = d.publishRawBlockVolume(mpathDevice, targetPath)
}
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
logger.Debugf("NodePublishVolume Finished: targetPath {%v} is now a mount point", targetPath)
return &csi.NodePublishVolumeResponse{}, nil
}
func (d *NodeService) publishFileSystemVolume(stagingPath string, targetPath string, fsType string) error {
mountOptions := []string{"bind"}
logger.Debugf("Bind mount staging: {%v} with target: {%v}, fs_type: {%v}", stagingPath, targetPath, fsType)
return d.Mounter.Mount(stagingPath, targetPath, fsType, mountOptions) // Passing without /host because k8s mounter uses mount\mkfs\fsck
}
func (d *NodeService) publishRawBlockVolume(mpathDevice string, targetPath string) error {
options := []string{"bind"}
logger.Debugf("Mount the device to raw block volume. Target : {%s}, device : {%s}", targetPath, mpathDevice)
return d.Mounter.Mount(mpathDevice, targetPath, "", options)
}
// targetPathWithHostPrefix: path of target
// isFSVolume: if we check volume with file system - true, otherwise for raw block false
// Returns: is <target mounted, error if occured>
func (d *NodeService) isTargetMounted(targetPathWithHostPrefix string, isFSVolume bool) (bool, error) {
logger.Debugf("Check if target {%s} is mounted", targetPathWithHostPrefix)
isNotMounted, err := d.NodeUtils.IsNotMountPoint(targetPathWithHostPrefix)
if err != nil {
logger.Warningf("Failed to check if (%s), is mounted", targetPathWithHostPrefix)
return false, status.Error(codes.Internal, err.Error())
}
if isNotMounted {
return false, nil
} else {
targetIsDir := d.NodeUtils.IsDirectory(targetPathWithHostPrefix)
if isFSVolume && !targetIsDir {
return true, status.Errorf(codes.AlreadyExists, "Required volume with file system but target {%s} is mounted and it is not a directory.", targetPathWithHostPrefix)
} else if !isFSVolume && targetIsDir {
return true, status.Errorf(codes.AlreadyExists, "Required raw block volume but target {%s} is mounted and it is a directory.", targetPathWithHostPrefix)
}
logger.Warningf("Idempotent case : targetPath already mounted (%s), so no need to mount again. Finish NodePublishVolume", targetPathWithHostPrefix)
return true, nil
}
}
func (d *NodeService) nodePublishVolumeRequestValidation(req *csi.NodePublishVolumeRequest) (codes.Code, error) {
volumeID := req.GetVolumeId()
if len(volumeID) == 0 {
return codes.InvalidArgument, &RequestValidationError{"Volume ID not provided"}
}
target := req.GetTargetPath()
if len(target) == 0 {
return codes.InvalidArgument, &RequestValidationError{"Target path not provided"}
}
volCap := req.GetVolumeCapability()
if volCap == nil {
return codes.InvalidArgument, &RequestValidationError{"Volume capability not provided"}
}
source := req.GetStagingTargetPath()
if len(source) == 0 {
return codes.FailedPrecondition, &RequestValidationError{"Staging target not provided"}
}
if !isValidVolumeCapabilitiesAccessMode([]*csi.VolumeCapability{volCap}) {
return codes.InvalidArgument, &RequestValidationError{"Volume capability AccessMode not supported"}
}
// If the access type is not mount and not block, should never happen
switch volCap.GetAccessType().(type) {
case *csi.VolumeCapability_Mount:
case *csi.VolumeCapability_Block:
default:
return codes.InvalidArgument, &RequestValidationError{"Volume Access Type is not supported"}
}
return codes.Internal, nil
}
func (d *NodeService) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) {
defer logger.Exit(logger.Enter(req))
volumeID := req.GetVolumeId()
if len(volumeID) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID not provided")
}
err := d.VolumeIdLocksMap.AddVolumeLock(volumeID, "NodeUnpublishVolume")
if err != nil {
logger.Errorf("Another operation is being perfomed on volume : {%s}", volumeID)
return nil, status.Error(codes.Aborted, err.Error())
}
defer d.VolumeIdLocksMap.RemoveVolumeLock(volumeID, "NodeUnpublishVolume")
target := req.GetTargetPath()
if len(target) == 0 {
return nil, status.Error(codes.InvalidArgument, "Target path not provided")
}
targetPathWithHostPrefix := d.NodeUtils.GetPodPath(target)
logger.Debugf("Check if target file exists %s", targetPathWithHostPrefix)
if !d.NodeUtils.IsPathExists(targetPathWithHostPrefix) {
logger.Warningf("Idempotent case: target file %s doesn't exist", targetPathWithHostPrefix)
return &csi.NodeUnpublishVolumeResponse{}, nil
}
// Unmount and delete mount point file/folder
logger.Debugf("Check if target %s is mounted", targetPathWithHostPrefix)
isNotMounted, err := d.NodeUtils.IsNotMountPoint(targetPathWithHostPrefix)
if err != nil {
logger.Errorf("Check is target mounted failed. Target : %q, err : %v", targetPathWithHostPrefix, err.Error())
return nil, status.Error(codes.Internal, err.Error())
}
if !isNotMounted {
err = d.Mounter.Unmount(target)
if err != nil {
logger.Errorf("Unmount failed. Target : %q, err : %v", target, err.Error())
return nil, status.Error(codes.Internal, err.Error())
}
}
logger.Debugf("Unmount finished. Target : {%s}", target)
if err = d.NodeUtils.RemoveFileOrDirectory(targetPathWithHostPrefix); err != nil {
logger.Errorf("Failed to remove mount path file/directory. Target %s: %v", targetPathWithHostPrefix, err)
return nil, status.Error(codes.Internal, err.Error())
}
logger.Debugf("Mount point deleted. Target : %s", targetPathWithHostPrefix)
return &csi.NodeUnpublishVolumeResponse{}, nil
}
func (d *NodeService) NodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVolumeStatsRequest) (*csi.NodeGetVolumeStatsResponse, error) {
volumeId := req.VolumeId
goid_info.SetAdditionalIDInfo(volumeId)
defer goid_info.DeleteAdditionalIDInfo()
volumePath := req.VolumePath
volumePathWithHostPrefix := d.NodeUtils.GetPodPath(volumePath)
err := d.nodeGetVolumeStatsRequestValidation(volumeId, volumePath)
if err != nil {
return nil, err
}
isPathExists := d.NodeUtils.IsPathExists(volumePathWithHostPrefix)
if !isPathExists {
return nil, status.Errorf(codes.NotFound, "volume path %q does not exist", volumePath)
}
volumeStats, err := d.getVolumeStats(volumePathWithHostPrefix, volumeId)
if err != nil {
return nil, err
}
return &csi.NodeGetVolumeStatsResponse{
Usage: []*csi.VolumeUsage{
{
Unit: csi.VolumeUsage_BYTES,
Available: volumeStats.AvailableBytes,
Total: volumeStats.TotalBytes,
Used: volumeStats.UsedBytes,
},
{
Unit: csi.VolumeUsage_INODES,
Available: volumeStats.AvailableInodes,
Total: volumeStats.TotalInodes,
Used: volumeStats.UsedInodes,
},
},
}, nil
}
func (d *NodeService) nodeGetVolumeStatsRequestValidation(volumeId string, volumePath string) error {
if volumeId == "" {
return status.Error(codes.InvalidArgument, "NodeGetVolumeStats Volume ID must be provided")
}
if volumePath == "" {
return status.Error(codes.InvalidArgument, "NodeGetVolumeStats Volume Path must be provided")
}
return nil
}
func (d *NodeService) getVolumeStats(path string, volumeId string) (VolumeStatistics, error) {
var volumeStats VolumeStatistics
isBlock, err := d.NodeUtils.IsBlock(path)
if err != nil {
return VolumeStatistics{}, status.Errorf(codes.Internal, "Failed to determine if %q is block device: %s", path, err)
}
if isBlock {
volumeStats, err = d.NodeUtils.GetBlockVolumeStats(volumeId)
if err != nil {
switch err.(type) {
case *device_connectivity.MultipathDeviceNotFoundForVolumeError:
return VolumeStatistics{}, status.Errorf(codes.NotFound, "Multipath device of volume id %q does not exist", volumeId)
default:
return VolumeStatistics{}, status.Errorf(codes.Internal, "Error while discovering the device : %s", err)
}
}
} else {
volumeUuid := d.NodeUtils.GetVolumeUuid(volumeId)
isVolumePathMatchesVolumeId, err := d.OsDeviceConnectivityHelper.IsVolumePathMatchesVolumeId(volumeUuid, path)
if err != nil {
return VolumeStatistics{}, status.Errorf(codes.Internal,
"Failed to determine if volume id [%q], is accessible on volume path [%q], error: %s",
volumeId, path, err)
}
if !isVolumePathMatchesVolumeId {
return VolumeStatistics{}, status.Errorf(codes.NotFound,
"Volume id [%q] is not accessible on volume path [%q]", volumeId, path)
}
volumeStats, err = d.NodeUtils.GetFileSystemVolumeStats(path)
if err != nil {
return VolumeStatistics{}, status.Errorf(codes.Internal, "Failed to get statistics: %s", err)
}
}
return volumeStats, nil
}
func (d *NodeService) NodeExpandVolume(ctx context.Context, req *csi.NodeExpandVolumeRequest) (*csi.NodeExpandVolumeResponse, error) {
defer logger.Exit(logger.Enter(req))
err := d.nodeExpandVolumeRequestValidation(req)
if err != nil {
return nil, err
}
volumeID := req.GetVolumeId()
err = d.VolumeIdLocksMap.AddVolumeLock(volumeID, "NodeExpandVolume")
if err != nil {
logger.Errorf("Another operation is being performed on volume : {%s}", volumeID)
return nil, status.Error(codes.Aborted, err.Error())
}
defer d.VolumeIdLocksMap.RemoveVolumeLock(volumeID, "NodeExpandVolume")
volumeUuid := d.NodeUtils.GetVolumeUuid(volumeID)
device, err := d.OsDeviceConnectivityHelper.GetMpathDevice(volumeUuid)
if err != nil {
logger.Errorf("Error while discovering the device : {%v}", err.Error())
return nil, status.Error(codes.Internal, err.Error())
}
logger.Debugf("Discovered device : {%v}", device)
baseDevice := path.Base(device)
sysDevices, err := d.NodeUtils.GetSysDevicesFromMpath(baseDevice)
if err != nil {
logger.Errorf("Error while trying to get sys devices : {%v}", err.Error())
return nil, status.Error(codes.Internal, err.Error())
}
devicesAreNvme, err := d.NodeUtils.DevicesAreNvme(sysDevices)
if err != nil {
logger.Errorf("Error while trying to check if sys devices are nvme devices : {%v}", err.Error())
return nil, status.Error(codes.Internal, err.Error())
}
if !devicesAreNvme {
err = d.NodeUtils.RescanPhysicalDevices(sysDevices)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
}
err = d.NodeUtils.ExpandMpathDevice(baseDevice)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
existingFormat, err := d.Mounter.GetDiskFormat(device)
if err != nil {
logger.Errorf("Could not determine if disk {%v} is formatted, error: %v", device, err)
return nil, status.Error(codes.Internal, err.Error())
}
mountPointToExpand := req.GetStagingTargetPath()
if mountPointToExpand == "" {
mountPointToExpand = req.GetVolumePath()
}
err = d.NodeUtils.ExpandFilesystem(device, mountPointToExpand, existingFormat)
if err != nil {
logger.Errorf("Could not resize {%v} file system of {%v} , error: %v", existingFormat, device, err)
return nil, status.Error(codes.Internal, err.Error())
}
return &csi.NodeExpandVolumeResponse{}, nil
}
func (d *NodeService) nodeExpandVolumeRequestValidation(req *csi.NodeExpandVolumeRequest) error {
volumeID := req.GetVolumeId()
if volumeID == "" {
err := &RequestValidationError{"Volume ID not provided"}
return status.Error(codes.InvalidArgument, err.Error())
}
if !strings.Contains(volumeID, d.ConfigYaml.Parameters.Object_id_info.Delimiter) {
errMsg := fmt.Sprintf("invalid Volume ID - no {%v} found", d.ConfigYaml.Parameters.Object_id_info.Delimiter)
err := &RequestValidationError{errMsg}
return status.Error(codes.NotFound, err.Error())
}
volumePath := req.GetVolumePath()
if volumePath == "" {
err := &RequestValidationError{"Volume path not provided"}
return status.Error(codes.InvalidArgument, err.Error())
}
return nil
}
func (d *NodeService) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetCapabilitiesRequest) (*csi.NodeGetCapabilitiesResponse, error) {
defer logger.Exit(logger.Enter(req))
var caps []*csi.NodeServiceCapability
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) {
defer logger.Exit(logger.Enter(req))
var nvmeNQN string
var fcWWNs []string
var iscsiIQN string
var err error
topologyLabels, err := d.NodeUtils.GetTopologyLabels(ctx, d.Hostname)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
logger.Debugf("discovered topology labels : %v", topologyLabels)
nvmeExists := d.NodeUtils.IsPathExists(NvmeFullPath)
if nvmeExists {
nvmeNQN, err = d.NodeUtils.ReadNvmeNqn()
if err != nil {
logger.Warning(err)
}
}
fcExists := d.NodeUtils.IsFCExists()
if fcExists {
fcWWNs, err = d.NodeUtils.ParseFCPorts()
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
}
iscsiExists := d.NodeUtils.IsPathExists(IscsiFullPath)
if iscsiExists {
iscsiIQN, err = d.NodeUtils.ParseIscsiInitiators()
if err != nil {
logger.Warning(err)
}
}
if nvmeNQN == "" && fcWWNs == nil && iscsiIQN == "" {
err := fmt.Errorf("Cannot find valid nvme nqn, fc wwns or iscsi iqn")
return nil, status.Error(codes.Internal, err.Error())
}
nodeId, err := d.NodeUtils.GenerateNodeID(d.Hostname, nvmeNQN, fcWWNs, iscsiIQN)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
logger.Debugf("node id is : %s", nodeId)
return &csi.NodeGetInfoResponse{
NodeId: nodeId,
AccessibleTopology: &csi.Topology{Segments: topologyLabels},
}, nil
}
func isValidVolumeCapabilitiesAccessMode(volCaps []*csi.VolumeCapability) bool {
hasSupport := func(cap *csi.VolumeCapability) bool {
for _, c := range volumeCaps {
if c.GetMode() == cap.AccessMode.GetMode() {
return true
}
}
return false
}
foundAll := true
for _, c := range volCaps {
if !hasSupport(c) {
foundAll = false
break
}
}
return foundAll
}