1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Converted IP address and gateway values to be proper types

rather than strings in the sandbox and driverapi protocol

Signed-off-by: Jana Radhakrishnan <mrjana@docker.com>
This commit is contained in:
Jana Radhakrishnan 2015-04-14 01:36:58 +00:00
parent dc7e065b01
commit 0a25032277
6 changed files with 42 additions and 56 deletions

View file

@ -1,6 +1,9 @@
package driverapi package driverapi
import "errors" import (
"errors"
"net"
)
var ( var (
// ErrEndpointExists is returned if more than one endpoint is added to the network // ErrEndpointExists is returned if more than one endpoint is added to the network
@ -50,10 +53,10 @@ type Interface struct {
DstName string DstName string
// IPv4 address for the interface. // IPv4 address for the interface.
Address string Address net.IPNet
// IPv6 address for the interface. // IPv6 address for the interface.
AddressIPv6 string AddressIPv6 net.IPNet
} }
// SandboxInfo represents all possible information that // SandboxInfo represents all possible information that
@ -63,10 +66,10 @@ type SandboxInfo struct {
Interfaces []*Interface Interfaces []*Interface
// IPv4 gateway for the sandbox. // IPv4 gateway for the sandbox.
Gateway string Gateway net.IP
// IPv6 gateway for the sandbox. // IPv6 gateway for the sandbox.
GatewayIPv6 string GatewayIPv6 net.IP
// TODO: Add routes and ip tables etc. // TODO: Add routes and ip tables etc.
} }

View file

@ -293,11 +293,11 @@ func (d *driver) CreateEndpoint(nid, eid driverapi.UUID, sboxKey string, config
intf := &driverapi.Interface{} intf := &driverapi.Interface{}
intf.SrcName = name2 intf.SrcName = name2
intf.DstName = "eth0" intf.DstName = "eth0"
intf.Address = ipv4Addr.String() intf.Address = ipv4Addr
sinfo.Gateway = n.bridge.bridgeIPv4.IP.String() sinfo.Gateway = n.bridge.bridgeIPv4.IP
if n.bridge.Config.EnableIPv6 { if n.bridge.Config.EnableIPv6 {
intf.AddressIPv6 = ipv6Addr.String() intf.AddressIPv6 = ipv6Addr
sinfo.GatewayIPv6 = n.bridge.bridgeIPv6.IP.String() sinfo.GatewayIPv6 = n.bridge.bridgeIPv6.IP
} }
n.endpoint.addressIPv4 = ip4 n.endpoint.addressIPv4 = ip4

View file

@ -1,7 +1,6 @@
package bridge package bridge
import ( import (
"net"
"testing" "testing"
"github.com/docker/libnetwork/driverapi" "github.com/docker/libnetwork/driverapi"
@ -41,33 +40,25 @@ func TestLinkCreate(t *testing.T) {
t.Fatalf("Could not find source link %s: %v", interfaces[0].SrcName, err) t.Fatalf("Could not find source link %s: %v", interfaces[0].SrcName, err)
} }
ip, _, err := net.ParseCIDR(interfaces[0].Address)
if err != nil {
t.Fatalf("Invalid IPv4 address returned, ip = %s: %v", interfaces[0].Address, err)
}
n := dr.network n := dr.network
ip := interfaces[0].Address.IP
if !n.bridge.bridgeIPv4.Contains(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()) t.Fatalf("IP %s is not a valid ip in the subnet %s", ip.String(), n.bridge.bridgeIPv4.String())
} }
ip6, _, err := net.ParseCIDR(interfaces[0].AddressIPv6) ip6 := interfaces[0].AddressIPv6.IP
if err != nil {
t.Fatalf("Invalid IPv6 address returned, ip = %s: %v", interfaces[0].AddressIPv6, err)
}
if !n.bridge.bridgeIPv6.Contains(ip6) { if !n.bridge.bridgeIPv6.Contains(ip6) {
t.Fatalf("IP %s is not a valid ip in the subnet %s", ip6.String(), bridgeIPv6.String()) t.Fatalf("IP %s is not a valid ip in the subnet %s", ip6.String(), bridgeIPv6.String())
} }
if sinfo.Gateway != n.bridge.bridgeIPv4.IP.String() { if sinfo.Gateway.String() != n.bridge.bridgeIPv4.IP.String() {
t.Fatalf("Invalid default gateway. Expected %s. Got %s", n.bridge.bridgeIPv4.IP.String(), t.Fatalf("Invalid default gateway. Expected %s. Got %s", n.bridge.bridgeIPv4.IP.String(),
sinfo.Gateway) sinfo.Gateway.String())
} }
if sinfo.GatewayIPv6 != n.bridge.bridgeIPv6.IP.String() { if sinfo.GatewayIPv6.String() != n.bridge.bridgeIPv6.IP.String() {
t.Fatalf("Invalid default gateway for IPv6. Expected %s. Got %s", n.bridge.bridgeIPv6.IP.String(), t.Fatalf("Invalid default gateway for IPv6. Expected %s. Got %s", n.bridge.bridgeIPv6.IP.String(),
sinfo.GatewayIPv6) sinfo.GatewayIPv6.String())
} }
} }
@ -115,9 +106,11 @@ func TestLinkCreateNoEnableIPv6(t *testing.T) {
} }
interfaces := sinfo.Interfaces interfaces := sinfo.Interfaces
if interfaces[0].AddressIPv6 != "" || if len(interfaces[0].AddressIPv6.IP) != 0 {
sinfo.GatewayIPv6 != "" { t.Fatalf("Expectd IPv6 address to be nil when IPv6 is not enabled. Got IPv6 = %s", interfaces[0].AddressIPv6.String())
t.Fatalf("Expected IPv6 address and GatewayIPv6 to be empty when IPv6 enabled. Instead got IPv6 = %s and GatewayIPv6 = %s", }
interfaces[0].AddressIPv6, sinfo.GatewayIPv6)
if sinfo.GatewayIPv6 != nil {
t.Fatalf("Expected GatewayIPv6 to be nil when IPv6 is not enabled. Got GatewayIPv6 = %s", sinfo.GatewayIPv6.String())
} }
} }

