fix: delete volume failure in archive mode when subDir contains /

This commit is contained in:
andyzhangx 2024-02-04 15:07:20 +00:00
parent dce3f89e62
commit 96ddd193dc
3 changed files with 40 additions and 1 deletions

View File

@ -244,6 +244,13 @@ func (cs *ControllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVol
if strings.EqualFold(nfsVol.onDelete, archive) { if strings.EqualFold(nfsVol.onDelete, archive) {
archivedInternalVolumePath := filepath.Join(getInternalMountPath(cs.Driver.workingMountDir, nfsVol), "archived-"+nfsVol.subDir) archivedInternalVolumePath := filepath.Join(getInternalMountPath(cs.Driver.workingMountDir, nfsVol), "archived-"+nfsVol.subDir)
if strings.Contains(nfsVol.subDir, "/") {
parentDir := filepath.Dir(archivedInternalVolumePath)
klog.V(2).Infof("DeleteVolume: subdirectory(%s) contains '/', make sure the parent directory(%s) exists", nfsVol.subDir, parentDir)
if err = os.MkdirAll(parentDir, 0777); err != nil {
return nil, status.Errorf(codes.Internal, "create parent directory(%s) of %s failed with %v", parentDir, archivedInternalVolumePath, err.Error())
}
}
// archive subdirectory under base-dir // archive subdirectory under base-dir
klog.V(2).Infof("archiving subdirectory %s --> %s", internalVolumePath, archivedInternalVolumePath) klog.V(2).Infof("archiving subdirectory %s --> %s", internalVolumePath, archivedInternalVolumePath)

View File

@ -351,7 +351,7 @@ var _ = ginkgo.Describe("Dynamic Provisioning", func() {
test.Run(ctx, cs, ns) test.Run(ctx, cs, ns)
}) })
ginkgo.It("should create a volume on demand with archive subdir on delete [nfs.csi.k8s.io]", func(ctx ginkgo.SpecContext) { ginkgo.It("should create a volume on demand with archive on delete [nfs.csi.k8s.io]", func(ctx ginkgo.SpecContext) {
pods := []testsuites.PodDetails{ pods := []testsuites.PodDetails{
{ {
Cmd: "echo 'hello world' > /mnt/test-1/data && grep 'hello world' /mnt/test-1/data", Cmd: "echo 'hello world' > /mnt/test-1/data && grep 'hello world' /mnt/test-1/data",
@ -373,4 +373,27 @@ var _ = ginkgo.Describe("Dynamic Provisioning", func() {
} }
test.Run(ctx, cs, ns) test.Run(ctx, cs, ns)
}) })
ginkgo.It("should create a volume on demand with archive subdir on delete [nfs.csi.k8s.io]", func(ctx ginkgo.SpecContext) {
pods := []testsuites.PodDetails{
{
Cmd: "echo 'hello world' > /mnt/test-1/data && grep 'hello world' /mnt/test-1/data",
Volumes: []testsuites.VolumeDetails{
{
ClaimSize: "10Gi",
VolumeMount: testsuites.VolumeMountDetails{
NameGenerate: "test-volume-",
MountPathGenerate: "/mnt/test-",
},
},
},
},
}
test := testsuites.DynamicallyProvisionedCmdVolumeTest{
CSIDriver: testDriver,
Pods: pods,
StorageClassParameters: archiveSubDirStorageClassParameters,
}
test.Run(ctx, cs, ns)
})
}) })

View File

@ -83,6 +83,15 @@ var (
"mountPermissions": "0755", "mountPermissions": "0755",
"onDelete": "archive", "onDelete": "archive",
} }
archiveSubDirStorageClassParameters = map[string]string{
"server": nfsServerAddress,
"share": nfsShare,
"subDir": "${pvc.metadata.namespace}/${pvc.metadata.name}",
"csi.storage.k8s.io/provisioner-secret-name": "mount-options",
"csi.storage.k8s.io/provisioner-secret-namespace": "default",
"mountPermissions": "0755",
"onDelete": "archive",
}
controllerServer *nfs.ControllerServer controllerServer *nfs.ControllerServer
) )