From b983d32d96df5880894114af49703e2555bf2ca5 Mon Sep 17 00:00:00 2001 From: "Tim St. Clair" Date: Fri, 15 Apr 2016 14:31:54 -0700 Subject: [PATCH 1/5] Refactor manager/machine.go -> utils/machine/info.go --- manager/manager.go | 19 +++++++++++- manager/machine.go => utils/machine/info.go | 33 +++++---------------- 2 files changed, 26 insertions(+), 26 deletions(-) rename manager/machine.go => utils/machine/info.go (82%) diff --git a/manager/manager.go b/manager/manager.go index 1179303a..21a47dc7 100644 --- a/manager/manager.go +++ b/manager/manager.go @@ -37,8 +37,10 @@ import ( info "github.com/google/cadvisor/info/v1" "github.com/google/cadvisor/info/v2" "github.com/google/cadvisor/utils/cpuload" + "github.com/google/cadvisor/utils/machine" "github.com/google/cadvisor/utils/oomparser" "github.com/google/cadvisor/utils/sysfs" + "github.com/google/cadvisor/version" "github.com/golang/glog" "github.com/opencontainers/runc/libcontainer/cgroups" @@ -174,7 +176,7 @@ func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, maxHousekeepingIn ignoreMetrics: ignoreMetricsSet, } - machineInfo, err := getMachineInfo(sysfs, fsInfo, inHostNamespace) + machineInfo, err := machine.Info(sysfs, fsInfo, inHostNamespace) if err != nil { return nil, err } @@ -1234,3 +1236,18 @@ func (m *manager) DebugInfo() map[string][]string { debugInfo["Managed containers"] = lines return debugInfo } + +func getVersionInfo() (*info.VersionInfo, error) { + + kernel_version := machine.KernelVersion() + container_os := machine.ContainerOsVersion() + docker_version := machine.DockerVersion() + + return &info.VersionInfo{ + KernelVersion: kernel_version, + ContainerOsVersion: container_os, + DockerVersion: docker_version, + CadvisorVersion: version.Info["version"], + CadvisorRevision: version.Info["revision"], + }, nil +} diff --git a/manager/machine.go b/utils/machine/info.go similarity index 82% rename from manager/machine.go rename to utils/machine/info.go index 60c90fae..07759e4a 100644 --- a/manager/machine.go +++ b/utils/machine/info.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package manager +package machine import ( "bytes" @@ -26,10 +26,8 @@ import ( "github.com/google/cadvisor/fs" info "github.com/google/cadvisor/info/v1" "github.com/google/cadvisor/utils/cloudinfo" - "github.com/google/cadvisor/utils/machine" "github.com/google/cadvisor/utils/sysfs" "github.com/google/cadvisor/utils/sysinfo" - version "github.com/google/cadvisor/version" "github.com/golang/glog" "golang.org/x/net/context" @@ -52,19 +50,19 @@ func getInfoFromFiles(filePaths string) string { return "" } -func getMachineInfo(sysFs sysfs.SysFs, fsInfo fs.FsInfo, inHostNamespace bool) (*info.MachineInfo, error) { +func Info(sysFs sysfs.SysFs, fsInfo fs.FsInfo, inHostNamespace bool) (*info.MachineInfo, error) { rootFs := "/" if !inHostNamespace { rootFs = "/rootfs" } cpuinfo, err := ioutil.ReadFile(filepath.Join(rootFs, "/proc/cpuinfo")) - clockSpeed, err := machine.GetClockSpeed(cpuinfo) + clockSpeed, err := GetClockSpeed(cpuinfo) if err != nil { return nil, err } - memoryCapacity, err := machine.GetMachineMemoryCapacity() + memoryCapacity, err := GetMachineMemoryCapacity() if err != nil { return nil, err } @@ -84,7 +82,7 @@ func getMachineInfo(sysFs sysfs.SysFs, fsInfo fs.FsInfo, inHostNamespace bool) ( glog.Errorf("Failed to get network devices: %v", err) } - topology, numCores, err := machine.GetTopology(sysFs, string(cpuinfo)) + topology, numCores, err := GetTopology(sysFs, string(cpuinfo)) if err != nil { glog.Errorf("Failed to get topology information: %v", err) } @@ -121,22 +119,7 @@ func getMachineInfo(sysFs sysfs.SysFs, fsInfo fs.FsInfo, inHostNamespace bool) ( return machineInfo, nil } -func getVersionInfo() (*info.VersionInfo, error) { - - kernel_version := getKernelVersion() - container_os := getContainerOsVersion() - docker_version := getDockerVersion() - - return &info.VersionInfo{ - KernelVersion: kernel_version, - ContainerOsVersion: container_os, - DockerVersion: docker_version, - CadvisorVersion: version.Info["version"], - CadvisorRevision: version.Info["revision"], - }, nil -} - -func getContainerOsVersion() string { +func ContainerOsVersion() string { container_os := "Unknown" os_release, err := ioutil.ReadFile("/etc/os-release") if err == nil { @@ -153,7 +136,7 @@ func getContainerOsVersion() string { return container_os } -func getDockerVersion() string { +func DockerVersion() string { docker_version := "Unknown" client, err := docker.Client() if err == nil { @@ -165,7 +148,7 @@ func getDockerVersion() string { return docker_version } -func getKernelVersion() string { +func KernelVersion() string { uname := &syscall.Utsname{} if err := syscall.Uname(uname); err != nil { From 0c89fd1b712a7b6890f7d8877320f1964c5d839b Mon Sep 17 00:00:00 2001 From: "Tim St. Clair" Date: Fri, 15 Apr 2016 16:07:15 -0700 Subject: [PATCH 2/5] Refactor docker-specific functions from manager to docker --- container/docker/docker.go | 162 ++++++++++++++++++++++++++++++++++++ container/docker/factory.go | 19 ----- container/docker/handler.go | 61 -------------- container/docker/types.go | 37 ++++++++ manager/manager.go | 90 +++----------------- pages/docker.go | 2 +- pages/pages.go | 3 +- utils/machine/info.go | 14 ---- 8 files changed, 212 insertions(+), 176 deletions(-) create mode 100644 container/docker/docker.go create mode 100644 container/docker/types.go diff --git a/container/docker/docker.go b/container/docker/docker.go new file mode 100644 index 00000000..203ac7f7 --- /dev/null +++ b/container/docker/docker.go @@ -0,0 +1,162 @@ +// Copyright 2016 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Provides global docker information. +package docker + +import ( + "fmt" + "strconv" + "strings" + + "golang.org/x/net/context" + + dockertypes "github.com/docker/engine-api/types" + "github.com/google/cadvisor/utils/machine" +) + +func Status() (DockerStatus, error) { + client, err := Client() + if err != nil { + return DockerStatus{}, fmt.Errorf("unable to communicate with docker daemon: %v", err) + } + dockerInfo, err := client.Info(context.Background()) + if err != nil { + return DockerStatus{}, err + } + + out := DockerStatus{} + out.Version = VersionString() + out.KernelVersion = machine.KernelVersion() + out.OS = dockerInfo.OperatingSystem + out.Hostname = dockerInfo.Name + out.RootDir = dockerInfo.DockerRootDir + out.Driver = dockerInfo.Driver + out.ExecDriver = dockerInfo.ExecutionDriver + out.NumImages = dockerInfo.Images + out.NumContainers = dockerInfo.Containers + out.DriverStatus = make(map[string]string, len(dockerInfo.DriverStatus)) + for _, v := range dockerInfo.DriverStatus { + out.DriverStatus[v[0]] = v[1] + } + return out, nil +} + +func Images() ([]DockerImage, error) { + client, err := Client() + if err != nil { + return nil, fmt.Errorf("unable to communicate with docker daemon: %v", err) + } + images, err := client.ImageList(context.Background(), dockertypes.ImageListOptions{All: false}) + if err != nil { + return nil, err + } + + out := []DockerImage{} + const unknownTag = ":" + for _, image := range images { + if len(image.RepoTags) == 1 && image.RepoTags[0] == unknownTag { + // images with repo or tags are uninteresting. + continue + } + di := DockerImage{ + ID: image.ID, + RepoTags: image.RepoTags, + Created: image.Created, + VirtualSize: image.VirtualSize, + Size: image.Size, + } + out = append(out, di) + } + return out, nil + +} + +// Checks whether the dockerInfo reflects a valid docker setup, and returns it if it does, or an +// error otherwise. +func ValidateInfo() (*dockertypes.Info, error) { + client, err := Client() + if err != nil { + return nil, fmt.Errorf("unable to communicate with docker daemon: %v", err) + } + + dockerInfo, err := client.Info(context.Background()) + if err != nil { + return nil, fmt.Errorf("failed to detect Docker info: %v", err) + } + + // Fall back to version API if ServerVersion is not set in info. + if dockerInfo.ServerVersion == "" { + version, err := client.ServerVersion(context.Background()) + if err != nil { + return nil, fmt.Errorf("unable to get docker version: %v", err) + } + dockerInfo.ServerVersion = version.Version + } + version, err := parseDockerVersion(dockerInfo.ServerVersion) + if err != nil { + return nil, err + } + + if version[0] < 1 { + return nil, fmt.Errorf("cAdvisor requires docker version %v or above but we have found version %v reported as %q", []int{1, 0, 0}, version, dockerInfo.ServerVersion) + } + + // Check that the libcontainer execdriver is used if the version is < 1.11 + // (execution drivers are no longer supported as of 1.11). + if version[0] <= 1 && version[1] <= 10 && + !strings.HasPrefix(dockerInfo.ExecutionDriver, "native") { + return nil, fmt.Errorf("docker found, but not using native exec driver") + } + + if dockerInfo.Driver == "" { + return nil, fmt.Errorf("failed to find docker storage driver") + } + + return &dockerInfo, nil +} + +func Version() ([]int, error) { + return parseDockerVersion(VersionString()) +} + +func VersionString() string { + docker_version := "Unknown" + client, err := Client() + if err == nil { + version, err := client.ServerVersion(context.Background()) + if err == nil { + docker_version = version.Version + } + } + return docker_version +} + +// TODO: switch to a semantic versioning library. +func parseDockerVersion(full_version_string string) ([]int, error) { + matches := version_re.FindAllStringSubmatch(full_version_string, -1) + if len(matches) != 1 { + return nil, fmt.Errorf("version string \"%v\" doesn't match expected regular expression: \"%v\"", full_version_string, version_regexp_string) + } + version_string_array := matches[0][1:] + version_array := make([]int, 3) + for index, version_string := range version_string_array { + version, err := strconv.Atoi(version_string) + if err != nil { + return nil, fmt.Errorf("error while parsing \"%v\" in \"%v\"", version_string, full_version_string) + } + version_array[index] = version + } + return version_array, nil +} diff --git a/container/docker/factory.go b/container/docker/factory.go index b3938bc1..302a53ff 100644 --- a/container/docker/factory.go +++ b/container/docker/factory.go @@ -19,7 +19,6 @@ import ( "fmt" "path" "regexp" - "strconv" "strings" "github.com/google/cadvisor/container" @@ -164,24 +163,6 @@ var ( version_re = regexp.MustCompile(version_regexp_string) ) -// TODO: switch to a semantic versioning library. -func parseDockerVersion(full_version_string string) ([]int, error) { - matches := version_re.FindAllStringSubmatch(full_version_string, -1) - if len(matches) != 1 { - return nil, fmt.Errorf("version string \"%v\" doesn't match expected regular expression: \"%v\"", full_version_string, version_regexp_string) - } - version_string_array := matches[0][1:] - version_array := make([]int, 3) - for index, version_string := range version_string_array { - version, err := strconv.Atoi(version_string) - if err != nil { - return nil, fmt.Errorf("error while parsing \"%v\" in \"%v\"", version_string, full_version_string) - } - version_array[index] = version - } - return version_array, nil -} - // Register root container before running this function! func Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, ignoreMetrics container.MetricSet) error { client, err := Client() diff --git a/container/docker/handler.go b/container/docker/handler.go index 5e813989..d94b7177 100644 --- a/container/docker/handler.go +++ b/container/docker/handler.go @@ -29,7 +29,6 @@ import ( info "github.com/google/cadvisor/info/v1" docker "github.com/docker/engine-api/client" - dockertypes "github.com/docker/engine-api/types" dockercontainer "github.com/docker/engine-api/types/container" "github.com/opencontainers/runc/libcontainer/cgroups" cgroupfs "github.com/opencontainers/runc/libcontainer/cgroups/fs" @@ -349,63 +348,3 @@ func (self *dockerContainerHandler) StopWatchingSubcontainers() error { func (self *dockerContainerHandler) Exists() bool { return common.CgroupExists(self.cgroupPaths) } - -func DockerInfo() (dockertypes.Info, error) { - client, err := Client() - if err != nil { - return dockertypes.Info{}, fmt.Errorf("unable to communicate with docker daemon: %v", err) - } - return client.Info(context.Background()) -} - -func DockerImages() ([]dockertypes.Image, error) { - client, err := Client() - if err != nil { - return nil, fmt.Errorf("unable to communicate with docker daemon: %v", err) - } - return client.ImageList(context.Background(), dockertypes.ImageListOptions{All: false}) -} - -// Checks whether the dockerInfo reflects a valid docker setup, and returns it if it does, or an -// error otherwise. -func ValidateInfo() (*dockertypes.Info, error) { - client, err := Client() - if err != nil { - return nil, fmt.Errorf("unable to communicate with docker daemon: %v", err) - } - - dockerInfo, err := client.Info(context.Background()) - if err != nil { - return nil, fmt.Errorf("failed to detect Docker info: %v", err) - } - - // Fall back to version API if ServerVersion is not set in info. - if dockerInfo.ServerVersion == "" { - version, err := client.ServerVersion(context.Background()) - if err != nil { - return nil, fmt.Errorf("unable to get docker version: %v", err) - } - dockerInfo.ServerVersion = version.Version - } - version, err := parseDockerVersion(dockerInfo.ServerVersion) - if err != nil { - return nil, err - } - - if version[0] < 1 { - return nil, fmt.Errorf("cAdvisor requires docker version %v or above but we have found version %v reported as %q", []int{1, 0, 0}, version, dockerInfo.ServerVersion) - } - - // Check that the libcontainer execdriver is used if the version is < 1.11 - // (execution drivers are no longer supported as of 1.11). - if version[0] <= 1 && version[1] <= 10 && - !strings.HasPrefix(dockerInfo.ExecutionDriver, "native") { - return nil, fmt.Errorf("docker found, but not using native exec driver") - } - - if dockerInfo.Driver == "" { - return nil, fmt.Errorf("failed to find docker storage driver") - } - - return &dockerInfo, nil -} diff --git a/container/docker/types.go b/container/docker/types.go new file mode 100644 index 00000000..8be66b86 --- /dev/null +++ b/container/docker/types.go @@ -0,0 +1,37 @@ +// Copyright 2016 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Types used for docker containers. +package docker + +type DockerStatus struct { + Version string `json:"version"` + KernelVersion string `json:"kernel_version"` + OS string `json:"os"` + Hostname string `json:"hostname"` + RootDir string `json:"root_dir"` + Driver string `json:"driver"` + DriverStatus map[string]string `json:"driver_status"` + ExecDriver string `json:"exec_driver"` + NumImages int `json:"num_images"` + NumContainers int `json:"num_containers"` +} + +type DockerImage struct { + ID string `json:"id"` + RepoTags []string `json:"repo_tags"` // repository name and tags. + Created int64 `json:"created"` // unix time since creation. + VirtualSize int64 `json:"virtual_size"` + Size int64 `json:"size"` +} diff --git a/manager/manager.go b/manager/manager.go index 21a47dc7..c4543a52 100644 --- a/manager/manager.go +++ b/manager/manager.go @@ -112,10 +112,10 @@ type Manager interface { CloseEventChannel(watch_id int) // Get status information about docker. - DockerInfo() (DockerStatus, error) + DockerInfo() (docker.DockerStatus, error) // Get details about interesting docker images. - DockerImages() ([]DockerImage, error) + DockerImages() ([]docker.DockerImage, error) // Returns debugging information. Map of lines per category. DebugInfo() map[string][]string @@ -134,7 +134,7 @@ func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, maxHousekeepingIn } glog.Infof("cAdvisor running in container: %q", selfContainer) - dockerInfo, err := dockerInfo() + dockerStatus, err := docker.Status() if err != nil { glog.Warningf("Unable to connect to Docker: %v", err) } @@ -146,8 +146,8 @@ func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, maxHousekeepingIn context := fs.Context{ Docker: fs.DockerContext{ Root: docker.RootDir(), - Driver: dockerInfo.Driver, - DriverStatus: dockerInfo.DriverStatus, + Driver: dockerStatus.Driver, + DriverStatus: dockerStatus.DriverStatus, }, RktPath: rktPath, } @@ -241,9 +241,6 @@ func (self *manager) Start() error { glog.Errorf("Registration of the raw container factory failed: %v", err) } - self.DockerInfo() - self.DockerImages() - if *enableLoadReader { // Create cpu load reader. cpuLoadReader, err := cpuload.New() @@ -1127,79 +1124,12 @@ func parseEventsStoragePolicy() events.StoragePolicy { return policy } -type DockerStatus struct { - Version string `json:"version"` - KernelVersion string `json:"kernel_version"` - OS string `json:"os"` - Hostname string `json:"hostname"` - RootDir string `json:"root_dir"` - Driver string `json:"driver"` - DriverStatus map[string]string `json:"driver_status"` - ExecDriver string `json:"exec_driver"` - NumImages int `json:"num_images"` - NumContainers int `json:"num_containers"` +func (m *manager) DockerImages() ([]docker.DockerImage, error) { + return docker.Images() } -type DockerImage struct { - ID string `json:"id"` - RepoTags []string `json:"repo_tags"` // repository name and tags. - Created int64 `json:"created"` // unix time since creation. - VirtualSize int64 `json:"virtual_size"` - Size int64 `json:"size"` -} - -func (m *manager) DockerImages() ([]DockerImage, error) { - images, err := docker.DockerImages() - if err != nil { - return nil, err - } - out := []DockerImage{} - const unknownTag = ":" - for _, image := range images { - if len(image.RepoTags) == 1 && image.RepoTags[0] == unknownTag { - // images with repo or tags are uninteresting. - continue - } - di := DockerImage{ - ID: image.ID, - RepoTags: image.RepoTags, - Created: image.Created, - VirtualSize: image.VirtualSize, - Size: image.Size, - } - out = append(out, di) - } - return out, nil -} - -func (m *manager) DockerInfo() (DockerStatus, error) { - return dockerInfo() -} - -func dockerInfo() (DockerStatus, error) { - dockerInfo, err := docker.DockerInfo() - if err != nil { - return DockerStatus{}, err - } - versionInfo, err := getVersionInfo() - if err != nil { - return DockerStatus{}, err - } - out := DockerStatus{} - out.Version = versionInfo.DockerVersion - out.KernelVersion = dockerInfo.KernelVersion - out.OS = dockerInfo.OperatingSystem - out.Hostname = dockerInfo.Name - out.RootDir = dockerInfo.DockerRootDir - out.Driver = dockerInfo.Driver - out.ExecDriver = dockerInfo.ExecutionDriver - out.NumImages = dockerInfo.Images - out.NumContainers = dockerInfo.Containers - out.DriverStatus = make(map[string]string, len(dockerInfo.DriverStatus)) - for _, v := range dockerInfo.DriverStatus { - out.DriverStatus[v[0]] = v[1] - } - return out, nil +func (m *manager) DockerInfo() (docker.DockerStatus, error) { + return docker.Status() } func (m *manager) DebugInfo() map[string][]string { @@ -1241,7 +1171,7 @@ func getVersionInfo() (*info.VersionInfo, error) { kernel_version := machine.KernelVersion() container_os := machine.ContainerOsVersion() - docker_version := machine.DockerVersion() + docker_version := docker.VersionString() return &info.VersionInfo{ KernelVersion: kernel_version, diff --git a/pages/docker.go b/pages/docker.go index 0a244295..1a5a6580 100644 --- a/pages/docker.go +++ b/pages/docker.go @@ -31,7 +31,7 @@ import ( const DockerPage = "/docker/" -func toStatusKV(status manager.DockerStatus) ([]keyVal, []keyVal) { +func toStatusKV(status docker.DockerStatus) ([]keyVal, []keyVal) { ds := []keyVal{ {Key: "Driver", Value: status.Driver}, } diff --git a/pages/pages.go b/pages/pages.go index c382d424..7f5b61ee 100644 --- a/pages/pages.go +++ b/pages/pages.go @@ -21,6 +21,7 @@ import ( "net/url" "strings" + "github.com/google/cadvisor/container/docker" httpmux "github.com/google/cadvisor/http/mux" info "github.com/google/cadvisor/info/v1" "github.com/google/cadvisor/manager" @@ -62,7 +63,7 @@ type pageData struct { Root string DockerStatus []keyVal DockerDriverStatus []keyVal - DockerImages []manager.DockerImage + DockerImages []docker.DockerImage } func init() { diff --git a/utils/machine/info.go b/utils/machine/info.go index 07759e4a..e26f950f 100644 --- a/utils/machine/info.go +++ b/utils/machine/info.go @@ -22,7 +22,6 @@ import ( "strings" "syscall" - "github.com/google/cadvisor/container/docker" "github.com/google/cadvisor/fs" info "github.com/google/cadvisor/info/v1" "github.com/google/cadvisor/utils/cloudinfo" @@ -30,7 +29,6 @@ import ( "github.com/google/cadvisor/utils/sysinfo" "github.com/golang/glog" - "golang.org/x/net/context" ) var machineIdFilePath = flag.String("machine_id_file", "/etc/machine-id,/var/lib/dbus/machine-id", "Comma-separated list of files to check for machine-id. Use the first one that exists.") @@ -136,18 +134,6 @@ func ContainerOsVersion() string { return container_os } -func DockerVersion() string { - docker_version := "Unknown" - client, err := docker.Client() - if err == nil { - version, err := client.ServerVersion(context.Background()) - if err == nil { - docker_version = version.Version - } - } - return docker_version -} - func KernelVersion() string { uname := &syscall.Utsname{} From 9961e371687b5d44cf885f456a7a176a7ee06fff Mon Sep 17 00:00:00 2001 From: "Tim St. Clair" Date: Fri, 15 Apr 2016 16:07:29 -0700 Subject: [PATCH 3/5] Delete unused ManagerMock --- manager/manager_mock.go | 119 ---------------------------------------- 1 file changed, 119 deletions(-) delete mode 100644 manager/manager_mock.go diff --git a/manager/manager_mock.go b/manager/manager_mock.go deleted file mode 100644 index ffec8108..00000000 --- a/manager/manager_mock.go +++ /dev/null @@ -1,119 +0,0 @@ -// Copyright 2014 Google Inc. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// +build test - -package manager - -import ( - "github.com/google/cadvisor/events" - info "github.com/google/cadvisor/info/v1" - "github.com/google/cadvisor/info/v2" - - "github.com/stretchr/testify/mock" -) - -type ManagerMock struct { - mock.Mock -} - -func (c *ManagerMock) Start() error { - args := c.Called() - return args.Error(0) -} - -func (c *ManagerMock) Stop() error { - args := c.Called() - return args.Error(0) -} - -func (c *ManagerMock) GetContainerInfo(name string, query *info.ContainerInfoRequest) (*info.ContainerInfo, error) { - args := c.Called(name, query) - return args.Get(0).(*info.ContainerInfo), args.Error(1) -} - -func (c *ManagerMock) SubcontainersInfo(containerName string, query *info.ContainerInfoRequest) ([]*info.ContainerInfo, error) { - args := c.Called(containerName, query) - return args.Get(0).([]*info.ContainerInfo), args.Error(1) -} - -func (c *ManagerMock) AllDockerContainers(query *info.ContainerInfoRequest) (map[string]info.ContainerInfo, error) { - args := c.Called(query) - return args.Get(0).(map[string]info.ContainerInfo), args.Error(1) -} - -func (c *ManagerMock) DockerContainer(name string, query *info.ContainerInfoRequest) (info.ContainerInfo, error) { - args := c.Called(name, query) - return args.Get(0).(info.ContainerInfo), args.Error(1) -} - -func (c *ManagerMock) GetContainerSpec(containerName string, options v2.RequestOptions) (map[string]v2.ContainerSpec, error) { - args := c.Called(containerName, options) - return args.Get(0).(map[string]v2.ContainerSpec), args.Error(1) -} - -func (c *ManagerMock) GetDerivedStats(containerName string, options v2.RequestOptions) (map[string]v2.DerivedStats, error) { - args := c.Called(containerName, options) - return args.Get(0).(map[string]v2.DerivedStats), args.Error(1) -} - -func (c *ManagerMock) GetRequestedContainersInfo(containerName string, options v2.RequestOptions) (map[string]*info.ContainerInfo, error) { - args := c.Called(containerName, options) - return args.Get(0).(map[string]*info.ContainerInfo), args.Error(1) -} - -func (c *ManagerMock) Exists(name string) bool { - args := c.Called(name) - return args.Get(0).(bool) -} - -func (c *ManagerMock) WatchForEvents(queryuest *events.Request, passedChannel chan *info.Event) error { - args := c.Called(queryuest, passedChannel) - return args.Error(0) -} - -func (c *ManagerMock) GetPastEvents(queryuest *events.Request) ([]*info.Event, error) { - args := c.Called(queryuest) - return args.Get(0).([]*info.Event), args.Error(1) -} - -func (c *ManagerMock) GetMachineInfo() (*info.MachineInfo, error) { - args := c.Called() - return args.Get(0).(*info.MachineInfo), args.Error(1) -} - -func (c *ManagerMock) GetVersionInfo() (*info.VersionInfo, error) { - args := c.Called() - return args.Get(0).(*info.VersionInfo), args.Error(1) -} - -func (c *ManagerMock) GetFsInfo() ([]v2.FsInfo, error) { - args := c.Called() - return args.Get(0).([]v2.FsInfo), args.Error(1) -} - -func (c *ManagerMock) GetProcessList(name string, options v2.RequestOptions) ([]v2.ProcessInfo, error) { - args := c.Called() - return args.Get(0).([]v2.ProcessInfo), args.Error(1) -} - -func (c *ManagerMock) DockerInfo() (DockerStatus, error) { - args := c.Called() - return args.Get(0).(DockerStatus), args.Error(1) -} - -func (c *ManagerMock) DockerImages() ([]DockerImage, error) { - args := c.Called() - return args.Get(0).([]DockerImage), args.Error(1) -} From f365c6a11570d573cede18152ac886a87d4a3841 Mon Sep 17 00:00:00 2001 From: "Tim St. Clair" Date: Mon, 2 May 2016 15:52:29 -0700 Subject: [PATCH 4/5] Move docker types to v1 API --- container/docker/docker.go | 17 +++++++++-------- container/docker/types.go => info/v1/docker.go | 2 +- manager/manager.go | 8 ++++---- pages/docker.go | 2 +- pages/pages.go | 3 +-- 5 files changed, 16 insertions(+), 16 deletions(-) rename container/docker/types.go => info/v1/docker.go (98%) diff --git a/container/docker/docker.go b/container/docker/docker.go index 203ac7f7..d164c4ba 100644 --- a/container/docker/docker.go +++ b/container/docker/docker.go @@ -20,23 +20,24 @@ import ( "strconv" "strings" + dockertypes "github.com/docker/engine-api/types" "golang.org/x/net/context" - dockertypes "github.com/docker/engine-api/types" + "github.com/google/cadvisor/info/v1" "github.com/google/cadvisor/utils/machine" ) -func Status() (DockerStatus, error) { +func Status() (v1.DockerStatus, error) { client, err := Client() if err != nil { - return DockerStatus{}, fmt.Errorf("unable to communicate with docker daemon: %v", err) + return v1.DockerStatus{}, fmt.Errorf("unable to communicate with docker daemon: %v", err) } dockerInfo, err := client.Info(context.Background()) if err != nil { - return DockerStatus{}, err + return v1.DockerStatus{}, err } - out := DockerStatus{} + out := v1.DockerStatus{} out.Version = VersionString() out.KernelVersion = machine.KernelVersion() out.OS = dockerInfo.OperatingSystem @@ -53,7 +54,7 @@ func Status() (DockerStatus, error) { return out, nil } -func Images() ([]DockerImage, error) { +func Images() ([]v1.DockerImage, error) { client, err := Client() if err != nil { return nil, fmt.Errorf("unable to communicate with docker daemon: %v", err) @@ -63,14 +64,14 @@ func Images() ([]DockerImage, error) { return nil, err } - out := []DockerImage{} + out := []v1.DockerImage{} const unknownTag = ":" for _, image := range images { if len(image.RepoTags) == 1 && image.RepoTags[0] == unknownTag { // images with repo or tags are uninteresting. continue } - di := DockerImage{ + di := v1.DockerImage{ ID: image.ID, RepoTags: image.RepoTags, Created: image.Created, diff --git a/container/docker/types.go b/info/v1/docker.go similarity index 98% rename from container/docker/types.go rename to info/v1/docker.go index 8be66b86..2703c534 100644 --- a/container/docker/types.go +++ b/info/v1/docker.go @@ -13,7 +13,7 @@ // limitations under the License. // Types used for docker containers. -package docker +package v1 type DockerStatus struct { Version string `json:"version"` diff --git a/manager/manager.go b/manager/manager.go index c4543a52..ddebc7a6 100644 --- a/manager/manager.go +++ b/manager/manager.go @@ -112,10 +112,10 @@ type Manager interface { CloseEventChannel(watch_id int) // Get status information about docker. - DockerInfo() (docker.DockerStatus, error) + DockerInfo() (info.DockerStatus, error) // Get details about interesting docker images. - DockerImages() ([]docker.DockerImage, error) + DockerImages() ([]info.DockerImage, error) // Returns debugging information. Map of lines per category. DebugInfo() map[string][]string @@ -1124,11 +1124,11 @@ func parseEventsStoragePolicy() events.StoragePolicy { return policy } -func (m *manager) DockerImages() ([]docker.DockerImage, error) { +func (m *manager) DockerImages() ([]info.DockerImage, error) { return docker.Images() } -func (m *manager) DockerInfo() (docker.DockerStatus, error) { +func (m *manager) DockerInfo() (info.DockerStatus, error) { return docker.Status() } diff --git a/pages/docker.go b/pages/docker.go index 1a5a6580..8cc0177c 100644 --- a/pages/docker.go +++ b/pages/docker.go @@ -31,7 +31,7 @@ import ( const DockerPage = "/docker/" -func toStatusKV(status docker.DockerStatus) ([]keyVal, []keyVal) { +func toStatusKV(status info.DockerStatus) ([]keyVal, []keyVal) { ds := []keyVal{ {Key: "Driver", Value: status.Driver}, } diff --git a/pages/pages.go b/pages/pages.go index 7f5b61ee..74e1c545 100644 --- a/pages/pages.go +++ b/pages/pages.go @@ -21,7 +21,6 @@ import ( "net/url" "strings" - "github.com/google/cadvisor/container/docker" httpmux "github.com/google/cadvisor/http/mux" info "github.com/google/cadvisor/info/v1" "github.com/google/cadvisor/manager" @@ -63,7 +62,7 @@ type pageData struct { Root string DockerStatus []keyVal DockerDriverStatus []keyVal - DockerImages []docker.DockerImage + DockerImages []info.DockerImage } func init() { From 4d3ef349fb1f64f6c579e29d24138470703d5432 Mon Sep 17 00:00:00 2001 From: "Tim St. Clair" Date: Mon, 2 May 2016 15:56:49 -0700 Subject: [PATCH 5/5] Move utils/machine -> machine --- container/docker/docker.go | 2 +- container/raw/handler.go | 2 +- {utils/machine => machine}/info.go | 0 {utils/machine => machine}/machine.go | 3 +-- {utils/machine => machine}/testdata/cpuinfo | 0 {utils/machine => machine}/topology_test.go | 0 manager/manager.go | 2 +- 7 files changed, 4 insertions(+), 5 deletions(-) rename {utils/machine => machine}/info.go (100%) rename {utils/machine => machine}/machine.go (98%) rename {utils/machine => machine}/testdata/cpuinfo (100%) rename {utils/machine => machine}/topology_test.go (100%) diff --git a/container/docker/docker.go b/container/docker/docker.go index d164c4ba..ae9f1fdf 100644 --- a/container/docker/docker.go +++ b/container/docker/docker.go @@ -24,7 +24,7 @@ import ( "golang.org/x/net/context" "github.com/google/cadvisor/info/v1" - "github.com/google/cadvisor/utils/machine" + "github.com/google/cadvisor/machine" ) func Status() (v1.DockerStatus, error) { diff --git a/container/raw/handler.go b/container/raw/handler.go index 0e55046e..7006a002 100644 --- a/container/raw/handler.go +++ b/container/raw/handler.go @@ -26,7 +26,7 @@ import ( "github.com/google/cadvisor/container/libcontainer" "github.com/google/cadvisor/fs" info "github.com/google/cadvisor/info/v1" - "github.com/google/cadvisor/utils/machine" + "github.com/google/cadvisor/machine" "github.com/golang/glog" "github.com/opencontainers/runc/libcontainer/cgroups" diff --git a/utils/machine/info.go b/machine/info.go similarity index 100% rename from utils/machine/info.go rename to machine/info.go diff --git a/utils/machine/machine.go b/machine/machine.go similarity index 98% rename from utils/machine/machine.go rename to machine/machine.go index e8949e50..7992216d 100644 --- a/utils/machine/machine.go +++ b/machine/machine.go @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +// The machine package contains functions that extract machine-level specs. package machine import ( @@ -33,8 +34,6 @@ import ( "github.com/golang/glog" ) -// The utils/machine package contains functions that extract machine-level specs. - var ( cpuRegExp = regexp.MustCompile(`^processor\s*:\s*([0-9]+)$`) coreRegExp = regexp.MustCompile(`^core id\s*:\s*([0-9]+)$`) diff --git a/utils/machine/testdata/cpuinfo b/machine/testdata/cpuinfo similarity index 100% rename from utils/machine/testdata/cpuinfo rename to machine/testdata/cpuinfo diff --git a/utils/machine/topology_test.go b/machine/topology_test.go similarity index 100% rename from utils/machine/topology_test.go rename to machine/topology_test.go diff --git a/manager/manager.go b/manager/manager.go index ddebc7a6..def695d5 100644 --- a/manager/manager.go +++ b/manager/manager.go @@ -36,8 +36,8 @@ import ( "github.com/google/cadvisor/fs" info "github.com/google/cadvisor/info/v1" "github.com/google/cadvisor/info/v2" + "github.com/google/cadvisor/machine" "github.com/google/cadvisor/utils/cpuload" - "github.com/google/cadvisor/utils/machine" "github.com/google/cadvisor/utils/oomparser" "github.com/google/cadvisor/utils/sysfs" "github.com/google/cadvisor/version"