Skip to content

Commit 902fe90

Browse files
authored
Merge pull request #850 from andyzhangx/remove-parent-dir
fix: remove parent dir in DeleteVolume
2 parents 679857c + b6ded24 commit 902fe90

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

pkg/smb/controllerserver.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,14 @@ func (d *Driver) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest)
232232
return nil, status.Errorf(codes.Internal, "archive subdirectory(%s, %s) failed with %v", internalVolumePath, archivedInternalVolumePath, err)
233233
}
234234
} else {
235-
klog.V(2).Infof("Removing subdirectory at %v", internalVolumePath)
235+
rootDir := getRootDir(smbVol.subDir)
236+
if rootDir != "" {
237+
rootDir = filepath.Join(getInternalMountPath(d.workingMountDir, smbVol), rootDir)
238+
} else {
239+
rootDir = internalVolumePath
240+
}
241+
242+
klog.V(2).Infof("removing subdirectory at %v on internalVolumePath %s", rootDir, internalVolumePath)
236243
if err = os.RemoveAll(internalVolumePath); err != nil {
237244
return nil, status.Errorf(codes.Internal, "failed to delete subdirectory: %v", err)
238245
}

pkg/smb/smb.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,9 @@ func appendMountOptions(mountOptions []string, extraMountOptions map[string]stri
263263
}
264264
return allMountOptions
265265
}
266+
267+
// getRootDir returns the root directory of the given directory
268+
func getRootDir(path string) string {
269+
parts := strings.Split(path, "/")
270+
return parts[0]
271+
}

pkg/smb/smb_test.go

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

test/e2e/suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ var (
6363
}
6464
subDirStorageClassParameters = map[string]string{
6565
"source": getSmbTestEnvVarValue(testSmbSourceEnvVar, defaultSmbSource),
66-
"subDir": "subDirectory-${pvc.metadata.name}",
66+
"subDir": "${pvc.metadata.namespace}/${pvc.metadata.name}",
6767
"csi.storage.k8s.io/provisioner-secret-name": getSmbTestEnvVarValue(testSmbSecretNameEnvVar, defaultSmbSecretName),
6868
"csi.storage.k8s.io/provisioner-secret-namespace": getSmbTestEnvVarValue(testSmbSecretNamespaceEnvVar, defaultSmbSecretNamespace),
6969
"csi.storage.k8s.io/node-stage-secret-name": getSmbTestEnvVarValue(testSmbSecretNameEnvVar, defaultSmbSecretName),

0 commit comments

Comments
 (0)