mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Use new plugin interfaces provided by plugin pkg
The use of `Client()` on v2 plugins is being deprecated so that we can be more flexible on the protocol used for plugins. This means checking specifically if the plugin implements the `Client() *plugins.Client` interface for V1 plugins, and for v2 plugins building a the client manually. Signed-off-by: Brian Goff <cpuguy83@gmail.com> (cherry picked from commit 45824a226b8a220d6f189c2d25fe16f9efc83db9) Signed-off-by: selansen <elango.siva@docker.com>
This commit is contained in:
parent
e07681c8ca
commit
791700aed3
4 changed files with 116 additions and 14 deletions
|
@ -1,16 +1,17 @@
|
|||
package remote
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/docker/docker/pkg/plugingetter"
|
||||
"github.com/docker/docker/pkg/plugins"
|
||||
"github.com/docker/libnetwork/datastore"
|
||||
"github.com/docker/libnetwork/discoverapi"
|
||||
"github.com/docker/libnetwork/driverapi"
|
||||
"github.com/docker/libnetwork/drivers/remote/api"
|
||||
"github.com/docker/libnetwork/types"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
|
@ -49,7 +50,11 @@ func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
|
|||
handleFunc = pg.Handle
|
||||
activePlugins := pg.GetAllManagedPluginsByCap(driverapi.NetworkPluginEndpointType)
|
||||
for _, ap := range activePlugins {
|
||||
newPluginHandler(ap.Name(), ap.Client())
|
||||
client, err := getPluginClient(ap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
newPluginHandler(ap.Name(), client)
|
||||
}
|
||||
}
|
||||
handleFunc(driverapi.NetworkPluginEndpointType, newPluginHandler)
|
||||
|
@ -57,6 +62,28 @@ func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func getPluginClient(p plugingetter.CompatPlugin) (*plugins.Client, error) {
|
||||
if v1, ok := p.(plugingetter.PluginWithV1Client); ok {
|
||||
return v1.Client(), nil
|
||||
}
|
||||
|
||||
pa, ok := p.(plugingetter.PluginAddr)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("unknown plugin type %T", p)
|
||||
}
|
||||
|
||||
if pa.Protocol() != plugins.ProtocolSchemeHTTPV1 {
|
||||
return nil, errors.Errorf("unsupported plugin protocol %s", pa.Protocol())
|
||||
}
|
||||
|
||||
addr := pa.Addr()
|
||||
client, err := plugins.NewClientWithTimeout(addr.Network()+"://"+addr.String(), nil, pa.Timeout())
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "error creating plugin client")
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// Get capability from client
|
||||
func (d *driver) getCapabilities() (*driverapi.Capability, error) {
|
||||
var capResp api.GetCapabilityResponse
|
||||
|
|
|
@ -219,7 +219,11 @@ func TestGetEmptyCapabilities(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
d := newDriver(plugin, p.Client())
|
||||
client, err := getPluginClient(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d := newDriver(plugin, client)
|
||||
if d.Type() != plugin {
|
||||
t.Fatal("Driver type does not match that given")
|
||||
}
|
||||
|
@ -249,7 +253,11 @@ func TestGetExtraCapabilities(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
d := newDriver(plugin, p.Client())
|
||||
client, err := getPluginClient(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d := newDriver(plugin, client)
|
||||
if d.Type() != plugin {
|
||||
t.Fatal("Driver type does not match that given")
|
||||
}
|
||||
|
@ -281,7 +289,11 @@ func TestGetInvalidCapabilities(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
d := newDriver(plugin, p.Client())
|
||||
client, err := getPluginClient(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d := newDriver(plugin, client)
|
||||
if d.Type() != plugin {
|
||||
t.Fatal("Driver type does not match that given")
|
||||
}
|
||||
|
@ -395,7 +407,11 @@ func TestRemoteDriver(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
d := newDriver(plugin, p.Client())
|
||||
client, err := getPluginClient(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d := newDriver(plugin, client)
|
||||
if d.Type() != plugin {
|
||||
t.Fatal("Driver type does not match that given")
|
||||
}
|
||||
|
@ -473,7 +489,11 @@ func TestDriverError(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
driver := newDriver(plugin, p.Client())
|
||||
client, err := getPluginClient(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
driver := newDriver(plugin, client)
|
||||
|
||||
if err := driver.CreateEndpoint("dummy", "dummy", &testEndpoint{t: t}, map[string]interface{}{}); err == nil {
|
||||
t.Fatal("Expected error from driver")
|
||||
|
@ -505,7 +525,12 @@ func TestMissingValues(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
driver := newDriver(plugin, p.Client())
|
||||
|
||||
client, err := getPluginClient(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
driver := newDriver(plugin, client)
|
||||
|
||||
if err := driver.CreateEndpoint("dummy", "dummy", ep, map[string]interface{}{}); err != nil {
|
||||
t.Fatal(err)
|
||||
|
@ -566,7 +591,12 @@ func TestRollback(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
driver := newDriver(plugin, p.Client())
|
||||
|
||||
client, err := getPluginClient(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
driver := newDriver(plugin, client)
|
||||
|
||||
ep := &rollbackEndpoint{}
|
||||
|
||||
|
|
|
@ -4,11 +4,13 @@ import (
|
|||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/docker/docker/pkg/plugingetter"
|
||||
"github.com/docker/docker/pkg/plugins"
|
||||
"github.com/docker/libnetwork/discoverapi"
|
||||
"github.com/docker/libnetwork/ipamapi"
|
||||
"github.com/docker/libnetwork/ipams/remote/api"
|
||||
"github.com/docker/libnetwork/types"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
|
@ -52,13 +54,39 @@ func Init(cb ipamapi.Callback, l, g interface{}) error {
|
|||
handleFunc = pg.Handle
|
||||
activePlugins := pg.GetAllManagedPluginsByCap(ipamapi.PluginEndpointType)
|
||||
for _, ap := range activePlugins {
|
||||
newPluginHandler(ap.Name(), ap.Client())
|
||||
client, err := getPluginClient(ap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
newPluginHandler(ap.Name(), client)
|
||||
}
|
||||
}
|
||||
handleFunc(ipamapi.PluginEndpointType, newPluginHandler)
|
||||
return nil
|
||||
}
|
||||
|
||||
func getPluginClient(p plugingetter.CompatPlugin) (*plugins.Client, error) {
|
||||
if v1, ok := p.(plugingetter.PluginWithV1Client); ok {
|
||||
return v1.Client(), nil
|
||||
}
|
||||
|
||||
pa, ok := p.(plugingetter.PluginAddr)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("unknown plugin type %T", p)
|
||||
}
|
||||
|
||||
if pa.Protocol() != plugins.ProtocolSchemeHTTPV1 {
|
||||
return nil, errors.Errorf("unsupported plugin protocol %s", pa.Protocol())
|
||||
}
|
||||
|
||||
addr := pa.Addr()
|
||||
client, err := plugins.NewClientWithTimeout(addr.Network()+"://"+addr.String(), nil, pa.Timeout())
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "error creating plugin client")
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func (a *allocator) call(methodName string, arg interface{}, retVal PluginResponse) error {
|
||||
method := ipamapi.PluginEndpointType + "." + methodName
|
||||
err := a.endpoint.Call(method, arg, retVal)
|
||||
|
|
|
@ -79,7 +79,11 @@ func TestGetCapabilities(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
d := newAllocator(plugin, p.Client())
|
||||
client, err := getPluginClient(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d := newAllocator(plugin, client)
|
||||
|
||||
caps, err := d.(*allocator).getCapabilities()
|
||||
if err != nil {
|
||||
|
@ -102,7 +106,12 @@ func TestGetCapabilitiesFromLegacyDriver(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
d := newAllocator(plugin, p.Client())
|
||||
client, err := getPluginClient(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
d := newAllocator(plugin, client)
|
||||
|
||||
if _, err := d.(*allocator).getCapabilities(); err == nil {
|
||||
t.Fatalf("Expected error, but got Success %v", err)
|
||||
|
@ -127,7 +136,11 @@ func TestGetDefaultAddressSpaces(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
d := newAllocator(plugin, p.Client())
|
||||
client, err := getPluginClient(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d := newAllocator(plugin, client)
|
||||
|
||||
l, g, err := d.(*allocator).GetDefaultAddressSpaces()
|
||||
if err != nil {
|
||||
|
@ -217,7 +230,11 @@ func TestRemoteDriver(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
d := newAllocator(plugin, p.Client())
|
||||
client, err := getPluginClient(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d := newAllocator(plugin, client)
|
||||
|
||||
l, g, err := d.(*allocator).GetDefaultAddressSpaces()
|
||||
if err != nil {
|
||||
|
|
Loading…
Add table
Reference in a new issue