Skip to content

Commit b472e1a

Browse files
committed
fix(node): add OS-specific HasMountReferences implementations to support Windows and Darwin
Refactored NodeUnstageVolume to use a platform-aware HasMountReferences() helper, moving Linux-specific /proc/mounts parsing into smb_common_linux.go and stubbing it out for Windows and Darwin to prevent test failures on non-Linux environments. - Fixes Windows e2e failures due to /proc/mounts not being available - Ensures future compatibility for multi-platform CSI driver builds - Preserves original Linux mount reference tracking behavior
1 parent c954b51 commit b472e1a

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

pkg/smb/nodeserver.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package smb
1818

1919
import (
20-
"bufio"
2120
"encoding/base64"
2221
"fmt"
2322
"os"
@@ -316,27 +315,12 @@ func (d *Driver) NodeUnstageVolume(_ context.Context, req *csi.NodeUnstageVolume
316315
}
317316
defer d.volumeLocks.Release(lockKey)
318317

319-
// Check if any other mounts still reference the staging path
320-
f, err := os.Open("/proc/mounts")
318+
inUse, err := HasMountReferences(stagingTargetPath)
321319
if err != nil {
322-
return nil, status.Errorf(codes.Internal, "failed to open /proc/mounts: %v", err)
323-
}
324-
defer f.Close()
325-
326-
scanner := bufio.NewScanner(f)
327-
refCount := 0
328-
for scanner.Scan() {
329-
line := scanner.Text()
330-
fields := strings.Fields(line)
331-
if len(fields) >= 2 {
332-
mountPoint := fields[1]
333-
if strings.HasPrefix(mountPoint, stagingTargetPath) && mountPoint != stagingTargetPath {
334-
refCount++
335-
}
336-
}
320+
return nil, status.Errorf(codes.Internal, "failed to check mount references: %v", err)
337321
}
338-
if refCount > 0 {
339-
klog.V(2).Infof("NodeUnstageVolume: staging path %s is still in use by %d other mounts", stagingTargetPath, refCount)
322+
if inUse {
323+
klog.V(2).Infof("NodeUnstageVolume: staging path %s is still in use by other mounts", stagingTargetPath)
340324
return &csi.NodeUnstageVolumeResponse{}, nil
341325
}
342326

pkg/smb/smb_common_darwin.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,8 @@ func prepareStagePath(path string, m *mount.SafeFormatAndMount) error {
4848
func Mkdir(m *mount.SafeFormatAndMount, name string, perm os.FileMode) error {
4949
return os.Mkdir(name, perm)
5050
}
51+
52+
func HasMountReferences(stagingTargetPath string) (bool, error) {
53+
// Stubbed for Windows/macOS — cannot inspect bind mounts
54+
return false, nil
55+
}

pkg/smb/smb_common_linux.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ limitations under the License.
2020
package smb
2121

2222
import (
23+
"bufio"
24+
"fmt"
2325
"os"
26+
"strings"
2427

2528
mount "k8s.io/mount-utils"
2629
)
@@ -48,3 +51,23 @@ func prepareStagePath(_ string, _ *mount.SafeFormatAndMount) error {
4851
func Mkdir(_ *mount.SafeFormatAndMount, name string, perm os.FileMode) error {
4952
return os.Mkdir(name, perm)
5053
}
54+
55+
func HasMountReferences(stagingTargetPath string) (bool, error) {
56+
f, err := os.Open("/proc/mounts")
57+
if err != nil {
58+
return false, fmt.Errorf("failed to open /proc/mounts: %v", err)
59+
}
60+
defer f.Close()
61+
62+
scanner := bufio.NewScanner(f)
63+
for scanner.Scan() {
64+
fields := strings.Fields(scanner.Text())
65+
if len(fields) >= 2 {
66+
mountPoint := fields[1]
67+
if strings.HasPrefix(mountPoint, stagingTargetPath) && mountPoint != stagingTargetPath {
68+
return true, nil
69+
}
70+
}
71+
}
72+
return false, nil
73+
}

pkg/smb/smb_common_windows.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,8 @@ func Mkdir(m *mount.SafeFormatAndMount, name string, perm os.FileMode) error {
8787
}
8888
return fmt.Errorf("could not cast to csi proxy class")
8989
}
90+
91+
func HasMountReferences(stagingTargetPath string) (bool, error) {
92+
// Stubbed for Windows/macOS — cannot inspect bind mounts
93+
return false, nil
94+
}

0 commit comments

Comments
 (0)