Skip to content

Commit 1ae75ab

Browse files
author
Power Cloud Robot
authored
Merge pull request #64 from Madhan-SWE/volume_locks
Mount verification added in nodeStageVolume
2 parents 618cfcb + 6fcd0be commit 1ae75ab

File tree

1 file changed

+39
-17
lines changed

1 file changed

+39
-17
lines changed

pkg/driver/node.go

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,29 @@ func (d *nodeService) NodeStageVolume(ctx context.Context, req *csi.NodeStageVol
109109
return nil, status.Error(codes.InvalidArgument, "Staging target not provided")
110110
}
111111

112+
mounted, err := d.isDirMounted(target)
113+
needsCreateDir := false
114+
if mounted {
115+
// Already mounted
116+
klog.V(4).Infof("NodeStageVolume succeeded on volume %v to staging target path %s, mount already exists.", volumeID, target)
117+
return &csi.NodeStageVolumeResponse{}, nil
118+
}
119+
120+
if err != nil {
121+
if os.IsNotExist(err) {
122+
needsCreateDir = true
123+
} else {
124+
return nil, err
125+
}
126+
}
127+
128+
if needsCreateDir {
129+
klog.V(4).Infof("NodeStageVolume attempting mkdir for path %s", target)
130+
if err := os.MkdirAll(target, 0750); err != nil {
131+
return nil, fmt.Errorf("mkdir failed for path %s (%v)", target, err)
132+
}
133+
}
134+
112135
volCap := req.GetVolumeCapability()
113136
if volCap == nil {
114137
return nil, status.Error(codes.InvalidArgument, "Volume capability not provided")
@@ -153,23 +176,6 @@ func (d *nodeService) NodeStageVolume(ctx context.Context, req *csi.NodeStageVol
153176

154177
klog.V(4).Infof("NodeStageVolume: find device path for wwn %s -> %s", wwn, source)
155178

156-
exists, err := d.mounter.ExistsPath(target)
157-
if err != nil {
158-
msg := fmt.Sprintf("failed to check if target %q exists: %v", target, err)
159-
return nil, status.Error(codes.Internal, msg)
160-
}
161-
// When exists is true it means target path was created but device isn't mounted.
162-
// We don't want to do anything in that case and let the operation proceed.
163-
// Otherwise we need to create the target directory.
164-
if !exists {
165-
// If target path does not exist we need to create the directory where volume will be staged
166-
klog.V(4).Infof("NodeStageVolume: creating target dir %q", target)
167-
if err = d.mounter.MakeDir(target); err != nil {
168-
msg := fmt.Sprintf("could not create target dir %q: %v", target, err)
169-
return nil, status.Error(codes.Internal, msg)
170-
}
171-
}
172-
173179
// Check if a device is mounted in target directory
174180
device, _, err := d.mounter.GetDeviceName(target)
175181
if err != nil {
@@ -521,3 +527,19 @@ func hasMountOption(options []string, opt string) bool {
521527
}
522528
return false
523529
}
530+
531+
// isDirMounted checks if the path is already a mount point
532+
func (d *nodeService) isDirMounted(target string) (bool, error) {
533+
// Check if mount already exists
534+
// TODO(msau): check why in-tree uses IsNotMountPoint
535+
// something related to squash and not having permissions to lstat
536+
notMnt, err := d.mounter.IsLikelyNotMountPoint(target)
537+
if err != nil {
538+
return false, err
539+
}
540+
if !notMnt {
541+
// Already mounted
542+
return true, nil
543+
}
544+
return false, nil
545+
}

0 commit comments

Comments
 (0)