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", Level: "info",
Address: "/run/containerd/debug.sock", 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", Level: "info",
Address: "/run/containerd/debug.sock", 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", Level: "info",
Address: `\\.\pipe\containerd-debug`, 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" "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{ return &InitContext{
plugins: plugins, plugins: plugins,
Root: filepath.Join(root, id), Root: filepath.Join(root, id),
@ -23,18 +23,17 @@ type InitContext struct {
Config interface{} Config interface{}
Emitter *events.Emitter Emitter *events.Emitter
plugins map[PluginType][]interface{} plugins map[PluginType]map[string]interface{}
} }
func (i *InitContext) Get(t PluginType) (interface{}, error) { func (i *InitContext) Get(t PluginType) (interface{}, error) {
p := i.plugins[t] for _, v := range i.plugins[t] {
if len(p) == 0 { return v, nil
return nil, fmt.Errorf("no plugins registered for %s", t)
} }
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] p, ok := i.plugins[t]
if !ok { if !ok {
return nil, fmt.Errorf("no plugins registered for %s", t) return nil, fmt.Errorf("no plugins registered for %s", t)

View File

@ -1,18 +1,31 @@
package plugin package plugin
import ( import (
"errors"
"fmt" "fmt"
"sync" "sync"
"github.com/pkg/errors"
"google.golang.org/grpc" "google.golang.org/grpc"
) )
var ( var (
ErrNoPluginType = errors.New("plugin: no type") ErrNoPluginType = errors.New("plugin: no type")
ErrNoPluginID = errors.New("plugin: no id") 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 type PluginType string
const ( const (

View File

@ -17,11 +17,6 @@ type Config struct {
Debug Debug `toml:"debug"` Debug Debug `toml:"debug"`
// Metrics and monitoring settings // Metrics and monitoring settings
Metrics MetricsConfig `toml:"metrics"` 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 provides plugin specific configuration for the initialization of a plugin
Plugins map[string]toml.Primitive `toml:"plugins"` Plugins map[string]toml.Primitive `toml:"plugins"`
// Enable containerd as a subreaper // Enable containerd as a subreaper

View File

@ -55,14 +55,10 @@ func New(ctx context.Context, config *Config) (*Server, error) {
rpc: rpc, rpc: rpc,
emitter: events.NewEmitter(), emitter: events.NewEmitter(),
} }
initialized = make(map[plugin.PluginType][]interface{}) initialized = make(map[plugin.PluginType]map[string]interface{})
) )
for _, p := range plugins { for _, p := range plugins {
id := p.URI() 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) log.G(ctx).WithField("type", p.Type).Infof("loading plugin %q...", id)
initContext := plugin.NewContext( initContext := plugin.NewContext(
@ -83,10 +79,21 @@ func New(ctx context.Context, config *Config) (*Server, error) {
} }
instance, err := p.Init(initContext) instance, err := p.Init(initContext)
if err != nil { if err != nil {
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) log.G(ctx).WithError(err).Warnf("failed to load plugin %s", id)
}
continue 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 // check for grpc services that should be registered with the server
if service, ok := instance.(plugin.Service); ok { if service, ok := instance.(plugin.Service); ok {
services = append(services, service) services = append(services, service)
@ -170,17 +177,6 @@ func loadPlugins(config *Config) ([]*plugin.Registration, error) {
return plugin.Graph(), nil 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( func interceptor(
ctx context.Context, ctx context.Context,
req interface{}, 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 ( import (
gocontext "context" gocontext "context"
"github.com/boltdb/bolt"
eventsapi "github.com/containerd/containerd/api/services/events/v1" eventsapi "github.com/containerd/containerd/api/services/events/v1"
snapshotapi "github.com/containerd/containerd/api/services/snapshot/v1" snapshotapi "github.com/containerd/containerd/api/services/snapshot/v1"
"github.com/containerd/containerd/api/types" "github.com/containerd/containerd/api/types"
"github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/events" "github.com/containerd/containerd/events"
"github.com/containerd/containerd/log" "github.com/containerd/containerd/log"
"github.com/containerd/containerd/metadata"
"github.com/containerd/containerd/mount" "github.com/containerd/containerd/mount"
"github.com/containerd/containerd/plugin" "github.com/containerd/containerd/plugin"
"github.com/containerd/containerd/snapshot" "github.com/containerd/containerd/snapshot"
"github.com/containerd/containerd/snapshot/namespaced"
protoempty "github.com/golang/protobuf/ptypes/empty" protoempty "github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
"golang.org/x/net/context" "golang.org/x/net/context"
"google.golang.org/grpc" "google.golang.org/grpc"
) )
type config struct {
// Default is the default snapshotter to use for the service
Default string `toml:"default,omitempty"`
}
func init() { func init() {
plugin.Register(&plugin.Registration{ plugin.Register(&plugin.Registration{
Type: plugin.GRPCPlugin, Type: plugin.GRPCPlugin,
ID: "snapshots", ID: "snapshots",
Requires: []plugin.PluginType{ Requires: []plugin.PluginType{
plugin.SnapshotPlugin, plugin.SnapshotPlugin,
plugin.MetadataPlugin,
}, },
Init: func(ic *plugin.InitContext) (interface{}, error) { Config: &config{
e := events.GetPoster(ic.Context) Default: defaultSnapshotter,
s, err := ic.Get(plugin.SnapshotPlugin)
if err != nil {
return nil, err
}
return newService(s.(snapshot.Snapshotter), e)
}, },
Init: newService,
}) })
} }
@ -43,9 +47,26 @@ type service struct {
emitter events.Poster 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{ return &service{
snapshotter: namespaced.NewSnapshotter(snapshotter), snapshotter: metadata.NewSnapshotter(md.(*bolt.DB), cfg.Default, sn.(snapshot.Snapshotter)),
emitter: evts, emitter: evts,
}, nil }, nil
} }