Skip to content

Commit 7f7988b

Browse files
nixpanicmergify[bot]
authored andcommitted
rbd: cleanup NodeServer.createTargetMountPath()
The inverse checking and returning of is-a-mounted-path makes it difficult to understand the function. It is easier to follow the code when the function just returns what it says it does, hence added the comment for the function too. Some errors were returned directly, others were converted to gRPC errors. This has been corrected now too, and the caller converts the plain error to a gRPC error now. Signed-off-by: Niels de Vos <ndevos@ibm.com>
1 parent 79cf032 commit 7f7988b

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

internal/rbd/nodeserver.go

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -721,12 +721,12 @@ func (ns *NodeServer) NodePublishVolume(
721721
defer ns.VolumeLocks.Release(targetPath)
722722

723723
// Check if that target path exists properly
724-
notMnt, err := ns.createTargetMountPath(ctx, targetPath, isBlock)
724+
isMnt, err := ns.createTargetMountPath(ctx, targetPath, isBlock)
725725
if err != nil {
726-
return nil, err
726+
return nil, status.Error(codes.Internal, err.Error())
727727
}
728728

729-
if !notMnt {
729+
if isMnt {
730730
return &csi.NodePublishVolumeResponse{}, nil
731731
}
732732

@@ -878,37 +878,45 @@ func (ns *NodeServer) mountVolume(ctx context.Context, stagingPath string, req *
878878
return nil
879879
}
880880

881+
// createTargetMountPath check if the mountPath already has something mounted
882+
// on it. If not, the directory (for a filesystem volume) will be created.
883+
//
884+
// This function returns 'true' in case there is something mounted on the given
885+
// path already, 'false' when the path exists, but nothing is mounted there.
881886
func (ns *NodeServer) createTargetMountPath(ctx context.Context, mountPath string, isBlock bool) (bool, error) {
882-
// Check if that mount path exists properly
883887
isMnt, err := ns.Mounter.IsMountPoint(mountPath)
884888
if err == nil {
885-
return !isMnt, nil
889+
return isMnt, nil
886890
}
887891
if !os.IsNotExist(err) {
888-
return false, status.Error(codes.Internal, err.Error())
892+
return false, fmt.Errorf("path %q exists, but detecting it as mount point failed: %w", mountPath, err)
889893
}
890-
if isBlock {
891-
// #nosec
892-
pathFile, e := os.OpenFile(mountPath, os.O_CREATE|os.O_RDWR, 0o750)
893-
if e != nil {
894-
log.DebugLog(ctx, "Failed to create mountPath:%s with error: %v", mountPath, err)
895894

896-
return !isMnt, status.Error(codes.Internal, e.Error())
897-
}
898-
if err = pathFile.Close(); err != nil {
899-
log.DebugLog(ctx, "Failed to close mountPath:%s with error: %v", mountPath, err)
900-
901-
return !isMnt, status.Error(codes.Internal, err.Error())
902-
}
903-
} else {
895+
// filesystem volume needs a directory
896+
if !isBlock {
904897
// Create a mountpath directory
905898
if err = util.CreateMountPoint(mountPath); err != nil {
906-
return !isMnt, status.Error(codes.Internal, err.Error())
899+
return false, fmt.Errorf("failed to create mount path %q: %w", mountPath, err)
907900
}
901+
902+
return false, nil
903+
}
904+
905+
// block volume checks
906+
// #nosec
907+
pathFile, err := os.OpenFile(mountPath, os.O_CREATE|os.O_RDWR, 0o750)
908+
if err != nil {
909+
log.DebugLog(ctx, "Failed to create mountPath:%s with error: %v", mountPath, err)
910+
911+
return false, fmt.Errorf("failed to create mount file %q: %w", mountPath, err)
912+
}
913+
if err = pathFile.Close(); err != nil {
914+
log.DebugLog(ctx, "Failed to close mountPath:%s with error: %v", mountPath, err)
915+
916+
return false, fmt.Errorf("failed to close mount file %q: %w", mountPath, err)
908917
}
909-
isMnt = false
910918

911-
return !isMnt, err
919+
return false, nil
912920
}
913921

914922
// NodeUnpublishVolume unmounts the volume from the target path.

0 commit comments

Comments
 (0)