From 9e47be7bdf6120c47f83d7385368c2cc1371e2a4 Mon Sep 17 00:00:00 2001 From: David Ashpole Date: Wed, 19 Oct 2016 10:39:35 -0700 Subject: [PATCH] Cadvisor allows the RootPath to be configured. The RootPath is used to determine which filesystem is the RootFs. --- cadvisor.go | 3 ++- fs/fs.go | 19 +++++++++++++++---- fs/fs_test.go | 6 +++--- manager/manager.go | 5 +++-- manager/manager_test.go | 3 ++- 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/cadvisor.go b/cadvisor.go index 48ddc8ae69..8c17a1f8c0 100644 --- a/cadvisor.go +++ b/cadvisor.go @@ -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" @@ -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) } diff --git a/fs/fs.go b/fs/fs.go index 46756d4e8b..aac9c393a3 100644 --- a/fs/fs.go +++ b/fs/fs.go @@ -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. @@ -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 { @@ -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 } @@ -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, diff --git a/fs/fs_test.go b/fs/fs_test.go index bba53a0ed5..a80a8f7e58 100644 --- a/fs/fs_test.go +++ b/fs/fs_test.go @@ -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) @@ -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) @@ -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) diff --git a/manager/manager.go b/manager/manager.go index 3622b0604a..f53bd4d590 100644 --- a/manager/manager.go +++ b/manager/manager.go @@ -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") } @@ -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 { diff --git a/manager/manager_test.go b/manager/manager_test.go index efa338169f..24eb1e13ba 100644 --- a/manager/manager_test.go +++ b/manager/manager_test.go @@ -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" @@ -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") }