Skip to content

Commit f3539ee

Browse files
committed
noot
1 parent 124ae23 commit f3539ee

File tree

5 files changed

+69
-29
lines changed

5 files changed

+69
-29
lines changed

pkg/collector/diskstats.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
var volumeLabels_ = []string{
1313
"device",
14+
"csi_device",
1415
"pvc",
1516
"namespace",
1617
"pv",
@@ -93,6 +94,7 @@ func (d *DiskstatsCollector) Update(volumes []*discovery.VolumeInfo, ch chan<- p
9394
func volumeLabels(vol *discovery.VolumeInfo) []string {
9495
return []string{
9596
vol.DeviceName,
97+
vol.CSIDevicePath,
9698
vol.PVCName,
9799
vol.PVCNamespace,
98100
vol.PVName,

pkg/discovery/csi.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,22 @@ func (d *CSIDiscoverer) discoverCSIVolumes(ctx context.Context, podUID, csiDir s
112112
continue
113113
}
114114

115-
deviceName, _ := mounts.GetDeviceName(mount.Device)
115+
// Resolve symlinks to get actual device for diskstats
116+
resolvedPath, deviceName := mounts.ResolveDevice(mount.Device)
116117

117118
vol := &VolumeInfo{
118-
PVName: volData.VolumeName,
119-
PVCName: extractPVCName(volData.VolumeName),
120-
PVCNamespace: volData.PodNamespace,
121-
PodName: volData.PodName,
122-
PodNamespace: volData.PodNamespace,
123-
PodUID: podUID,
124-
CSIDriver: volData.DriverName,
125-
VolumeHandle: volData.VolumeHandle,
126-
DevicePath: mount.Device,
127-
DeviceName: deviceName,
128-
MountPath: mountPath,
119+
PVName: volData.VolumeName,
120+
PVCName: extractPVCName(volData.VolumeName),
121+
PVCNamespace: volData.PodNamespace,
122+
PodName: volData.PodName,
123+
PodNamespace: volData.PodNamespace,
124+
PodUID: podUID,
125+
CSIDriver: volData.DriverName,
126+
VolumeHandle: volData.VolumeHandle,
127+
CSIDevicePath: mount.Device,
128+
DevicePath: resolvedPath,
129+
DeviceName: deviceName,
130+
MountPath: mountPath,
129131
}
130132

131133
volumes = append(volumes, vol)

pkg/discovery/k8sapi.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ func (d *K8sAPIDiscoverer) Discover(ctx context.Context) ([]*VolumeInfo, error)
164164
continue
165165
}
166166

167-
deviceName, _ := mounts.GetDeviceName(mount.Device)
167+
// Resolve symlinks to get actual device for diskstats
168+
resolvedPath, deviceName := mounts.ResolveDevice(mount.Device)
168169

169170
// Find container mount path
170171
containerMountPath := findContainerMountPath(&pod, vol.Name)
@@ -178,7 +179,8 @@ func (d *K8sAPIDiscoverer) Discover(ctx context.Context) ([]*VolumeInfo, error)
178179
PodName: pod.Name,
179180
PodNamespace: pod.Namespace,
180181
PodUID: string(pod.UID),
181-
DevicePath: mount.Device,
182+
CSIDevicePath: mount.Device,
183+
DevicePath: resolvedPath,
182184
DeviceName: deviceName,
183185
MountPath: mountPath,
184186
ContainerMountPath: containerMountPath,

pkg/discovery/types.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ type VolumeInfo struct {
2020
VolumeHandle string // CSI volume handle / cloud provider volume ID
2121

2222
// Node-local info
23-
DevicePath string // e.g., /dev/sda
24-
DeviceName string // e.g., sda
23+
DevicePath string // resolved device path, e.g., /dev/sda
24+
DeviceName string // device name for diskstats, e.g., sda
25+
CSIDevicePath string // original CSI device path, e.g., /dev/disk/by-id/scsi-0DO_Volume_...
2526
MountPath string // host path, e.g., /var/lib/kubelet/pods/.../volumes/...
2627
ContainerMountPath string // path inside container, e.g., /data
2728
}

pkg/mounts/mounts.go

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,24 +95,57 @@ func GetCapacity(mountPoint string) (*Capacity, error) {
9595
}, nil
9696
}
9797

98-
// GetDeviceName extracts the base device name from a device path
99-
// e.g., /dev/sda1 -> sda1, /dev/mapper/foo -> dm-X (via symlink resolution)
100-
func GetDeviceName(devicePath string) (string, error) {
101-
// Resolve symlinks
102-
resolved, err := os.Readlink(devicePath)
98+
// ResolveDevice resolves a device path (following symlinks) and returns both
99+
// the resolved path and the device name for diskstats
100+
func ResolveDevice(devicePath string) (resolvedPath, deviceName string) {
101+
// Try to fully resolve symlinks
102+
resolved, err := evalSymlinks(devicePath)
103103
if err != nil {
104-
// Not a symlink, use basename
105-
parts := strings.Split(devicePath, "/")
106-
return parts[len(parts)-1], nil
104+
resolved = devicePath
107105
}
108106

109-
// If relative path, it's relative to /dev
110-
if !strings.HasPrefix(resolved, "/") {
111-
resolved = "/dev/" + resolved
107+
// Extract device name (basename)
108+
parts := strings.Split(resolved, "/")
109+
name := parts[len(parts)-1]
110+
111+
return resolved, name
112+
}
113+
114+
// evalSymlinks resolves all symlinks in a path
115+
func evalSymlinks(path string) (string, error) {
116+
// Use filepath.EvalSymlinks equivalent
117+
for i := 0; i < 255; i++ { // limit iterations to prevent infinite loops
118+
fi, err := os.Lstat(path)
119+
if err != nil {
120+
return path, err
121+
}
122+
123+
if fi.Mode()&os.ModeSymlink == 0 {
124+
return path, nil
125+
}
126+
127+
target, err := os.Readlink(path)
128+
if err != nil {
129+
return path, err
130+
}
131+
132+
if !strings.HasPrefix(target, "/") {
133+
// Relative symlink - resolve relative to parent dir
134+
dir := path[:strings.LastIndex(path, "/")+1]
135+
path = dir + target
136+
} else {
137+
path = target
138+
}
112139
}
113140

114-
parts := strings.Split(resolved, "/")
115-
return parts[len(parts)-1], nil
141+
return path, fmt.Errorf("too many symlinks")
142+
}
143+
144+
// GetDeviceName extracts the base device name from a device path
145+
// e.g., /dev/sda1 -> sda1, /dev/mapper/foo -> dm-X (via symlink resolution)
146+
func GetDeviceName(devicePath string) (string, error) {
147+
_, name := ResolveDevice(devicePath)
148+
return name, nil
116149
}
117150

118151
// FindMountByPath finds a mount that contains the given path

0 commit comments

Comments
 (0)