Browse Source

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>
Brian Goff 7 years ago
parent
commit
791700aed3

+ 29 - 2
libnetwork/drivers/remote/driver.go

@@ -1,16 +1,17 @@
 package remote
 package remote
 
 
 import (
 import (
-	"errors"
 	"fmt"
 	"fmt"
 	"net"
 	"net"
 
 
+	"github.com/docker/docker/pkg/plugingetter"
 	"github.com/docker/docker/pkg/plugins"
 	"github.com/docker/docker/pkg/plugins"
 	"github.com/docker/libnetwork/datastore"
 	"github.com/docker/libnetwork/datastore"
 	"github.com/docker/libnetwork/discoverapi"
 	"github.com/docker/libnetwork/discoverapi"
 	"github.com/docker/libnetwork/driverapi"
 	"github.com/docker/libnetwork/driverapi"
 	"github.com/docker/libnetwork/drivers/remote/api"
 	"github.com/docker/libnetwork/drivers/remote/api"
 	"github.com/docker/libnetwork/types"
 	"github.com/docker/libnetwork/types"
+	"github.com/pkg/errors"
 	"github.com/sirupsen/logrus"
 	"github.com/sirupsen/logrus"
 )
 )
 
 
@@ -49,7 +50,11 @@ func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
 		handleFunc = pg.Handle
 		handleFunc = pg.Handle
 		activePlugins := pg.GetAllManagedPluginsByCap(driverapi.NetworkPluginEndpointType)
 		activePlugins := pg.GetAllManagedPluginsByCap(driverapi.NetworkPluginEndpointType)
 		for _, ap := range activePlugins {
 		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)
 	handleFunc(driverapi.NetworkPluginEndpointType, newPluginHandler)
@@ -57,6 +62,28 @@ func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
 	return nil
 	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
 // Get capability from client
 func (d *driver) getCapabilities() (*driverapi.Capability, error) {
 func (d *driver) getCapabilities() (*driverapi.Capability, error) {
 	var capResp api.GetCapabilityResponse
 	var capResp api.GetCapabilityResponse

+ 37 - 7
libnetwork/drivers/remote/driver_test.go

@@ -219,7 +219,11 @@ func TestGetEmptyCapabilities(t *testing.T) {
 		t.Fatal(err)
 		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 {
 	if d.Type() != plugin {
 		t.Fatal("Driver type does not match that given")
 		t.Fatal("Driver type does not match that given")
 	}
 	}
@@ -249,7 +253,11 @@ func TestGetExtraCapabilities(t *testing.T) {
 		t.Fatal(err)
 		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 {
 	if d.Type() != plugin {
 		t.Fatal("Driver type does not match that given")
 		t.Fatal("Driver type does not match that given")
 	}
 	}
@@ -281,7 +289,11 @@ func TestGetInvalidCapabilities(t *testing.T) {
 		t.Fatal(err)
 		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 {
 	if d.Type() != plugin {
 		t.Fatal("Driver type does not match that given")
 		t.Fatal("Driver type does not match that given")
 	}
 	}
@@ -395,7 +407,11 @@ func TestRemoteDriver(t *testing.T) {
 		t.Fatal(err)
 		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 {
 	if d.Type() != plugin {
 		t.Fatal("Driver type does not match that given")
 		t.Fatal("Driver type does not match that given")
 	}
 	}
@@ -473,7 +489,11 @@ func TestDriverError(t *testing.T) {
 		t.Fatal(err)
 		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 {
 	if err := driver.CreateEndpoint("dummy", "dummy", &testEndpoint{t: t}, map[string]interface{}{}); err == nil {
 		t.Fatal("Expected error from driver")
 		t.Fatal("Expected error from driver")
@@ -505,7 +525,12 @@ func TestMissingValues(t *testing.T) {
 	if err != nil {
 	if err != nil {
 		t.Fatal(err)
 		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 {
 	if err := driver.CreateEndpoint("dummy", "dummy", ep, map[string]interface{}{}); err != nil {
 		t.Fatal(err)
 		t.Fatal(err)
@@ -566,7 +591,12 @@ func TestRollback(t *testing.T) {
 	if err != nil {
 	if err != nil {
 		t.Fatal(err)
 		t.Fatal(err)
 	}
 	}
-	driver := newDriver(plugin, p.Client())
+
+	client, err := getPluginClient(p)
+	if err != nil {
+		t.Fatal(err)
+	}
+	driver := newDriver(plugin, client)
 
 
 	ep := &rollbackEndpoint{}
 	ep := &rollbackEndpoint{}
 
 

+ 29 - 1
libnetwork/ipams/remote/remote.go

@@ -4,11 +4,13 @@ import (
 	"fmt"
 	"fmt"
 	"net"
 	"net"
 
 
+	"github.com/docker/docker/pkg/plugingetter"
 	"github.com/docker/docker/pkg/plugins"
 	"github.com/docker/docker/pkg/plugins"
 	"github.com/docker/libnetwork/discoverapi"
 	"github.com/docker/libnetwork/discoverapi"
 	"github.com/docker/libnetwork/ipamapi"
 	"github.com/docker/libnetwork/ipamapi"
 	"github.com/docker/libnetwork/ipams/remote/api"
 	"github.com/docker/libnetwork/ipams/remote/api"
 	"github.com/docker/libnetwork/types"
 	"github.com/docker/libnetwork/types"
+	"github.com/pkg/errors"
 	"github.com/sirupsen/logrus"
 	"github.com/sirupsen/logrus"
 )
 )
 
 
@@ -52,13 +54,39 @@ func Init(cb ipamapi.Callback, l, g interface{}) error {
 		handleFunc = pg.Handle
 		handleFunc = pg.Handle
 		activePlugins := pg.GetAllManagedPluginsByCap(ipamapi.PluginEndpointType)
 		activePlugins := pg.GetAllManagedPluginsByCap(ipamapi.PluginEndpointType)
 		for _, ap := range activePlugins {
 		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)
 	handleFunc(ipamapi.PluginEndpointType, newPluginHandler)
 	return nil
 	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 {
 func (a *allocator) call(methodName string, arg interface{}, retVal PluginResponse) error {
 	method := ipamapi.PluginEndpointType + "." + methodName
 	method := ipamapi.PluginEndpointType + "." + methodName
 	err := a.endpoint.Call(method, arg, retVal)
 	err := a.endpoint.Call(method, arg, retVal)

+ 21 - 4
libnetwork/ipams/remote/remote_test.go

@@ -79,7 +79,11 @@ func TestGetCapabilities(t *testing.T) {
 		t.Fatal(err)
 		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()
 	caps, err := d.(*allocator).getCapabilities()
 	if err != nil {
 	if err != nil {
@@ -102,7 +106,12 @@ func TestGetCapabilitiesFromLegacyDriver(t *testing.T) {
 		t.Fatal(err)
 		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 {
 	if _, err := d.(*allocator).getCapabilities(); err == nil {
 		t.Fatalf("Expected error, but got Success %v", err)
 		t.Fatalf("Expected error, but got Success %v", err)
@@ -127,7 +136,11 @@ func TestGetDefaultAddressSpaces(t *testing.T) {
 		t.Fatal(err)
 		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()
 	l, g, err := d.(*allocator).GetDefaultAddressSpaces()
 	if err != nil {
 	if err != nil {
@@ -217,7 +230,11 @@ func TestRemoteDriver(t *testing.T) {
 		t.Fatal(err)
 		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()
 	l, g, err := d.(*allocator).GetDefaultAddressSpaces()
 	if err != nil {
 	if err != nil {