Skip to content

Commit

Permalink
Allow overriding the default /proc folder in blkioController
Browse files Browse the repository at this point in the history
A host may share its /proc FS folder with a privileged container, mounted in an
alternative folder (e.g. /host/proc), as some users prefer to run their
monitoring software inside a container instead of as a host process.

This patch allows overriding the default /proc folder to allow the blkio
Controller working inside containerized monitoring software.

The /proc root can be overriden via an optional function (ProcRoot) to
avoid introducing a new NewBlkio constructor or introducing breaking changes
in the current API.

Then, the NewBlkio constructor can be invoked as usual, or in the following
form, to override the /proc path:

ctrl := NewBlkio("/sys/fs/cgroup", ProcRoot("/host/proc"))

Signed-off-by: Mario Macias <mariomac@gmail.com>
  • Loading branch information
mariomac committed Sep 21, 2019
1 parent bf292b2 commit af0bc55
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
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)
}
}

0 comments on commit af0bc55

Please sign in to comment.