Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions blkio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
28 changes: 28 additions & 0 deletions blkio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}