2018-02-05 21:05:59 +00:00
|
|
|
package plugin // import "github.com/docker/docker/plugin"
|
2016-08-26 17:02:38 +00:00
|
|
|
|
|
|
|
import (
|
2016-10-06 14:09:54 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2017-01-26 00:54:18 +00:00
|
|
|
"github.com/docker/distribution/reference"
|
2018-01-11 19:53:06 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2016-10-07 20:53:14 +00:00
|
|
|
"github.com/docker/docker/pkg/plugingetter"
|
2016-08-26 17:02:38 +00:00
|
|
|
"github.com/docker/docker/pkg/plugins"
|
2019-08-05 14:37:47 +00:00
|
|
|
v2 "github.com/docker/docker/plugin/v2"
|
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
2016-12-12 23:05:53 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 21:42:13 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-08-26 17:02:38 +00:00
|
|
|
)
|
|
|
|
|
2016-10-06 14:09:54 +00:00
|
|
|
/* allowV1PluginsFallback determines daemon's support for V1 plugins.
|
|
|
|
* When the time comes to remove support for V1 plugins, flipping
|
|
|
|
* this bool is all that will be needed.
|
|
|
|
*/
|
2018-05-19 11:38:54 +00:00
|
|
|
const allowV1PluginsFallback = true
|
2016-10-06 14:09:54 +00:00
|
|
|
|
|
|
|
/* defaultAPIVersion is the version of the plugin API for volume, network,
|
|
|
|
IPAM and authz. This is a very stable API. When we update this API, then
|
2016-12-08 17:35:32 +00:00
|
|
|
pluginType should include a version. e.g. "networkdriver/2.0".
|
2016-10-06 14:09:54 +00:00
|
|
|
*/
|
2018-05-19 11:38:54 +00:00
|
|
|
const defaultAPIVersion = "1.0"
|
2016-10-06 14:09:54 +00:00
|
|
|
|
2017-01-02 14:01:19 +00:00
|
|
|
// GetV2Plugin retrieves a plugin by name, id or partial ID.
|
2016-12-12 23:05:53 +00:00
|
|
|
func (ps *Store) GetV2Plugin(refOrID string) (*v2.Plugin, error) {
|
2016-10-06 14:09:54 +00:00
|
|
|
ps.RLock()
|
|
|
|
defer ps.RUnlock()
|
|
|
|
|
2016-12-12 23:05:53 +00:00
|
|
|
id, err := ps.resolvePluginID(refOrID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-10-06 14:09:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p, idOk := ps.plugins[id]
|
|
|
|
if !idOk {
|
2017-07-19 14:20:13 +00:00
|
|
|
return nil, errors.WithStack(errNotFound(id))
|
2016-10-06 14:09:54 +00:00
|
|
|
}
|
2016-12-12 23:05:53 +00:00
|
|
|
|
2016-10-06 14:09:54 +00:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2016-12-12 23:05:53 +00:00
|
|
|
// validateName returns error if name is already reserved. always call with lock and full name
|
|
|
|
func (ps *Store) validateName(name string) error {
|
|
|
|
for _, p := range ps.plugins {
|
|
|
|
if p.Name() == name {
|
2017-07-19 14:20:13 +00:00
|
|
|
return alreadyExistsError(name)
|
2016-12-12 23:05:53 +00:00
|
|
|
}
|
2016-10-06 14:09:54 +00:00
|
|
|
}
|
2016-12-12 23:05:53 +00:00
|
|
|
return nil
|
2016-10-06 14:09:54 +00:00
|
|
|
}
|
|
|
|
|
2017-01-02 14:01:19 +00:00
|
|
|
// GetAll retrieves all plugins.
|
2016-10-06 14:09:54 +00:00
|
|
|
func (ps *Store) GetAll() map[string]*v2.Plugin {
|
|
|
|
ps.RLock()
|
|
|
|
defer ps.RUnlock()
|
|
|
|
return ps.plugins
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAll initialized plugins during daemon restore.
|
|
|
|
func (ps *Store) SetAll(plugins map[string]*v2.Plugin) {
|
|
|
|
ps.Lock()
|
|
|
|
defer ps.Unlock()
|
2017-12-13 20:24:51 +00:00
|
|
|
|
|
|
|
for _, p := range plugins {
|
|
|
|
ps.setSpecOpts(p)
|
|
|
|
}
|
2016-10-06 14:09:54 +00:00
|
|
|
ps.plugins = plugins
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *Store) getAllByCap(capability string) []plugingetter.CompatPlugin {
|
|
|
|
ps.RLock()
|
|
|
|
defer ps.RUnlock()
|
|
|
|
|
|
|
|
result := make([]plugingetter.CompatPlugin, 0, 1)
|
|
|
|
for _, p := range ps.plugins {
|
2016-11-01 00:59:45 +00:00
|
|
|
if p.IsEnabled() {
|
|
|
|
if _, err := p.FilterByCap(capability); err == nil {
|
|
|
|
result = append(result, p)
|
|
|
|
}
|
2016-10-06 14:09:54 +00:00
|
|
|
}
|
2016-08-26 17:02:38 +00:00
|
|
|
}
|
2016-10-06 14:09:54 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetState sets the active state of the plugin and updates plugindb.
|
|
|
|
func (ps *Store) SetState(p *v2.Plugin, state bool) {
|
|
|
|
ps.Lock()
|
|
|
|
defer ps.Unlock()
|
|
|
|
|
|
|
|
p.PluginObj.Enabled = state
|
|
|
|
}
|
|
|
|
|
2017-12-13 20:24:51 +00:00
|
|
|
func (ps *Store) setSpecOpts(p *v2.Plugin) {
|
|
|
|
var specOpts []SpecOpt
|
|
|
|
for _, typ := range p.GetTypes() {
|
|
|
|
opts, ok := ps.specOpts[typ.String()]
|
|
|
|
if ok {
|
|
|
|
specOpts = append(specOpts, opts...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p.SetSpecOptModifier(func(s *specs.Spec) {
|
|
|
|
for _, o := range specOpts {
|
|
|
|
o(s)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-10-06 14:09:54 +00:00
|
|
|
// Add adds a plugin to memory and plugindb.
|
2016-11-22 17:42:58 +00:00
|
|
|
// An error will be returned if there is a collision.
|
|
|
|
func (ps *Store) Add(p *v2.Plugin) error {
|
2016-10-06 14:09:54 +00:00
|
|
|
ps.Lock()
|
2016-11-22 17:42:58 +00:00
|
|
|
defer ps.Unlock()
|
|
|
|
|
|
|
|
if v, exist := ps.plugins[p.GetID()]; exist {
|
|
|
|
return fmt.Errorf("plugin %q has the same ID %s as %q", p.Name(), p.GetID(), v.Name())
|
|
|
|
}
|
2017-12-13 20:24:51 +00:00
|
|
|
|
|
|
|
ps.setSpecOpts(p)
|
|
|
|
|
2016-11-22 17:42:58 +00:00
|
|
|
ps.plugins[p.GetID()] = p
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-06 14:09:54 +00:00
|
|
|
// Remove removes a plugin from memory and plugindb.
|
|
|
|
func (ps *Store) Remove(p *v2.Plugin) {
|
|
|
|
ps.Lock()
|
|
|
|
delete(ps.plugins, p.GetID())
|
|
|
|
ps.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-09-09 01:52:29 +00:00
|
|
|
// Get returns an enabled plugin matching the given name and capability.
|
2016-10-06 14:09:54 +00:00
|
|
|
func (ps *Store) Get(name, capability string, mode int) (plugingetter.CompatPlugin, error) {
|
|
|
|
// Lookup using new model.
|
|
|
|
if ps != nil {
|
2017-10-09 08:21:00 +00:00
|
|
|
p, err := ps.GetV2Plugin(name)
|
2016-10-06 14:09:54 +00:00
|
|
|
if err == nil {
|
2016-09-09 01:52:29 +00:00
|
|
|
if p.IsEnabled() {
|
2017-10-20 19:39:47 +00:00
|
|
|
fp, err := p.FilterByCap(capability)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.AddRefCount(mode)
|
|
|
|
return fp, nil
|
2016-09-09 01:52:29 +00:00
|
|
|
}
|
2017-10-20 19:39:47 +00:00
|
|
|
|
2016-09-09 01:52:29 +00:00
|
|
|
// Plugin was found but it is disabled, so we should not fall back to legacy plugins
|
|
|
|
// but we should error out right away
|
2017-07-19 14:20:13 +00:00
|
|
|
return nil, errDisabled(name)
|
2016-10-06 14:09:54 +00:00
|
|
|
}
|
2020-04-17 10:01:01 +00:00
|
|
|
var ierr errNotFound
|
|
|
|
if !errors.As(err, &ierr) {
|
2016-10-06 14:09:54 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-09 08:21:00 +00:00
|
|
|
if !allowV1PluginsFallback {
|
|
|
|
return nil, errNotFound(name)
|
2016-10-06 14:09:54 +00:00
|
|
|
}
|
|
|
|
|
2017-10-09 08:21:00 +00:00
|
|
|
p, err := plugins.Get(name, capability)
|
|
|
|
if err == nil {
|
|
|
|
return p, nil
|
|
|
|
}
|
2020-04-17 10:01:01 +00:00
|
|
|
if errors.Is(err, plugins.ErrNotFound) {
|
2017-10-09 08:21:00 +00:00
|
|
|
return nil, errNotFound(name)
|
|
|
|
}
|
2017-11-29 04:09:37 +00:00
|
|
|
return nil, errors.Wrap(errdefs.System(err), "legacy plugin")
|
2016-10-06 14:09:54 +00:00
|
|
|
}
|
|
|
|
|
2016-12-22 18:26:04 +00:00
|
|
|
// GetAllManagedPluginsByCap returns a list of managed plugins matching the given capability.
|
|
|
|
func (ps *Store) GetAllManagedPluginsByCap(capability string) []plugingetter.CompatPlugin {
|
|
|
|
return ps.getAllByCap(capability)
|
|
|
|
}
|
|
|
|
|
2016-09-09 01:52:29 +00:00
|
|
|
// GetAllByCap returns a list of enabled plugins matching the given capability.
|
2016-10-06 14:09:54 +00:00
|
|
|
func (ps *Store) GetAllByCap(capability string) ([]plugingetter.CompatPlugin, error) {
|
|
|
|
result := make([]plugingetter.CompatPlugin, 0, 1)
|
|
|
|
|
|
|
|
/* Daemon start always calls plugin.Init thereby initializing a store.
|
|
|
|
* So store on experimental builds can never be nil, even while
|
|
|
|
* handling legacy plugins. However, there are legacy plugin unit
|
|
|
|
* tests where the volume subsystem directly talks with the plugin,
|
|
|
|
* bypassing the daemon. For such tests, this check is necessary.
|
|
|
|
*/
|
|
|
|
if ps != nil {
|
|
|
|
result = ps.getAllByCap(capability)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup with legacy model
|
|
|
|
if allowV1PluginsFallback {
|
|
|
|
pl, err := plugins.GetAll(capability)
|
|
|
|
if err != nil {
|
2017-11-29 04:09:37 +00:00
|
|
|
return nil, errors.Wrap(errdefs.System(err), "legacy plugin")
|
2016-10-06 14:09:54 +00:00
|
|
|
}
|
|
|
|
for _, p := range pl {
|
|
|
|
result = append(result, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
2016-09-06 21:30:55 +00:00
|
|
|
}
|
2016-09-26 18:06:26 +00:00
|
|
|
|
2017-12-13 20:24:51 +00:00
|
|
|
func pluginType(cap string) string {
|
|
|
|
return fmt.Sprintf("docker.%s/%s", strings.ToLower(cap), defaultAPIVersion)
|
|
|
|
}
|
|
|
|
|
2016-09-26 18:06:26 +00:00
|
|
|
// Handle sets a callback for a given capability. It is only used by network
|
|
|
|
// and ipam drivers during plugin registration. The callback registers the
|
|
|
|
// driver with the subsystem (network, ipam).
|
|
|
|
func (ps *Store) Handle(capability string, callback func(string, *plugins.Client)) {
|
2017-12-13 20:24:51 +00:00
|
|
|
typ := pluginType(capability)
|
2016-10-06 14:09:54 +00:00
|
|
|
|
|
|
|
// Register callback with new plugin model.
|
|
|
|
ps.Lock()
|
2017-12-13 20:24:51 +00:00
|
|
|
handlers, ok := ps.handlers[typ]
|
2016-10-06 14:09:54 +00:00
|
|
|
if !ok {
|
|
|
|
handlers = []func(string, *plugins.Client){}
|
|
|
|
}
|
|
|
|
handlers = append(handlers, callback)
|
2017-12-13 20:24:51 +00:00
|
|
|
ps.handlers[typ] = handlers
|
2016-10-06 14:09:54 +00:00
|
|
|
ps.Unlock()
|
|
|
|
|
|
|
|
// Register callback with legacy plugin model.
|
|
|
|
if allowV1PluginsFallback {
|
|
|
|
plugins.Handle(capability, callback)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 20:24:51 +00:00
|
|
|
// RegisterRuntimeOpt stores a list of SpecOpts for the provided capability.
|
|
|
|
// These options are applied to the runtime spec before a plugin is started for the specified capability.
|
|
|
|
func (ps *Store) RegisterRuntimeOpt(cap string, opts ...SpecOpt) {
|
|
|
|
ps.Lock()
|
|
|
|
defer ps.Unlock()
|
|
|
|
typ := pluginType(cap)
|
|
|
|
ps.specOpts[typ] = append(ps.specOpts[typ], opts...)
|
|
|
|
}
|
|
|
|
|
2016-10-06 14:09:54 +00:00
|
|
|
// CallHandler calls the registered callback. It is invoked during plugin enable.
|
|
|
|
func (ps *Store) CallHandler(p *v2.Plugin) {
|
|
|
|
for _, typ := range p.GetTypes() {
|
|
|
|
for _, handler := range ps.handlers[typ.String()] {
|
|
|
|
handler(p.Name(), p.Client())
|
|
|
|
}
|
|
|
|
}
|
2016-09-26 18:06:26 +00:00
|
|
|
}
|
2016-11-24 04:04:44 +00:00
|
|
|
|
2020-04-28 15:37:35 +00:00
|
|
|
// resolvePluginID must be protected by ps.RLock
|
2016-12-12 23:05:53 +00:00
|
|
|
func (ps *Store) resolvePluginID(idOrName string) (string, error) {
|
|
|
|
if validFullID.MatchString(idOrName) {
|
|
|
|
return idOrName, nil
|
|
|
|
}
|
|
|
|
|
2017-01-26 00:54:18 +00:00
|
|
|
ref, err := reference.ParseNormalizedNamed(idOrName)
|
2016-12-12 23:05:53 +00:00
|
|
|
if err != nil {
|
2017-07-19 14:20:13 +00:00
|
|
|
return "", errors.WithStack(errNotFound(idOrName))
|
2016-12-12 23:05:53 +00:00
|
|
|
}
|
|
|
|
if _, ok := ref.(reference.Canonical); ok {
|
2017-01-26 00:54:18 +00:00
|
|
|
logrus.Warnf("canonical references cannot be resolved: %v", reference.FamiliarString(ref))
|
2017-07-19 14:20:13 +00:00
|
|
|
return "", errors.WithStack(errNotFound(idOrName))
|
2016-12-12 23:05:53 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 00:54:18 +00:00
|
|
|
ref = reference.TagNameOnly(ref)
|
2016-12-12 23:05:53 +00:00
|
|
|
|
|
|
|
for _, p := range ps.plugins {
|
2017-01-26 00:54:18 +00:00
|
|
|
if p.PluginObj.Name == reference.FamiliarString(ref) {
|
2016-12-12 23:05:53 +00:00
|
|
|
return p.PluginObj.ID, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 04:04:44 +00:00
|
|
|
var found *v2.Plugin
|
2016-12-12 23:05:53 +00:00
|
|
|
for id, p := range ps.plugins { // this can be optimized
|
|
|
|
if strings.HasPrefix(id, idOrName) {
|
2016-11-24 04:04:44 +00:00
|
|
|
if found != nil {
|
2017-07-19 14:20:13 +00:00
|
|
|
return "", errors.WithStack(errAmbiguous(idOrName))
|
2016-11-24 04:04:44 +00:00
|
|
|
}
|
|
|
|
found = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if found == nil {
|
2017-07-19 14:20:13 +00:00
|
|
|
return "", errors.WithStack(errNotFound(idOrName))
|
2016-11-24 04:04:44 +00:00
|
|
|
}
|
2016-12-12 23:05:53 +00:00
|
|
|
return found.PluginObj.ID, nil
|
2016-11-24 04:04:44 +00:00
|
|
|
}
|