Update cadvisor version to latest version

This commit is contained in:
Manjunath A Kumatagi
2017-04-04 13:20:20 -04:00
parent e36cca4b5d
commit 2b42d71a94
18 changed files with 594 additions and 106 deletions

View File

@@ -31,6 +31,7 @@ go_library(
"//vendor/github.com/google/cadvisor/machine:go_default_library",
"//vendor/github.com/google/cadvisor/manager/watcher:go_default_library",
"//vendor/github.com/google/cadvisor/utils/docker:go_default_library",
"//vendor/github.com/google/cadvisor/zfs:go_default_library",
"//vendor/github.com/opencontainers/runc/libcontainer/cgroups:go_default_library",
"//vendor/github.com/opencontainers/runc/libcontainer/cgroups/fs:go_default_library",
"//vendor/github.com/opencontainers/runc/libcontainer/configs:go_default_library",

View File

@@ -17,6 +17,7 @@ package docker
import (
"fmt"
"regexp"
"strconv"
"strings"
@@ -39,6 +40,7 @@ func Status() (v1.DockerStatus, error) {
out := v1.DockerStatus{}
out.Version = VersionString()
out.APIVersion = APIVersionString()
out.KernelVersion = machine.KernelVersion()
out.OS = dockerInfo.OperatingSystem
out.Hostname = dockerInfo.Name
@@ -105,7 +107,7 @@ func ValidateInfo() (*dockertypes.Info, error) {
}
dockerInfo.ServerVersion = version.Version
}
version, err := parseDockerVersion(dockerInfo.ServerVersion)
version, err := parseVersion(dockerInfo.ServerVersion, version_re, 3)
if err != nil {
return nil, err
}
@@ -129,7 +131,11 @@ func ValidateInfo() (*dockertypes.Info, error) {
}
func Version() ([]int, error) {
return parseDockerVersion(VersionString())
return parseVersion(VersionString(), version_re, 3)
}
func APIVersion() ([]int, error) {
return parseVersion(APIVersionString(), apiversion_re, 2)
}
func VersionString() string {
@@ -144,18 +150,29 @@ func VersionString() string {
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)
func APIVersionString() string {
docker_api_version := "Unknown"
client, err := Client()
if err == nil {
version, err := client.ServerVersion(context.Background())
if err == nil {
docker_api_version = version.APIVersion
}
}
return docker_api_version
}
func parseVersion(version_string string, regex *regexp.Regexp, length int) ([]int, error) {
matches := regex.FindAllStringSubmatch(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)
return nil, fmt.Errorf("version string \"%v\" doesn't match expected regular expression: \"%v\"", version_string, regex.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)
version_array := make([]int, length)
for index, version_str := range version_string_array {
version, err := strconv.Atoi(version_str)
if err != nil {
return nil, fmt.Errorf("error while parsing \"%v\" in \"%v\"", version_string, full_version_string)
return nil, fmt.Errorf("error while parsing \"%v\" in \"%v\"", version_str, version_string)
}
version_array[index] = version
}

View File

@@ -33,6 +33,7 @@ import (
"github.com/google/cadvisor/machine"
"github.com/google/cadvisor/manager/watcher"
dockerutil "github.com/google/cadvisor/utils/docker"
"github.com/google/cadvisor/zfs"
docker "github.com/docker/engine-api/client"
"github.com/golang/glog"
@@ -102,9 +103,13 @@ type dockerFactory struct {
dockerVersion []int
dockerAPIVersion []int
ignoreMetrics container.MetricSet
thinPoolWatcher *devicemapper.ThinPoolWatcher
zfsWatcher *zfs.ZfsWatcher
}
func (self *dockerFactory) String() string {
@@ -132,6 +137,7 @@ func (self *dockerFactory) NewContainerHandler(name string, inHostNamespace bool
self.dockerVersion,
self.ignoreMetrics,
self.thinPoolWatcher,
self.zfsWatcher,
)
return
}
@@ -181,8 +187,10 @@ func (self *dockerFactory) DebugInfo() map[string][]string {
}
var (
version_regexp_string = `(\d+)\.(\d+)\.(\d+)`
version_re = regexp.MustCompile(version_regexp_string)
version_regexp_string = `(\d+)\.(\d+)\.(\d+)`
version_re = regexp.MustCompile(version_regexp_string)
apiversion_regexp_string = `(\d+)\.(\d+)`
apiversion_re = regexp.MustCompile(apiversion_regexp_string)
)
func startThinPoolWatcher(dockerInfo *dockertypes.Info) (*devicemapper.ThinPoolWatcher, error) {
@@ -218,6 +226,21 @@ func startThinPoolWatcher(dockerInfo *dockertypes.Info) (*devicemapper.ThinPoolW
return thinPoolWatcher, nil
}
func startZfsWatcher(dockerInfo *dockertypes.Info) (*zfs.ZfsWatcher, error) {
filesystem, err := dockerutil.DockerZfsFilesystem(*dockerInfo)
if err != nil {
return nil, err
}
zfsWatcher, err := zfs.NewZfsWatcher(filesystem)
if err != nil {
return nil, err
}
go zfsWatcher.Start()
return zfsWatcher, nil
}
func ensureThinLsKernelVersion(kernelVersion string) error {
// kernel 4.4.0 has the proper bug fixes to allow thin_ls to work without corrupting the thin pool
minKernelVersion := semver.MustParse("4.4.0")
@@ -291,7 +314,9 @@ func Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, ignoreMetrics c
}
// Version already validated above, assume no error here.
dockerVersion, _ := parseDockerVersion(dockerInfo.ServerVersion)
dockerVersion, _ := parseVersion(dockerInfo.ServerVersion, version_re, 3)
dockerAPIVersion, _ := APIVersion()
cgroupSubsystems, err := libcontainer.GetCgroupSubsystems()
if err != nil {
@@ -306,17 +331,27 @@ func Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, ignoreMetrics c
}
}
var zfsWatcher *zfs.ZfsWatcher
if storageDriver(dockerInfo.Driver) == zfsStorageDriver {
zfsWatcher, err = startZfsWatcher(dockerInfo)
if err != nil {
glog.Errorf("zfs filesystem stats will not be reported: %v", err)
}
}
glog.Infof("Registering Docker factory")
f := &dockerFactory{
cgroupSubsystems: cgroupSubsystems,
client: client,
dockerVersion: dockerVersion,
dockerAPIVersion: dockerAPIVersion,
fsInfo: fsInfo,
machineInfoFactory: factory,
storageDriver: storageDriver(dockerInfo.Driver),
storageDir: RootDir(),
ignoreMetrics: ignoreMetrics,
thinPoolWatcher: thinPoolWatcher,
zfsWatcher: zfsWatcher,
}
container.RegisterContainerHandlerFactory(f, []watcher.ContainerWatchSource{watcher.Raw})

View File

@@ -29,6 +29,7 @@ import (
"github.com/google/cadvisor/fs"
info "github.com/google/cadvisor/info/v1"
dockerutil "github.com/google/cadvisor/utils/docker"
"github.com/google/cadvisor/zfs"
docker "github.com/docker/engine-api/client"
dockercontainer "github.com/docker/engine-api/types/container"
@@ -42,6 +43,7 @@ import (
const (
// The read write layers exist here.
aufsRWLayer = "diff"
// Path to the directory where docker stores log files if the json logging driver is enabled.
pathToContainersDir = "containers"
)
@@ -72,6 +74,12 @@ type dockerContainerHandler struct {
// the devicemapper device id for the container
deviceID string
// zfs Filesystem
zfsFilesystem string
// zfsParent is the parent for docker zfs
zfsParent string
// Time at which this container was created.
creationTime time.Time
@@ -101,6 +109,9 @@ type dockerContainerHandler struct {
// thin pool watcher
thinPoolWatcher *devicemapper.ThinPoolWatcher
// zfs watcher
zfsWatcher *zfs.ZfsWatcher
}
var _ container.ContainerHandler = &dockerContainerHandler{}
@@ -136,6 +147,7 @@ func newDockerContainerHandler(
dockerVersion []int,
ignoreMetrics container.MetricSet,
thinPoolWatcher *devicemapper.ThinPoolWatcher,
zfsWatcher *zfs.ZfsWatcher,
) (container.ContainerHandler, error) {
// Create the cgroup paths.
cgroupPaths := make(map[string]string, len(cgroupSubsystems.MountPoints))
@@ -172,12 +184,21 @@ func newDockerContainerHandler(
var (
rootfsStorageDir string
poolName string
zfsFilesystem string
zfsParent string
)
switch storageDriver {
case aufsStorageDriver:
rootfsStorageDir = path.Join(storageDir, string(aufsStorageDriver), aufsRWLayer, rwLayerID)
case overlayStorageDriver:
rootfsStorageDir = path.Join(storageDir, string(overlayStorageDriver), rwLayerID)
case zfsStorageDriver:
status, err := Status()
if err != nil {
return nil, fmt.Errorf("unable to determine docker status: %v", err)
}
zfsParent = status.DriverStatus[dockerutil.DriverStatusParentDataset]
zfsFilesystem = path.Join(zfsParent, rwLayerID)
case devicemapperStorageDriver:
status, err := Status()
if err != nil {
@@ -199,10 +220,13 @@ func newDockerContainerHandler(
fsInfo: fsInfo,
rootFs: rootFs,
poolName: poolName,
zfsFilesystem: zfsFilesystem,
rootfsStorageDir: rootfsStorageDir,
envs: make(map[string]string),
ignoreMetrics: ignoreMetrics,
thinPoolWatcher: thinPoolWatcher,
zfsWatcher: zfsWatcher,
zfsParent: zfsParent,
}
// We assume that if Inspect fails then the container is not known to docker.
@@ -245,7 +269,9 @@ func newDockerContainerHandler(
handler.fsHandler = &dockerFsHandler{
fsHandler: common.NewFsHandler(common.DefaultPeriod, rootfsStorageDir, otherStorageDir, fsInfo),
thinPoolWatcher: thinPoolWatcher,
zfsWatcher: zfsWatcher,
deviceID: handler.deviceID,
zfsFilesystem: zfsFilesystem,
}
}
@@ -265,7 +291,7 @@ func newDockerContainerHandler(
}
// dockerFsHandler is a composite FsHandler implementation the incorporates
// the common fs handler and a devicemapper ThinPoolWatcher.
// the common fs handler, a devicemapper ThinPoolWatcher, and a zfsWatcher
type dockerFsHandler struct {
fsHandler common.FsHandler
@@ -273,6 +299,11 @@ type dockerFsHandler struct {
thinPoolWatcher *devicemapper.ThinPoolWatcher
// deviceID is the id of the container's fs device
deviceID string
// zfsWatcher is the zfs filesystem watcher
zfsWatcher *zfs.ZfsWatcher
// zfsFilesystem is the docker zfs filesystem
zfsFilesystem string
}
var _ common.FsHandler = &dockerFsHandler{}
@@ -306,6 +337,15 @@ func (h *dockerFsHandler) Usage() common.FsUsage {
}
}
if h.zfsWatcher != nil {
zfsUsage, err := h.zfsWatcher.GetUsage(h.zfsFilesystem)
if err != nil {
glog.V(5).Infof("unable to get fs usage from zfs for filesystem %s: %v", h.zfsFilesystem, err)
} else {
usage.BaseUsageBytes = zfsUsage
usage.TotalUsageBytes += zfsUsage
}
}
return usage
}
@@ -359,12 +399,14 @@ func (self *dockerContainerHandler) getFsStats(stats *info.ContainerStats) error
// Device has to be the pool name to correlate with the device name as
// set in the machine info filesystems.
device = self.poolName
case aufsStorageDriver, overlayStorageDriver, zfsStorageDriver:
case aufsStorageDriver, overlayStorageDriver:
deviceInfo, err := self.fsInfo.GetDirFsDevice(self.rootfsStorageDir)
if err != nil {
return fmt.Errorf("unable to determine device info for dir: %v: %v", self.rootfsStorageDir, err)
}
device = deviceInfo.Device
case zfsStorageDriver:
device = self.zfsParent
default:
return nil
}