Skip to content

Commit ade197c

Browse files
committed
fix: remove parent dir in DeleteVolume
1 parent f72a3b4 commit ade197c

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
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+
}

0 commit comments

Comments
 (0)