Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions drivers/remote/driver.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -49,14 +50,40 @@ 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)

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
Expand Down
44 changes: 37 additions & 7 deletions drivers/remote/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{}

Expand Down
30 changes: 29 additions & 1 deletion ipams/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this function declared twice in the same remote package?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this has been declared twice but in different packages.
github.com/docker/libnetwork/IPAMS/remote
github.com/docker/libnetwork/drivers/remote
I thought its same package initially and wasted my time figuring out why it throws error when I remove one API definition :(

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)
Expand Down
25 changes: 21 additions & 4 deletions ipams/remote/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 6 additions & 5 deletions libnetwork_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (
"github.com/docker/libnetwork/osl"
"github.com/docker/libnetwork/testutils"
"github.com/docker/libnetwork/types"
"github.com/opencontainers/runc/libcontainer"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
"github.com/vishvananda/netns"
Expand Down Expand Up @@ -521,14 +519,17 @@ func externalKeyTest(t *testing.T, reexec bool) {
}

func reexecSetKey(key string, containerID string, controllerID string) error {
type libcontainerState struct {
NamespacePaths map[string]string
}
var (
state libcontainer.State
state libcontainerState
b []byte
err error
)

state.NamespacePaths = make(map[configs.NamespaceType]string)
state.NamespacePaths[configs.NamespaceType("NEWNET")] = key
state.NamespacePaths = make(map[string]string)
state.NamespacePaths["NEWNET"] = key
if b, err = json.Marshal(state); err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions libnetwork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sync"
"testing"

"github.com/docker/docker/errdefs"
"github.com/docker/docker/pkg/plugins"
"github.com/docker/docker/pkg/reexec"
"github.com/docker/libnetwork"
Expand Down Expand Up @@ -209,7 +210,7 @@ func TestUnknownDriver(t *testing.T) {
t.Fatal("Expected to fail. But instead succeeded")
}

if _, ok := err.(types.NotFoundError); !ok {
if !errdefs.IsNotFound(err) {
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
}
}
Expand All @@ -221,7 +222,7 @@ func TestNilRemoteDriver(t *testing.T) {
t.Fatal("Expected to fail. But instead succeeded")
}

if _, ok := err.(types.NotFoundError); !ok {
if !errdefs.IsNotFound(err) {
t.Fatalf("Did not fail with expected error. Actual error: %v", err)
}
}
Expand Down Expand Up @@ -1396,7 +1397,7 @@ func TestValidRemoteDriver(t *testing.T) {
libnetwork.NetworkOptionGeneric(getEmptyGenericOption()))
if err != nil {
// Only fail if we could not find the plugin driver
if _, ok := err.(types.NotFoundError); ok {
if errdefs.IsNotFound(err) {
t.Fatal(err)
}
return
Expand Down
Loading