mirror of
https://github.com/clearlinux/libnetwork.git
synced 2026-09-03 20:31:30 +00:00
Remove multiple interface in an endpoint
Currently the endpoint data model consists of multiple interfaces per-endpoint. This seems to be an overkill since there is no real use case for it. Removing it to remove unnecessary complexity from the code. Signed-off-by: Jana Radhakrishnan <mrjana@docker.com>
This commit is contained in:
@@ -32,7 +32,6 @@ const (
|
||||
vethLen = 7
|
||||
containerVethPrefix = "eth"
|
||||
maxAllocatePortAttempts = 10
|
||||
ifaceID = 1
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -883,8 +882,8 @@ func (d *driver) CreateEndpoint(nid, eid string, epInfo driverapi.EndpointInfo,
|
||||
return errors.New("invalid endpoint info passed")
|
||||
}
|
||||
|
||||
if len(epInfo.Interfaces()) != 0 {
|
||||
return errors.New("non empty interface list passed to bridge(local) driver")
|
||||
if epInfo.Interface() != nil {
|
||||
return errors.New("non-nil interface passed to bridge(local) driver")
|
||||
}
|
||||
|
||||
// Get the network handler and make sure it exists
|
||||
@@ -1070,7 +1069,7 @@ func (d *driver) CreateEndpoint(nid, eid string, epInfo driverapi.EndpointInfo,
|
||||
endpoint.addrv6 = ipv6Addr
|
||||
}
|
||||
|
||||
err = epInfo.AddInterface(ifaceID, endpoint.macAddress, *ipv4Addr, *ipv6Addr)
|
||||
err = epInfo.AddInterface(endpoint.macAddress, *ipv4Addr, *ipv6Addr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1244,14 +1243,10 @@ func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo,
|
||||
return EndpointNotFoundError(eid)
|
||||
}
|
||||
|
||||
for _, iNames := range jinfo.InterfaceNames() {
|
||||
// Make sure to set names on the correct interface ID.
|
||||
if iNames.ID() == ifaceID {
|
||||
err = iNames.SetNames(endpoint.srcName, containerVethPrefix)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
iNames := jinfo.InterfaceName()
|
||||
err = iNames.SetNames(endpoint.srcName, containerVethPrefix)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = jinfo.SetGateway(network.bridge.gatewayIPv4)
|
||||
|
||||
@@ -58,13 +58,14 @@ func TestCreateFullOptions(t *testing.T) {
|
||||
|
||||
// Verify the IP address allocated for the endpoint belongs to the container network
|
||||
epOptions := make(map[string]interface{})
|
||||
te := &testEndpoint{ifaces: []*testInterface{}}
|
||||
te := &testEndpoint{}
|
||||
err = d.CreateEndpoint("dummy", "ep1", te, epOptions)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create an endpoint : %s", err.Error())
|
||||
}
|
||||
if !cnw.Contains(te.Interfaces()[0].Address().IP) {
|
||||
t.Fatalf("endpoint got assigned address outside of container network(%s): %s", cnw.String(), te.Interfaces()[0].Address())
|
||||
|
||||
if !cnw.Contains(te.Interface().Address().IP) {
|
||||
t.Fatalf("endpoint got assigned address outside of container network(%s): %s", cnw.String(), te.Interface().Address())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,7 +208,6 @@ func verifyV4INCEntries(networks map[string]*bridgeNetwork, numEntries int, t *t
|
||||
}
|
||||
|
||||
type testInterface struct {
|
||||
id int
|
||||
mac net.HardwareAddr
|
||||
addr net.IPNet
|
||||
addrv6 net.IPNet
|
||||
@@ -216,7 +216,7 @@ type testInterface struct {
|
||||
}
|
||||
|
||||
type testEndpoint struct {
|
||||
ifaces []*testInterface
|
||||
iface *testInterface
|
||||
gw net.IP
|
||||
gw6 net.IP
|
||||
hostsPath string
|
||||
@@ -224,24 +224,18 @@ type testEndpoint struct {
|
||||
routes []types.StaticRoute
|
||||
}
|
||||
|
||||
func (te *testEndpoint) Interfaces() []driverapi.InterfaceInfo {
|
||||
iList := make([]driverapi.InterfaceInfo, len(te.ifaces))
|
||||
|
||||
for i, iface := range te.ifaces {
|
||||
iList[i] = iface
|
||||
func (te *testEndpoint) Interface() driverapi.InterfaceInfo {
|
||||
if te.iface != nil {
|
||||
return te.iface
|
||||
}
|
||||
|
||||
return iList
|
||||
}
|
||||
|
||||
func (te *testEndpoint) AddInterface(id int, mac net.HardwareAddr, ipv4 net.IPNet, ipv6 net.IPNet) error {
|
||||
iface := &testInterface{id: id, addr: ipv4, addrv6: ipv6}
|
||||
te.ifaces = append(te.ifaces, iface)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *testInterface) ID() int {
|
||||
return i.id
|
||||
func (te *testEndpoint) AddInterface(mac net.HardwareAddr, ipv4 net.IPNet, ipv6 net.IPNet) error {
|
||||
iface := &testInterface{addr: ipv4, addrv6: ipv6}
|
||||
te.iface = iface
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *testInterface) MacAddress() net.HardwareAddr {
|
||||
@@ -262,14 +256,12 @@ func (i *testInterface) SetNames(srcName string, dstName string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (te *testEndpoint) InterfaceNames() []driverapi.InterfaceNameInfo {
|
||||
iList := make([]driverapi.InterfaceNameInfo, len(te.ifaces))
|
||||
|
||||
for i, iface := range te.ifaces {
|
||||
iList[i] = iface
|
||||
func (te *testEndpoint) InterfaceName() driverapi.InterfaceNameInfo {
|
||||
if te.iface != nil {
|
||||
return te.iface
|
||||
}
|
||||
|
||||
return iList
|
||||
return nil
|
||||
}
|
||||
|
||||
func (te *testEndpoint) SetGateway(gw net.IP) error {
|
||||
@@ -282,8 +274,8 @@ func (te *testEndpoint) SetGatewayIPv6(gw6 net.IP) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (te *testEndpoint) AddStaticRoute(destination *net.IPNet, routeType int, nextHop net.IP, interfaceID int) error {
|
||||
te.routes = append(te.routes, types.StaticRoute{Destination: destination, RouteType: routeType, NextHop: nextHop, InterfaceID: interfaceID})
|
||||
func (te *testEndpoint) AddStaticRoute(destination *net.IPNet, routeType int, nextHop net.IP) error {
|
||||
te.routes = append(te.routes, types.StaticRoute{Destination: destination, RouteType: routeType, NextHop: nextHop})
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -327,7 +319,7 @@ func testQueryEndpointInfo(t *testing.T, ulPxyEnabled bool) {
|
||||
epOptions := make(map[string]interface{})
|
||||
epOptions[netlabel.PortMap] = portMappings
|
||||
|
||||
te := &testEndpoint{ifaces: []*testInterface{}}
|
||||
te := &testEndpoint{}
|
||||
err = d.CreateEndpoint("net1", "ep1", te, epOptions)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create an endpoint : %s", err.Error())
|
||||
@@ -387,7 +379,7 @@ func TestCreateLinkWithOptions(t *testing.T) {
|
||||
epOptions := make(map[string]interface{})
|
||||
epOptions[netlabel.MacAddress] = mac
|
||||
|
||||
te := &testEndpoint{ifaces: []*testInterface{}}
|
||||
te := &testEndpoint{}
|
||||
err = d.CreateEndpoint("net1", "ep", te, epOptions)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create an endpoint: %s", err.Error())
|
||||
@@ -398,7 +390,7 @@ func TestCreateLinkWithOptions(t *testing.T) {
|
||||
t.Fatalf("Failed to join the endpoint: %v", err)
|
||||
}
|
||||
|
||||
ifaceName := te.ifaces[0].srcName
|
||||
ifaceName := te.iface.srcName
|
||||
veth, err := netlink.LinkByName(ifaceName)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -456,24 +448,24 @@ func TestLinkContainers(t *testing.T) {
|
||||
epOptions := make(map[string]interface{})
|
||||
epOptions[netlabel.ExposedPorts] = exposedPorts
|
||||
|
||||
te1 := &testEndpoint{ifaces: []*testInterface{}}
|
||||
te1 := &testEndpoint{}
|
||||
err = d.CreateEndpoint("net1", "ep1", te1, epOptions)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create an endpoint : %s", err.Error())
|
||||
}
|
||||
|
||||
addr1 := te1.ifaces[0].addr
|
||||
addr1 := te1.iface.addr
|
||||
if addr1.IP.To4() == nil {
|
||||
t.Fatalf("No Ipv4 address assigned to the endpoint: ep1")
|
||||
}
|
||||
|
||||
te2 := &testEndpoint{ifaces: []*testInterface{}}
|
||||
te2 := &testEndpoint{}
|
||||
err = d.CreateEndpoint("net1", "ep2", te2, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create an endpoint : %s", err.Error())
|
||||
}
|
||||
|
||||
addr2 := te2.ifaces[0].addr
|
||||
addr2 := te2.iface.addr
|
||||
if addr2.IP.To4() == nil {
|
||||
t.Fatalf("No Ipv4 address assigned to the endpoint: ep2")
|
||||
}
|
||||
@@ -683,7 +675,7 @@ func TestSetDefaultGw(t *testing.T) {
|
||||
t.Fatalf("Failed to create bridge: %v", err)
|
||||
}
|
||||
|
||||
te := &testEndpoint{ifaces: []*testInterface{}}
|
||||
te := &testEndpoint{}
|
||||
err = d.CreateEndpoint("dummy", "ep", te, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create endpoint: %v", err)
|
||||
|
||||
@@ -32,7 +32,7 @@ func TestLinkCreate(t *testing.T) {
|
||||
t.Fatalf("Failed to create bridge: %v", err)
|
||||
}
|
||||
|
||||
te := &testEndpoint{ifaces: []*testInterface{}}
|
||||
te := &testEndpoint{}
|
||||
err = d.CreateEndpoint("dummy", "", te, nil)
|
||||
if err != nil {
|
||||
if _, ok := err.(InvalidEndpointIDError); !ok {
|
||||
@@ -54,7 +54,7 @@ func TestLinkCreate(t *testing.T) {
|
||||
}
|
||||
|
||||
// Verify sbox endoint interface inherited MTU value from bridge config
|
||||
sboxLnk, err := netlink.LinkByName(te.ifaces[0].srcName)
|
||||
sboxLnk, err := netlink.LinkByName(te.iface.srcName)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -64,35 +64,31 @@ func TestLinkCreate(t *testing.T) {
|
||||
// TODO: if we could get peer name from (sboxLnk.(*netlink.Veth)).PeerName
|
||||
// then we could check the MTU on hostLnk as well.
|
||||
|
||||
te1 := &testEndpoint{ifaces: []*testInterface{}}
|
||||
te1 := &testEndpoint{iface: &testInterface{}}
|
||||
err = d.CreateEndpoint("dummy", "ep", te1, nil)
|
||||
if err == nil {
|
||||
t.Fatalf("Failed to detect duplicate endpoint id on same network")
|
||||
}
|
||||
|
||||
if len(te.ifaces) != 1 {
|
||||
t.Fatalf("Expected exactly one interface. Instead got %d interface(s)", len(te.ifaces))
|
||||
}
|
||||
|
||||
if te.ifaces[0].dstName == "" {
|
||||
if te.iface.dstName == "" {
|
||||
t.Fatal("Invalid Dstname returned")
|
||||
}
|
||||
|
||||
_, err = netlink.LinkByName(te.ifaces[0].srcName)
|
||||
_, err = netlink.LinkByName(te.iface.srcName)
|
||||
if err != nil {
|
||||
t.Fatalf("Could not find source link %s: %v", te.ifaces[0].srcName, err)
|
||||
t.Fatalf("Could not find source link %s: %v", te.iface.srcName, err)
|
||||
}
|
||||
|
||||
n, ok := dr.networks["dummy"]
|
||||
if !ok {
|
||||
t.Fatalf("Cannot find network %s inside driver", "dummy")
|
||||
}
|
||||
ip := te.ifaces[0].addr.IP
|
||||
ip := te.iface.addr.IP
|
||||
if !n.bridge.bridgeIPv4.Contains(ip) {
|
||||
t.Fatalf("IP %s is not a valid ip in the subnet %s", ip.String(), n.bridge.bridgeIPv4.String())
|
||||
}
|
||||
|
||||
ip6 := te.ifaces[0].addrv6.IP
|
||||
ip6 := te.iface.addrv6.IP
|
||||
if !n.bridge.bridgeIPv6.Contains(ip6) {
|
||||
t.Fatalf("IP %s is not a valid ip in the subnet %s", ip6.String(), bridgeIPv6.String())
|
||||
}
|
||||
@@ -127,13 +123,13 @@ func TestLinkCreateTwo(t *testing.T) {
|
||||
t.Fatalf("Failed to create bridge: %v", err)
|
||||
}
|
||||
|
||||
te1 := &testEndpoint{ifaces: []*testInterface{}}
|
||||
te1 := &testEndpoint{}
|
||||
err = d.CreateEndpoint("dummy", "ep", te1, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create a link: %s", err.Error())
|
||||
}
|
||||
|
||||
te2 := &testEndpoint{ifaces: []*testInterface{}}
|
||||
te2 := &testEndpoint{}
|
||||
err = d.CreateEndpoint("dummy", "ep", te2, nil)
|
||||
if err != nil {
|
||||
if _, ok := err.(driverapi.ErrEndpointExists); !ok {
|
||||
@@ -162,15 +158,15 @@ func TestLinkCreateNoEnableIPv6(t *testing.T) {
|
||||
t.Fatalf("Failed to create bridge: %v", err)
|
||||
}
|
||||
|
||||
te := &testEndpoint{ifaces: []*testInterface{}}
|
||||
te := &testEndpoint{}
|
||||
err = d.CreateEndpoint("dummy", "ep", te, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create a link: %s", err.Error())
|
||||
}
|
||||
|
||||
interfaces := te.ifaces
|
||||
if interfaces[0].addrv6.IP.To16() != nil {
|
||||
t.Fatalf("Expectd IPv6 address to be nil when IPv6 is not enabled. Got IPv6 = %s", interfaces[0].addrv6.String())
|
||||
iface := te.iface
|
||||
if iface.addrv6.IP.To16() != nil {
|
||||
t.Fatalf("Expectd IPv6 address to be nil when IPv6 is not enabled. Got IPv6 = %s", iface.addrv6.String())
|
||||
}
|
||||
|
||||
if te.gw6.To16() != nil {
|
||||
@@ -197,7 +193,7 @@ func TestLinkDelete(t *testing.T) {
|
||||
t.Fatalf("Failed to create bridge: %v", err)
|
||||
}
|
||||
|
||||
te := &testEndpoint{ifaces: []*testInterface{}}
|
||||
te := &testEndpoint{}
|
||||
err = d.CreateEndpoint("dummy", "ep1", te, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create a link: %s", err.Error())
|
||||
|
||||
@@ -49,7 +49,7 @@ func TestPortMappingConfig(t *testing.T) {
|
||||
t.Fatalf("Failed to create bridge: %v", err)
|
||||
}
|
||||
|
||||
te := &testEndpoint{ifaces: []*testInterface{}}
|
||||
te := &testEndpoint{}
|
||||
err = d.CreateEndpoint("dummy", "ep1", te, epOptions)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create the endpoint: %s", err.Error())
|
||||
|
||||
@@ -65,13 +65,10 @@ func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo,
|
||||
return fmt.Errorf("could not set mac address to the container interface: %v", err)
|
||||
}
|
||||
|
||||
for _, iNames := range jinfo.InterfaceNames() {
|
||||
// Make sure to set names on the correct interface ID.
|
||||
if iNames.ID() == 1 {
|
||||
err = iNames.SetNames(name2, "eth")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if iNames := jinfo.InterfaceName(); iNames != nil {
|
||||
err = iNames.SetNames(name2, "eth")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -51,10 +51,10 @@ func (d *driver) CreateEndpoint(nid, eid string, epInfo driverapi.EndpointInfo,
|
||||
id: eid,
|
||||
}
|
||||
|
||||
if epInfo != nil && (len(epInfo.Interfaces()) > 0) {
|
||||
addr := epInfo.Interfaces()[0].Address()
|
||||
if epInfo != nil && epInfo.Interface() != nil {
|
||||
addr := epInfo.Interface().Address()
|
||||
ep.addr = &addr
|
||||
ep.mac = epInfo.Interfaces()[0].MacAddress()
|
||||
ep.mac = epInfo.Interface().MacAddress()
|
||||
n.addEndpoint(ep)
|
||||
return nil
|
||||
}
|
||||
@@ -74,7 +74,7 @@ func (d *driver) CreateEndpoint(nid, eid string, epInfo driverapi.EndpointInfo,
|
||||
|
||||
ep.mac = netutils.GenerateMACFromIP(ep.addr.IP)
|
||||
|
||||
err = epInfo.AddInterface(1, ep.mac, *ep.addr, net.IPNet{})
|
||||
err = epInfo.AddInterface(ep.mac, *ep.addr, net.IPNet{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not add interface to endpoint info: %v", err)
|
||||
}
|
||||
|
||||
@@ -48,13 +48,12 @@ type CreateEndpointRequest struct {
|
||||
NetworkID string
|
||||
// The ID of the endpoint for later reference.
|
||||
EndpointID string
|
||||
Interfaces []*EndpointInterface
|
||||
Interface *EndpointInterface
|
||||
Options map[string]interface{}
|
||||
}
|
||||
|
||||
// EndpointInterface represents an interface endpoint.
|
||||
type EndpointInterface struct {
|
||||
ID int
|
||||
Address string
|
||||
AddressIPv6 string
|
||||
MacAddress string
|
||||
@@ -63,12 +62,11 @@ type EndpointInterface struct {
|
||||
// CreateEndpointResponse is the response to the CreateEndpoint action.
|
||||
type CreateEndpointResponse struct {
|
||||
Response
|
||||
Interfaces []*EndpointInterface
|
||||
Interface *EndpointInterface
|
||||
}
|
||||
|
||||
// Interface is the representation of a linux interface.
|
||||
type Interface struct {
|
||||
ID int
|
||||
Address *net.IPNet
|
||||
AddressIPv6 *net.IPNet
|
||||
MacAddress net.HardwareAddr
|
||||
@@ -118,16 +116,15 @@ type StaticRoute struct {
|
||||
Destination string
|
||||
RouteType int
|
||||
NextHop string
|
||||
InterfaceID int
|
||||
}
|
||||
|
||||
// JoinResponse is the response to a JoinRequest.
|
||||
type JoinResponse struct {
|
||||
Response
|
||||
InterfaceNames []*InterfaceName
|
||||
Gateway string
|
||||
GatewayIPv6 string
|
||||
StaticRoutes []StaticRoute
|
||||
InterfaceName *InterfaceName
|
||||
Gateway string
|
||||
GatewayIPv6 string
|
||||
StaticRoutes []StaticRoute
|
||||
}
|
||||
|
||||
// LeaveRequest describes the API for detaching an endpoint from a sandbox.
|
||||
|
||||
+36
-39
@@ -71,16 +71,17 @@ func (d *driver) DeleteNetwork(nid string) error {
|
||||
}
|
||||
|
||||
func (d *driver) CreateEndpoint(nid, eid string, epInfo driverapi.EndpointInfo, epOptions map[string]interface{}) error {
|
||||
var reqIface *api.EndpointInterface
|
||||
|
||||
if epInfo == nil {
|
||||
return fmt.Errorf("must not be called with nil EndpointInfo")
|
||||
}
|
||||
|
||||
reqIfaces := make([]*api.EndpointInterface, len(epInfo.Interfaces()))
|
||||
for i, iface := range epInfo.Interfaces() {
|
||||
iface := epInfo.Interface()
|
||||
if iface != nil {
|
||||
addr4 := iface.Address()
|
||||
addr6 := iface.AddressIPv6()
|
||||
reqIfaces[i] = &api.EndpointInterface{
|
||||
ID: iface.ID(),
|
||||
reqIface = &api.EndpointInterface{
|
||||
Address: addr4.String(),
|
||||
AddressIPv6: addr6.String(),
|
||||
MacAddress: iface.MacAddress().String(),
|
||||
@@ -89,7 +90,7 @@ func (d *driver) CreateEndpoint(nid, eid string, epInfo driverapi.EndpointInfo,
|
||||
create := &api.CreateEndpointRequest{
|
||||
NetworkID: nid,
|
||||
EndpointID: eid,
|
||||
Interfaces: reqIfaces,
|
||||
Interface: reqIface,
|
||||
Options: epOptions,
|
||||
}
|
||||
var res api.CreateEndpointResponse
|
||||
@@ -97,25 +98,26 @@ func (d *driver) CreateEndpoint(nid, eid string, epInfo driverapi.EndpointInfo,
|
||||
return err
|
||||
}
|
||||
|
||||
ifaces, err := parseInterfaces(res)
|
||||
inIface, err := parseInterface(res)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(reqIfaces) > 0 && len(ifaces) > 0 {
|
||||
// We're not supposed to add interfaces if there already are
|
||||
// some. Attempt to roll back
|
||||
return errorWithRollback("driver attempted to add more interfaces", d.DeleteEndpoint(nid, eid))
|
||||
if reqIface != nil && inIface != nil {
|
||||
// We're not supposed to add interface if there is already
|
||||
// one. Attempt to roll back
|
||||
return errorWithRollback("driver attempted to add interface ignoring the one provided", d.DeleteEndpoint(nid, eid))
|
||||
}
|
||||
for _, iface := range ifaces {
|
||||
|
||||
if inIface != nil {
|
||||
var addr4, addr6 net.IPNet
|
||||
if iface.Address != nil {
|
||||
addr4 = *(iface.Address)
|
||||
if inIface.Address != nil {
|
||||
addr4 = *(inIface.Address)
|
||||
}
|
||||
if iface.AddressIPv6 != nil {
|
||||
addr6 = *(iface.AddressIPv6)
|
||||
if inIface.AddressIPv6 != nil {
|
||||
addr6 = *(inIface.AddressIPv6)
|
||||
}
|
||||
if err := epInfo.AddInterface(iface.ID, iface.MacAddress, addr4, addr6); err != nil {
|
||||
return errorWithRollback(fmt.Sprintf("failed to AddInterface %v: %s", iface, err), d.DeleteEndpoint(nid, eid))
|
||||
if err := epInfo.AddInterface(inIface.MacAddress, addr4, addr6); err != nil {
|
||||
return errorWithRollback(fmt.Sprintf("failed to AddInterface %v: %s", inIface, err), d.DeleteEndpoint(nid, eid))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -165,18 +167,13 @@ func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo,
|
||||
return err
|
||||
}
|
||||
|
||||
// Expect each interface ID given by CreateEndpoint to have an
|
||||
// entry at that index in the names supplied here. In other words,
|
||||
// if you supply 0..n interfaces with IDs 0..n above, you should
|
||||
// supply the names in the same order.
|
||||
ifaceNames := res.InterfaceNames
|
||||
for _, iface := range jinfo.InterfaceNames() {
|
||||
i := iface.ID()
|
||||
if i >= len(ifaceNames) || i < 0 {
|
||||
return fmt.Errorf("no correlating interface %d in supplied interface names", i)
|
||||
}
|
||||
supplied := ifaceNames[i]
|
||||
if err := iface.SetNames(supplied.SrcName, supplied.DstPrefix); err != nil {
|
||||
ifaceName := res.InterfaceName
|
||||
if ifaceName == nil {
|
||||
return fmt.Errorf("no interface name information received")
|
||||
}
|
||||
|
||||
if iface := jinfo.InterfaceName(); iface != nil {
|
||||
if err := iface.SetNames(ifaceName.SrcName, ifaceName.DstPrefix); err != nil {
|
||||
return errorWithRollback(fmt.Sprintf("failed to set interface name: %s", err), d.Leave(nid, eid))
|
||||
}
|
||||
}
|
||||
@@ -204,7 +201,7 @@ func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo,
|
||||
return err
|
||||
}
|
||||
for _, route := range routes {
|
||||
if jinfo.AddStaticRoute(route.Destination, route.RouteType, route.NextHop, route.InterfaceID) != nil {
|
||||
if jinfo.AddStaticRoute(route.Destination, route.RouteType, route.NextHop) != nil {
|
||||
return errorWithRollback(fmt.Sprintf("failed to set static route: %v", route), d.Leave(nid, eid))
|
||||
}
|
||||
}
|
||||
@@ -229,7 +226,7 @@ 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
|
||||
outRoute := &types.StaticRoute{InterfaceID: inRoute.InterfaceID, RouteType: inRoute.RouteType}
|
||||
outRoute := &types.StaticRoute{RouteType: inRoute.RouteType}
|
||||
|
||||
if inRoute.Destination != "" {
|
||||
if outRoute.Destination, err = toAddr(inRoute.Destination); err != nil {
|
||||
@@ -250,13 +247,13 @@ func parseStaticRoutes(r api.JoinResponse) ([]*types.StaticRoute, error) {
|
||||
}
|
||||
|
||||
// parseInterfaces validates all the parameters of an Interface and returns them.
|
||||
func parseInterfaces(r api.CreateEndpointResponse) ([]*api.Interface, error) {
|
||||
var (
|
||||
Interfaces = make([]*api.Interface, len(r.Interfaces))
|
||||
)
|
||||
for i, inIf := range r.Interfaces {
|
||||
func parseInterface(r api.CreateEndpointResponse) (*api.Interface, error) {
|
||||
var outIf *api.Interface
|
||||
|
||||
inIf := r.Interface
|
||||
if inIf != nil {
|
||||
var err error
|
||||
outIf := &api.Interface{ID: inIf.ID}
|
||||
outIf = &api.Interface{}
|
||||
if inIf.Address != "" {
|
||||
if outIf.Address, err = toAddr(inIf.Address); err != nil {
|
||||
return nil, err
|
||||
@@ -272,9 +269,9 @@ func parseInterfaces(r api.CreateEndpointResponse) ([]*api.Interface, error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
Interfaces[i] = outIf
|
||||
}
|
||||
return Interfaces, nil
|
||||
|
||||
return outIf, nil
|
||||
}
|
||||
|
||||
func toAddr(ipAddr string) (*net.IPNet, error) {
|
||||
|
||||
@@ -59,7 +59,6 @@ func setupPlugin(t *testing.T, name string, mux *http.ServeMux) func() {
|
||||
|
||||
type testEndpoint struct {
|
||||
t *testing.T
|
||||
id int
|
||||
src string
|
||||
dst string
|
||||
address string
|
||||
@@ -74,16 +73,11 @@ type testEndpoint struct {
|
||||
routeType int
|
||||
}
|
||||
|
||||
func (test *testEndpoint) Interfaces() []driverapi.InterfaceInfo {
|
||||
// return an empty one so we don't trip the check for existing
|
||||
// interfaces; we don't care about this after that
|
||||
return []driverapi.InterfaceInfo{}
|
||||
func (test *testEndpoint) Interface() driverapi.InterfaceInfo {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (test *testEndpoint) AddInterface(ID int, mac net.HardwareAddr, ipv4 net.IPNet, ipv6 net.IPNet) error {
|
||||
if ID != test.id {
|
||||
test.t.Fatalf("Wrong ID passed to AddInterface: %d", ID)
|
||||
}
|
||||
func (test *testEndpoint) AddInterface(mac net.HardwareAddr, ipv4 net.IPNet, ipv6 net.IPNet) error {
|
||||
ip4, net4, _ := net.ParseCIDR(test.address)
|
||||
ip6, net6, _ := net.ParseCIDR(test.addressIPv6)
|
||||
if ip4 != nil {
|
||||
@@ -104,8 +98,8 @@ func (test *testEndpoint) AddInterface(ID int, mac net.HardwareAddr, ipv4 net.IP
|
||||
return nil
|
||||
}
|
||||
|
||||
func (test *testEndpoint) InterfaceNames() []driverapi.InterfaceNameInfo {
|
||||
return []driverapi.InterfaceNameInfo{test}
|
||||
func (test *testEndpoint) InterfaceName() driverapi.InterfaceNameInfo {
|
||||
return test
|
||||
}
|
||||
|
||||
func compareIPs(t *testing.T, kind string, shouldBe string, supplied net.IP) {
|
||||
@@ -148,7 +142,7 @@ func (test *testEndpoint) SetNames(src string, dst string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (test *testEndpoint) AddStaticRoute(destination *net.IPNet, routeType int, nextHop net.IP, interfaceID int) error {
|
||||
func (test *testEndpoint) AddStaticRoute(destination *net.IPNet, routeType int, nextHop net.IP) error {
|
||||
compareIPNets(test.t, "Destination", test.destination, *destination)
|
||||
compareIPs(test.t, "NextHop", test.nextHop, nextHop)
|
||||
|
||||
@@ -156,17 +150,9 @@ func (test *testEndpoint) AddStaticRoute(destination *net.IPNet, routeType int,
|
||||
test.t.Fatalf(`Wrong RouteType; expected "%d", got "%d"`, test.routeType, routeType)
|
||||
}
|
||||
|
||||
if test.id != interfaceID {
|
||||
test.t.Fatalf(`Wrong InterfaceID; expected "%d", got "%d"`, test.id, interfaceID)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (test *testEndpoint) ID() int {
|
||||
return test.id
|
||||
}
|
||||
|
||||
func TestRemoteDriver(t *testing.T) {
|
||||
var plugin = "test-net-driver"
|
||||
|
||||
@@ -207,13 +193,12 @@ func TestRemoteDriver(t *testing.T) {
|
||||
})
|
||||
handle(t, mux, "CreateEndpoint", func(msg map[string]interface{}) interface{} {
|
||||
iface := map[string]interface{}{
|
||||
"ID": ep.id,
|
||||
"Address": ep.address,
|
||||
"AddressIPv6": ep.addressIPv6,
|
||||
"MacAddress": ep.macAddress,
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"Interfaces": []interface{}{iface},
|
||||
"Interface": iface,
|
||||
}
|
||||
})
|
||||
handle(t, mux, "Join", func(msg map[string]interface{}) interface{} {
|
||||
@@ -227,17 +212,14 @@ func TestRemoteDriver(t *testing.T) {
|
||||
"GatewayIPv6": ep.gatewayIPv6,
|
||||
"HostsPath": ep.hostsPath,
|
||||
"ResolvConfPath": ep.resolvConfPath,
|
||||
"InterfaceNames": []map[string]interface{}{
|
||||
map[string]interface{}{
|
||||
"SrcName": ep.src,
|
||||
"DstPrefix": ep.dst,
|
||||
},
|
||||
"InterfaceName": map[string]interface{}{
|
||||
"SrcName": ep.src,
|
||||
"DstPrefix": ep.dst,
|
||||
},
|
||||
"StaticRoutes": []map[string]interface{}{
|
||||
map[string]interface{}{
|
||||
"Destination": ep.destination,
|
||||
"RouteType": ep.routeType,
|
||||
"InterfaceID": ep.id,
|
||||
"NextHop": ep.nextHop,
|
||||
},
|
||||
},
|
||||
@@ -343,13 +325,11 @@ func TestMissingValues(t *testing.T) {
|
||||
defer setupPlugin(t, plugin, mux)()
|
||||
|
||||
ep := &testEndpoint{
|
||||
t: t,
|
||||
id: 0,
|
||||
t: t,
|
||||
}
|
||||
|
||||
handle(t, mux, "CreateEndpoint", func(msg map[string]interface{}) interface{} {
|
||||
iface := map[string]interface{}{
|
||||
"ID": ep.id,
|
||||
"Address": ep.address,
|
||||
"AddressIPv6": ep.addressIPv6,
|
||||
"MacAddress": ep.macAddress,
|
||||
@@ -373,11 +353,11 @@ func TestMissingValues(t *testing.T) {
|
||||
type rollbackEndpoint struct {
|
||||
}
|
||||
|
||||
func (r *rollbackEndpoint) Interfaces() []driverapi.InterfaceInfo {
|
||||
return []driverapi.InterfaceInfo{}
|
||||
func (r *rollbackEndpoint) Interface() driverapi.InterfaceInfo {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *rollbackEndpoint) AddInterface(_ int, _ net.HardwareAddr, _ net.IPNet, _ net.IPNet) error {
|
||||
func (r *rollbackEndpoint) AddInterface(_ net.HardwareAddr, _ net.IPNet, _ net.IPNet) error {
|
||||
return fmt.Errorf("fail this to trigger a rollback")
|
||||
}
|
||||
|
||||
@@ -391,13 +371,12 @@ func TestRollback(t *testing.T) {
|
||||
|
||||
handle(t, mux, "CreateEndpoint", func(msg map[string]interface{}) interface{} {
|
||||
iface := map[string]interface{}{
|
||||
"ID": 0,
|
||||
"Address": "192.168.4.5/16",
|
||||
"AddressIPv6": "",
|
||||
"MacAddress": "7a:12:34:56:78:90",
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"Interfaces": []interface{}{iface},
|
||||
"Interface": interface{}(iface),
|
||||
}
|
||||
})
|
||||
handle(t, mux, "DeleteEndpoint", func(msg map[string]interface{}) interface{} {
|
||||
|
||||
Reference in New Issue
Block a user