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
11 changed files with 82 additions and 47 deletions

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 (