mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Allow plugins to have multiple handlers
Currently the plugins pkg allows a single handler. This assumption breaks down if there are mutiple listeners to a plugin of a certain Manifest such as NetworkDriver or IpamDriver when swarm-mode is enabled. Signed-off-by: Madhu Venugopal <madhu@docker.com>
This commit is contained in:
parent
88cae7412d
commit
5e9c78aeaf
4 changed files with 26 additions and 11 deletions
|
@ -362,9 +362,8 @@ func (daemon *Daemon) GetNetworkDriverList() []string {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Replace this with proper libnetwork API
|
pluginList := daemon.netController.BuiltinDrivers()
|
||||||
pluginList := []string{"overlay"}
|
pluginMap := make(map[string]bool)
|
||||||
pluginMap := map[string]bool{"overlay": true}
|
|
||||||
|
|
||||||
networks := daemon.netController.Networks()
|
networks := daemon.netController.Networks()
|
||||||
|
|
||||||
|
|
|
@ -43,7 +43,7 @@ type plugins struct {
|
||||||
|
|
||||||
var (
|
var (
|
||||||
storage = plugins{plugins: make(map[string]*Plugin)}
|
storage = plugins{plugins: make(map[string]*Plugin)}
|
||||||
extpointHandlers = make(map[string]func(string, *Client))
|
extpointHandlers = make(map[string][]func(string, *Client))
|
||||||
)
|
)
|
||||||
|
|
||||||
// Manifest lists what a plugin implements.
|
// Manifest lists what a plugin implements.
|
||||||
|
@ -129,11 +129,13 @@ func (p *Plugin) activateWithLock() error {
|
||||||
p.Manifest = m
|
p.Manifest = m
|
||||||
|
|
||||||
for _, iface := range m.Implements {
|
for _, iface := range m.Implements {
|
||||||
handler, handled := extpointHandlers[iface]
|
handlers, handled := extpointHandlers[iface]
|
||||||
if !handled {
|
if !handled {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
handler(p.name, p.client)
|
for _, handler := range handlers {
|
||||||
|
handler(p.name, p.client)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -226,7 +228,16 @@ func Get(name, imp string) (*Plugin, error) {
|
||||||
|
|
||||||
// Handle adds the specified function to the extpointHandlers.
|
// Handle adds the specified function to the extpointHandlers.
|
||||||
func Handle(iface string, fn func(string, *Client)) {
|
func Handle(iface string, fn func(string, *Client)) {
|
||||||
extpointHandlers[iface] = fn
|
handlers, ok := extpointHandlers[iface]
|
||||||
|
if !ok {
|
||||||
|
handlers = []func(string, *Client){}
|
||||||
|
}
|
||||||
|
|
||||||
|
handlers = append(handlers, fn)
|
||||||
|
extpointHandlers[iface] = handlers
|
||||||
|
for _, p := range storage.plugins {
|
||||||
|
p.activated = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAll returns all the plugins for the specified implementation
|
// GetAll returns all the plugins for the specified implementation
|
||||||
|
|
|
@ -15,7 +15,7 @@ type Store struct {
|
||||||
/* handlers are necessary for transition path of legacy plugins
|
/* handlers are necessary for transition path of legacy plugins
|
||||||
* to the new model. Legacy plugins use Handle() for registering an
|
* to the new model. Legacy plugins use Handle() for registering an
|
||||||
* activation callback.*/
|
* activation callback.*/
|
||||||
handlers map[string]func(string, *plugins.Client)
|
handlers map[string][]func(string, *plugins.Client)
|
||||||
nameToID map[string]string
|
nameToID map[string]string
|
||||||
plugindb string
|
plugindb string
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ type Store struct {
|
||||||
func NewStore(libRoot string) *Store {
|
func NewStore(libRoot string) *Store {
|
||||||
return &Store{
|
return &Store{
|
||||||
plugins: make(map[string]*v2.Plugin),
|
plugins: make(map[string]*v2.Plugin),
|
||||||
handlers: make(map[string]func(string, *plugins.Client)),
|
handlers: make(map[string][]func(string, *plugins.Client)),
|
||||||
nameToID: make(map[string]string),
|
nameToID: make(map[string]string),
|
||||||
plugindb: filepath.Join(libRoot, "plugins", "plugins.json"),
|
plugindb: filepath.Join(libRoot, "plugins", "plugins.json"),
|
||||||
}
|
}
|
||||||
|
|
|
@ -212,7 +212,12 @@ func (ps *Store) Handle(capability string, callback func(string, *plugins.Client
|
||||||
|
|
||||||
// Register callback with new plugin model.
|
// Register callback with new plugin model.
|
||||||
ps.Lock()
|
ps.Lock()
|
||||||
ps.handlers[pluginType] = callback
|
handlers, ok := ps.handlers[pluginType]
|
||||||
|
if !ok {
|
||||||
|
handlers = []func(string, *plugins.Client){}
|
||||||
|
}
|
||||||
|
handlers = append(handlers, callback)
|
||||||
|
ps.handlers[pluginType] = handlers
|
||||||
ps.Unlock()
|
ps.Unlock()
|
||||||
|
|
||||||
// Register callback with legacy plugin model.
|
// Register callback with legacy plugin model.
|
||||||
|
@ -224,7 +229,7 @@ func (ps *Store) Handle(capability string, callback func(string, *plugins.Client
|
||||||
// CallHandler calls the registered callback. It is invoked during plugin enable.
|
// CallHandler calls the registered callback. It is invoked during plugin enable.
|
||||||
func (ps *Store) CallHandler(p *v2.Plugin) {
|
func (ps *Store) CallHandler(p *v2.Plugin) {
|
||||||
for _, typ := range p.GetTypes() {
|
for _, typ := range p.GetTypes() {
|
||||||
if handler := ps.handlers[typ.String()]; handler != nil {
|
for _, handler := range ps.handlers[typ.String()] {
|
||||||
handler(p.Name(), p.Client())
|
handler(p.Name(), p.Client())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue