fix: remove parent dir in DeleteVolume

This commit is contained in:
andyzhangx 2024-09-15 02:54:19 +00:00
parent f72a3b4cee
commit ade197cd1d
3 changed files with 55 additions and 2 deletions

View File

@ -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)
}
}

View File

@ -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]
}

View File

@ -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)
}
}
}