Move auto-registration to explicit install packages, register plugin interfaces

This commit is contained in:
Jordan Liggitt 2019-04-09 22:08:14 +00:00
parent f7b509202d
commit e24fd90ae9
16 changed files with 271 additions and 57 deletions

View File

@ -34,6 +34,9 @@ import (
"github.com/google/cadvisor/utils/sysfs" "github.com/google/cadvisor/utils/sysfs"
"github.com/google/cadvisor/version" "github.com/google/cadvisor/version"
// Register container providers
_ "github.com/google/cadvisor/container/install"
// Register CloudProviders // Register CloudProviders
_ "github.com/google/cadvisor/utils/cloudinfo/aws" _ "github.com/google/cadvisor/utils/cloudinfo/aws"
_ "github.com/google/cadvisor/utils/cloudinfo/azure" _ "github.com/google/cadvisor/utils/cloudinfo/azure"

View File

@ -12,21 +12,17 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package containerd // The install package registers containerd.NewPlugin() as the "containerd" container provider when imported
package install
import ( import (
"github.com/google/cadvisor/container" "github.com/google/cadvisor/container"
"github.com/google/cadvisor/fs" "github.com/google/cadvisor/container/containerd"
info "github.com/google/cadvisor/info/v1"
"github.com/google/cadvisor/watcher"
"k8s.io/klog" "k8s.io/klog"
) )
func init() { func init() {
err := container.RegisterPlugin("containerd", func(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) { err := container.RegisterPlugin("containerd", containerd.NewPlugin())
err := Register(factory, fsInfo, includedMetrics)
return nil, err
})
if err != nil { if err != nil {
klog.Fatalf("Failed to register containerd plugin: %v", err) klog.Fatalf("Failed to register containerd plugin: %v", err)
} }

View File

@ -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
}

View File

@ -12,21 +12,17 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package crio // The install package registers crio.NewPlugin() as the "crio" container provider when imported
package install
import ( import (
"github.com/google/cadvisor/container" "github.com/google/cadvisor/container"
"github.com/google/cadvisor/fs" "github.com/google/cadvisor/container/crio"
info "github.com/google/cadvisor/info/v1"
"github.com/google/cadvisor/watcher"
"k8s.io/klog" "k8s.io/klog"
) )
func init() { func init() {
err := container.RegisterPlugin("crio", func(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) { err := container.RegisterPlugin("crio", crio.NewPlugin())
err := Register(factory, fsInfo, includedMetrics)
return nil, err
})
if err != nil { if err != nil {
klog.Fatalf("Failed to register crio plugin: %v", err) klog.Fatalf("Failed to register crio plugin: %v", err)
} }

34
container/crio/plugin.go Normal file
View File

@ -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
}

View File

@ -12,21 +12,17 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package docker // The install package registers docker.NewPlugin() as the "docker" container provider when imported
package install
import ( import (
"github.com/google/cadvisor/container" "github.com/google/cadvisor/container"
"github.com/google/cadvisor/fs" "github.com/google/cadvisor/container/docker"
info "github.com/google/cadvisor/info/v1"
"github.com/google/cadvisor/watcher"
"k8s.io/klog" "k8s.io/klog"
) )
func init() { func init() {
err := container.RegisterPlugin("docker", func(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) { err := container.RegisterPlugin("docker", docker.NewPlugin())
err := Register(factory, fsInfo, includedMetrics)
return nil, err
})
if err != nil { if err != nil {
klog.Fatalf("Failed to register docker plugin: %v", err) klog.Fatalf("Failed to register docker plugin: %v", err)
} }

View File

@ -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
}

View File

@ -73,13 +73,17 @@ func (ms MetricSet) Add(mk MetricKind) {
ms[mk] = struct{}{} ms[mk] = struct{}{}
} }
type Register func(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics MetricSet) (watcher.ContainerWatcher, error)
// All registered auth provider plugins. // All registered auth provider plugins.
var pluginsLock sync.Mutex 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() pluginsLock.Lock()
defer pluginsLock.Unlock() defer pluginsLock.Unlock()
if _, found := plugins[name]; found { if _, found := plugins[name]; found {
@ -95,8 +99,8 @@ func InitializePlugins(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includ
defer pluginsLock.Unlock() defer pluginsLock.Unlock()
containerWatchers := []watcher.ContainerWatcher{} containerWatchers := []watcher.ContainerWatcher{}
for name, register := range plugins { for name, plugin := range plugins {
watcher, err := register(factory, fsInfo, includedMetrics) watcher, err := plugin.Register(factory, fsInfo, includedMetrics)
if err != nil { if err != nil {
klog.V(5).Infof("Registration of the %s container factory failed: %v", name, err) klog.V(5).Infof("Registration of the %s container factory failed: %v", name, err)
} }

View File

@ -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"
)

View File

@ -12,21 +12,17 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package mesos // The install package registers mesos.NewPlugin() as the "mesos" container provider when imported
package install
import ( import (
"github.com/google/cadvisor/container" "github.com/google/cadvisor/container"
"github.com/google/cadvisor/fs" "github.com/google/cadvisor/container/mesos"
info "github.com/google/cadvisor/info/v1"
"github.com/google/cadvisor/watcher"
"k8s.io/klog" "k8s.io/klog"
) )
func init() { func init() {
err := container.RegisterPlugin("mesos", func(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) { err := container.RegisterPlugin("mesos", mesos.NewPlugin())
err := Register(factory, fsInfo, includedMetrics)
return nil, err
})
if err != nil { if err != nil {
klog.Fatalf("Failed to register mesos plugin: %v", err) klog.Fatalf("Failed to register mesos plugin: %v", err)
} }

34
container/mesos/plugin.go Normal file
View File

@ -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
}

View File

@ -12,24 +12,17 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package rkt // The install package registers rkt.NewPlugin() as the "rkt" container provider when imported
package install
import ( import (
"github.com/google/cadvisor/container" "github.com/google/cadvisor/container"
"github.com/google/cadvisor/fs" "github.com/google/cadvisor/container/rkt"
info "github.com/google/cadvisor/info/v1"
"github.com/google/cadvisor/watcher"
"k8s.io/klog" "k8s.io/klog"
) )
func init() { func init() {
err := container.RegisterPlugin("rkt", func(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) { err := container.RegisterPlugin("rkt", rkt.NewPlugin())
err := Register(factory, fsInfo, includedMetrics)
if err != nil {
return nil, err
}
return NewRktContainerWatcher()
})
if err != nil { if err != nil {
klog.Fatalf("Failed to register rkt plugin: %v", err) klog.Fatalf("Failed to register rkt plugin: %v", err)
} }

37
container/rkt/plugin.go Normal file
View File

@ -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()
}

View File

@ -12,21 +12,17 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package systemd // The install package registers systemd.NewPlugin() as the "systemd" container provider when imported
package install
import ( import (
"github.com/google/cadvisor/container" "github.com/google/cadvisor/container"
"github.com/google/cadvisor/fs" "github.com/google/cadvisor/container/systemd"
info "github.com/google/cadvisor/info/v1"
"github.com/google/cadvisor/watcher"
"k8s.io/klog" "k8s.io/klog"
) )
func init() { func init() {
err := container.RegisterPlugin("systemd", func(factory info.MachineInfoFactory, fsInfo fs.FsInfo, includedMetrics container.MetricSet) (watcher.ContainerWatcher, error) { err := container.RegisterPlugin("systemd", systemd.NewPlugin())
err := Register(factory, fsInfo, includedMetrics)
return nil, err
})
if err != nil { if err != nil {
klog.Fatalf("Failed to register systemd plugin: %v", err) klog.Fatalf("Failed to register systemd plugin: %v", err)
} }

View File

@ -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
}

View File

@ -35,6 +35,8 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
clock "k8s.io/utils/clock/testing" clock "k8s.io/utils/clock/testing"
_ "github.com/google/cadvisor/container/install"
) )
// TODO(vmarmol): Refactor these tests. // TODO(vmarmol): Refactor these tests.