Update plugin load and snapshot service

Allow plugins to be mapped and returned by their ID.
Add skip plugin to allow plugins to decide whether they should
be loaded.

Signed-off-by: Derek McGowan <derek@mcgstyle.net>
This commit is contained in:
Derek McGowan 2017-06-27 16:55:50 -07:00
parent 5b105f86ce
commit 3db8adc5d7
No known key found for this signature in database
GPG Key ID: F58C5D0A4405ACDB
11 changed files with 82 additions and 47 deletions

View File

@ -15,7 +15,5 @@ func defaultConfig() *server.Config {
Level: "info",
Address: "/run/containerd/debug.sock",
},
Snapshotter: "io.containerd.snapshotter.v1.overlayfs",
Differ: "io.containerd.differ.v1.base-diff",
}
}

View File

@ -14,7 +14,5 @@ func defaultConfig() *server.Config {
Level: "info",
Address: "/run/containerd/debug.sock",
},
Snapshotter: "io.containerd.snapshotter.v1.naive",
Differ: "io.containerd.differ.v1.base-diff",
}
}

View File

@ -17,7 +17,5 @@ func defaultConfig() *server.Config {
Level: "info",
Address: `\\.\pipe\containerd-debug`,
},
Snapshotter: "io.containerd.snapshotter.v1.windows",
Differ: "io.containerd.differ.v1.base-diff",
}
}

View File

