Skip to content

Commit

Permalink
Cadvisor allows the RootPath to be configured. The RootPath is used t…
Browse files Browse the repository at this point in the history
…o determine which filesystem is the RootFs.
  • Loading branch information
dashpole committed Oct 19, 2016
1 parent cae5bfa commit 9e47be7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
3 changes: 2 additions & 1 deletion cadvisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"time"

"github.com/google/cadvisor/container"
"github.com/google/cadvisor/fs"
cadvisorhttp "github.com/google/cadvisor/http"
"github.com/google/cadvisor/manager"
"github.com/google/cadvisor/utils/sysfs"
Expand Down Expand Up @@ -124,7 +125,7 @@ func main() {

collectorHttpClient := createCollectorHttpClient(*collectorCert, *collectorKey)

containerManager, err := manager.New(memoryStorage, sysFs, *maxHousekeepingInterval, *allowDynamicHousekeeping, ignoreMetrics.MetricSet, &collectorHttpClient)
containerManager, err := manager.New(memoryStorage, sysFs, *maxHousekeepingInterval, *allowDynamicHousekeeping, ignoreMetrics.MetricSet, &collectorHttpClient, fs.DefaultRootPath)
if err != nil {
glog.Fatalf("Failed to create a Container Manager: %s", err)
}
Expand Down
19 changes: 15 additions & 4 deletions fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
LabelSystemRoot = "root"
LabelDockerImages = "docker-images"
LabelRktImages = "rkt-images"
DefaultRootPath = "/"
)

// The maximum number of `du` and `find` tasks that can be running at once.
Expand Down Expand Up @@ -87,6 +88,8 @@ type Context struct {
// docker root directory.
Docker DockerContext
RktPath string
// The "rootFs" filesystem is the filesystem that contains the RootPath
RootPath string
}

type DockerContext struct {
Expand Down Expand Up @@ -115,7 +118,15 @@ func NewFsInfo(context Context) (FsInfo, error) {
fsInfo.addDockerImagesLabel(context, mounts)

glog.Infof("Filesystem partitions: %+v", fsInfo.partitions)
fsInfo.addSystemRootLabel(mounts)
rootDevice, err := fsInfo.GetDirFsDevice(context.RootPath)
if err != nil {
return nil, fmt.Errorf("error trying to get filesystem Device for rootPath %v: err: %v", context.RootPath, err)
}
rootMountpoint, err := fsInfo.GetMountpointForDevice(rootDevice.Device)
if err != nil {
return nil, fmt.Errorf("error trying to get MountPoint for Root Device: %v, err: %v", rootDevice, err)
}
fsInfo.addSystemRootLabel(rootMountpoint, mounts)
return fsInfo, nil
}

Expand Down Expand Up @@ -187,10 +198,10 @@ func (self *RealFsInfo) getDockerDeviceMapperInfo(context DockerContext) (string
}, nil
}

// addSystemRootLabel attempts to determine which device contains the mount for /.
func (self *RealFsInfo) addSystemRootLabel(mounts []*mount.Info) {
// addSystemRootLabel attempts to determine which device contains the mount for rootMountpoint.
func (self *RealFsInfo) addSystemRootLabel(rootMountpoint string, mounts []*mount.Info) {
for _, m := range mounts {
if m.Mountpoint == "/" {
if m.Mountpoint == rootMountpoint {
self.partitions[m.Source] = partition{
fsType: m.Fstype,
mountpoint: m.Mountpoint,
Expand Down
6 changes: 3 additions & 3 deletions fs/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestFileNotExist(t *testing.T) {

func TestDirDiskUsage(t *testing.T) {
as := assert.New(t)
fsInfo, err := NewFsInfo(Context{})
fsInfo, err := NewFsInfo(Context{RootPath: DefaultRootPath})
as.NoError(err)
dir, err := ioutil.TempDir(os.TempDir(), "")
as.NoError(err)
Expand All @@ -108,7 +108,7 @@ func TestDirDiskUsage(t *testing.T) {

func TestDirInodeUsage(t *testing.T) {
as := assert.New(t)
fsInfo, err := NewFsInfo(Context{})
fsInfo, err := NewFsInfo(Context{RootPath: DefaultRootPath})
as.NoError(err)
dir, err := ioutil.TempDir(os.TempDir(), "")
as.NoError(err)
Expand Down Expand Up @@ -199,7 +199,7 @@ func TestAddSystemRootLabel(t *testing.T) {
labels: map[string]string{},
partitions: map[string]partition{},
}
fsInfo.addSystemRootLabel(tt.mounts)
fsInfo.addSystemRootLabel(DefaultRootPath, tt.mounts)

if source, ok := fsInfo.labels[LabelSystemRoot]; !ok || source != tt.expected {
t.Errorf("case %d: expected mount source '%s', got '%s'", i, tt.expected, source)
Expand Down
5 changes: 3 additions & 2 deletions manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ type Manager interface {
}

// New takes a memory storage and returns a new manager.
func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, maxHousekeepingInterval time.Duration, allowDynamicHousekeeping bool, ignoreMetricsSet container.MetricSet, collectorHttpClient *http.Client) (Manager, error) {
func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, maxHousekeepingInterval time.Duration, allowDynamicHousekeeping bool, ignoreMetricsSet container.MetricSet, collectorHttpClient *http.Client, rootPath string) (Manager, error) {
if memoryCache == nil {
return nil, fmt.Errorf("manager requires memory storage")
}
Expand All @@ -154,7 +154,8 @@ func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, maxHousekeepingIn
Driver: dockerStatus.Driver,
DriverStatus: dockerStatus.DriverStatus,
},
RktPath: rktPath,
RktPath: rktPath,
RootPath: rootPath,
}
fsInfo, err := fs.NewFsInfo(context)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/google/cadvisor/container"
"github.com/google/cadvisor/container/docker"
containertest "github.com/google/cadvisor/container/testing"
"github.com/google/cadvisor/fs"
info "github.com/google/cadvisor/info/v1"
itest "github.com/google/cadvisor/info/v1/test"
"github.com/google/cadvisor/info/v2"
Expand Down Expand Up @@ -300,7 +301,7 @@ func TestDockerContainersInfo(t *testing.T) {
}

func TestNewNilManager(t *testing.T) {
_, err := New(nil, nil, 60*time.Second, true, container.MetricSet{}, http.DefaultClient)
_, err := New(nil, nil, 60*time.Second, true, container.MetricSet{}, http.DefaultClient, fs.DefaultRootPath)
if err == nil {
t.Fatalf("Expected nil manager to return error")
}
Expand Down

0 comments on commit 9e47be7

Please sign in to comment.