Skip to content

Commit 047a69b

Browse files
committed
refactor: create mounter in outer scope for node publish
Using shared mounter across NodePublishVolume method.
1 parent f782651 commit 047a69b

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

pkg/hostpath/nodeserver.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ func (hp *hostPath) NodePublishVolume(ctx context.Context, req *csi.NodePublishV
6060
hp.mutex.Lock()
6161
defer hp.mutex.Unlock()
6262

63+
mounter := mount.New("")
64+
6365
// if ephemeral is specified, create volume here to avoid errors
6466
if ephemeralVolume {
6567
volID := req.GetVolumeId()
@@ -102,8 +104,6 @@ func (hp *hostPath) NodePublishVolume(ctx context.Context, req *csi.NodePublishV
102104
return nil, fmt.Errorf("failed to get the loop device: %w", err)
103105
}
104106

105-
mounter := mount.New("")
106-
107107
// Check if the target path exists. Create if not present.
108108
_, err = os.Lstat(targetPath)
109109
if os.IsNotExist(err) {
@@ -130,15 +130,15 @@ func (hp *hostPath) NodePublishVolume(ctx context.Context, req *csi.NodePublishV
130130
}
131131

132132
options := []string{"bind"}
133-
if err := mount.New("").Mount(loopDevice, targetPath, "", options); err != nil {
133+
if err := mounter.Mount(loopDevice, targetPath, "", options); err != nil {
134134
return nil, fmt.Errorf("failed to mount block device: %s at %s: %w", loopDevice, targetPath, err)
135135
}
136136
} else if req.GetVolumeCapability().GetMount() != nil {
137137
if vol.VolAccessType != state.MountAccess {
138138
return nil, status.Error(codes.InvalidArgument, "cannot publish a non-mount volume as mount volume")
139139
}
140140

141-
notMnt, err := mount.IsNotMountPoint(mount.New(""), targetPath)
141+
notMnt, err := mount.IsNotMountPoint(mounter, targetPath)
142142
if err != nil {
143143
if os.IsNotExist(err) {
144144
if err = os.Mkdir(targetPath, 0750); err != nil {
@@ -173,7 +173,6 @@ func (hp *hostPath) NodePublishVolume(ctx context.Context, req *csi.NodePublishV
173173
if readOnly {
174174
options = append(options, "ro")
175175
}
176-
mounter := mount.New("")
177176
path := hp.getVolumePath(volumeId)
178177

179178
if err := mounter.Mount(path, targetPath, "", options); err != nil {

0 commit comments

Comments
 (0)