Merge pull request #37177 from cpuguy83/minor_plugin_addr_change

Move plugin client to separate interface
This commit is contained in:
Anusha Ragunathan 2018-05-31 08:22:04 -07:00 committed by GitHub
commit 71cd53e4a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 14 deletions

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"path/filepath" "path/filepath"
"github.com/docker/docker/errdefs"
"github.com/docker/docker/pkg/plugingetter" "github.com/docker/docker/pkg/plugingetter"
"github.com/docker/docker/pkg/plugins" "github.com/docker/docker/pkg/plugins"
"github.com/docker/docker/plugin/v2" "github.com/docker/docker/plugin/v2"
@ -33,19 +34,22 @@ func newPluginDriver(name string, pl plugingetter.CompatPlugin, config Options)
var proxy *graphDriverProxy var proxy *graphDriverProxy
pa, ok := pl.(plugingetter.PluginAddr) switch pt := pl.(type) {
if !ok { case plugingetter.PluginWithV1Client:
proxy = &graphDriverProxy{name, pl, Capabilities{}, pl.Client()} proxy = &graphDriverProxy{name, pl, Capabilities{}, pt.Client()}
} else { case plugingetter.PluginAddr:
if pa.Protocol() != plugins.ProtocolSchemeHTTPV1 { if pt.Protocol() != plugins.ProtocolSchemeHTTPV1 {
return nil, errors.Errorf("plugin protocol not supported: %s", pa.Protocol()) return nil, errors.Errorf("plugin protocol not supported: %s", pt.Protocol())
} }
addr := pa.Addr() addr := pt.Addr()
client, err := plugins.NewClientWithTimeout(addr.Network()+"://"+addr.String(), nil, pa.Timeout()) client, err := plugins.NewClientWithTimeout(addr.Network()+"://"+addr.String(), nil, pt.Timeout())
if err != nil { if err != nil {
return nil, errors.Wrap(err, "error creating plugin client") return nil, errors.Wrap(err, "error creating plugin client")
} }
proxy = &graphDriverProxy{name, pl, Capabilities{}, client} proxy = &graphDriverProxy{name, pl, Capabilities{}, client}
default:
return nil, errdefs.System(errors.Errorf("got unknown plugin type %T", pt))
} }
return proxy, proxy.Init(filepath.Join(home, name), config.DriverOptions, config.UIDMaps, config.GIDMaps) return proxy, proxy.Init(filepath.Join(home, name), config.DriverOptions, config.UIDMaps, config.GIDMaps)
} }

View file

@ -7,6 +7,7 @@ import (
"path/filepath" "path/filepath"
"github.com/docker/docker/api/types/plugins/logdriver" "github.com/docker/docker/api/types/plugins/logdriver"
"github.com/docker/docker/errdefs"
getter "github.com/docker/docker/pkg/plugingetter" getter "github.com/docker/docker/pkg/plugingetter"
"github.com/docker/docker/pkg/plugins" "github.com/docker/docker/pkg/plugins"
"github.com/docker/docker/pkg/stringid" "github.com/docker/docker/pkg/stringid"
@ -46,9 +47,12 @@ func getPlugin(name string, mode int) (Creator, error) {
} }
func makePluginClient(p getter.CompatPlugin) (logPlugin, error) { func makePluginClient(p getter.CompatPlugin) (logPlugin, error) {
if pc, ok := p.(getter.PluginWithV1Client); ok {
return &logPluginProxy{pc.Client()}, nil
}
pa, ok := p.(getter.PluginAddr) pa, ok := p.(getter.PluginAddr)
if !ok { if !ok {
return &logPluginProxy{p.Client()}, nil return nil, errdefs.System(errors.Errorf("got unknown plugin type %T", p))
} }
if pa.Protocol() != plugins.ProtocolSchemeHTTPV1 { if pa.Protocol() != plugins.ProtocolSchemeHTTPV1 {

View file

@ -3,6 +3,7 @@ package daemon // import "github.com/docker/docker/daemon"
import ( import (
"sync" "sync"
"github.com/docker/docker/errdefs"
"github.com/docker/docker/pkg/plugingetter" "github.com/docker/docker/pkg/plugingetter"
"github.com/docker/docker/pkg/plugins" "github.com/docker/docker/pkg/plugins"
"github.com/docker/go-metrics" "github.com/docker/go-metrics"
@ -142,11 +143,16 @@ type metricsPlugin interface {
StopMetrics() error StopMetrics() error
} }
func makePluginAdapter(p plugingetter.CompatPlugin) (metricsPlugin, error) { func makePluginAdapter(p plugingetter.CompatPlugin) (metricsPlugin, error) { // nolint: interfacer
if pc, ok := p.(plugingetter.PluginWithV1Client); ok {
return &metricsPluginAdapter{pc.Client(), p.Name()}, nil
}
pa, ok := p.(plugingetter.PluginAddr) pa, ok := p.(plugingetter.PluginAddr)
if !ok { if !ok {
return &metricsPluginAdapter{p.Client(), p.Name()}, nil return nil, errdefs.System(errors.Errorf("got unknown plugin type %T", p))
} }
if pa.Protocol() != plugins.ProtocolSchemeHTTPV1 { if pa.Protocol() != plugins.ProtocolSchemeHTTPV1 {
return nil, errors.Errorf("plugin protocol not supported: %s", pa.Protocol()) return nil, errors.Errorf("plugin protocol not supported: %s", pa.Protocol())
} }

View file

@ -18,15 +18,19 @@ const (
// CompatPlugin is an abstraction to handle both v2(new) and v1(legacy) plugins. // CompatPlugin is an abstraction to handle both v2(new) and v1(legacy) plugins.
type CompatPlugin interface { type CompatPlugin interface {
Client() *plugins.Client
Name() string Name() string
ScopedPath(string) string ScopedPath(string) string
IsV1() bool IsV1() bool
PluginWithV1Client
}
// PluginWithV1Client is a plugin that directly utilizes the v1/http plugin client
type PluginWithV1Client interface {
Client() *plugins.Client
} }
// PluginAddr is a plugin that exposes the socket address for creating custom clients rather than the built-in `*plugins.Client` // PluginAddr is a plugin that exposes the socket address for creating custom clients rather than the built-in `*plugins.Client`
type PluginAddr interface { type PluginAddr interface {
CompatPlugin
Addr() net.Addr Addr() net.Addr
Timeout() time.Duration Timeout() time.Duration
Protocol() string Protocol() string

View file

@ -212,9 +212,13 @@ func (s *Store) GetAllDrivers() ([]volume.Driver, error) {
} }
func makePluginAdapter(p getter.CompatPlugin) (*volumeDriverAdapter, error) { func makePluginAdapter(p getter.CompatPlugin) (*volumeDriverAdapter, error) {
if pc, ok := p.(getter.PluginWithV1Client); ok {
return &volumeDriverAdapter{name: p.Name(), scopePath: p.ScopedPath, proxy: &volumeDriverProxy{pc.Client()}}, nil
}
pa, ok := p.(getter.PluginAddr) pa, ok := p.(getter.PluginAddr)
if !ok { if !ok {
return &volumeDriverAdapter{name: p.Name(), scopePath: p.ScopedPath, proxy: &volumeDriverProxy{p.Client()}}, nil return nil, errdefs.System(errors.Errorf("got unknown plugin instance %T", p))
} }
if pa.Protocol() != plugins.ProtocolSchemeHTTPV1 { if pa.Protocol() != plugins.ProtocolSchemeHTTPV1 {