properly strip path.rootfs from mountpoint labels (#1421)

Change-type: patch
Connects-to: #1418
Signed-off-by: dt-rush <nickp@balena.io>
This commit is contained in:
dt-rush 2019-07-19 14:51:17 +00:00 committed by Ben Kochie
parent d8e47a9f9f
commit 5d3e2ce2ef
7 changed files with 44 additions and 3 deletions

View File

@ -17,6 +17,7 @@
* [ENHANCEMENT] Include additional XFS runtime statistics. #1423
* [BUGFIX] Renamed label `state` to `name` on `node_systemd_service_restart_total`. #1393
* [BUGFIX] Fix netdev nil reference on Darwin #1414
* [BUGFIX] Strip path.rootfs from mountpoint labels #1421
## 0.18.1 / 2019-06-04

View File

@ -69,7 +69,7 @@ func (c *filesystemCollector) GetStats() (stats []filesystemStats, err error) {
stats = append(stats, filesystemStats{
labels: filesystemLabels{
device: device,
mountPoint: mountpoint,
mountPoint: rootfsStripPrefix(mountpoint),
fsType: fstype,
},
size: float64(mnt[i].f_blocks) * float64(mnt[i].f_bsize),

View File

@ -73,7 +73,7 @@ func (c *filesystemCollector) GetStats() ([]filesystemStats, error) {
stats = append(stats, filesystemStats{
labels: filesystemLabels{
device: device,
mountPoint: mountpoint,
mountPoint: rootfsStripPrefix(mountpoint),
fsType: fstype,
},
size: float64(fs.Blocks) * float64(fs.Bsize),

View File

@ -165,7 +165,7 @@ func parseFilesystemLabels(r io.Reader) ([]filesystemLabels, error) {
filesystems = append(filesystems, filesystemLabels{
device: parts[0],
mountPoint: parts[1],
mountPoint: rootfsStripPrefix(parts[1]),
fsType: parts[2],
options: parts[3],
})

View File

@ -112,3 +112,30 @@ func TestMountsFallback(t *testing.T) {
}
}
}
func TestPathRootfs(t *testing.T) {
if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "./fixtures_bindmount/proc", "--path.rootfs", "/host"}); err != nil {
t.Fatal(err)
}
expected := map[string]string{
// should modify these mountpoints (removes /host, see fixture proc file)
"/media/volume1": "",
"/media/volume2": "",
// should not modify these mountpoints
"/dev/shm": "",
"/run/lock": "",
"/sys/fs/cgroup": "",
}
filesystems, err := mountPointDetails()
if err != nil {
t.Log(err)
}
for _, fs := range filesystems {
if _, ok := expected[fs.mountPoint]; !ok {
t.Errorf("Got unexpected %s", fs.mountPoint)
}
}
}

View File

@ -0,0 +1,5 @@
/dev/nvme1n1 /host/media/volume1 ext4 rw,seclabel,relatime,data=ordered 0 0
/dev/nvme1n2 /host/media/volume2 ext4 rw,seclabel,relatime,data=ordered 0 0
tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0
tmpfs /run/lock tmpfs rw,nosuid,nodev,noexec,relatime,size=5120k 0 0
tmpfs /sys/fs/cgroup tmpfs ro,nosuid,nodev,noexec,mode=755 0 0

View File

@ -15,6 +15,7 @@ package collector
import (
"path/filepath"
"strings"
"github.com/prometheus/procfs"
kingpin "gopkg.in/alecthomas/kingpin.v2"
@ -38,3 +39,10 @@ func sysFilePath(name string) string {
func rootfsFilePath(name string) string {
return filepath.Join(*rootfsPath, name)
}
func rootfsStripPrefix(path string) string {
if *rootfsPath == "/" {
return path
}
return strings.TrimPrefix(path, *rootfsPath)
}