2015-05-06 19:57:38 -04:00
|
|
|
package remote
|
|
|
|
|
|
|
|
import (
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2015-05-06 19:57:38 -04:00
|
|
|
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/datastore"
|
|
|
|
"github.com/docker/docker/libnetwork/discoverapi"
|
|
|
|
"github.com/docker/docker/libnetwork/driverapi"
|
|
|
|
"github.com/docker/docker/libnetwork/drivers/remote/api"
|
|
|
|
"github.com/docker/docker/libnetwork/types"
|
2021-05-27 20:15:56 -04:00
|
|
|
"github.com/docker/docker/pkg/plugingetter"
|
|
|
|
"github.com/docker/docker/pkg/plugins"
|
2018-05-31 12:36:58 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 17:18:31 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-05-06 19:57:38 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type driver struct {
|
2015-05-15 21:14:36 -04:00
|
|
|
endpoint *plugins.Client
|
|
|
|
networkType string
|
2015-05-06 19:57:38 -04:00
|
|
|
}
|
|
|
|
|
2015-06-03 00:51:10 -04:00
|
|
|
type maybeError interface {
|
|
|
|
GetError() string
|
|
|
|
}
|
|
|
|
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
func newDriver(name string, client *plugins.Client) driverapi.Driver {
|
|
|
|
return &driver{networkType: name, endpoint: client}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init makes sure a remote driver is registered when a network driver
|
|
|
|
// plugin is activated.
|
2015-09-18 17:00:36 -04:00
|
|
|
func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
|
2016-12-15 21:34:50 -05:00
|
|
|
newPluginHandler := func(name string, client *plugins.Client) {
|
2015-09-13 08:00:05 -04:00
|
|
|
// negotiate driver capability with client
|
|
|
|
d := newDriver(name, client)
|
|
|
|
c, err := d.(*driver).getCapabilities()
|
|
|
|
if err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Errorf("error getting capability for %s due to %v", name, err)
|
2015-09-13 08:00:05 -04:00
|
|
|
return
|
2015-06-06 13:21:51 -04:00
|
|
|
}
|
2015-09-13 08:00:05 -04:00
|
|
|
if err = dc.RegisterDriver(name, d, *c); err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Errorf("error registering driver for %s due to %v", name, err)
|
2015-05-15 21:14:36 -04:00
|
|
|
}
|
2016-12-15 21:34:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unit test code is unaware of a true PluginStore. So we fall back to v1 plugins.
|
|
|
|
handleFunc := plugins.Handle
|
|
|
|
if pg := dc.GetPluginGetter(); pg != nil {
|
|
|
|
handleFunc = pg.Handle
|
2016-12-22 16:46:19 -05:00
|
|
|
activePlugins := pg.GetAllManagedPluginsByCap(driverapi.NetworkPluginEndpointType)
|
2016-12-15 21:34:50 -05:00
|
|
|
for _, ap := range activePlugins {
|
2018-05-31 12:36:58 -04:00
|
|
|
client, err := getPluginClient(ap)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newPluginHandler(ap.Name(), client)
|
2016-12-15 21:34:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
handleFunc(driverapi.NetworkPluginEndpointType, newPluginHandler)
|
|
|
|
|
Make driver packages register themselves via DriverCallback
In the present code, each driver package provides a `New()` method
which constructs a driver of its type, which is then registered with
the controller.
However, this is not suitable for the `drivers/remote` package, since
it does not provide a (singleton) driver, but a mechanism for drivers
to be added dynamically. As a result, the implementation is oddly
dual-purpose, and a spurious `"remote"` driver is added to the
controller's list of available drivers.
Instead, it is better to provide the registration callback to each
package and let it register its own driver or drivers. That way, the
singleton driver packages can construct one and register it, and the
remote package can hook the callback up with whatever the dynamic
driver mechanism turns out to be.
NB there are some method signature changes; in particular to
controller.New, which can return an error if the built-in driver
packages fail to initialise.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-11 08:46:29 -04:00
|
|
|
return nil
|
2015-05-06 19:57:38 -04:00
|
|
|
}
|
|
|
|
|
2018-05-31 12:36:58 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-09-13 08:00:05 -04:00
|
|
|
// Get capability from client
|
|
|
|
func (d *driver) getCapabilities() (*driverapi.Capability, error) {
|
|
|
|
var capResp api.GetCapabilityResponse
|
|
|
|
if err := d.call("GetCapabilities", nil, &capResp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &driverapi.Capability{}
|
|
|
|
switch capResp.Scope {
|
|
|
|
case "global":
|
2015-09-16 07:39:46 -04:00
|
|
|
c.DataScope = datastore.GlobalScope
|
2015-09-13 08:00:05 -04:00
|
|
|
case "local":
|
2015-09-16 07:39:46 -04:00
|
|
|
c.DataScope = datastore.LocalScope
|
2015-09-13 08:00:05 -04:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("invalid capability: expecting 'local' or 'global', got %s", capResp.Scope)
|
|
|
|
}
|
|
|
|
|
2017-04-07 16:31:44 -04:00
|
|
|
switch capResp.ConnectivityScope {
|
|
|
|
case "global":
|
|
|
|
c.ConnectivityScope = datastore.GlobalScope
|
|
|
|
case "local":
|
|
|
|
c.ConnectivityScope = datastore.LocalScope
|
|
|
|
case "":
|
|
|
|
c.ConnectivityScope = c.DataScope
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("invalid capability: expecting 'local' or 'global', got %s", capResp.Scope)
|
|
|
|
}
|
|
|
|
|
2015-09-13 08:00:05 -04:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
// Config is not implemented for remote drivers, since it is assumed
|
|
|
|
// to be supplied to the remote process out-of-band (e.g., as command
|
|
|
|
// line arguments).
|
2015-05-06 19:57:38 -04:00
|
|
|
func (d *driver) Config(option map[string]interface{}) error {
|
2015-05-14 17:56:15 -04:00
|
|
|
return &driverapi.ErrNotImplemented{}
|
2015-05-06 19:57:38 -04:00
|
|
|
}
|
|
|
|
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
func (d *driver) call(methodName string, arg interface{}, retVal maybeError) error {
|
|
|
|
method := driverapi.NetworkPluginEndpointType + "." + methodName
|
|
|
|
err := d.endpoint.Call(method, arg, retVal)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-03 00:51:10 -04:00
|
|
|
if e := retVal.GetError(); e != "" {
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
return fmt.Errorf("remote: %s", e)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-30 08:48:14 -04:00
|
|
|
func (d *driver) NetworkAllocate(id string, options map[string]string, ipV4Data, ipV6Data []driverapi.IPAMData) (map[string]string, error) {
|
|
|
|
create := &api.AllocateNetworkRequest{
|
|
|
|
NetworkID: id,
|
|
|
|
Options: options,
|
|
|
|
IPv4Data: ipV4Data,
|
|
|
|
IPv6Data: ipV6Data,
|
|
|
|
}
|
|
|
|
retVal := api.AllocateNetworkResponse{}
|
|
|
|
err := d.call("AllocateNetwork", create, &retVal)
|
|
|
|
return retVal.Options, err
|
2016-02-26 14:53:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driver) NetworkFree(id string) error {
|
2016-06-30 08:48:14 -04:00
|
|
|
fr := &api.FreeNetworkRequest{NetworkID: id}
|
|
|
|
return d.call("FreeNetwork", fr, &api.FreeNetworkResponse{})
|
2016-02-26 14:53:13 -05:00
|
|
|
}
|
|
|
|
|
2016-04-18 22:55:39 -04:00
|
|
|
func (d *driver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
|
|
|
|
}
|
|
|
|
|
2017-03-02 02:57:37 -05:00
|
|
|
func (d *driver) DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2016-04-18 22:55:39 -04:00
|
|
|
func (d *driver) CreateNetwork(id string, options map[string]interface{}, nInfo driverapi.NetworkInfo, ipV4Data, ipV6Data []driverapi.IPAMData) error {
|
2015-06-03 00:51:10 -04:00
|
|
|
create := &api.CreateNetworkRequest{
|
2015-07-02 01:00:48 -04:00
|
|
|
NetworkID: id,
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
Options: options,
|
2015-10-03 19:11:50 -04:00
|
|
|
IPv4Data: ipV4Data,
|
|
|
|
IPv6Data: ipV6Data,
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
}
|
2015-06-03 00:51:10 -04:00
|
|
|
return d.call("CreateNetwork", create, &api.CreateNetworkResponse{})
|
2015-05-06 19:57:38 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func (d *driver) DeleteNetwork(nid string) error {
|
|
|
|
delete := &api.DeleteNetworkRequest{NetworkID: nid}
|
2015-06-03 00:51:10 -04:00
|
|
|
return d.call("DeleteNetwork", delete, &api.DeleteNetworkResponse{})
|
2015-05-06 19:57:38 -04:00
|
|
|
}
|
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo, epOptions map[string]interface{}) error {
|
|
|
|
if ifInfo == nil {
|
2016-11-21 20:29:53 -05:00
|
|
|
return errors.New("must not be called with nil InterfaceInfo")
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
}
|
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
reqIface := &api.EndpointInterface{}
|
|
|
|
if ifInfo.Address() != nil {
|
|
|
|
reqIface.Address = ifInfo.Address().String()
|
|
|
|
}
|
|
|
|
if ifInfo.AddressIPv6() != nil {
|
|
|
|
reqIface.AddressIPv6 = ifInfo.AddressIPv6().String()
|
|
|
|
}
|
|
|
|
if ifInfo.MacAddress() != nil {
|
|
|
|
reqIface.MacAddress = ifInfo.MacAddress().String()
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
}
|
2015-10-03 19:11:50 -04:00
|
|
|
|
2015-06-03 00:51:10 -04:00
|
|
|
create := &api.CreateEndpointRequest{
|
2015-07-02 01:00:48 -04:00
|
|
|
NetworkID: nid,
|
|
|
|
EndpointID: eid,
|
2015-09-09 19:06:35 -04:00
|
|
|
Interface: reqIface,
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
Options: epOptions,
|
|
|
|
}
|
2015-06-03 00:51:10 -04:00
|
|
|
var res api.CreateEndpointResponse
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
if err := d.call("CreateEndpoint", create, &res); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-09 19:06:35 -04:00
|
|
|
inIface, err := parseInterface(res)
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-03 19:11:50 -04:00
|
|
|
if inIface == nil {
|
|
|
|
// Remote driver did not set any field
|
|
|
|
return nil
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
}
|
2015-09-09 19:06:35 -04:00
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
if inIface.MacAddress != nil {
|
|
|
|
if err := ifInfo.SetMacAddress(inIface.MacAddress); err != nil {
|
|
|
|
return errorWithRollback(fmt.Sprintf("driver modified interface MAC address: %v", err), d.DeleteEndpoint(nid, eid))
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
}
|
2015-10-03 19:11:50 -04:00
|
|
|
}
|
|
|
|
if inIface.Address != nil {
|
|
|
|
if err := ifInfo.SetIPAddress(inIface.Address); err != nil {
|
|
|
|
return errorWithRollback(fmt.Sprintf("driver modified interface address: %v", err), d.DeleteEndpoint(nid, eid))
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
}
|
2015-10-03 19:11:50 -04:00
|
|
|
}
|
|
|
|
if inIface.AddressIPv6 != nil {
|
|
|
|
if err := ifInfo.SetIPAddress(inIface.AddressIPv6); err != nil {
|
|
|
|
return errorWithRollback(fmt.Sprintf("driver modified interface address: %v", err), d.DeleteEndpoint(nid, eid))
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
}
|
|
|
|
}
|
2015-10-03 19:11:50 -04:00
|
|
|
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func errorWithRollback(msg string, err error) error {
|
|
|
|
rollback := "rolled back"
|
|
|
|
if err != nil {
|
|
|
|
rollback = "failed to roll back: " + err.Error()
|
|
|
|
}
|
|
|
|
return fmt.Errorf("%s; %s", msg, rollback)
|
2015-05-06 19:57:38 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func (d *driver) DeleteEndpoint(nid, eid string) error {
|
2015-06-03 00:51:10 -04:00
|
|
|
delete := &api.DeleteEndpointRequest{
|
2015-07-02 01:00:48 -04:00
|
|
|
NetworkID: nid,
|
|
|
|
EndpointID: eid,
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
}
|
2015-06-03 00:51:10 -04:00
|
|
|
return d.call("DeleteEndpoint", delete, &api.DeleteEndpointResponse{})
|
2015-05-06 19:57:38 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func (d *driver) EndpointOperInfo(nid, eid string) (map[string]interface{}, error) {
|
2015-06-03 00:51:10 -04:00
|
|
|
info := &api.EndpointInfoRequest{
|
2015-07-02 01:00:48 -04:00
|
|
|
NetworkID: nid,
|
|
|
|
EndpointID: eid,
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
}
|
2015-06-03 00:51:10 -04:00
|
|
|
var res api.EndpointInfoResponse
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
if err := d.call("EndpointOperInfo", info, &res); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res.Value, nil
|
2015-05-06 19:57:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Join method is invoked when a Sandbox is attached to an endpoint.
|
2015-07-02 01:00:48 -04:00
|
|
|
func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo, options map[string]interface{}) error {
|
2015-06-03 00:51:10 -04:00
|
|
|
join := &api.JoinRequest{
|
2015-07-02 01:00:48 -04:00
|
|
|
NetworkID: nid,
|
|
|
|
EndpointID: eid,
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
SandboxKey: sboxKey,
|
|
|
|
Options: options,
|
|
|
|
}
|
|
|
|
var (
|
2015-06-03 00:51:10 -04:00
|
|
|
res api.JoinResponse
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
err error
|
|
|
|
)
|
|
|
|
if err = d.call("Join", join, &res); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-09 19:06:35 -04:00
|
|
|
ifaceName := res.InterfaceName
|
2015-10-03 19:11:50 -04:00
|
|
|
if iface := jinfo.InterfaceName(); iface != nil && ifaceName != nil {
|
2015-09-09 19:06:35 -04:00
|
|
|
if err := iface.SetNames(ifaceName.SrcName, ifaceName.DstPrefix); err != nil {
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
return errorWithRollback(fmt.Sprintf("failed to set interface name: %s", err), d.Leave(nid, eid))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var addr net.IP
|
|
|
|
if res.Gateway != "" {
|
|
|
|
if addr = net.ParseIP(res.Gateway); addr == nil {
|
|
|
|
return fmt.Errorf(`unable to parse Gateway "%s"`, res.Gateway)
|
|
|
|
}
|
|
|
|
if jinfo.SetGateway(addr) != nil {
|
|
|
|
return errorWithRollback(fmt.Sprintf("failed to set gateway: %v", addr), d.Leave(nid, eid))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if res.GatewayIPv6 != "" {
|
|
|
|
if addr = net.ParseIP(res.GatewayIPv6); addr == nil {
|
|
|
|
return fmt.Errorf(`unable to parse GatewayIPv6 "%s"`, res.GatewayIPv6)
|
|
|
|
}
|
|
|
|
if jinfo.SetGatewayIPv6(addr) != nil {
|
|
|
|
return errorWithRollback(fmt.Sprintf("failed to set gateway IPv6: %v", addr), d.Leave(nid, eid))
|
|
|
|
}
|
|
|
|
}
|
2015-05-27 21:39:46 -04:00
|
|
|
if len(res.StaticRoutes) > 0 {
|
2015-06-03 00:51:10 -04:00
|
|
|
routes, err := parseStaticRoutes(res)
|
2015-05-27 21:39:46 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, route := range routes {
|
2015-09-09 19:06:35 -04:00
|
|
|
if jinfo.AddStaticRoute(route.Destination, route.RouteType, route.NextHop) != nil {
|
2015-05-27 21:39:46 -04:00
|
|
|
return errorWithRollback(fmt.Sprintf("failed to set static route: %v", route), d.Leave(nid, eid))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-02 21:07:44 -05:00
|
|
|
if res.DisableGatewayService {
|
|
|
|
jinfo.DisableGatewayService()
|
|
|
|
}
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
return nil
|
2015-05-06 19:57:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Leave method is invoked when a Sandbox detaches from an endpoint.
|
2015-07-02 01:00:48 -04:00
|
|
|
func (d *driver) Leave(nid, eid string) error {
|
2015-06-03 00:51:10 -04:00
|
|
|
leave := &api.LeaveRequest{
|
2015-07-02 01:00:48 -04:00
|
|
|
NetworkID: nid,
|
|
|
|
EndpointID: eid,
|
Remote driver implementation
In essense, this just involves marshalling structs back and forth to a
remote process, via the plugin client. There are a couple of types
that don't JSONify well, notably `net.IPNet`, so there is some
translation to be done.
To conform to the driverapi interface, we must give the list of
endpoint interfaces to the remote process, and let it puzzle out what
it's supposed to do; including the possibility of returning an error.
The constraints on EndpointInfo are enforced by the remote driver
implementation; namely:
* It can't be nil
* If it's got non-empty Interfaces(), the remote process can't put
more in
In the latter case, or if we fail to add an interface for some
(future) reason, we try to roll the endpoint creation back. Likewise
for join -- if we fail to set the fields of the JoinInfo, we roll the
join back by leaving.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-16 12:27:21 -04:00
|
|
|
}
|
2015-06-03 00:51:10 -04:00
|
|
|
return d.call("Leave", leave, &api.LeaveResponse{})
|
2015-05-06 19:57:38 -04:00
|
|
|
}
|
|
|
|
|
2015-12-07 17:45:51 -05:00
|
|
|
// ProgramExternalConnectivity is invoked to program the rules to allow external connectivity for the endpoint.
|
|
|
|
func (d *driver) ProgramExternalConnectivity(nid, eid string, options map[string]interface{}) error {
|
|
|
|
data := &api.ProgramExternalConnectivityRequest{
|
|
|
|
NetworkID: nid,
|
|
|
|
EndpointID: eid,
|
|
|
|
Options: options,
|
|
|
|
}
|
|
|
|
err := d.call("ProgramExternalConnectivity", data, &api.ProgramExternalConnectivityResponse{})
|
2016-04-09 00:38:30 -04:00
|
|
|
if err != nil && plugins.IsNotFound(err) {
|
2015-12-07 17:45:51 -05:00
|
|
|
// It is not mandatory yet to support this method
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// RevokeExternalConnectivity method is invoked to remove any external connectivity programming related to the endpoint.
|
|
|
|
func (d *driver) RevokeExternalConnectivity(nid, eid string) error {
|
|
|
|
data := &api.RevokeExternalConnectivityRequest{
|
|
|
|
NetworkID: nid,
|
|
|
|
EndpointID: eid,
|
|
|
|
}
|
|
|
|
err := d.call("RevokeExternalConnectivity", data, &api.RevokeExternalConnectivityResponse{})
|
2016-04-09 00:38:30 -04:00
|
|
|
if err != nil && plugins.IsNotFound(err) {
|
2015-12-07 17:45:51 -05:00
|
|
|
// It is not mandatory yet to support this method
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-06 19:57:38 -04:00
|
|
|
func (d *driver) Type() string {
|
2015-05-15 21:14:36 -04:00
|
|
|
return d.networkType
|
2015-05-06 19:57:38 -04:00
|
|
|
}
|
2015-06-03 00:51:10 -04:00
|
|
|
|
2016-12-18 22:56:34 -05:00
|
|
|
func (d *driver) IsBuiltIn() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-09-18 15:54:08 -04:00
|
|
|
// DiscoverNew is a notification for a new discovery event, such as a new node joining a cluster
|
2016-01-28 14:54:03 -05:00
|
|
|
func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
|
|
|
|
if dType != discoverapi.NodeDiscovery {
|
2016-06-25 19:23:11 -04:00
|
|
|
return nil
|
2015-09-18 15:54:08 -04:00
|
|
|
}
|
|
|
|
notif := &api.DiscoveryNotification{
|
|
|
|
DiscoveryType: dType,
|
|
|
|
DiscoveryData: data,
|
|
|
|
}
|
|
|
|
return d.call("DiscoverNew", notif, &api.DiscoveryResponse{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DiscoverDelete is a notification for a discovery delete event, such as a node leaving a cluster
|
2016-01-28 14:54:03 -05:00
|
|
|
func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
|
|
|
|
if dType != discoverapi.NodeDiscovery {
|
2016-06-25 19:23:11 -04:00
|
|
|
return nil
|
2015-09-18 15:54:08 -04:00
|
|
|
}
|
|
|
|
notif := &api.DiscoveryNotification{
|
|
|
|
DiscoveryType: dType,
|
|
|
|
DiscoveryData: data,
|
|
|
|
}
|
|
|
|
return d.call("DiscoverDelete", notif, &api.DiscoveryResponse{})
|
|
|
|
}
|
|
|
|
|
2015-06-03 00:51:10 -04:00
|
|
|
func parseStaticRoutes(r api.JoinResponse) ([]*types.StaticRoute, error) {
|
|
|
|
var routes = make([]*types.StaticRoute, len(r.StaticRoutes))
|
|
|
|
for i, inRoute := range r.StaticRoutes {
|
|
|
|
var err error
|
2015-09-09 19:06:35 -04:00
|
|
|
outRoute := &types.StaticRoute{RouteType: inRoute.RouteType}
|
2015-06-03 00:51:10 -04:00
|
|
|
|
|
|
|
if inRoute.Destination != "" {
|
2015-10-03 19:11:50 -04:00
|
|
|
if outRoute.Destination, err = types.ParseCIDR(inRoute.Destination); err != nil {
|
2015-06-03 00:51:10 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if inRoute.NextHop != "" {
|
|
|
|
outRoute.NextHop = net.ParseIP(inRoute.NextHop)
|
|
|
|
if outRoute.NextHop == nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse nexthop IP %s", inRoute.NextHop)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
routes[i] = outRoute
|
|
|
|
}
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseInterfaces validates all the parameters of an Interface and returns them.
|
2015-09-09 19:06:35 -04:00
|
|
|
func parseInterface(r api.CreateEndpointResponse) (*api.Interface, error) {
|
|
|
|
var outIf *api.Interface
|
|
|
|
|
|
|
|
inIf := r.Interface
|
|
|
|
if inIf != nil {
|
2015-06-03 00:51:10 -04:00
|
|
|
var err error
|
2015-09-09 19:06:35 -04:00
|
|
|
outIf = &api.Interface{}
|
2015-06-03 00:51:10 -04:00
|
|
|
if inIf.Address != "" {
|
2015-10-03 19:11:50 -04:00
|
|
|
if outIf.Address, err = types.ParseCIDR(inIf.Address); err != nil {
|
2015-06-03 00:51:10 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if inIf.AddressIPv6 != "" {
|
2015-10-03 19:11:50 -04:00
|
|
|
if outIf.AddressIPv6, err = types.ParseCIDR(inIf.AddressIPv6); err != nil {
|
2015-06-03 00:51:10 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if inIf.MacAddress != "" {
|
|
|
|
if outIf.MacAddress, err = net.ParseMAC(inIf.MacAddress); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-09 19:06:35 -04:00
|
|
|
|
|
|
|
return outIf, nil
|
2015-06-03 00:51:10 -04:00
|
|
|
}
|