diff --git a/blkio.go b/blkio.go index 5be154c8..c198c528 100644 --- a/blkio.go +++ b/blkio.go @@ -30,14 +30,29 @@ import ( specs "github.com/opencontainers/runtime-spec/specs-go" ) -func NewBlkio(root string) *blkioController { - return &blkioController{ - root: filepath.Join(root, string(Blkio)), +// NewBlkio returns a Blkio controller given the root folder of cgroups. +// It may optionally accept other configuration options, such as ProcRoot(path) +func NewBlkio(root string, options ...func(controller *blkioController)) *blkioController { + ctrl := &blkioController{ + root: filepath.Join(root, string(Blkio)), + procRoot: "/proc", + } + for _, opt := range options { + opt(ctrl) + } + return ctrl +} + +// ProcRoot overrides the default location of the "/proc" filesystem +func ProcRoot(path string) func(controller *blkioController) { + return func(c *blkioController) { + c.procRoot = path } } type blkioController struct { - root string + root string + procRoot string } func (b *blkioController) Name() Name { @@ -123,7 +138,7 @@ func (b *blkioController) Stat(path string, stats *v1.Metrics) error { }, ) } - f, err := os.Open("/proc/diskstats") + f, err := os.Open(filepath.Join(b.procRoot, "diskstats")) if err != nil { return err } diff --git a/blkio_test.go b/blkio_test.go index b63f5c30..da463d9b 100644 --- a/blkio_test.go +++ b/blkio_test.go @@ -49,3 +49,31 @@ func TestGetDevices(t *testing.T) { t.Fatalf("expected device name %q but received %q", expected, name) } } + +func TestNewBlkio(t *testing.T) { + const root = "/test/folder" + const expected = "/test/folder/blkio" + const expectedProc = "/proc" + + ctrl := NewBlkio(root) + if ctrl.root != expected { + t.Fatalf("expected cgroups root %q but received %q", expected, ctrl.root) + } + if ctrl.procRoot != expectedProc { + t.Fatalf("expected proc FS root %q but received %q", expectedProc, ctrl.procRoot) + } +} + +func TestNewBlkio_Proc(t *testing.T) { + const root = "/test/folder" + const expected = "/test/folder/blkio" + const expectedProc = "/test/proc" + + ctrl := NewBlkio(root, ProcRoot(expectedProc)) + if ctrl.root != expected { + t.Fatalf("expected cgroups root %q but received %q", expected, ctrl.root) + } + if ctrl.procRoot != expectedProc { + t.Fatalf("expected proc FS root %q but received %q", expectedProc, ctrl.procRoot) + } +}