From f7b509202d859cbd2885d4ae82598f73bdb8ec47 Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Tue, 9 Apr 2019 16:59:14 -0400 Subject: [PATCH 1/7] Restore manager.New signature, initialization code --- cadvisor.go | 2 +- cadvisor_helper.go | 167 ------------------------------------------- manager/manager.go | 171 ++++++++++++++++++++++++++++++++++----------- 3 files changed, 131 insertions(+), 209 deletions(-) delete mode 100644 cadvisor_helper.go diff --git a/cadvisor.go b/cadvisor.go index 13394546..02b1ea69 100644 --- a/cadvisor.go +++ b/cadvisor.go @@ -150,7 +150,7 @@ func main() { collectorHttpClient := createCollectorHttpClient(*collectorCert, *collectorKey) - containerManager, err := New(memoryStorage, sysFs, *maxHousekeepingInterval, *allowDynamicHousekeeping, includedMetrics, &collectorHttpClient, strings.Split(*rawCgroupPrefixWhiteList, ",")) + containerManager, err := manager.New(memoryStorage, sysFs, *maxHousekeepingInterval, *allowDynamicHousekeeping, includedMetrics, &collectorHttpClient, strings.Split(*rawCgroupPrefixWhiteList, ",")) if err != nil { klog.Fatalf("Failed to create a Container Manager: %s", err) } diff --git a/cadvisor_helper.go b/cadvisor_helper.go deleted file mode 100644 index eb24004d..00000000 --- a/cadvisor_helper.go +++ /dev/null @@ -1,167 +0,0 @@ -// Copyright 2019 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. - -package main - -import ( - "context" - "fmt" - "net/http" - "os" - "time" - - "github.com/google/cadvisor/accelerators" - "github.com/google/cadvisor/cache/memory" - "github.com/google/cadvisor/container" - _ "github.com/google/cadvisor/container/containerd" - "github.com/google/cadvisor/container/crio" - "github.com/google/cadvisor/container/docker" - _ "github.com/google/cadvisor/container/mesos" - _ "github.com/google/cadvisor/container/raw" - "github.com/google/cadvisor/container/rkt" - _ "github.com/google/cadvisor/container/systemd" - "github.com/google/cadvisor/fs" - info "github.com/google/cadvisor/info/v1" - "github.com/google/cadvisor/machine" - "github.com/google/cadvisor/manager" - "github.com/google/cadvisor/utils/sysfs" - "github.com/google/cadvisor/watcher" - - "github.com/opencontainers/runc/libcontainer/cgroups" - "k8s.io/klog" -) - -const dockerClientTimeout = 10 * time.Second - -// New takes a memory storage and returns a new manager. -func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, maxHousekeepingInterval time.Duration, allowDynamicHousekeeping bool, includedMetricsSet container.MetricSet, collectorHttpClient *http.Client, rawContainerCgroupPathPrefixWhiteList []string) (manager.Manager, error) { - if memoryCache == nil { - return nil, fmt.Errorf("manager requires memory storage") - } - - // Detect the container we are running on. - selfContainer, err := cgroups.GetOwnCgroupPath("cpu") - if err != nil { - return nil, err - } - klog.V(2).Infof("cAdvisor running in container: %q", selfContainer) - - var ( - dockerStatus info.DockerStatus - rktPath string - ) - docker.SetTimeout(dockerClientTimeout) - // Try to connect to docker indefinitely on startup. - dockerStatus = retryDockerStatus() - - if tmpRktPath, err := rkt.RktPath(); err != nil { - klog.V(5).Infof("Rkt not connected: %v", err) - } else { - rktPath = tmpRktPath - } - - crioClient, err := crio.Client() - if err != nil { - return nil, err - } - crioInfo, err := crioClient.Info() - if err != nil { - klog.V(5).Infof("CRI-O not connected: %v", err) - } - - context := fs.Context{ - Docker: fs.DockerContext{ - Root: docker.RootDir(), - Driver: dockerStatus.Driver, - DriverStatus: dockerStatus.DriverStatus, - }, - RktPath: rktPath, - Crio: fs.CrioContext{ - Root: crioInfo.StorageRoot, - }, - } - fsInfo, err := fs.NewFsInfo(context) - if err != nil { - return nil, err - } - - // If cAdvisor was started with host's rootfs mounted, assume that its running - // in its own namespaces. - inHostNamespace := false - if _, err := os.Stat("/rootfs/proc"); os.IsNotExist(err) { - inHostNamespace = true - } - - // Register for new subcontainers. - eventsChannel := make(chan watcher.ContainerEvent, 16) - - machineInfo, err := machine.Info(sysfs, fsInfo, inHostNamespace) - if err != nil { - return nil, err - } - klog.V(1).Infof("Machine: %+v", *machineInfo) - - newManager := manager.New( - memoryCache, - fsInfo, - sysfs, - *machineInfo, - make([]chan error, 0, 2), - selfContainer, - inHostNamespace, - time.Now(), - maxHousekeepingInterval, - allowDynamicHousekeeping, - includedMetricsSet, - []watcher.ContainerWatcher{}, - eventsChannel, - collectorHttpClient, - &accelerators.NvidiaManager{}, - rawContainerCgroupPathPrefixWhiteList) - - versionInfo, err := newManager.GetVersionInfo() - if err != nil { - return nil, err - } - klog.V(1).Infof("Version: %+v", *versionInfo) - return newManager, nil -} - -func retryDockerStatus() info.DockerStatus { - startupTimeout := dockerClientTimeout - maxTimeout := 4 * startupTimeout - for { - ctx, e := context.WithTimeout(context.Background(), startupTimeout) - if e != nil { - klog.V(5).Infof("error during timeout: %v", e) - } - dockerStatus, err := docker.StatusWithContext(ctx) - if err == nil { - return dockerStatus - } - - switch err { - case context.DeadlineExceeded: - klog.Warningf("Timeout trying to communicate with docker during initialization, will retry") - default: - klog.V(5).Infof("Docker not connected: %v", err) - return info.DockerStatus{} - } - - startupTimeout = 2 * startupTimeout - if startupTimeout > maxTimeout { - startupTimeout = maxTimeout - } - } -} diff --git a/manager/manager.go b/manager/manager.go index 5f4fc251..b90b1860 100644 --- a/manager/manager.go +++ b/manager/manager.go @@ -19,6 +19,7 @@ import ( "flag" "fmt" "net/http" + "os" "path" "strconv" "strings" @@ -29,8 +30,10 @@ import ( "github.com/google/cadvisor/cache/memory" "github.com/google/cadvisor/collector" "github.com/google/cadvisor/container" + "github.com/google/cadvisor/container/crio" "github.com/google/cadvisor/container/docker" "github.com/google/cadvisor/container/raw" + "github.com/google/cadvisor/container/rkt" "github.com/google/cadvisor/events" "github.com/google/cadvisor/fs" info "github.com/google/cadvisor/info/v1" @@ -41,6 +44,8 @@ import ( "github.com/google/cadvisor/version" "github.com/google/cadvisor/watcher" + "github.com/opencontainers/runc/libcontainer/cgroups" + "golang.org/x/net/context" "k8s.io/klog" "k8s.io/utils/clock" ) @@ -52,6 +57,8 @@ var eventStorageAgeLimit = flag.String("event_storage_age_limit", "default=24h", var eventStorageEventLimit = flag.String("event_storage_event_limit", "default=100000", "Max number of events to store (per type). Value is a comma separated list of key values, where the keys are event types (e.g.: creation, oom) or \"default\" and the value is an integer. Default is applied to all non-specified event types") var applicationMetricsCountLimit = flag.Int("application_metrics_count_limit", 100, "Max number of application metrics to store (per container)") +const dockerClientTimeout = 10 * time.Second + // The Manager interface defines operations for starting a manager and getting // container and machine information. type Manager interface { @@ -130,6 +137,129 @@ type Manager interface { DebugInfo() map[string][]string } +// New takes a memory storage and returns a new manager. +func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, maxHousekeepingInterval time.Duration, allowDynamicHousekeeping bool, includedMetricsSet container.MetricSet, collectorHttpClient *http.Client, rawContainerCgroupPathPrefixWhiteList []string) (Manager, error) { + if memoryCache == nil { + return nil, fmt.Errorf("manager requires memory storage") + } + + // Detect the container we are running on. + selfContainer, err := cgroups.GetOwnCgroupPath("cpu") + if err != nil { + return nil, err + } + klog.V(2).Infof("cAdvisor running in container: %q", selfContainer) + + var ( + dockerStatus info.DockerStatus + rktPath string + ) + docker.SetTimeout(dockerClientTimeout) + // Try to connect to docker indefinitely on startup. + dockerStatus = retryDockerStatus() + + if tmpRktPath, err := rkt.RktPath(); err != nil { + klog.V(5).Infof("Rkt not connected: %v", err) + } else { + rktPath = tmpRktPath + } + + crioClient, err := crio.Client() + if err != nil { + return nil, err + } + crioInfo, err := crioClient.Info() + if err != nil { + klog.V(5).Infof("CRI-O not connected: %v", err) + } + + context := fs.Context{ + Docker: fs.DockerContext{ + Root: docker.RootDir(), + Driver: dockerStatus.Driver, + DriverStatus: dockerStatus.DriverStatus, + }, + RktPath: rktPath, + Crio: fs.CrioContext{ + Root: crioInfo.StorageRoot, + }, + } + fsInfo, err := fs.NewFsInfo(context) + if err != nil { + return nil, err + } + + // If cAdvisor was started with host's rootfs mounted, assume that its running + // in its own namespaces. + inHostNamespace := false + if _, err := os.Stat("/rootfs/proc"); os.IsNotExist(err) { + inHostNamespace = true + } + + // Register for new subcontainers. + eventsChannel := make(chan watcher.ContainerEvent, 16) + + newManager := &manager{ + containers: make(map[namespacedContainerName]*containerData), + quitChannels: make([]chan error, 0, 2), + memoryCache: memoryCache, + fsInfo: fsInfo, + sysFs: sysfs, + cadvisorContainer: selfContainer, + inHostNamespace: inHostNamespace, + startupTime: time.Now(), + maxHousekeepingInterval: maxHousekeepingInterval, + allowDynamicHousekeeping: allowDynamicHousekeeping, + includedMetrics: includedMetricsSet, + containerWatchers: []watcher.ContainerWatcher{}, + eventsChannel: eventsChannel, + collectorHttpClient: collectorHttpClient, + nvidiaManager: &accelerators.NvidiaManager{}, + rawContainerCgroupPathPrefixWhiteList: rawContainerCgroupPathPrefixWhiteList, + } + + machineInfo, err := machine.Info(sysfs, fsInfo, inHostNamespace) + if err != nil { + return nil, err + } + newManager.machineInfo = *machineInfo + klog.V(1).Infof("Machine: %+v", newManager.machineInfo) + + versionInfo, err := getVersionInfo() + if err != nil { + return nil, err + } + klog.V(1).Infof("Version: %+v", *versionInfo) + + newManager.eventHandler = events.NewEventManager(parseEventsStoragePolicy()) + return newManager, nil +} + +func retryDockerStatus() info.DockerStatus { + startupTimeout := dockerClientTimeout + maxTimeout := 4 * startupTimeout + for { + ctx, _ := context.WithTimeout(context.Background(), startupTimeout) + dockerStatus, err := docker.StatusWithContext(ctx) + if err == nil { + return dockerStatus + } + + switch err { + case context.DeadlineExceeded: + klog.Warningf("Timeout trying to communicate with docker during initialization, will retry") + default: + klog.V(5).Infof("Docker not connected: %v", err) + return info.DockerStatus{} + } + + startupTimeout = 2 * startupTimeout + if startupTimeout > maxTimeout { + startupTimeout = maxTimeout + } + } +} + // A namespaced container name. type namespacedContainerName struct { // The namespace of the container. Can be empty for the root namespace. @@ -139,47 +269,6 @@ type namespacedContainerName struct { Name string } -func New( - memoryCache *memory.InMemoryCache, - fsInfo fs.FsInfo, - sysFs sysfs.SysFs, - machineInfo info.MachineInfo, - quitChannels []chan error, - cadvisorContainer string, - inHostNamespace bool, - startupTime time.Time, - maxHousekeepingInterval time.Duration, - allowDynamicHousekeeping bool, - includedMetrics container.MetricSet, - containerWatchers []watcher.ContainerWatcher, - eventsChannel chan watcher.ContainerEvent, - collectorHttpClient *http.Client, - nvidiaManager accelerators.AcceleratorManager, - rawContainerCgroupPathPrefixWhiteList []string, -) Manager { - impl := &manager{ - containers: make(map[namespacedContainerName]*containerData), - memoryCache: memoryCache, - fsInfo: fsInfo, - sysFs: sysFs, - machineInfo: machineInfo, - quitChannels: quitChannels, - cadvisorContainer: cadvisorContainer, - inHostNamespace: inHostNamespace, - startupTime: startupTime, - maxHousekeepingInterval: maxHousekeepingInterval, - allowDynamicHousekeeping: allowDynamicHousekeeping, - includedMetrics: includedMetrics, - containerWatchers: containerWatchers, - eventsChannel: eventsChannel, - collectorHttpClient: collectorHttpClient, - nvidiaManager: nvidiaManager, - rawContainerCgroupPathPrefixWhiteList: rawContainerCgroupPathPrefixWhiteList, - } - impl.eventHandler = events.NewEventManager(parseEventsStoragePolicy()) - return impl -} - type manager struct { containers map[namespacedContainerName]*containerData containersLock sync.RWMutex From e24fd90ae9cea33e1e2091f645b7e84445e521a9 Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Tue, 9 Apr 2019 22:08:14 +0000 Subject: [PATCH 2/7] Move auto-registration to explicit install packages, register plugin interfaces --- cadvisor.go | 3 ++ .../{init.go => install/install.go} | 12 ++---- container/containerd/plugin.go | 34 +++++++++++++++++ .../crio/{init.go => install/install.go} | 12 ++---- container/crio/plugin.go | 34 +++++++++++++++++ .../docker/{init.go => install/install.go} | 12 ++---- container/docker/plugin.go | 34 +++++++++++++++++ container/factory.go | 16 +++++--- container/install/install.go | 25 +++++++++++++ .../mesos/{init.go => install/install.go} | 12 ++---- container/mesos/plugin.go | 34 +++++++++++++++++ container/rkt/{init.go => install/install.go} | 15 ++------ container/rkt/plugin.go | 37 +++++++++++++++++++ .../systemd/{init.go => install/install.go} | 12 ++---- container/systemd/plugin.go | 34 +++++++++++++++++ manager/manager_test.go | 2 + 16 files changed, 271 insertions(+), 57 deletions(-) rename container/containerd/{init.go => install/install.go} (66%) create mode 100644 container/containerd/plugin.go rename container/crio/{init.go => install/install.go} (67%) create mode 100644 container/crio/plugin.go rename container/docker/{init.go => install/install.go} (67%) create mode 100644 container/docker/plugin.go create mode 100644 container/install/install.go rename container/mesos/{init.go => install/install.go} (67%) create mode 100644 container/mesos/plugin.go rename container/rkt/{init.go => install/install.go} (64%) create mode 100644 container/rkt/plugin.go rename container/systemd/{init.go => install/install.go} (67%) create mode 100644 container/systemd/plugin.go diff --git a/cadvisor.go b/cadvisor.go index 02b1ea69..2401ec72 100644 --- a/cadvisor.go +++ b/cadvisor.go @@ -34,6 +34,9 @@ import ( "github.com/google/cadvisor/utils/sysfs" "github.com/google/cadvisor/version" + // Register container providers + _ "github.com/google/cadvisor/container/install" + // Register CloudProviders _ "github.com/google/cadvisor/utils/cloudinfo/aws" _ "github.com/google/cadvisor/utils/cloudinfo/azure" diff --git a/container/containerd/init.go b/container/containerd/install/install.go similarity index 66% rename from container/containerd/init.go rename to container/containerd/install/install.go index 2509e19d..8f70e71d 100644 --- a/container/containerd/init.go +++ b/container/containerd/install/install.go @@ -12,21 +12,17 @@ // See the License for the specific language governing permissions and // limitations under the License. -package containerd +// The install package registers containerd.NewPlugin() as the "containerd" container provider when imported +package install import ( "github.com/google/cadvisor/container" - "github.com/google/cadvisor/fs" - info "github.com/google/cadvisor/info/v1" - "github.com/google/cadvisor/watcher" + "github.com/google/cadvisor/container/containerd" "k8s.io/klog" ) func init() { - err := container.RegisterPlugin("containerd", func(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) { - err := Register(factory, fsInfo, includedMetrics) - return nil, err - }) + err := container.RegisterPlugin("containerd", containerd.NewPlugin()) if err != nil { klog.Fatalf("Failed to register containerd plugin: %v", err) } diff --git a/container/containerd/plugin.go b/container/containerd/plugin.go new file mode 100644 index 00000000..a7dca6e5 --- /dev/null +++ b/container/containerd/plugin.go @@ -0,0 +1,34 @@ +// Copyright 2019 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. + +package containerd + +import ( + "github.com/google/cadvisor/container" + "github.com/google/cadvisor/fs" + info "github.com/google/cadvisor/info/v1" + "github.com/google/cadvisor/watcher" +) + +// NewPlugin returns an implementation of container.Plugin suitable for passing to container.RegisterPlugin() +func NewPlugin() container.Plugin { + return &plugin{} +} + +type plugin struct{} + +func (p *plugin) Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) { + err := Register(factory, fsInfo, includedMetrics) + return nil, err +} diff --git a/container/crio/init.go b/container/crio/install/install.go similarity index 67% rename from container/crio/init.go rename to container/crio/install/install.go index 5437b3eb..3b7c414b 100644 --- a/container/crio/init.go +++ b/container/crio/install/install.go @@ -12,21 +12,17 @@ // See the License for the specific language governing permissions and // limitations under the License. -package crio +// The install package registers crio.NewPlugin() as the "crio" container provider when imported +package install import ( "github.com/google/cadvisor/container" - "github.com/google/cadvisor/fs" - info "github.com/google/cadvisor/info/v1" - "github.com/google/cadvisor/watcher" + "github.com/google/cadvisor/container/crio" "k8s.io/klog" ) func init() { - err := container.RegisterPlugin("crio", func(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) { - err := Register(factory, fsInfo, includedMetrics) - return nil, err - }) + err := container.RegisterPlugin("crio", crio.NewPlugin()) if err != nil { klog.Fatalf("Failed to register crio plugin: %v", err) } diff --git a/container/crio/plugin.go b/container/crio/plugin.go new file mode 100644 index 00000000..12116dc9 --- /dev/null +++ b/container/crio/plugin.go @@ -0,0 +1,34 @@ +// Copyright 2019 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. + +package crio + +import ( + "github.com/google/cadvisor/container" + "github.com/google/cadvisor/fs" + info "github.com/google/cadvisor/info/v1" + "github.com/google/cadvisor/watcher" +) + +// NewPlugin returns an implementation of container.Plugin suitable for passing to container.RegisterPlugin() +func NewPlugin() container.Plugin { + return &plugin{} +} + +type plugin struct{} + +func (p *plugin) Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) { + err := Register(factory, fsInfo, includedMetrics) + return nil, err +} diff --git a/container/docker/init.go b/container/docker/install/install.go similarity index 67% rename from container/docker/init.go rename to container/docker/install/install.go index 0f5fe39f..332e646e 100644 --- a/container/docker/init.go +++ b/container/docker/install/install.go @@ -12,21 +12,17 @@ // See the License for the specific language governing permissions and // limitations under the License. -package docker +// The install package registers docker.NewPlugin() as the "docker" container provider when imported +package install import ( "github.com/google/cadvisor/container" - "github.com/google/cadvisor/fs" - info "github.com/google/cadvisor/info/v1" - "github.com/google/cadvisor/watcher" + "github.com/google/cadvisor/container/docker" "k8s.io/klog" ) func init() { - err := container.RegisterPlugin("docker", func(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) { - err := Register(factory, fsInfo, includedMetrics) - return nil, err - }) + err := container.RegisterPlugin("docker", docker.NewPlugin()) if err != nil { klog.Fatalf("Failed to register docker plugin: %v", err) } diff --git a/container/docker/plugin.go b/container/docker/plugin.go new file mode 100644 index 00000000..9fd97182 --- /dev/null +++ b/container/docker/plugin.go @@ -0,0 +1,34 @@ +// Copyright 2019 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. + +package docker + +import ( + "github.com/google/cadvisor/container" + "github.com/google/cadvisor/fs" + info "github.com/google/cadvisor/info/v1" + "github.com/google/cadvisor/watcher" +) + +// NewPlugin returns an implementation of container.Plugin suitable for passing to container.RegisterPlugin() +func NewPlugin() container.Plugin { + return &plugin{} +} + +type plugin struct{} + +func (p *plugin) Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) { + err := Register(factory, fsInfo, includedMetrics) + return nil, err +} diff --git a/container/factory.go b/container/factory.go index 900af747..4094f6cb 100644 --- a/container/factory.go +++ b/container/factory.go @@ -73,13 +73,17 @@ func (ms MetricSet) Add(mk MetricKind) { ms[mk] = struct{}{} } -type Register func(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics MetricSet) (watcher.ContainerWatcher, error) - // All registered auth provider plugins. var pluginsLock sync.Mutex -var plugins = make(map[string]Register) +var plugins = make(map[string]Plugin) -func RegisterPlugin(name string, plugin Register) error { +type Plugin interface { + // Register is invoked when starting a manager. It can optionally return a container watcher. + // A returned error is logged, but is not fatal. + Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics MetricSet) (watcher.ContainerWatcher, error) +} + +func RegisterPlugin(name string, plugin Plugin) error { pluginsLock.Lock() defer pluginsLock.Unlock() if _, found := plugins[name]; found { @@ -95,8 +99,8 @@ func InitializePlugins(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includ defer pluginsLock.Unlock() containerWatchers := []watcher.ContainerWatcher{} - for name, register := range plugins { - watcher, err := register(factory, fsInfo, includedMetrics) + for name, plugin := range plugins { + watcher, err := plugin.Register(factory, fsInfo, includedMetrics) if err != nil { klog.V(5).Infof("Registration of the %s container factory failed: %v", name, err) } diff --git a/container/install/install.go b/container/install/install.go new file mode 100644 index 00000000..85725a33 --- /dev/null +++ b/container/install/install.go @@ -0,0 +1,25 @@ +// Copyright 2019 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. + +// The install package registers all included container providers when imported +package install + +import ( + _ "github.com/google/cadvisor/container/containerd/install" + _ "github.com/google/cadvisor/container/crio/install" + _ "github.com/google/cadvisor/container/docker/install" + _ "github.com/google/cadvisor/container/mesos/install" + _ "github.com/google/cadvisor/container/rkt/install" + _ "github.com/google/cadvisor/container/systemd/install" +) diff --git a/container/mesos/init.go b/container/mesos/install/install.go similarity index 67% rename from container/mesos/init.go rename to container/mesos/install/install.go index cccde2ce..4b9f211e 100644 --- a/container/mesos/init.go +++ b/container/mesos/install/install.go @@ -12,21 +12,17 @@ // See the License for the specific language governing permissions and // limitations under the License. -package mesos +// The install package registers mesos.NewPlugin() as the "mesos" container provider when imported +package install import ( "github.com/google/cadvisor/container" - "github.com/google/cadvisor/fs" - info "github.com/google/cadvisor/info/v1" - "github.com/google/cadvisor/watcher" + "github.com/google/cadvisor/container/mesos" "k8s.io/klog" ) func init() { - err := container.RegisterPlugin("mesos", func(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) { - err := Register(factory, fsInfo, includedMetrics) - return nil, err - }) + err := container.RegisterPlugin("mesos", mesos.NewPlugin()) if err != nil { klog.Fatalf("Failed to register mesos plugin: %v", err) } diff --git a/container/mesos/plugin.go b/container/mesos/plugin.go new file mode 100644 index 00000000..63ce2a35 --- /dev/null +++ b/container/mesos/plugin.go @@ -0,0 +1,34 @@ +// Copyright 2019 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. + +package mesos + +import ( + "github.com/google/cadvisor/container" + "github.com/google/cadvisor/fs" + info "github.com/google/cadvisor/info/v1" + "github.com/google/cadvisor/watcher" +) + +// NewPlugin returns an implementation of container.Plugin suitable for passing to container.RegisterPlugin() +func NewPlugin() container.Plugin { + return &plugin{} +} + +type plugin struct{} + +func (p *plugin) Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) { + err := Register(factory, fsInfo, includedMetrics) + return nil, err +} diff --git a/container/rkt/init.go b/container/rkt/install/install.go similarity index 64% rename from container/rkt/init.go rename to container/rkt/install/install.go index dc104e07..8ae45376 100644 --- a/container/rkt/init.go +++ b/container/rkt/install/install.go @@ -12,24 +12,17 @@ // See the License for the specific language governing permissions and // limitations under the License. -package rkt +// The install package registers rkt.NewPlugin() as the "rkt" container provider when imported +package install import ( "github.com/google/cadvisor/container" - "github.com/google/cadvisor/fs" - info "github.com/google/cadvisor/info/v1" - "github.com/google/cadvisor/watcher" + "github.com/google/cadvisor/container/rkt" "k8s.io/klog" ) func init() { - err := container.RegisterPlugin("rkt", func(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) { - err := Register(factory, fsInfo, includedMetrics) - if err != nil { - return nil, err - } - return NewRktContainerWatcher() - }) + err := container.RegisterPlugin("rkt", rkt.NewPlugin()) if err != nil { klog.Fatalf("Failed to register rkt plugin: %v", err) } diff --git a/container/rkt/plugin.go b/container/rkt/plugin.go new file mode 100644 index 00000000..d3e7440c --- /dev/null +++ b/container/rkt/plugin.go @@ -0,0 +1,37 @@ +// Copyright 2019 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. + +package rkt + +import ( + "github.com/google/cadvisor/container" + "github.com/google/cadvisor/fs" + info "github.com/google/cadvisor/info/v1" + "github.com/google/cadvisor/watcher" +) + +// NewPlugin returns an implementation of container.Plugin suitable for passing to container.RegisterPlugin() +func NewPlugin() container.Plugin { + return &plugin{} +} + +type plugin struct{} + +func (p *plugin) Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) { + err := Register(factory, fsInfo, includedMetrics) + if err != nil { + return nil, err + } + return NewRktContainerWatcher() +} diff --git a/container/systemd/init.go b/container/systemd/install/install.go similarity index 67% rename from container/systemd/init.go rename to container/systemd/install/install.go index 1c91023d..e083b4bc 100644 --- a/container/systemd/init.go +++ b/container/systemd/install/install.go @@ -12,21 +12,17 @@ // See the License for the specific language governing permissions and // limitations under the License. -package systemd +// The install package registers systemd.NewPlugin() as the "systemd" container provider when imported +package install import ( "github.com/google/cadvisor/container" - "github.com/google/cadvisor/fs" - info "github.com/google/cadvisor/info/v1" - "github.com/google/cadvisor/watcher" + "github.com/google/cadvisor/container/systemd" "k8s.io/klog" ) func init() { - err := container.RegisterPlugin("systemd", func(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) { - err := Register(factory, fsInfo, includedMetrics) - return nil, err - }) + err := container.RegisterPlugin("systemd", systemd.NewPlugin()) if err != nil { klog.Fatalf("Failed to register systemd plugin: %v", err) } diff --git a/container/systemd/plugin.go b/container/systemd/plugin.go new file mode 100644 index 00000000..d0538821 --- /dev/null +++ b/container/systemd/plugin.go @@ -0,0 +1,34 @@ +// Copyright 2019 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. + +package systemd + +import ( + "github.com/google/cadvisor/container" + "github.com/google/cadvisor/fs" + info "github.com/google/cadvisor/info/v1" + "github.com/google/cadvisor/watcher" +) + +// NewPlugin returns an implementation of container.Plugin suitable for passing to container.RegisterPlugin() +func NewPlugin() container.Plugin { + return &plugin{} +} + +type plugin struct{} + +func (p *plugin) Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) { + err := Register(factory, fsInfo, includedMetrics) + return nil, err +} diff --git a/manager/manager_test.go b/manager/manager_test.go index 07c1debe..cdf00e17 100644 --- a/manager/manager_test.go +++ b/manager/manager_test.go @@ -35,6 +35,8 @@ import ( "github.com/stretchr/testify/assert" clock "k8s.io/utils/clock/testing" + + _ "github.com/google/cadvisor/container/install" ) // TODO(vmarmol): Refactor these tests. From 7dc4594b3232c56ee751a82c217ee0be40b7596f Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Tue, 9 Apr 2019 21:24:57 +0000 Subject: [PATCH 3/7] Add InitializeFSContext hook to plugins --- container/containerd/plugin.go | 4 ++++ container/crio/plugin.go | 4 ++++ container/docker/plugin.go | 4 ++++ container/factory.go | 17 +++++++++++++++++ container/mesos/plugin.go | 4 ++++ container/rkt/plugin.go | 4 ++++ container/systemd/plugin.go | 4 ++++ manager/manager.go | 5 +++++ 8 files changed, 46 insertions(+) diff --git a/container/containerd/plugin.go b/container/containerd/plugin.go index a7dca6e5..9d42b5d4 100644 --- a/container/containerd/plugin.go +++ b/container/containerd/plugin.go @@ -28,6 +28,10 @@ func NewPlugin() container.Plugin { type plugin struct{} +func (p *plugin) InitializeFSContext(context *fs.Context) error { + return nil +} + func (p *plugin) Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) { err := Register(factory, fsInfo, includedMetrics) return nil, err diff --git a/container/crio/plugin.go b/container/crio/plugin.go index 12116dc9..612bcbf5 100644 --- a/container/crio/plugin.go +++ b/container/crio/plugin.go @@ -28,6 +28,10 @@ func NewPlugin() container.Plugin { type plugin struct{} +func (p *plugin) InitializeFSContext(context *fs.Context) error { + return nil +} + func (p *plugin) Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) { err := Register(factory, fsInfo, includedMetrics) return nil, err diff --git a/container/docker/plugin.go b/container/docker/plugin.go index 9fd97182..63938db8 100644 --- a/container/docker/plugin.go +++ b/container/docker/plugin.go @@ -28,6 +28,10 @@ func NewPlugin() container.Plugin { type plugin struct{} +func (p *plugin) InitializeFSContext(context *fs.Context) error { + return nil +} + func (p *plugin) Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) { err := Register(factory, fsInfo, includedMetrics) return nil, err diff --git a/container/factory.go b/container/factory.go index 4094f6cb..e3d0ff3c 100644 --- a/container/factory.go +++ b/container/factory.go @@ -78,6 +78,10 @@ var pluginsLock sync.Mutex var plugins = make(map[string]Plugin) type Plugin interface { + // InitializeFSContext is invoked when populating an fs.Context object for a new manager. + // A returned error here is fatal. + InitializeFSContext(context *fs.Context) error + // Register is invoked when starting a manager. It can optionally return a container watcher. // A returned error is logged, but is not fatal. Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics MetricSet) (watcher.ContainerWatcher, error) @@ -94,6 +98,19 @@ func RegisterPlugin(name string, plugin Plugin) error { return nil } +func InitializeFSContext(context *fs.Context) error { + pluginsLock.Lock() + defer pluginsLock.Unlock() + for name, plugin := range plugins { + err := plugin.InitializeFSContext(context) + if err != nil { + klog.V(5).Infof("Initialization of the %s context failed: %v", name, err) + return err + } + } + return nil +} + func InitializePlugins(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics MetricSet) []watcher.ContainerWatcher { pluginsLock.Lock() defer pluginsLock.Unlock() diff --git a/container/mesos/plugin.go b/container/mesos/plugin.go index 63ce2a35..3f327ddb 100644 --- a/container/mesos/plugin.go +++ b/container/mesos/plugin.go @@ -28,6 +28,10 @@ func NewPlugin() container.Plugin { type plugin struct{} +func (p *plugin) InitializeFSContext(context *fs.Context) error { + return nil +} + func (p *plugin) Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) { err := Register(factory, fsInfo, includedMetrics) return nil, err diff --git a/container/rkt/plugin.go b/container/rkt/plugin.go index d3e7440c..08970d79 100644 --- a/container/rkt/plugin.go +++ b/container/rkt/plugin.go @@ -28,6 +28,10 @@ func NewPlugin() container.Plugin { type plugin struct{} +func (p *plugin) InitializeFSContext(context *fs.Context) error { + return nil +} + func (p *plugin) Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) { err := Register(factory, fsInfo, includedMetrics) if err != nil { diff --git a/container/systemd/plugin.go b/container/systemd/plugin.go index d0538821..28801a63 100644 --- a/container/systemd/plugin.go +++ b/container/systemd/plugin.go @@ -28,6 +28,10 @@ func NewPlugin() container.Plugin { type plugin struct{} +func (p *plugin) InitializeFSContext(context *fs.Context) error { + return nil +} + func (p *plugin) Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) { err := Register(factory, fsInfo, includedMetrics) return nil, err diff --git a/manager/manager.go b/manager/manager.go index b90b1860..a234d8ff 100644 --- a/manager/manager.go +++ b/manager/manager.go @@ -184,6 +184,11 @@ func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, maxHousekeepingIn Root: crioInfo.StorageRoot, }, } + + if err := container.InitializeFSContext(&context); err != nil { + return nil, err + } + fsInfo, err := fs.NewFsInfo(context) if err != nil { return nil, err From 6757727a00e67e0fae05656a5a496477c70cfc36 Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Tue, 9 Apr 2019 21:28:46 +0000 Subject: [PATCH 4/7] Split rkt context initialization --- container/rkt/plugin.go | 6 ++++++ manager/manager.go | 9 --------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/container/rkt/plugin.go b/container/rkt/plugin.go index 08970d79..cfacc60d 100644 --- a/container/rkt/plugin.go +++ b/container/rkt/plugin.go @@ -19,6 +19,7 @@ import ( "github.com/google/cadvisor/fs" info "github.com/google/cadvisor/info/v1" "github.com/google/cadvisor/watcher" + "k8s.io/klog" ) // NewPlugin returns an implementation of container.Plugin suitable for passing to container.RegisterPlugin() @@ -29,6 +30,11 @@ func NewPlugin() container.Plugin { type plugin struct{} func (p *plugin) InitializeFSContext(context *fs.Context) error { + if tmpRktPath, err := RktPath(); err != nil { + klog.V(5).Infof("Rkt not connected: %v", err) + } else { + context.RktPath = tmpRktPath + } return nil } diff --git a/manager/manager.go b/manager/manager.go index a234d8ff..569be786 100644 --- a/manager/manager.go +++ b/manager/manager.go @@ -33,7 +33,6 @@ import ( "github.com/google/cadvisor/container/crio" "github.com/google/cadvisor/container/docker" "github.com/google/cadvisor/container/raw" - "github.com/google/cadvisor/container/rkt" "github.com/google/cadvisor/events" "github.com/google/cadvisor/fs" info "github.com/google/cadvisor/info/v1" @@ -152,18 +151,11 @@ func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, maxHousekeepingIn var ( dockerStatus info.DockerStatus - rktPath string ) docker.SetTimeout(dockerClientTimeout) // Try to connect to docker indefinitely on startup. dockerStatus = retryDockerStatus() - if tmpRktPath, err := rkt.RktPath(); err != nil { - klog.V(5).Infof("Rkt not connected: %v", err) - } else { - rktPath = tmpRktPath - } - crioClient, err := crio.Client() if err != nil { return nil, err @@ -179,7 +171,6 @@ func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, maxHousekeepingIn Driver: dockerStatus.Driver, DriverStatus: dockerStatus.DriverStatus, }, - RktPath: rktPath, Crio: fs.CrioContext{ Root: crioInfo.StorageRoot, }, From e9a44a2984e304307e14faf1cea32cc9a9b34647 Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Tue, 9 Apr 2019 21:37:33 +0000 Subject: [PATCH 5/7] Split crio context initialization --- container/crio/plugin.go | 12 ++++++++++++ manager/manager.go | 13 ------------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/container/crio/plugin.go b/container/crio/plugin.go index 612bcbf5..6b21a198 100644 --- a/container/crio/plugin.go +++ b/container/crio/plugin.go @@ -19,6 +19,7 @@ import ( "github.com/google/cadvisor/fs" info "github.com/google/cadvisor/info/v1" "github.com/google/cadvisor/watcher" + "k8s.io/klog" ) // NewPlugin returns an implementation of container.Plugin suitable for passing to container.RegisterPlugin() @@ -29,6 +30,17 @@ func NewPlugin() container.Plugin { type plugin struct{} func (p *plugin) InitializeFSContext(context *fs.Context) error { + crioClient, err := Client() + if err != nil { + return err + } + + crioInfo, err := crioClient.Info() + if err != nil { + klog.V(5).Infof("CRI-O not connected: %v", err) + } else { + context.Crio = fs.CrioContext{Root: crioInfo.StorageRoot} + } return nil } diff --git a/manager/manager.go b/manager/manager.go index 569be786..e9225aa1 100644 --- a/manager/manager.go +++ b/manager/manager.go @@ -30,7 +30,6 @@ import ( "github.com/google/cadvisor/cache/memory" "github.com/google/cadvisor/collector" "github.com/google/cadvisor/container" - "github.com/google/cadvisor/container/crio" "github.com/google/cadvisor/container/docker" "github.com/google/cadvisor/container/raw" "github.com/google/cadvisor/events" @@ -156,24 +155,12 @@ func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, maxHousekeepingIn // Try to connect to docker indefinitely on startup. dockerStatus = retryDockerStatus() - crioClient, err := crio.Client() - if err != nil { - return nil, err - } - crioInfo, err := crioClient.Info() - if err != nil { - klog.V(5).Infof("CRI-O not connected: %v", err) - } - context := fs.Context{ Docker: fs.DockerContext{ Root: docker.RootDir(), Driver: dockerStatus.Driver, DriverStatus: dockerStatus.DriverStatus, }, - Crio: fs.CrioContext{ - Root: crioInfo.StorageRoot, - }, } if err := container.InitializeFSContext(&context); err != nil { From a022fa71b7459640ce520ef88d023fdb893998b8 Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Tue, 9 Apr 2019 21:50:26 +0000 Subject: [PATCH 6/7] Split docker context initialization --- container/docker/plugin.go | 39 ++++++++++++++++++++++++++++++++++ manager/manager.go | 43 +------------------------------------- 2 files changed, 40 insertions(+), 42 deletions(-) diff --git a/container/docker/plugin.go b/container/docker/plugin.go index 63938db8..c4ca43a4 100644 --- a/container/docker/plugin.go +++ b/container/docker/plugin.go @@ -15,12 +15,18 @@ package docker import ( + "time" + "github.com/google/cadvisor/container" "github.com/google/cadvisor/fs" info "github.com/google/cadvisor/info/v1" "github.com/google/cadvisor/watcher" + "golang.org/x/net/context" + "k8s.io/klog" ) +const dockerClientTimeout = 10 * time.Second + // NewPlugin returns an implementation of container.Plugin suitable for passing to container.RegisterPlugin() func NewPlugin() container.Plugin { return &plugin{} @@ -29,6 +35,14 @@ func NewPlugin() container.Plugin { type plugin struct{} func (p *plugin) InitializeFSContext(context *fs.Context) error { + SetTimeout(dockerClientTimeout) + // Try to connect to docker indefinitely on startup. + dockerStatus := retryDockerStatus() + context.Docker = fs.DockerContext{ + Root: RootDir(), + Driver: dockerStatus.Driver, + DriverStatus: dockerStatus.DriverStatus, + } return nil } @@ -36,3 +50,28 @@ func (p *plugin) Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo, inc err := Register(factory, fsInfo, includedMetrics) return nil, err } + +func retryDockerStatus() info.DockerStatus { + startupTimeout := dockerClientTimeout + maxTimeout := 4 * startupTimeout + for { + ctx, _ := context.WithTimeout(context.Background(), startupTimeout) + dockerStatus, err := StatusWithContext(ctx) + if err == nil { + return dockerStatus + } + + switch err { + case context.DeadlineExceeded: + klog.Warningf("Timeout trying to communicate with docker during initialization, will retry") + default: + klog.V(5).Infof("Docker not connected: %v", err) + return info.DockerStatus{} + } + + startupTimeout = 2 * startupTimeout + if startupTimeout > maxTimeout { + startupTimeout = maxTimeout + } + } +} diff --git a/manager/manager.go b/manager/manager.go index e9225aa1..ab1770b8 100644 --- a/manager/manager.go +++ b/manager/manager.go @@ -43,7 +43,6 @@ import ( "github.com/google/cadvisor/watcher" "github.com/opencontainers/runc/libcontainer/cgroups" - "golang.org/x/net/context" "k8s.io/klog" "k8s.io/utils/clock" ) @@ -55,8 +54,6 @@ var eventStorageAgeLimit = flag.String("event_storage_age_limit", "default=24h", var eventStorageEventLimit = flag.String("event_storage_event_limit", "default=100000", "Max number of events to store (per type). Value is a comma separated list of key values, where the keys are event types (e.g.: creation, oom) or \"default\" and the value is an integer. Default is applied to all non-specified event types") var applicationMetricsCountLimit = flag.Int("application_metrics_count_limit", 100, "Max number of application metrics to store (per container)") -const dockerClientTimeout = 10 * time.Second - // The Manager interface defines operations for starting a manager and getting // container and machine information. type Manager interface { @@ -148,20 +145,7 @@ func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, maxHousekeepingIn } klog.V(2).Infof("cAdvisor running in container: %q", selfContainer) - var ( - dockerStatus info.DockerStatus - ) - docker.SetTimeout(dockerClientTimeout) - // Try to connect to docker indefinitely on startup. - dockerStatus = retryDockerStatus() - - context := fs.Context{ - Docker: fs.DockerContext{ - Root: docker.RootDir(), - Driver: dockerStatus.Driver, - DriverStatus: dockerStatus.DriverStatus, - }, - } + context := fs.Context{} if err := container.InitializeFSContext(&context); err != nil { return nil, err @@ -218,31 +202,6 @@ func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, maxHousekeepingIn return newManager, nil } -func retryDockerStatus() info.DockerStatus { - startupTimeout := dockerClientTimeout - maxTimeout := 4 * startupTimeout - for { - ctx, _ := context.WithTimeout(context.Background(), startupTimeout) - dockerStatus, err := docker.StatusWithContext(ctx) - if err == nil { - return dockerStatus - } - - switch err { - case context.DeadlineExceeded: - klog.Warningf("Timeout trying to communicate with docker during initialization, will retry") - default: - klog.V(5).Infof("Docker not connected: %v", err) - return info.DockerStatus{} - } - - startupTimeout = 2 * startupTimeout - if startupTimeout > maxTimeout { - startupTimeout = maxTimeout - } - } -} - // A namespaced container name. type namespacedContainerName struct { // The namespace of the container. Can be empty for the root namespace. From f8a73e018b9690a8c6f0c5ce05c367d6bb59bb66 Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Wed, 10 Apr 2019 02:13:24 +0000 Subject: [PATCH 7/7] Move fs.Context to types.go This config struct is now used in the plugin InitializeContext method, so it should be defined in the os-neutral types.go file --- fs/fs.go | 17 ----------------- fs/types.go | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/fs/fs.go b/fs/fs.go index a7542158..269e914f 100644 --- a/fs/fs.go +++ b/fs/fs.go @@ -91,23 +91,6 @@ type RealFsInfo struct { fsUUIDToDeviceName map[string]string } -type Context struct { - // docker root directory. - Docker DockerContext - RktPath string - Crio CrioContext -} - -type DockerContext struct { - Root string - Driver string - DriverStatus map[string]string -} - -type CrioContext struct { - Root string -} - func NewFsInfo(context Context) (FsInfo, error) { mounts, err := mount.GetMounts(nil) if err != nil { diff --git a/fs/types.go b/fs/types.go index 5074f713..0deaa780 100644 --- a/fs/types.go +++ b/fs/types.go @@ -18,6 +18,23 @@ import ( "errors" ) +type Context struct { + // docker root directory. + Docker DockerContext + RktPath string + Crio CrioContext +} + +type DockerContext struct { + Root string + Driver string + DriverStatus map[string]string +} + +type CrioContext struct { + Root string +} + type DeviceInfo struct { Device string Major uint