From e24fd90ae9cea33e1e2091f645b7e84445e521a9 Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Tue, 9 Apr 2019 22:08:14 +0000 Subject: [PATCH] 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.