From 5867d495fd40bf67f0157a7793353efa243cc988 Mon Sep 17 00:00:00 2001 From: Masaki Kimura Date: Thu, 15 Nov 2018 02:06:42 +0000 Subject: [PATCH] Change csi rbd to create/delete targetPath for publish/unpublish --- pkg/rbd/nodeserver.go | 69 ++++++++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/pkg/rbd/nodeserver.go b/pkg/rbd/nodeserver.go index 092754aba..069874e38 100644 --- a/pkg/rbd/nodeserver.go +++ b/pkg/rbd/nodeserver.go @@ -50,19 +50,6 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis // Get volName from targetPath s := strings.Split(targetPath, "/") volName = s[len(s)-1] - - // Check if that target path exists properly - // targetPath should exists and should be a file - st, err := os.Stat(targetPath) - if err != nil { - if os.IsNotExist(err) { - return nil, status.Error(codes.NotFound, "targetPath not mounted") - } - return nil, status.Error(codes.Internal, err.Error()) - } - if !st.Mode().IsRegular() { - return nil, status.Error(codes.Internal, "targetPath is not regular file") - } } else { // Get volName from targetPath if !strings.HasSuffix(targetPath, "/mount") { @@ -70,23 +57,39 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis } s := strings.Split(strings.TrimSuffix(targetPath, "/mount"), "/") volName = s[len(s)-1] + } - // Check if that target path exists properly - notMnt, err := ns.mounter.IsLikelyNotMountPoint(targetPath) - if err != nil { - if os.IsNotExist(err) { + // Check if that target path exists properly + // IsLikelyNotMountPoint doesn't return right result to bind mount of device file + // TODO: Need to fix this to a proper check + notMnt, err := ns.mounter.IsLikelyNotMountPoint(targetPath) + if err != nil { + if os.IsNotExist(err) { + if isBlock { + // create an empty file + targetPathFile, err := os.OpenFile(targetPath, os.O_CREATE|os.O_RDWR, 0750) + if err != nil { + glog.V(4).Infof("Failed to create targetPath:%s with error: %v", targetPath, err) + return nil, status.Error(codes.Internal, err.Error()) + } + if err := targetPathFile.Close(); err != nil { + glog.V(4).Infof("Failed to close targetPath:%s with error: %v", targetPath, err) + return nil, status.Error(codes.Internal, err.Error()) + } + } else { + // Create a directory if err = os.MkdirAll(targetPath, 0750); err != nil { return nil, status.Error(codes.Internal, err.Error()) } - notMnt = true - } else { - return nil, status.Error(codes.Internal, err.Error()) } + notMnt = true + } else { + return nil, status.Error(codes.Internal, err.Error()) } + } - if !notMnt { - return &csi.NodePublishVolumeResponse{}, nil - } + if !notMnt { + return &csi.NodePublishVolumeResponse{}, nil } volOptions, err := getRBDVolumeOptions(req.GetVolumeContext()) if err != nil { @@ -134,6 +137,19 @@ func (ns *nodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpu targetPathMutex.LockKey(targetPath) defer targetPathMutex.UnlockKey(targetPath) + // IsLikelyNotMountPoint doesn't return right result to bind mount of device file + // So, just use it to check if file exists, not a mount point + // TODO: Need to fix this to a proper check + _, err := ns.mounter.IsLikelyNotMountPoint(targetPath) + if err != nil { + if os.IsNotExist(err) { + // targetPath has already been deleted + glog.V(4).Infof("targetPath: %s has already been deleted", targetPath) + return &csi.NodeUnpublishVolumeResponse{}, nil + } + return nil, status.Error(codes.NotFound, err.Error()) + } + devicePath, cnt, err := mount.GetDeviceNameFromMount(ns.mounter, targetPath) if err != nil { return nil, status.Error(codes.Internal, err.Error()) @@ -142,6 +158,7 @@ func (ns *nodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpu // Unmounting the image err = ns.mounter.Unmount(targetPath) if err != nil { + glog.V(3).Infof("failed to unmount targetPath: %s with error: %v", targetPath, err) return nil, status.Error(codes.Internal, err.Error()) } @@ -156,6 +173,12 @@ func (ns *nodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpu return nil, err } + // Remove targetPath + if err := os.RemoveAll(targetPath); err != nil { + glog.V(3).Infof("failed to remove targetPath: %s with error: %v", targetPath, err) + return nil, err + } + return &csi.NodeUnpublishVolumeResponse{}, nil }