From ade197cd1d1bbd43ba9acb09188226cc13beb5d6 Mon Sep 17 00:00:00 2001 From: andyzhangx Date: Sun, 15 Sep 2024 02:54:19 +0000 Subject: [PATCH 1/2] fix: remove parent dir in DeleteVolume --- pkg/nfs/controllerserver.go | 10 +++++++-- pkg/nfs/utils.go | 6 ++++++ pkg/nfs/utils_test.go | 41 +++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/pkg/nfs/controllerserver.go b/pkg/nfs/controllerserver.go index 95ac0448d..39a38e675 100644 --- a/pkg/nfs/controllerserver.go +++ b/pkg/nfs/controllerserver.go @@ -291,9 +291,15 @@ func (cs *ControllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVol } klog.V(2).Infof("archived subdirectory %s --> %s", internalVolumePath, archivedInternalVolumePath) } else { + rootDir := getRootDir(nfsVol.subDir) + if rootDir != "" { + rootDir = filepath.Join(getInternalMountPath(cs.Driver.workingMountDir, nfsVol), rootDir) + } else { + rootDir = internalVolumePath + } // delete subdirectory under base-dir - klog.V(2).Infof("removing subdirectory at %v", internalVolumePath) - if err = os.RemoveAll(internalVolumePath); err != nil { + klog.V(2).Infof("removing subdirectory at %v on internalVolumePath %s", rootDir, internalVolumePath) + if err = os.RemoveAll(rootDir); err != nil { return nil, status.Errorf(codes.Internal, "delete subdirectory(%s) failed with %v", internalVolumePath, err) } } diff --git a/pkg/nfs/utils.go b/pkg/nfs/utils.go index d0c202e6e..3e54923e6 100644 --- a/pkg/nfs/utils.go +++ b/pkg/nfs/utils.go @@ -215,3 +215,9 @@ func waitForPathNotExistWithTimeout(path string, timeout time.Duration) error { time.Sleep(500 * time.Microsecond) } } + +// getRootDir returns the root directory of the given directory +func getRootDir(path string) string { + parts := strings.Split(path, "/") + return parts[0] +} diff --git a/pkg/nfs/utils_test.go b/pkg/nfs/utils_test.go index 34f3b3e25..1d8430858 100644 --- a/pkg/nfs/utils_test.go +++ b/pkg/nfs/utils_test.go @@ -387,3 +387,44 @@ func TestWaitForPathNotExistWithTimeout(t *testing.T) { } } } + +func TestGetRootPath(t *testing.T) { + tests := []struct { + desc string + dir string + expected string + }{ + { + desc: "empty path", + dir: "", + expected: "", + }, + { + desc: "root path", + dir: "/", + expected: "", + }, + { + desc: "subdir path", + dir: "/subdir", + expected: "", + }, + { + desc: "subdir path without leading slash", + dir: "subdir", + expected: "subdir", + }, + { + desc: "multiple subdir path without leading slash", + dir: "subdir/subdir2", + expected: "subdir", + }, + } + + for _, test := range tests { + result := getRootDir(test.dir) + if result != test.expected { + t.Errorf("Unexpected result: %s, expected: %s", result, test.expected) + } + } +} From ac100207b465d0ea49558e4df063daf21bfb7d79 Mon Sep 17 00:00:00 2001 From: andyzhangx Date: Sun, 15 Sep 2024 04:11:38 +0000 Subject: [PATCH 2/2] test: change one e2e test --- test/e2e/e2e_suite_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/e2e_suite_test.go b/test/e2e/e2e_suite_test.go index 8142cea91..fddf1f89f 100644 --- a/test/e2e/e2e_suite_test.go +++ b/test/e2e/e2e_suite_test.go @@ -62,7 +62,7 @@ var ( subDirStorageClassParameters = map[string]string{ "server": nfsServerAddress, "share": nfsShare, - "subDir": "subDirectory-${pvc.metadata.namespace}", + "subDir": "${pvc.metadata.namespace}/${pvc.metadata.name}", "csi.storage.k8s.io/provisioner-secret-name": "mount-options", "csi.storage.k8s.io/provisioner-secret-namespace": "default", "mountPermissions": "0755",