View file

@ -17,8 +17,6 @@ func configureInterface(iface netlink.Link, settings *driverapi.Interface) error
{setInterfaceName, fmt.Sprintf("error renaming interface %q to %q", ifaceName, settings.DstName)}, {setInterfaceName, fmt.Sprintf("error renaming interface %q to %q", ifaceName, settings.DstName)},
{setInterfaceIP, fmt.Sprintf("error setting interface %q IP to %q", ifaceName, settings.Address)}, {setInterfaceIP, fmt.Sprintf("error setting interface %q IP to %q", ifaceName, settings.Address)},
{setInterfaceIPv6, fmt.Sprintf("error setting interface %q IPv6 to %q", ifaceName, settings.AddressIPv6)}, {setInterfaceIPv6, fmt.Sprintf("error setting interface %q IPv6 to %q", ifaceName, settings.AddressIPv6)},
/* {setInterfaceGateway, fmt.Sprintf("error setting interface %q gateway to %q", ifaceName, settings.Gateway)},
{setInterfaceGatewayIPv6, fmt.Sprintf("error setting interface %q IPv6 gateway to %q", ifaceName, settings.GatewayIPv6)}, */
} }
for _, config := range ifaceConfigurators { for _, config := range ifaceConfigurators {
@ -29,36 +27,21 @@ func configureInterface(iface netlink.Link, settings *driverapi.Interface) error
return nil return nil
} }
func setGatewayIP(gw string) error { func setGatewayIP(gw net.IP) error {
ip := net.ParseIP(gw)
if ip == nil {
return fmt.Errorf("bad address format %q", gw)
}
return netlink.RouteAdd(&netlink.Route{ return netlink.RouteAdd(&netlink.Route{
Scope: netlink.SCOPE_UNIVERSE, Scope: netlink.SCOPE_UNIVERSE,
Gw: ip, Gw: gw,
}) })
} }
func setInterfaceIP(iface netlink.Link, settings *driverapi.Interface) error { func setInterfaceIP(iface netlink.Link, settings *driverapi.Interface) error {
ipAddr, err := netlink.ParseAddr(settings.Address) ipAddr := &netlink.Addr{IPNet: &settings.Address, Label: ""}
if err == nil { return netlink.AddrAdd(iface, ipAddr)
err = netlink.AddrAdd(iface, ipAddr)
}
return err
} }
func setInterfaceIPv6(iface netlink.Link, settings *driverapi.Interface) error { func setInterfaceIPv6(iface netlink.Link, settings *driverapi.Interface) error {
if settings.AddressIPv6 == "" { ipAddr := &netlink.Addr{IPNet: &settings.Address, Label: ""}
return nil return netlink.AddrAdd(iface, ipAddr)
}
ipAddr, err := netlink.ParseAddr(settings.AddressIPv6)
if err == nil {
err = netlink.AddrAdd(iface, ipAddr)
}
return err
} }
func setInterfaceName(iface netlink.Link, settings *driverapi.Interface) error { func setInterfaceName(iface netlink.Link, settings *driverapi.Interface) error {

View file

@ -2,6 +2,7 @@ package sandbox
import ( import (
"fmt" "fmt"
"net"
"os" "os"
"runtime" "runtime"
"syscall" "syscall"
@ -120,7 +121,7 @@ func (n *networkNamespace) AddInterface(i *driverapi.Interface) error {
return nil return nil
} }
func (n *networkNamespace) SetGateway(gw string) error { func (n *networkNamespace) SetGateway(gw net.IP) error {
err := setGatewayIP(gw) err := setGatewayIP(gw)
if err == nil { if err == nil {
n.sinfo.Gateway = gw n.sinfo.Gateway = gw
@ -129,7 +130,7 @@ func (n *networkNamespace) SetGateway(gw string) error {
return err return err
} }
func (n *networkNamespace) SetGatewayIPv6(gw string) error { func (n *networkNamespace) SetGatewayIPv6(gw net.IP) error {
err := setGatewayIP(gw) err := setGatewayIP(gw)
if err == nil { if err == nil {
n.sinfo.GatewayIPv6 = gw n.sinfo.GatewayIPv6 = gw

View file

@ -1,6 +1,10 @@
package sandbox package sandbox
import "github.com/docker/libnetwork/driverapi" import (
"net"
"github.com/docker/libnetwork/driverapi"
)
// Sandbox represents a network sandbox, identified by a specific key. It // Sandbox represents a network sandbox, identified by a specific key. It
// holds a list of Interfaces, routes etc, and more can be added dynamically. // holds a list of Interfaces, routes etc, and more can be added dynamically.
@ -19,7 +23,9 @@ type Sandbox interface {
// interface according to the specified settings. // interface according to the specified settings.
AddInterface(*driverapi.Interface) error AddInterface(*driverapi.Interface) error
SetGateway(gw string) error // Set default IPv4 gateway for the sandbox
SetGateway(gw net.IP) error
SetGatewayIPv6(gw string) error // Set default IPv6 gateway for the sandbox
SetGatewayIPv6(gw net.IP) error
} }