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> Upstream-commit: 5e9c78aeaf1d88000921190b88a1d91d6261208c Component: engine
This commit is contained in:
@ -15,7 +15,7 @@ type Store struct {
|
||||
/* handlers are necessary for transition path of legacy plugins
|
||||
* to the new model. Legacy plugins use Handle() for registering an
|
||||
* activation callback.*/
|
||||
handlers map[string]func(string, *plugins.Client)
|
||||
handlers map[string][]func(string, *plugins.Client)
|
||||
nameToID map[string]string
|
||||
plugindb string
|
||||
}
|
||||
@ -24,7 +24,7 @@ type Store struct {
|
||||
func NewStore(libRoot string) *Store {
|
||||
return &Store{
|
||||
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),
|
||||
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.
|
||||
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()
|
||||
|
||||
// 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.
|
||||
func (ps *Store) CallHandler(p *v2.Plugin) {
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user