Skip to content

Commit 35b085e

Browse files
committed
fix: mounting the same volume more than once doesn't work
1 parent f1a9c63 commit 35b085e

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

cmd/plugin/node_server.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (n NodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublishV
145145
return
146146
}
147147

148-
if mountstatus.Get(namedRef) == mountstatus.Mounted {
148+
if mountstatus.Get(req.VolumeId) == mountstatus.Mounted {
149149
return &csi.NodePublishVolumeResponse{}, nil
150150
}
151151

@@ -193,6 +193,11 @@ func (n NodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpubl
193193
return
194194
}
195195

196+
// Clear the mountstatus since the volume has been unmounted
197+
// Not doing this will make mount not work properly if the same volume is
198+
// attempted to mount twice
199+
mountstatus.Delete(req.VolumeId)
200+
196201
return &csi.NodeUnpublishVolumeResponse{}, nil
197202
}
198203

pkg/mountexecutor/mountexecutor.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func NewMountExecutor(o *MountExecutorOptions) *MountExecutor {
5858
// StartMounting starts the mounting
5959
func (m *MountExecutor) StartMounting(o *MountOptions) error {
6060

61-
if pullstatus.Get(o.NamedRef) != pullstatus.Pulled || mountstatus.Get(o.NamedRef) == mountstatus.StillMounting {
61+
if pullstatus.Get(o.NamedRef) != pullstatus.Pulled || mountstatus.Get(o.VolumeId) == mountstatus.StillMounting {
6262
return nil
6363
}
6464

@@ -67,12 +67,12 @@ func (m *MountExecutor) StartMounting(o *MountOptions) error {
6767
o.VolumeCapability.AccessMode.Mode == csi.VolumeCapability_AccessMode_MULTI_NODE_READER_ONLY
6868

6969
if !m.asyncMount {
70-
mountstatus.Update(o.NamedRef, mountstatus.StillMounting)
70+
mountstatus.Update(o.VolumeId, mountstatus.StillMounting)
7171
if err := m.mounter.Mount(o.Context, o.VolumeId, backend.MountTarget(o.TargetPath), o.NamedRef, ro); err != nil {
72-
mountstatus.Update(o.NamedRef, mountstatus.Errored)
72+
mountstatus.Update(o.VolumeId, mountstatus.Errored)
7373
return err
7474
}
75-
mountstatus.Update(o.NamedRef, mountstatus.Mounted)
75+
mountstatus.Update(o.VolumeId, mountstatus.Mounted)
7676
return nil
7777
}
7878

@@ -81,15 +81,15 @@ func (m *MountExecutor) StartMounting(o *MountOptions) error {
8181
ctx, cancel := context.WithTimeout(context.Background(), mountCtxTimeout)
8282
defer cancel()
8383

84-
mountstatus.Update(o.NamedRef, mountstatus.StillMounting)
84+
mountstatus.Update(o.VolumeId, mountstatus.StillMounting)
8585
if err := m.mounter.Mount(ctx, o.VolumeId, backend.MountTarget(o.TargetPath), o.NamedRef, ro); err != nil {
8686
klog.Errorf("mount err: %v", err.Error())
87-
mountstatus.Update(o.NamedRef, mountstatus.Errored)
87+
mountstatus.Update(o.VolumeId, mountstatus.Errored)
8888
m.asyncErrs[o.NamedRef] = fmt.Errorf("err: %v: %v", err, m.asyncErrs[o.NamedRef])
8989
m.mutex.Unlock()
9090
return
9191
}
92-
mountstatus.Update(o.NamedRef, mountstatus.Mounted)
92+
mountstatus.Update(o.VolumeId, mountstatus.Mounted)
9393
m.mutex.Unlock()
9494
}()
9595

@@ -107,7 +107,7 @@ func (m *MountExecutor) WaitForMount(o *MountOptions) error {
107107
}
108108

109109
mountCondFn := func() (done bool, err error) {
110-
if mountstatus.Get(o.NamedRef) == mountstatus.Mounted {
110+
if mountstatus.Get(o.VolumeId) == mountstatus.Mounted {
111111
return true, nil
112112
}
113113
if m.asyncErrs[o.NamedRef] != nil {

pkg/mountstatus/mountstatus.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package mountstatus
22

33
import (
44
"sync"
5-
6-
"github.com/containerd/containerd/reference/docker"
75
)
86

97
// ImagePullStatus represents mount status of an image
@@ -23,43 +21,43 @@ const (
2321

2422
// ImageMountStatusRecorder records the status of image mounts
2523
type ImageMountStatusRecorder struct {
26-
status map[docker.Named]ImageMountStatus
24+
status map[string]ImageMountStatus
2725
mutex sync.Mutex
2826
}
2927

3028
var i ImageMountStatusRecorder
3129

3230
func init() {
3331
i = ImageMountStatusRecorder{
34-
status: make(map[docker.Named]ImageMountStatus),
32+
status: make(map[string]ImageMountStatus),
3533
mutex: sync.Mutex{},
3634
}
3735
}
3836

3937
// Update updates the mount status of an image
40-
func Update(imageRef docker.Named, status ImageMountStatus) {
38+
func Update(volumeId string, status ImageMountStatus) {
4139
i.mutex.Lock()
4240
defer i.mutex.Unlock()
4341

44-
i.status[imageRef] = status
42+
i.status[volumeId] = status
4543
}
4644

4745
// Delete deletes the mount status of an image
48-
func Delete(imageRef docker.Named) {
46+
func Delete(volumeId string) {
4947
i.mutex.Lock()
5048
defer i.mutex.Unlock()
5149

52-
delete(i.status, imageRef)
50+
delete(i.status, volumeId)
5351
}
5452

5553
// Get gets the mount status of an image
56-
func Get(imageRef docker.Named) ImageMountStatus {
54+
func Get(volumeId string) ImageMountStatus {
5755
i.mutex.Lock()
5856
defer i.mutex.Unlock()
5957

60-
if _, ok := i.status[imageRef]; !ok {
58+
if _, ok := i.status[volumeId]; !ok {
6159
return StatusNotFound
6260
}
6361

64-
return i.status[imageRef]
62+
return i.status[volumeId]
6563
}

0 commit comments

Comments
 (0)