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
|
|
|
|
2015-05-15 21:14:36 -04:00
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/pkg/plugins"
|
2015-09-16 07:39:46 -04:00
|
|
|
"github.com/docker/libnetwork/datastore"
|
2015-05-06 19:57:38 -04:00
|
|
|
"github.com/docker/libnetwork/driverapi"
|
2015-06-03 00:51:10 -04:00
|
|
|
"github.com/docker/libnetwork/drivers/remote/api"
|
2015-05-06 19:57:38 -04:00
|
|
|
"github.com/docker/libnetwork/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2015-05-15 21:14:36 -04:00
|
|
|
plugins.Handle(driverapi.NetworkPluginEndpointType, 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 {
|
|
|
|
log.Errorf("error getting capability for %s due to %v", name, err)
|
|
|
|
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 {
|
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
|
|
|
log.Errorf("error registering driver for %s due to %v", name, err)
|
2015-05-15 21:14:36 -04:00
|
|
|
}
|
|
|
|
})
|
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
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
func (d *driver) CreateNetwork(id string, options map[string]interface{}, 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 {
|
|
|
|
return fmt.Errorf("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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
func (d *driver) DiscoverNew(dType driverapi.DiscoveryType, data interface{}) error {
|
|
|
|
if dType != driverapi.NodeDiscovery {
|
|
|
|
return fmt.Errorf("Unknown discovery type : %v", dType)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
func (d *driver) DiscoverDelete(dType driverapi.DiscoveryType, data interface{}) error {
|
|
|
|
if dType != driverapi.NodeDiscovery {
|
|
|
|
return fmt.Errorf("Unknown discovery type : %v", dType)
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|