diff --git a/pkg/smb/controllerserver.go b/pkg/smb/controllerserver.go index 5f443084935..f0ccf298dbc 100644 --- a/pkg/smb/controllerserver.go +++ b/pkg/smb/controllerserver.go @@ -232,7 +232,14 @@ func (d *Driver) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest) return nil, status.Errorf(codes.Internal, "archive subdirectory(%s, %s) failed with %v", internalVolumePath, archivedInternalVolumePath, err) } } else { - klog.V(2).Infof("Removing subdirectory at %v", internalVolumePath) + rootDir := getRootDir(smbVol.subDir) + if rootDir != "" { + rootDir = filepath.Join(getInternalMountPath(d.workingMountDir, smbVol), rootDir) + } else { + rootDir = internalVolumePath + } + + klog.V(2).Infof("removing subdirectory at %v on internalVolumePath %s", rootDir, internalVolumePath) if err = os.RemoveAll(internalVolumePath); err != nil { return nil, status.Errorf(codes.Internal, "failed to delete subdirectory: %v", err) } diff --git a/pkg/smb/smb.go b/pkg/smb/smb.go index d2d72c4e202..e51ec591be4 100644 --- a/pkg/smb/smb.go +++ b/pkg/smb/smb.go @@ -263,3 +263,9 @@ func appendMountOptions(mountOptions []string, extraMountOptions map[string]stri } return allMountOptions } + +// 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/smb/smb_test.go b/pkg/smb/smb_test.go index ee0a98d3bea..090652a7966 100644 --- a/pkg/smb/smb_test.go +++ b/pkg/smb/smb_test.go @@ -379,3 +379,44 @@ func TestAppendMountOptions(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) + } + } +} diff --git a/test/e2e/suite_test.go b/test/e2e/suite_test.go index 560eab91a3f..0f39b65e312 100644 --- a/test/e2e/suite_test.go +++ b/test/e2e/suite_test.go @@ -63,7 +63,7 @@ var ( } subDirStorageClassParameters = map[string]string{ "source": getSmbTestEnvVarValue(testSmbSourceEnvVar, defaultSmbSource), - "subDir": "subDirectory-${pvc.metadata.name}", + "subDir": "${pvc.metadata.namespace}/${pvc.metadata.name}", "csi.storage.k8s.io/provisioner-secret-name": getSmbTestEnvVarValue(testSmbSecretNameEnvVar, defaultSmbSecretName), "csi.storage.k8s.io/provisioner-secret-namespace": getSmbTestEnvVarValue(testSmbSecretNamespaceEnvVar, defaultSmbSecretNamespace), "csi.storage.k8s.io/node-stage-secret-name": getSmbTestEnvVarValue(testSmbSecretNameEnvVar, defaultSmbSecretName),