From 404e011ae9936beaea3264927de976cdea0b3969 Mon Sep 17 00:00:00 2001 From: Rakshith R Date: Tue, 15 Jun 2021 17:55:29 +0530 Subject: [PATCH] cleanup: added helper func isNotMountPoint Added helper func isNotMountPoint to check mountPoint, validate error and reduce complexity of NodeStageVolume. Signed-off-by: Rakshith R --- internal/rbd/nodeserver.go | 9 +++------ internal/rbd/rbd_util.go | 11 +++++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/internal/rbd/nodeserver.go b/internal/rbd/nodeserver.go index fbabe370f..ca84ea1e2 100644 --- a/internal/rbd/nodeserver.go +++ b/internal/rbd/nodeserver.go @@ -154,14 +154,11 @@ func (ns *NodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVol } } - var isNotMnt bool // check if stagingPath is already mounted - isNotMnt, err = mount.IsNotMountPoint(ns.mounter, stagingTargetPath) - if err != nil && !os.IsNotExist(err) { + isNotMnt, err := isNotMountPoint(ns.mounter, stagingTargetPath) + if err != nil { return nil, status.Error(codes.Internal, err.Error()) - } - - if !isNotMnt { + } else if !isNotMnt { util.DebugLog(ctx, "rbd: volume %s is already mounted to %s, skipping", volID, stagingTargetPath) return &csi.NodeStageVolumeResponse{}, nil } diff --git a/internal/rbd/rbd_util.go b/internal/rbd/rbd_util.go index ad590f606..982880608 100644 --- a/internal/rbd/rbd_util.go +++ b/internal/rbd/rbd_util.go @@ -38,6 +38,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/cloud-provider/volume/helpers" + mount "k8s.io/mount-utils" ) const ( @@ -455,6 +456,16 @@ func checkImageFeatures(imageFeatures string, ok, static bool) bool { return static && (!ok || imageFeatures == "") } +// isNotMountPoint checks whether MountPoint does not exists and +// also discards error indicating mountPoint exists. +func isNotMountPoint(mounter mount.Interface, stagingTargetPath string) (bool, error) { + isNotMnt, err := mount.IsNotMountPoint(mounter, stagingTargetPath) + if os.IsNotExist(err) { + err = nil + } + return isNotMnt, err +} + // addRbdManagerTask adds a ceph manager task to execute command // asynchronously. If command is not found returns a bool set to false // example arg ["trash", "remove","pool/image"].