Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove parent dir in DeleteVolume #850

Merged
merged 2 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading