@@ -45,6 +45,7 @@ func (d *Driver) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolu
45
45
if req .GetVolumeCapability () == nil {
46
46
return nil , status .Error (codes .InvalidArgument , "Volume capability missing in request" )
47
47
}
48
+ volumeID := req .GetVolumeId ()
48
49
if len (req .GetVolumeId ()) == 0 {
49
50
return nil , status .Error (codes .InvalidArgument , "Volume ID missing in request" )
50
51
}
@@ -64,18 +65,23 @@ func (d *Driver) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolu
64
65
mountOptions = append (mountOptions , "ro" )
65
66
}
66
67
67
- if err := d .ensureMountPoint (target ); err != nil {
68
+ mnt , err := d .ensureMountPoint (target )
69
+ if err != nil {
68
70
return nil , status .Errorf (codes .Internal , "Could not mount target %q: %v" , target , err )
69
71
}
72
+ if mnt {
73
+ klog .V (2 ).Infof ("NodePublishVolume: volume %s is already mounted on %s" , volumeID , target )
74
+ return & csi.NodePublishVolumeResponse {}, nil
75
+ }
70
76
71
- klog .V (2 ).Infof ("NodePublishVolume: mounting %s at %s with mountOptions: %v" , source , target , mountOptions )
77
+ klog .V (2 ).Infof ("NodePublishVolume: volume %s mounting %s at %s with mountOptions: %v" , volumeID , source , target , mountOptions )
72
78
if err := d .mounter .Mount (source , target , "" , mountOptions ); err != nil {
73
79
if removeErr := os .Remove (target ); removeErr != nil {
74
80
return nil , status .Errorf (codes .Internal , "Could not remove mount target %q: %v" , target , removeErr )
75
81
}
76
82
return nil , status .Errorf (codes .Internal , "Could not mount %q at %q: %v" , source , target , err )
77
83
}
78
- klog .V (2 ).Infof ("NodePublishVolume: mount %s at %s successfully" , source , target )
84
+ klog .V (2 ).Infof ("NodePublishVolume: volume %s mount %s at %s successfully" , volumeID , source , target )
79
85
80
86
return & csi.NodePublishVolumeResponse {}, nil
81
87
}
@@ -103,7 +109,8 @@ func (d *Driver) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublish
103
109
104
110
// NodeStageVolume mount the volume to a staging path
105
111
func (d * Driver ) NodeStageVolume (ctx context.Context , req * csi.NodeStageVolumeRequest ) (* csi.NodeStageVolumeResponse , error ) {
106
- if len (req .GetVolumeId ()) == 0 {
112
+ volumeID := req .GetVolumeId ()
113
+ if len (volumeID ) == 0 {
107
114
return nil , status .Error (codes .InvalidArgument , "Volume ID missing in request" )
108
115
}
109
116
targetPath := req .GetStagingTargetPath ()
@@ -115,11 +122,15 @@ func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRe
115
122
return nil , status .Error (codes .InvalidArgument , "Volume capability not provided" )
116
123
}
117
124
118
- if err := d .ensureMountPoint (targetPath ); err != nil {
125
+ mnt , err := d .ensureMountPoint (targetPath )
126
+ if err != nil {
119
127
return nil , status .Errorf (codes .Internal , "Could not mount target %q: %v" , targetPath , err )
120
128
}
129
+ if mnt {
130
+ klog .V (2 ).Infof ("NodeStageVolume: volume %s is already mounted on %s" , volumeID , targetPath )
131
+ return & csi.NodeStageVolumeResponse {}, nil
132
+ }
121
133
122
- volumeID := req .GetVolumeId ()
123
134
mountFlags := req .GetVolumeCapability ().GetMount ().GetMountFlags ()
124
135
attrib := req .GetVolumeContext ()
125
136
secrets := req .GetSecrets ()
@@ -228,12 +239,12 @@ func (d *Driver) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstageVolu
228
239
return nil , status .Error (codes .InvalidArgument , "Staging target not provided" )
229
240
}
230
241
231
- klog .V (2 ).Infof ("NodeUnstageVolume: unmounting %s" , stagingTargetPath )
242
+ klog .V (2 ).Infof ("NodeUnstageVolume: volume %s unmounting on %s" , volumeID , stagingTargetPath )
232
243
err := mount .CleanupMountPoint (stagingTargetPath , d .mounter , false )
233
244
if err != nil {
234
245
return nil , status .Errorf (codes .Internal , "failed to unmount staing target %q: %v" , stagingTargetPath , err )
235
246
}
236
- klog .V (2 ).Infof ("NodeUnstageVolume: unmount %s successfully" , stagingTargetPath )
247
+ klog .V (2 ).Infof ("NodeUnstageVolume: volume %s unmount on %s successfully" , volumeID , stagingTargetPath )
237
248
238
249
return & csi.NodeUnstageVolumeResponse {}, nil
239
250
}
@@ -324,14 +335,15 @@ func (d *Driver) NodeExpandVolume(ctx context.Context, req *csi.NodeExpandVolume
324
335
}
325
336
326
337
// ensureMountPoint: create mount point if not exists
327
- func (d * Driver ) ensureMountPoint (target string ) error {
338
+ // return <true, nil> if it's already a mounted point otherwise return <false, nil>
339
+ func (d * Driver ) ensureMountPoint (target string ) (bool , error ) {
328
340
notMnt , err := d .mounter .IsLikelyNotMountPoint (target )
329
341
if err != nil && ! os .IsNotExist (err ) {
330
342
if IsCorruptedDir (target ) {
331
343
notMnt = false
332
344
klog .Warningf ("detected corrupted mount for targetPath [%s]" , target )
333
345
} else {
334
- return err
346
+ return ! notMnt , err
335
347
}
336
348
}
337
349
@@ -340,21 +352,20 @@ func (d *Driver) ensureMountPoint(target string) error {
340
352
_ , err := ioutil .ReadDir (target )
341
353
if err == nil {
342
354
klog .V (2 ).Infof ("already mounted to target %s" , target )
343
- return nil
355
+ return ! notMnt , nil
344
356
}
345
357
// mount link is invalid, now unmount and remount later
346
358
klog .Warningf ("ReadDir %s failed with %v, unmount this directory" , target , err )
347
359
if err := d .mounter .Unmount (target ); err != nil {
348
360
klog .Errorf ("Unmount directory %s failed with %v" , target , err )
349
- return err
361
+ return ! notMnt , err
350
362
}
351
- // notMnt = true
363
+ notMnt = true
364
+ return ! notMnt , err
352
365
}
353
-
354
366
if err := volumehelper .MakeDir (target ); err != nil {
355
367
klog .Errorf ("MakeDir failed on target: %s (%v)" , target , err )
356
- return err
368
+ return ! notMnt , err
357
369
}
358
-
359
- return nil
370
+ return ! notMnt , nil
360
371
}
0 commit comments