2017-03-07 01:23:00 +00:00
|
|
|
package plugin
|
2017-02-16 22:45:13 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
2017-03-07 01:23:00 +00:00
|
|
|
"github.com/docker/containerd"
|
2017-02-16 22:45:13 +00:00
|
|
|
"github.com/docker/containerd/content"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PluginType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
RuntimePlugin PluginType = iota + 1
|
|
|
|
GRPCPlugin
|
|
|
|
)
|
|
|
|
|
|
|
|
type Registration struct {
|
|
|
|
Type PluginType
|
|
|
|
Config interface{}
|
|
|
|
Init func(*InitContext) (interface{}, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type InitContext struct {
|
|
|
|
Root string
|
|
|
|
State string
|
2017-03-07 01:23:00 +00:00
|
|
|
Runtimes map[string]containerd.Runtime
|
2017-02-16 22:45:13 +00:00
|
|
|
Store *content.Store
|
|
|
|
Config interface{}
|
|
|
|
Context context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
type Service interface {
|
|
|
|
Register(*grpc.Server) error
|
|
|
|
}
|
|
|
|
|
|
|
|
var register = struct {
|
|
|
|
sync.Mutex
|
|
|
|
r map[string]*Registration
|
|
|
|
}{
|
|
|
|
r: make(map[string]*Registration),
|
|
|
|
}
|
|
|
|
|
2017-02-22 00:35:42 +00:00
|
|
|
// Load loads all plugins at the provided path into containerd
|
2017-02-16 22:45:13 +00:00
|
|
|
func Load(path string) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if v := recover(); v != nil {
|
|
|
|
rerr, ok := v.(error)
|
|
|
|
if !ok {
|
2017-02-22 00:35:42 +00:00
|
|
|
rerr = fmt.Errorf("%s", v)
|
2017-02-16 22:45:13 +00:00
|
|
|
}
|
|
|
|
err = rerr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return loadPlugins(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Register(name string, r *Registration) error {
|
|
|
|
register.Lock()
|
|
|
|
defer register.Unlock()
|
|
|
|
if _, ok := register.r[name]; ok {
|
|
|
|
return fmt.Errorf("plugin already registered as %q", name)
|
|
|
|
}
|
|
|
|
register.r[name] = r
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Registrations() map[string]*Registration {
|
|
|
|
return register.r
|
|
|
|
}
|