fix: remove parent dir in DeleteVolume
This commit is contained in:
parent
f72a3b4cee
commit
ade197cd1d
@ -291,9 +291,15 @@ func (cs *ControllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVol
|
|||||||
}
|
}
|
||||||
klog.V(2).Infof("archived subdirectory %s --> %s", internalVolumePath, archivedInternalVolumePath)
|
klog.V(2).Infof("archived subdirectory %s --> %s", internalVolumePath, archivedInternalVolumePath)
|
||||||
} else {
|
} else {
|
||||||
|
rootDir := getRootDir(nfsVol.subDir)
|
||||||
|
if rootDir != "" {
|
||||||
|
rootDir = filepath.Join(getInternalMountPath(cs.Driver.workingMountDir, nfsVol), rootDir)
|
||||||
|
} else {
|
||||||
|
rootDir = internalVolumePath
|
||||||
|
}
|
||||||
// delete subdirectory under base-dir
|
// delete subdirectory under base-dir
|
||||||
klog.V(2).Infof("removing subdirectory at %v", internalVolumePath)
|
klog.V(2).Infof("removing subdirectory at %v on internalVolumePath %s", rootDir, internalVolumePath)
|
||||||
if err = os.RemoveAll(internalVolumePath); err != nil {
|
if err = os.RemoveAll(rootDir); err != nil {
|
||||||
return nil, status.Errorf(codes.Internal, "delete subdirectory(%s) failed with %v", internalVolumePath, err)
|
return nil, status.Errorf(codes.Internal, "delete subdirectory(%s) failed with %v", internalVolumePath, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -215,3 +215,9 @@ func waitForPathNotExistWithTimeout(path string, timeout time.Duration) error {
|
|||||||
time.Sleep(500 * time.Microsecond)
|
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]
|
||||||
|
}
|
||||||
|
|||||||
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user