Skip to content

Commit 0961635

Browse files
authored
Merge pull request #770 from andyzhangx/remove-parent-dir
fix: remove parent dir in DeleteVolume
2 parents f72a3b4 + ac10020 commit 0961635

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

pkg/nfs/controllerserver.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,15 @@ func (cs *ControllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVol
291291
}
292292
klog.V(2).Infof("archived subdirectory %s --> %s", internalVolumePath, archivedInternalVolumePath)
293293
} else {
294+
rootDir := getRootDir(nfsVol.subDir)
295+
if rootDir != "" {
296+
rootDir = filepath.Join(getInternalMountPath(cs.Driver.workingMountDir, nfsVol), rootDir)
297+
} else {
298+
rootDir = internalVolumePath
299+
}
294300
// delete subdirectory under base-dir
295-
klog.V(2).Infof("removing subdirectory at %v", internalVolumePath)
296-
if err = os.RemoveAll(internalVolumePath); err != nil {
301+
klog.V(2).Infof("removing subdirectory at %v on internalVolumePath %s", rootDir, internalVolumePath)
302+
if err = os.RemoveAll(rootDir); err != nil {
297303
return nil, status.Errorf(codes.Internal, "delete subdirectory(%s) failed with %v", internalVolumePath, err)
298304
}
299305
}

pkg/nfs/utils.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,9 @@ func waitForPathNotExistWithTimeout(path string, timeout time.Duration) error {
215215
time.Sleep(500 * time.Microsecond)
216216
}
217217
}
218+
219+
// getRootDir returns the root directory of the given directory
220+
func getRootDir(path string) string {
221+
parts := strings.Split(path, "/")
222+
return parts[0]
223+
}

pkg/nfs/utils_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,44 @@ func TestWaitForPathNotExistWithTimeout(t *testing.T) {
387387
}
388388
}
389389
}
390+
391+
func TestGetRootPath(t *testing.T) {
392+
tests := []struct {
393+
desc string
394+
dir string
395+
expected string
396+
}{
397+
{
398+
desc: "empty path",
399+
dir: "",
400+
expected: "",
401+
},
402+
{
403+
desc: "root path",
404+
dir: "/",
405+
expected: "",
406+
},
407+
{
408+
desc: "subdir path",
409+
dir: "/subdir",
410+
expected: "",
411+
},
412+
{
413+
desc: "subdir path without leading slash",
414+
dir: "subdir",
415+
expected: "subdir",
416+
},
417+
{
418+
desc: "multiple subdir path without leading slash",
419+
dir: "subdir/subdir2",
420+
expected: "subdir",
421+
},
422+
}
423+
424+
for _, test := range tests {
425+
result := getRootDir(test.dir)
426+
if result != test.expected {
427+
t.Errorf("Unexpected result: %s, expected: %s", result, test.expected)
428+
}
429+
}
430+
}

test/e2e/e2e_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ var (
6262
subDirStorageClassParameters = map[string]string{
6363
"server": nfsServerAddress,
6464
"share": nfsShare,
65-
"subDir": "subDirectory-${pvc.metadata.namespace}",
65+
"subDir": "${pvc.metadata.namespace}/${pvc.metadata.name}",
6666
"csi.storage.k8s.io/provisioner-secret-name": "mount-options",
6767
"csi.storage.k8s.io/provisioner-secret-namespace": "default",
6868
"mountPermissions": "0755",

0 commit comments

Comments
 (0)