From ff95639565d8d87c3670e55a4e1b8af7818c42e4 Mon Sep 17 00:00:00 2001 From: sys-liqian Date: Fri, 22 Dec 2023 22:28:14 -0500 Subject: [PATCH] fix: :bug: fix umount webdav server fix umount webdav server Signed-off-by: sys-liqian --- Makefile | 17 +++++++++++++++++ examples/csi-webdav-storageclass.yaml | 2 +- pkg/webdav/controller.go | 20 ++++++++++++-------- pkg/webdav/node.go | 15 +++++++++++++-- 4 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b9befb6 --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +GIT_COMMIT = $(shell git rev-parse HEAD) +BUILD_DATE = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ") + +PKG = github.com/sys-liqian/csi-driver-webdav +LDFLAGS = -X ${PKG}/pkg/webdav.driverVersion=${IMAGE_VERSION} -X ${PKG}/pkg/webdav.gitCommit=${GIT_COMMIT} -X ${PKG}/pkg/webdav.buildDate=${BUILD_DATE} +EXT_LDFLAGS = -s -w -extldflags "-static" + +IMAGE_VERSION ?= v0.0.1 +LOCAL_REPOSITORY ?= localhost:5000 + +.PHONY: go-build +go-build: + CGO_ENABLED=0 GOOS=linux GOARCH=$(ARCH) go build -a -ldflags "${LDFLAGS} ${EXT_LDFLAGS}" -o bin/webdavplugin ./cmd/webdav + +.PHONY: build-local-image +build-local-image: + docker build --network host -t $(LOCAL_REPOSITORY)/webdavplugin:$(IMAGE_VERSION) . \ No newline at end of file diff --git a/examples/csi-webdav-storageclass.yaml b/examples/csi-webdav-storageclass.yaml index 9709aba..9ad0955 100644 --- a/examples/csi-webdav-storageclass.yaml +++ b/examples/csi-webdav-storageclass.yaml @@ -5,7 +5,7 @@ metadata: name: webdav-sc provisioner: webdav.csi.io parameters: - share: http://172.25.16.2/remote.php/dav/files/admin + share: http://172.25.16.2:5244/dav/data csi.storage.k8s.io/provisioner-secret-name: "webdav-secrect" csi.storage.k8s.io/provisioner-secret-namespace: "default" csi.storage.k8s.io/node-publish-secret-name: "webdav-secrect" diff --git a/pkg/webdav/controller.go b/pkg/webdav/controller.go index e803601..7ca2485 100644 --- a/pkg/webdav/controller.go +++ b/pkg/webdav/controller.go @@ -101,6 +101,12 @@ func (c *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolu return nil, status.Errorf(codes.Internal, "failed to make subdirectory: %v", err.Error()) } + defer func() { + if err = c.mounter.Unmount(targetPath); err != nil { + klog.Warningf("failed to unmount targetpath %s: %v", targetPath, err.Error()) + } + }() + if mountPermissions > 0 { // Reset directory permissions because of umask problems if err = os.Chmod(internalVolumePath, os.FileMode(mountPermissions)); err != nil { @@ -108,10 +114,6 @@ func (c *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolu } } - if err = c.mounter.Unmount(targetPath); err != nil { - return nil, status.Errorf(codes.Internal, "failed to unmount targetpath %s %v", targetPath, err) - } - return &csi.CreateVolumeResponse{ Volume: &csi.Volume{ VolumeId: MakeVolumeId(sourcePath, req.Name), @@ -142,16 +144,18 @@ func (c *ControllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVolu return nil, status.Errorf(codes.Internal, fmt.Sprintf("mount failed: %v", err.Error())) } + defer func() { + if err = c.mounter.Unmount(targetPath); err != nil { + klog.Warningf("failed to unmount targetpath %s: %v", targetPath, err.Error()) + } + }() + internalVolumePath := filepath.Join(targetPath, subDir) klog.V(2).Infof("Removing subdirectory at %v", internalVolumePath) if err = os.RemoveAll(internalVolumePath); err != nil { return nil, status.Errorf(codes.Internal, "failed to delete subdirectory: %v", err.Error()) } - if err = c.mounter.Unmount(targetPath); err != nil { - return nil, status.Errorf(codes.Internal, "failed to unmount targetpath %s %v", targetPath, err) - } - return &csi.DeleteVolumeResponse{}, nil } diff --git a/pkg/webdav/node.go b/pkg/webdav/node.go index 83d822f..5db49f4 100644 --- a/pkg/webdav/node.go +++ b/pkg/webdav/node.go @@ -110,9 +110,20 @@ func (n *NodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpub if len(targetPath) == 0 { return nil, status.Error(codes.InvalidArgument, "Target path missing in request") } - klog.V(2).Infof("NodeUnpublishVolume: unmounting volume %s on %s", volumeID, targetPath) - err := n.mounter.Unmount(targetPath) + notMnt, err := n.mounter.IsLikelyNotMountPoint(targetPath) + if err != nil { + if os.IsNotExist(err) { + return nil, status.Error(codes.NotFound, "Targetpath not found") + } + return nil, status.Error(codes.Internal, err.Error()) + } + if notMnt { + return &csi.NodeUnpublishVolumeResponse{}, nil + } + + klog.V(2).Infof("NodeUnpublishVolume: unmounting volume %s on %s", volumeID, targetPath) + err = n.mounter.Unmount(targetPath) if err != nil { return nil, status.Errorf(codes.Internal, "failed to unmount target %q: %v", targetPath, err) }