Skip to content

Commit

Permalink
Merge pull request #850 from andyzhangx/remove-parent-dir
Browse files Browse the repository at this point in the history
fix: remove parent dir in DeleteVolume
  • Loading branch information
andyzhangx authored Sep 15, 2024
2 parents 679857c + b6ded24 commit 902fe90
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
9 changes: 8 additions & 1 deletion pkg/smb/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/smb/smb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
41 changes: 41 additions & 0 deletions pkg/smb/smb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
2 changes: 1 addition & 1 deletion test/e2e/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 902fe90

Please sign in to comment.