@ -9,7 +9,7 @@ import (
"github.com/containerd/containerd/log"
)
func NewContext(ctx context.Context, plugins map[PluginType][]interface{}, root, id string) *InitContext {
func NewContext(ctx context.Context, plugins map[PluginType]map[string]interface{}, root, id string) *InitContext {
return &InitContext{
plugins: plugins,
Root: filepath.Join(root, id),
@ -23,18 +23,17 @@ type InitContext struct {
Config interface{}
Emitter *events.Emitter
plugins map[PluginType][]interface{}
plugins map[PluginType]map[string]interface{}
}
func (i *InitContext) Get(t PluginType) (interface{}, error) {
p := i.plugins[t]
if len(p) == 0 {
return nil, fmt.Errorf("no plugins registered for %s", t)
for _, v := range i.plugins[t] {
return v, nil
}
return p[0], nil
return nil, fmt.Errorf("no plugins registered for %s", t)
}
func (i *InitContext) GetAll(t PluginType) ([]interface{}, error) {
func (i *InitContext) GetAll(t PluginType) (map[string]interface{}, error) {
p, ok := i.plugins[t]
if !ok {
return nil, fmt.Errorf("no plugins registered for %s", t)

View File

@ -1,18 +1,31 @@
package plugin
import (
"errors"
"fmt"
"sync"
"github.com/pkg/errors"
"google.golang.org/grpc"
)
var (
ErrNoPluginType = errors.New("plugin: no type")
ErrNoPluginID = errors.New("plugin: no id")
// SkipPlugin is used when a plugin is not initialized and should not be loaded,
// this allows the plugin loader differentiate between a plugin which is configured
// not to load and one that fails to load.
SkipPlugin = errors.New("skip plugin")
)
// IsSkipPlugin returns true if the error is skipping the plugin
func IsSkipPlugin(err error) bool {
if errors.Cause(err) == SkipPlugin {
return true
}
return false
}
type PluginType string
const (

View File

@ -17,11 +17,6 @@ type Config struct {
Debug Debug `toml:"debug"`
// Metrics and monitoring settings
Metrics MetricsConfig `toml:"metrics"`
// Snapshotter specifies which snapshot driver to use
Snapshotter string `toml:"snapshotter"`
// Differ specifies which differ to use. Differ is tightly coupled with the snapshotter
// so not all combinations may work.
Differ string `toml:"differ"`
// Plugins provides plugin specific configuration for the initialization of a plugin
Plugins map[string]toml.Primitive `toml:"plugins"`
// Enable containerd as a subreaper

View File

@ -55,14 +55,10 @@ func New(ctx context.Context, config *Config) (*Server, error) {
rpc: rpc,
emitter: events.NewEmitter(),
}
initialized = make(map[plugin.PluginType][]interface{})
initialized = make(map[plugin.PluginType]map[string]interface{})
)
for _, p := range plugins {
id := p.URI()
if !shouldLoadPlugin(p, config) {
log.G(ctx).WithField("type", p.Type).Infof("skip loading plugin %q...", id)
continue
}
log.G(ctx).WithField("type", p.Type).Infof("loading plugin %q...", id)
initContext := plugin.NewContext(
@ -83,10 +79,21 @@ func New(ctx context.Context, config *Config) (*Server, error) {
}
instance, err := p.Init(initContext)
if err != nil {
log.G(ctx).WithError(err).Warnf("failed to load plugin %s", id)
if plugin.IsSkipPlugin(err) {
log.G(ctx).WithField("type", p.Type).Infof("skip loading plugin %q...", id)
} else {
log.G(ctx).WithError(err).Warnf("failed to load plugin %s", id)
}
continue
}
initialized[p.Type] = append(initialized[p.Type], instance)
if types, ok := initialized[p.Type]; ok {
types[p.ID] = instance
} else {
initialized[p.Type] = map[string]interface{}{
p.ID: instance,
}
}
// check for grpc services that should be registered with the server
if service, ok := instance.(plugin.Service); ok {
services = append(services, service)
@ -170,17 +177,6 @@ func loadPlugins(config *Config) ([]*plugin.Registration, error) {
return plugin.Graph(), nil
}
func shouldLoadPlugin(p *plugin.Registration, config *Config) bool {
switch p.Type {
case plugin.SnapshotPlugin:
return p.URI() == config.Snapshotter
case plugin.DiffPlugin:
return p.URI() == config.Differ
default:
return true
}
}
func interceptor(
ctx context.Context,
req interface{},

View File

@ -0,0 +1,5 @@
package snapshot
const (
defaultSnapshotter = "overlayfs"
)

View File

@ -0,0 +1,7 @@
// +build darwin freebsd
package snapshot
const (
defaultSnapshotter = "naive"
)

View File

@ -0,0 +1,5 @@
package snapshot
const (
defaultSnapshotter = "windows"
)

View File

@ -3,36 +3,40 @@ package snapshot
import (
gocontext "context"
"github.com/boltdb/bolt"
eventsapi "github.com/containerd/containerd/api/services/events/v1"
snapshotapi "github.com/containerd/containerd/api/services/snapshot/v1"
"github.com/containerd/containerd/api/types"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/events"
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/metadata"
"github.com/containerd/containerd/mount"
"github.com/containerd/containerd/plugin"
"github.com/containerd/containerd/snapshot"
"github.com/containerd/containerd/snapshot/namespaced"
protoempty "github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
type config struct {
// Default is the default snapshotter to use for the service
Default string `toml:"default,omitempty"`
}
func init() {
plugin.Register(&plugin.Registration{
Type: plugin.GRPCPlugin,
ID: "snapshots",
Requires: []plugin.PluginType{
plugin.SnapshotPlugin,
plugin.MetadataPlugin,
},
Init: func(ic *plugin.InitContext) (interface{}, error) {
e := events.GetPoster(ic.Context)
s, err := ic.Get(plugin.SnapshotPlugin)
if err != nil {
return nil, err
}
return newService(s.(snapshot.Snapshotter), e)
Config: &config{
Default: defaultSnapshotter,
},
Init: newService,
})
}
@ -43,9 +47,26 @@ type service struct {
emitter events.Poster
}
func newService(snapshotter snapshot.Snapshotter, evts events.Poster) (*service, error) {
func newService(ic *plugin.InitContext) (interface{}, error) {
evts := events.GetPoster(ic.Context)
snapshotters, err := ic.GetAll(plugin.SnapshotPlugin)
if err != nil {
return nil, err
}
cfg := ic.Config.(*config)
sn, ok := snapshotters[cfg.Default]
if !ok {
return nil, errors.Errorf("default snapshotter not loaded: %s", cfg.Default)
}
md, err := ic.Get(plugin.MetadataPlugin)
if err != nil {
return nil, err
}
return &service{
snapshotter: namespaced.NewSnapshotter(snapshotter),
snapshotter: metadata.NewSnapshotter(md.(*bolt.DB), cfg.Default, sn.(snapshot.Snapshotter)),
emitter: evts,
}, nil
}