plugin: refactor plugin system to support initialization reporting

Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
Stephen J Day
2017-10-09 20:43:04 -07:00
parent fe52d9369f
commit 8508e8252b
21 changed files with 216 additions and 80 deletions

View File

@@ -23,7 +23,7 @@ func init() {
Requires: []plugin.Type{
plugin.MetadataPlugin,
},
Init: func(ic *plugin.InitContext) (interface{}, error) {
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
m, err := ic.Get(plugin.MetadataPlugin)
if err != nil {
return nil, err

View File

@@ -41,19 +41,22 @@ func init() {
Requires: []plugin.Type{
plugin.MetadataPlugin,
},
Init: NewService,
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
m, err := ic.Get(plugin.MetadataPlugin)
if err != nil {
return nil, err
}
s, err := NewService(m.(*metadata.DB).ContentStore(), ic.Events)
return s, err
},
})
}
func NewService(ic *plugin.InitContext) (interface{}, error) {
m, err := ic.Get(plugin.MetadataPlugin)
if err != nil {
return nil, err
}
func NewService(cs content.Store, publisher events.Publisher) (*Service, error) {
return &Service{
store: m.(*metadata.DB).ContentStore(),
publisher: ic.Events,
store: cs,
publisher: publisher,
}, nil
}

View File

@@ -32,8 +32,8 @@ func init() {
Config: &config{
Order: []string{"walking"},
},
Init: func(ic *plugin.InitContext) (interface{}, error) {
differs, err := ic.GetAll(plugin.DiffPlugin)
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
differs, err := ic.GetByType(plugin.DiffPlugin)
if err != nil {
return nil, err
}
@@ -41,10 +41,15 @@ func init() {
orderedNames := ic.Config.(*config).Order
ordered := make([]plugin.Differ, len(orderedNames))
for i, n := range orderedNames {
differ, ok := differs[n]
differp, ok := differs[n]
if !ok {
return nil, errors.Errorf("needed differ not loaded: %s", n)
}
differ, err := differp.Instance()
if err != nil {
return nil, errors.Wrapf(err, "could not load required differ due plugin init error: %s", n)
}
ordered[i] = differ.(plugin.Differ)
}

View File

@@ -15,7 +15,7 @@ func init() {
plugin.Register(&plugin.Registration{
Type: plugin.GRPCPlugin,
ID: "events",
Init: func(ic *plugin.InitContext) (interface{}, error) {
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
return NewService(ic.Events), nil
},
})

View File

@@ -16,11 +16,13 @@ func init() {
plugin.Register(&plugin.Registration{
Type: plugin.GRPCPlugin,
ID: "healthcheck",
Init: NewService,
InitFn: func(*plugin.InitContext) (interface{}, error) {
return NewService()
},
})
}
func NewService(ic *plugin.InitContext) (interface{}, error) {
func NewService() (*Service, error) {
return &Service{
health.NewServer(),
}, nil

View File

@@ -23,7 +23,7 @@ func init() {
Requires: []plugin.Type{
plugin.MetadataPlugin,
},
Init: func(ic *plugin.InitContext) (interface{}, error) {
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
m, err := ic.Get(plugin.MetadataPlugin)
if err != nil {
return nil, err

View File

@@ -24,7 +24,7 @@ func init() {
Requires: []plugin.Type{
plugin.MetadataPlugin,
},
Init: func(ic *plugin.InitContext) (interface{}, error) {
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
m, err := ic.Get(plugin.MetadataPlugin)
if err != nil {
return nil, err

View File

@@ -25,7 +25,7 @@ func init() {
Requires: []plugin.Type{
plugin.MetadataPlugin,
},
Init: newService,
InitFn: newService,
})
}

View File

@@ -2,6 +2,7 @@ package tasks
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
@@ -46,15 +47,16 @@ func init() {
plugin.RuntimePlugin,
plugin.MetadataPlugin,
},
Init: New,
InitFn: New,
})
}
func New(ic *plugin.InitContext) (interface{}, error) {
rt, err := ic.GetAll(plugin.RuntimePlugin)
rt, err := ic.GetByType(plugin.RuntimePlugin)
if err != nil {
return nil, err
}
m, err := ic.Get(plugin.MetadataPlugin)
if err != nil {
return nil, err
@@ -62,9 +64,18 @@ func New(ic *plugin.InitContext) (interface{}, error) {
cs := m.(*metadata.DB).ContentStore()
runtimes := make(map[string]runtime.Runtime)
for _, rr := range rt {
r := rr.(runtime.Runtime)
ri, err := rr.Instance()
if err != nil {
log.G(ic.Context).WithError(err).Warn("could not load runtime instance due to initialization error")
continue
}
r := ri.(runtime.Runtime)
runtimes[r.ID()] = r
}
if len(runtimes) == 0 {
return nil, errors.New("no runtimes available to create task service")
}
return &Service{
runtimes: runtimes,
db: m.(*metadata.DB),

View File

@@ -13,9 +13,9 @@ var _ api.VersionServer = &Service{}
func init() {
plugin.Register(&plugin.Registration{
Type: plugin.GRPCPlugin,
ID: "version",
Init: New,
Type: plugin.GRPCPlugin,
ID: "version",
InitFn: New,
})
}