Merge pull request #122 from mrjana/cnm_integ

Add enable ipv6  network configuration
This commit is contained in:
Madhu Venugopal 2015-05-06 11:02:00 -07:00
commit db8100743b
32 changed files with 359 additions and 233 deletions

View File

@ -29,7 +29,7 @@ There are many networking solutions available to suit a broad range of use-cases
driverOptions := options.Generic{}
genericOption := make(map[string]interface{})
genericOption[options.GenericData] = driverOptions
genericOption[netlabel.GenericData] = driverOptions
err := controller.ConfigureNetworkDriver(networkType, genericOption)
if err != nil {
return
@ -64,7 +64,7 @@ There are many networking solutions available to suit a broad range of use-cases
// libentwork client can check the endpoint's operational data via the Info() API
epInfo, err := ep.Info()
mapData, ok := epInfo[options.PortMap]
mapData, ok := epInfo[netlabel.PortMap]
if ok {
portMapping, ok := mapData.([]netutils.PortBinding)
if ok {

View File

@ -5,6 +5,7 @@ import (
"github.com/docker/libnetwork"
"github.com/docker/libnetwork/netutils"
"github.com/docker/libnetwork/pkg/netlabel"
"github.com/docker/libnetwork/pkg/options"
)
@ -17,7 +18,7 @@ func main() {
driverOptions := options.Generic{}
genericOption := make(map[string]interface{})
genericOption[options.GenericData] = driverOptions
genericOption[netlabel.GenericData] = driverOptions
err := controller.ConfigureNetworkDriver(networkType, genericOption)
if err != nil {
return
@ -52,7 +53,7 @@ func main() {
// libentwork client can check the endpoint's operational data via the Info() API
epInfo, err := ep.Info()
mapData, ok := epInfo[options.PortMap]
mapData, ok := epInfo[netlabel.PortMap]
if ok {
portMapping, ok := mapData.([]netutils.PortBinding)
if ok {

View File

@ -10,7 +10,7 @@ create network namespaces and allocate interfaces for containers to use.
driverOptions := options.Generic{}
genericOption := make(map[string]interface{})
genericOption[options.GenericData] = driverOptions
genericOption[netlabel.GenericData] = driverOptions
err := controller.ConfigureNetworkDriver(networkType, genericOption)
if err != nil {
return

View File

@ -8,6 +8,7 @@ import (
"github.com/docker/libnetwork/driverapi"
"github.com/docker/libnetwork/ipallocator"
"github.com/docker/libnetwork/netutils"
"github.com/docker/libnetwork/pkg/netlabel"
"github.com/docker/libnetwork/pkg/options"
"github.com/docker/libnetwork/portmapper"
"github.com/docker/libnetwork/sandbox"
@ -30,6 +31,11 @@ var (
// Configuration info for the "bridge" driver.
type Configuration struct {
EnableIPForwarding bool
}
// NetworkConfiguration for network specific configuration
type NetworkConfiguration struct {
BridgeName string
AddressIPv4 *net.IPNet
FixedCIDR *net.IPNet
@ -38,12 +44,11 @@ type Configuration struct {
EnableIPTables bool
EnableIPMasquerade bool
EnableICC bool
EnableIPForwarding bool
AllowNonDefaultBridge bool
Mtu int
DefaultGatewayIPv4 net.IP
DefaultGatewayIPv6 net.IP
DefaultBindingIP net.IP
AllowNonDefaultBridge bool
}
// EndpointConfiguration represents the user specified configuration for the sandbox endpoint
@ -70,7 +75,8 @@ type bridgeEndpoint struct {
type bridgeNetwork struct {
id types.UUID
bridge *bridgeInterface // The bridge's L3 interface
bridge *bridgeInterface // The bridge's L3 interface
config *NetworkConfiguration
endpoints map[types.UUID]*bridgeEndpoint // key: endpoint id
sync.Mutex
}
@ -91,9 +97,9 @@ func New() (string, driverapi.Driver) {
return networkType, &driver{}
}
// Validate performs a static validation on the configuration parameters.
// Validate performs a static validation on the network configuration parameters.
// Whatever can be assessed a priori before attempting any programming.
func (c *Configuration) Validate() error {
func (c *NetworkConfiguration) Validate() error {
if c.Mtu < 0 {
return ErrInvalidMtu
}
@ -156,8 +162,8 @@ func (d *driver) Config(option map[string]interface{}) error {
return ErrConfigExists
}
genericData := option[options.GenericData]
if genericData != nil {
genericData, ok := option[netlabel.GenericData]
if ok && genericData != nil {
switch opt := genericData.(type) {
case options.Generic:
opaqueConfig, err := options.GenerateFromModel(opt, &Configuration{})
@ -171,10 +177,13 @@ func (d *driver) Config(option map[string]interface{}) error {
return ErrInvalidDriverConfig
}
if err := config.Validate(); err != nil {
return err
}
d.config = config
} else {
config = &Configuration{}
}
if config.EnableIPForwarding {
return setupIPForwarding(config)
}
return nil
@ -188,17 +197,44 @@ func (d *driver) getNetwork(id types.UUID) (*bridgeNetwork, error) {
return d.network, nil
}
func parseNetworkOptions(option options.Generic) (*NetworkConfiguration, error) {
var config *NetworkConfiguration
genericData, ok := option[netlabel.GenericData]
if ok && genericData != nil {
switch opt := genericData.(type) {
case options.Generic:
opaqueConfig, err := options.GenerateFromModel(opt, &NetworkConfiguration{})
if err != nil {
return nil, err
}
config = opaqueConfig.(*NetworkConfiguration)
case *NetworkConfiguration:
config = opt
default:
return nil, ErrInvalidNetworkConfig
}
if err := config.Validate(); err != nil {
return nil, err
}
} else {
config = &NetworkConfiguration{}
}
if _, ok := option[netlabel.EnableIPv6]; ok {
config.EnableIPv6 = option[netlabel.EnableIPv6].(bool)
}
return config, nil
}
// Create a new network using bridge plugin
func (d *driver) CreateNetwork(id types.UUID, option map[string]interface{}) error {
var err error
// Driver must be configured
d.Lock()
if d.config == nil {
d.Unlock()
return ErrInvalidNetworkConfig
}
config := d.config
// Sanity checks
if d.network != nil {
@ -208,6 +244,7 @@ func (d *driver) CreateNetwork(id types.UUID, option map[string]interface{}) err
// Create and set network handler in driver
d.network = &bridgeNetwork{id: id, endpoints: make(map[types.UUID]*bridgeEndpoint)}
network := d.network
d.Unlock()
// On failure make sure to reset driver network handler to nil
@ -219,9 +256,15 @@ func (d *driver) CreateNetwork(id types.UUID, option map[string]interface{}) err
}
}()
config, err := parseNetworkOptions(option)
if err != nil {
return err
}
network.config = config
// Create or retrieve the bridge L3 interface
bridgeIface := newInterface(config)
d.network.bridge = bridgeIface
network.bridge = bridgeIface
// Prepare the bridge setup configuration
bridgeSetup := newBridgeSetup(config, bridgeIface)
@ -262,9 +305,6 @@ func (d *driver) CreateNetwork(id types.UUID, option map[string]interface{}) err
// Setup IPTables.
{config.EnableIPTables, setupIPTables},
// Setup IP forwarding.
{config.EnableIPForwarding, setupIPForwarding},
// Setup DefaultGatewayIPv4
{config.DefaultGatewayIPv4 != nil, setupGatewayIPv4},
@ -335,7 +375,7 @@ func (d *driver) CreateEndpoint(nid, eid types.UUID, epOptions map[string]interf
// Get the network handler and make sure it exists
d.Lock()
n := d.network
config := d.config
config := n.config
d.Unlock()
if n == nil {
return nil, driverapi.ErrNoNetwork
@ -516,7 +556,7 @@ func (d *driver) DeleteEndpoint(nid, eid types.UUID) error {
// Get the network handler and make sure it exists
d.Lock()
n := d.network
config := d.config
config := n.config
d.Unlock()
if n == nil {
return driverapi.ErrNoNetwork
@ -617,11 +657,11 @@ func (d *driver) EndpointInfo(nid, eid types.UUID) (map[string]interface{}, erro
for _, pm := range ep.portMapping {
pmc = append(pmc, pm.GetCopy())
}
m[options.PortMap] = pmc
m[netlabel.PortMap] = pmc
}
if len(ep.macAddress) != 0 {
m[options.MacAddress] = ep.macAddress
m[netlabel.MacAddress] = ep.macAddress
}
return m, nil
@ -629,23 +669,29 @@ func (d *driver) EndpointInfo(nid, eid types.UUID) (map[string]interface{}, erro
// Join method is invoked when a Sandbox is attached to an endpoint.
func (d *driver) Join(nid, eid types.UUID, sboxKey string, options map[string]interface{}) (*driverapi.JoinInfo, error) {
d.Lock()
config := d.config
d.Unlock()
if !config.EnableICC {
network, err := d.getNetwork(nid)
if err != nil {
return nil, err
}
if !network.config.EnableICC {
return nil, d.link(nid, eid, options, true)
}
return nil, nil
}
// Leave method is invoked when a Sandbox detaches from an endpoint.
func (d *driver) Leave(nid, eid types.UUID, options map[string]interface{}) error {
d.Lock()
config := d.config
d.Unlock()
if !config.EnableICC {
network, err := d.getNetwork(nid)
if err != nil {
return err
}
if !network.config.EnableICC {
return d.link(nid, eid, options, false)
}
return nil
}
@ -690,11 +736,9 @@ func (d *driver) link(nid, eid types.UUID, options map[string]interface{}, enabl
return err
}
d.Lock()
l := newLink(parentEndpoint.intf.Address.IP.String(),
endpoint.intf.Address.IP.String(),
endpoint.config.ExposedPorts, d.config.BridgeName)
d.Unlock()
endpoint.config.ExposedPorts, network.config.BridgeName)
if enable {
err = l.Enable()
if err != nil {
@ -724,11 +768,10 @@ func (d *driver) link(nid, eid types.UUID, options map[string]interface{}, enabl
if childEndpoint.config == nil || childEndpoint.config.ExposedPorts == nil {
continue
}
d.Lock()
l := newLink(endpoint.intf.Address.IP.String(),
childEndpoint.intf.Address.IP.String(),
childEndpoint.config.ExposedPorts, d.config.BridgeName)
d.Unlock()
childEndpoint.config.ExposedPorts, network.config.BridgeName)
if enable {
err = l.Enable()
if err != nil {
@ -762,7 +805,7 @@ func parseEndpointOptions(epOptions map[string]interface{}) (*EndpointConfigurat
ec := &EndpointConfiguration{}
if opt, ok := epOptions[options.MacAddress]; ok {
if opt, ok := epOptions[netlabel.MacAddress]; ok {
if mac, ok := opt.(net.HardwareAddr); ok {
ec.MacAddress = mac
} else {
@ -770,7 +813,7 @@ func parseEndpointOptions(epOptions map[string]interface{}) (*EndpointConfigurat
}
}
if opt, ok := epOptions[options.PortMap]; ok {
if opt, ok := epOptions[netlabel.PortMap]; ok {
if bs, ok := opt.([]netutils.PortBinding); ok {
ec.PortBindings = bs
} else {
@ -778,7 +821,7 @@ func parseEndpointOptions(epOptions map[string]interface{}) (*EndpointConfigurat
}
}
if opt, ok := epOptions[options.ExposedPorts]; ok {
if opt, ok := epOptions[netlabel.ExposedPorts]; ok {
if ports, ok := opt.([]netutils.TransportPort); ok {
ec.ExposedPorts = ports
} else {
@ -793,7 +836,7 @@ func parseContainerOptions(cOptions map[string]interface{}) (*ContainerConfigura
if cOptions == nil {
return nil, nil
}
genericData := cOptions[options.GenericData]
genericData := cOptions[netlabel.GenericData]
if genericData == nil {
return nil, nil
}

View File

@ -9,7 +9,7 @@ import (
"github.com/docker/docker/pkg/iptables"
"github.com/docker/libnetwork/netutils"
"github.com/docker/libnetwork/pkg/options"
"github.com/docker/libnetwork/pkg/netlabel"
"github.com/vishvananda/netlink"
)
@ -18,21 +18,27 @@ func TestCreateFullOptions(t *testing.T) {
_, d := New()
config := &Configuration{
BridgeName: DefaultBridgeName,
EnableIPv6: true,
FixedCIDR: bridgeNetworks[0],
EnableIPTables: true,
EnableIPForwarding: true,
}
_, config.FixedCIDRv6, _ = net.ParseCIDR("2001:db8::/48")
netConfig := &NetworkConfiguration{
BridgeName: DefaultBridgeName,
EnableIPv6: true,
FixedCIDR: bridgeNetworks[0],
EnableIPTables: true,
}
_, netConfig.FixedCIDRv6, _ = net.ParseCIDR("2001:db8::/48")
genericOption := make(map[string]interface{})
genericOption[options.GenericData] = config
genericOption[netlabel.GenericData] = config
if err := d.Config(genericOption); err != nil {
t.Fatalf("Failed to setup driver config: %v", err)
}
err := d.CreateNetwork("dummy", nil)
netOption := make(map[string]interface{})
netOption[netlabel.GenericData] = netConfig
err := d.CreateNetwork("dummy", netOption)
if err != nil {
t.Fatalf("Failed to create bridge: %v", err)
}
@ -42,15 +48,11 @@ func TestCreate(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
_, d := New()
config := &Configuration{BridgeName: DefaultBridgeName}
config := &NetworkConfiguration{BridgeName: DefaultBridgeName}
genericOption := make(map[string]interface{})
genericOption[options.GenericData] = config
genericOption[netlabel.GenericData] = config
if err := d.Config(genericOption); err != nil {
t.Fatalf("Failed to setup driver config: %v", err)
}
if err := d.CreateNetwork("dummy", nil); err != nil {
if err := d.CreateNetwork("dummy", genericOption); err != nil {
t.Fatalf("Failed to create bridge: %v", err)
}
}
@ -59,15 +61,11 @@ func TestCreateFail(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
_, d := New()
config := &Configuration{BridgeName: "dummy0"}
config := &NetworkConfiguration{BridgeName: "dummy0"}
genericOption := make(map[string]interface{})
genericOption[options.GenericData] = config
genericOption[netlabel.GenericData] = config
if err := d.Config(genericOption); err != nil {
t.Fatalf("Failed to setup driver config: %v", err)
}
if err := d.CreateNetwork("dummy", nil); err == nil {
if err := d.CreateNetwork("dummy", genericOption); err == nil {
t.Fatal("Bridge creation was expected to fail")
}
}
@ -77,26 +75,22 @@ func TestQueryEndpointInfo(t *testing.T) {
_, d := New()
config := &Configuration{
config := &NetworkConfiguration{
BridgeName: DefaultBridgeName,
EnableIPTables: true,
EnableICC: false,
}
genericOption := make(map[string]interface{})
genericOption[options.GenericData] = config
genericOption[netlabel.GenericData] = config
if err := d.Config(genericOption); err != nil {
t.Fatalf("Failed to setup driver config: %v", err)
}
err := d.CreateNetwork("net1", nil)
err := d.CreateNetwork("net1", genericOption)
if err != nil {
t.Fatalf("Failed to create bridge: %v", err)
}
portMappings := getPortMapping()
epOptions := make(map[string]interface{})
epOptions[options.PortMap] = portMappings
epOptions[netlabel.PortMap] = portMappings
_, err = d.CreateEndpoint("net1", "ep1", epOptions)
if err != nil {
@ -109,7 +103,7 @@ func TestQueryEndpointInfo(t *testing.T) {
if err != nil {
t.Fatalf("Failed to ask for endpoint operational data: %v", err)
}
pmd, ok := data[options.PortMap]
pmd, ok := data[netlabel.PortMap]
if !ok {
t.Fatalf("Endpoint operational data does not contain port mapping data")
}
@ -138,22 +132,18 @@ func TestCreateLinkWithOptions(t *testing.T) {
_, d := New()
config := &Configuration{BridgeName: DefaultBridgeName}
driverOptions := make(map[string]interface{})
driverOptions[options.GenericData] = config
config := &NetworkConfiguration{BridgeName: DefaultBridgeName}
netOptions := make(map[string]interface{})
netOptions[netlabel.GenericData] = config
if err := d.Config(driverOptions); err != nil {
t.Fatalf("Failed to setup driver config: %v", err)
}
err := d.CreateNetwork("net1", nil)
err := d.CreateNetwork("net1", netOptions)
if err != nil {
t.Fatalf("Failed to create bridge: %v", err)
}
mac := net.HardwareAddr([]byte{0x1e, 0x67, 0x66, 0x44, 0x55, 0x66})
epOptions := make(map[string]interface{})
epOptions[options.MacAddress] = mac
epOptions[netlabel.MacAddress] = mac
sinfo, err := d.CreateEndpoint("net1", "ep", epOptions)
if err != nil {
@ -192,26 +182,22 @@ func TestLinkContainers(t *testing.T) {
_, d := New()
config := &Configuration{
config := &NetworkConfiguration{
BridgeName: DefaultBridgeName,
EnableIPTables: true,
EnableICC: false,
}
genericOption := make(map[string]interface{})
genericOption[options.GenericData] = config
genericOption[netlabel.GenericData] = config
if err := d.Config(genericOption); err != nil {
t.Fatalf("Failed to setup driver config: %v", err)
}
err := d.CreateNetwork("net1", nil)
err := d.CreateNetwork("net1", genericOption)
if err != nil {
t.Fatalf("Failed to create bridge: %v", err)
}
exposedPorts := getExposedPorts()
epOptions := make(map[string]interface{})
epOptions[options.ExposedPorts] = exposedPorts
epOptions[netlabel.ExposedPorts] = exposedPorts
sinfo, err := d.CreateEndpoint("net1", "ep1", epOptions)
if err != nil {
@ -236,7 +222,7 @@ func TestLinkContainers(t *testing.T) {
ce := []string{"ep1"}
cConfig := &ContainerConfiguration{ChildEndpoints: ce}
genericOption = make(map[string]interface{})
genericOption[options.GenericData] = cConfig
genericOption[netlabel.GenericData] = cConfig
_, err = d.Join("net1", "ep2", "", genericOption)
if err != nil {
@ -284,7 +270,7 @@ func TestLinkContainers(t *testing.T) {
ce = []string{"ep1", "ep4"}
cConfig = &ContainerConfiguration{ChildEndpoints: ce}
genericOption = make(map[string]interface{})
genericOption[options.GenericData] = cConfig
genericOption[netlabel.GenericData] = cConfig
_, err = d.Join("net1", "ep2", "", genericOption)
if err != nil {
@ -311,7 +297,7 @@ func TestLinkContainers(t *testing.T) {
func TestValidateConfig(t *testing.T) {
// Test mtu
c := Configuration{Mtu: -2}
c := NetworkConfiguration{Mtu: -2}
err := c.Validate()
if err == nil {
t.Fatalf("Failed to detect invalid MTU number")
@ -328,7 +314,7 @@ func TestValidateConfig(t *testing.T) {
// Test FixedCIDR
_, containerSubnet, _ := net.ParseCIDR("172.27.0.0/16")
c = Configuration{
c = NetworkConfiguration{
AddressIPv4: network,
FixedCIDR: containerSubnet,
}
@ -374,7 +360,7 @@ func TestValidateConfig(t *testing.T) {
// Test v6 gw
_, containerSubnet, _ = net.ParseCIDR("2001:1234:ae:b004::/64")
c = Configuration{
c = NetworkConfiguration{
EnableIPv6: true,
FixedCIDRv6: containerSubnet,
DefaultGatewayIPv6: net.ParseIP("2001:1234:ac:b004::bad:a55"),
@ -406,7 +392,7 @@ func TestSetDefaultGw(t *testing.T) {
gw4[3] = 254
gw6 := net.ParseIP("2001:db8:ea9:9abc:b0c4::254")
config := &Configuration{
config := &NetworkConfiguration{
BridgeName: DefaultBridgeName,
EnableIPv6: true,
FixedCIDRv6: subnetv6,
@ -415,13 +401,9 @@ func TestSetDefaultGw(t *testing.T) {
}
genericOption := make(map[string]interface{})
genericOption[options.GenericData] = config
genericOption[netlabel.GenericData] = config
if err := d.Config(genericOption); err != nil {
t.Fatalf("Failed to setup driver config: %v", err)
}
err := d.CreateNetwork("dummy", nil)
err := d.CreateNetwork("dummy", genericOption)
if err != nil {
t.Fatalf("Failed to create bridge: %v", err)
}

View File

@ -39,6 +39,10 @@ var (
// ErrInvalidMtu is returned when the user provided MTU is not valid.
ErrInvalidMtu = errors.New("invalid MTU number")
// ErrIPFwdCfg is returned when ip forwarding setup is invoked when the configuration
// not enabled.
ErrIPFwdCfg = errors.New("unexpected request to enable IP Forwarding")
)
// ErrInvalidPort is returned when the container or host port specified in the port binding is not valid.
@ -133,12 +137,6 @@ func (fcv6 *FixedCIDRv6Error) Error() string {
return fmt.Sprintf("setup FixedCIDRv6 failed for subnet %s in %s: %v", fcv6.net, fcv6.net, fcv6.err)
}
type ipForwardCfgError bridgeInterface
func (i *ipForwardCfgError) Error() string {
return fmt.Sprintf("unexpected request to enable IP Forwarding for: %v", *i)
}
type ipTableCfgError string
func (name ipTableCfgError) Error() string {

View File

@ -25,7 +25,7 @@ type bridgeInterface struct {
// an already existing device identified by the Configuration BridgeName field,
// or the default bridge name when unspecified), but doesn't attempt to create
// one when missing
func newInterface(config *Configuration) *bridgeInterface {
func newInterface(config *NetworkConfiguration) *bridgeInterface {
i := &bridgeInterface{}
// Initialize the bridge name to the default if unspecified.

View File

@ -10,7 +10,7 @@ import (
func TestInterfaceDefaultName(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
config := &Configuration{}
config := &NetworkConfiguration{}
if _ = newInterface(config); config.BridgeName != DefaultBridgeName {
t.Fatalf("Expected default interface name %q, got %q", DefaultBridgeName, config.BridgeName)
}
@ -19,7 +19,7 @@ func TestInterfaceDefaultName(t *testing.T) {
func TestAddressesEmptyInterface(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
inf := newInterface(&Configuration{})
inf := newInterface(&NetworkConfiguration{})
addrv4, addrsv6, err := inf.addresses()
if err != nil {
t.Fatalf("Failed to get addresses of default interface: %v", err)

View File

@ -5,7 +5,7 @@ import (
"github.com/docker/libnetwork/driverapi"
"github.com/docker/libnetwork/netutils"
"github.com/docker/libnetwork/pkg/options"
"github.com/docker/libnetwork/pkg/netlabel"
"github.com/vishvananda/netlink"
)
@ -15,18 +15,15 @@ func TestLinkCreate(t *testing.T) {
dr := d.(*driver)
mtu := 1490
config := &Configuration{
config := &NetworkConfiguration{
BridgeName: DefaultBridgeName,
Mtu: mtu,
EnableIPv6: true,
}
genericOption := make(map[string]interface{})
genericOption[options.GenericData] = config
if err := d.Config(genericOption); err != nil {
t.Fatalf("Failed to setup driver config: %v", err)
}
genericOption[netlabel.GenericData] = config
err := d.CreateNetwork("dummy", nil)
err := d.CreateNetwork("dummy", genericOption)
if err != nil {
t.Fatalf("Failed to create bridge: %v", err)
}
@ -102,16 +99,13 @@ func TestLinkCreateTwo(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
_, d := New()
config := &Configuration{
config := &NetworkConfiguration{
BridgeName: DefaultBridgeName,
EnableIPv6: true}
genericOption := make(map[string]interface{})
genericOption[options.GenericData] = config
if err := d.Config(genericOption); err != nil {
t.Fatalf("Failed to setup driver config: %v", err)
}
genericOption[netlabel.GenericData] = config
err := d.CreateNetwork("dummy", nil)
err := d.CreateNetwork("dummy", genericOption)
if err != nil {
t.Fatalf("Failed to create bridge: %v", err)
}
@ -135,16 +129,12 @@ func TestLinkCreateNoEnableIPv6(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
_, d := New()
config := &Configuration{
config := &NetworkConfiguration{
BridgeName: DefaultBridgeName}
genericOption := make(map[string]interface{})
genericOption[options.GenericData] = config
genericOption[netlabel.GenericData] = config
if err := d.Config(genericOption); err != nil {
t.Fatalf("Failed to setup driver config: %v", err)
}
err := d.CreateNetwork("dummy", nil)
err := d.CreateNetwork("dummy", genericOption)
if err != nil {
t.Fatalf("Failed to create bridge: %v", err)
}
@ -168,17 +158,13 @@ func TestLinkDelete(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
_, d := New()
config := &Configuration{
config := &NetworkConfiguration{
BridgeName: DefaultBridgeName,
EnableIPv6: true}
genericOption := make(map[string]interface{})
genericOption[options.GenericData] = config
genericOption[netlabel.GenericData] = config
if err := d.Config(genericOption); err != nil {
t.Fatalf("Failed to setup driver config: %v", err)
}
err := d.CreateNetwork("dummy", nil)
err := d.CreateNetwork("dummy", genericOption)
if err != nil {
t.Fatalf("Failed to create bridge: %v", err)
}

View File

@ -6,7 +6,7 @@ import (
"github.com/docker/docker/pkg/reexec"
"github.com/docker/libnetwork/netutils"
"github.com/docker/libnetwork/pkg/options"
"github.com/docker/libnetwork/pkg/netlabel"
)
func TestMain(m *testing.M) {
@ -25,20 +25,16 @@ func TestPortMappingConfig(t *testing.T) {
portBindings := []netutils.PortBinding{binding1, binding2}
epOptions := make(map[string]interface{})
epOptions[options.PortMap] = portBindings
epOptions[netlabel.PortMap] = portBindings
driverConfig := &Configuration{
netConfig := &NetworkConfiguration{
BridgeName: DefaultBridgeName,
EnableIPTables: true,
}
driverOptions := make(map[string]interface{})
driverOptions[options.GenericData] = driverConfig
netOptions := make(map[string]interface{})
netOptions[netlabel.GenericData] = netConfig
if err := d.Config(driverOptions); err != nil {
t.Fatalf("Failed to setup driver config: %v", err)
}
err := d.CreateNetwork("dummy", nil)
err := d.CreateNetwork("dummy", netOptions)
if err != nil {
t.Fatalf("Failed to create bridge: %v", err)
}

View File

@ -1,14 +1,14 @@
package bridge
type setupStep func(*Configuration, *bridgeInterface) error
type setupStep func(*NetworkConfiguration, *bridgeInterface) error
type bridgeSetup struct {
config *Configuration
config *NetworkConfiguration
bridge *bridgeInterface
steps []setupStep
}
func newBridgeSetup(c *Configuration, i *bridgeInterface) *bridgeSetup {
func newBridgeSetup(c *NetworkConfiguration, i *bridgeInterface) *bridgeSetup {
return &bridgeSetup{config: c, bridge: i}
}

View File

@ -8,7 +8,7 @@ import (
)
// SetupDevice create a new bridge interface/
func setupDevice(config *Configuration, i *bridgeInterface) error {
func setupDevice(config *NetworkConfiguration, i *bridgeInterface) error {
// We only attempt to create the bridge when the requested device name is
// the default one.
if config.BridgeName != DefaultBridgeName && !config.AllowNonDefaultBridge {
@ -35,7 +35,7 @@ func setupDevice(config *Configuration, i *bridgeInterface) error {
}
// SetupDeviceUp ups the given bridge interface.
func setupDeviceUp(config *Configuration, i *bridgeInterface) error {
func setupDeviceUp(config *NetworkConfiguration, i *bridgeInterface) error {
err := netlink.LinkSetUp(i.Link)
if err != nil {
return err

View File

@ -12,7 +12,7 @@ import (
func TestSetupNewBridge(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
config := &Configuration{BridgeName: DefaultBridgeName}
config := &NetworkConfiguration{BridgeName: DefaultBridgeName}
br := &bridgeInterface{}
if err := setupDevice(config, br); err != nil {
@ -32,7 +32,7 @@ func TestSetupNewBridge(t *testing.T) {
func TestSetupNewNonDefaultBridge(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
config := &Configuration{BridgeName: "test0"}
config := &NetworkConfiguration{BridgeName: "test0"}
br := &bridgeInterface{}
err := setupDevice(config, br)
@ -48,7 +48,7 @@ func TestSetupNewNonDefaultBridge(t *testing.T) {
func TestSetupDeviceUp(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
config := &Configuration{BridgeName: DefaultBridgeName}
config := &NetworkConfiguration{BridgeName: DefaultBridgeName}
br := &bridgeInterface{}
if err := setupDevice(config, br); err != nil {

View File

@ -2,7 +2,7 @@ package bridge
import log "github.com/Sirupsen/logrus"
func setupFixedCIDRv4(config *Configuration, i *bridgeInterface) error {
func setupFixedCIDRv4(config *NetworkConfiguration, i *bridgeInterface) error {
addrv4, _, err := i.addresses()
if err != nil {
return err

View File

@ -10,7 +10,7 @@ import (
func TestSetupFixedCIDRv4(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
config := &Configuration{
config := &NetworkConfiguration{
BridgeName: DefaultBridgeName,
AddressIPv4: &net.IPNet{IP: net.ParseIP("192.168.1.1"), Mask: net.CIDRMask(16, 32)},
FixedCIDR: &net.IPNet{IP: net.ParseIP("192.168.2.0"), Mask: net.CIDRMask(24, 32)}}
@ -37,7 +37,7 @@ func TestSetupFixedCIDRv4(t *testing.T) {
func TestSetupBadFixedCIDRv4(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
config := &Configuration{
config := &NetworkConfiguration{
BridgeName: DefaultBridgeName,
AddressIPv4: &net.IPNet{IP: net.ParseIP("192.168.1.1"), Mask: net.CIDRMask(24, 32)},
FixedCIDR: &net.IPNet{IP: net.ParseIP("192.168.2.0"), Mask: net.CIDRMask(24, 32)}}

View File

@ -2,7 +2,7 @@ package bridge
import log "github.com/Sirupsen/logrus"
func setupFixedCIDRv6(config *Configuration, i *bridgeInterface) error {
func setupFixedCIDRv6(config *NetworkConfiguration, i *bridgeInterface) error {
log.Debugf("Using IPv6 subnet: %v", config.FixedCIDRv6)
if err := ipAllocator.RegisterSubnet(config.FixedCIDRv6, config.FixedCIDRv6); err != nil {
return &FixedCIDRv6Error{net: config.FixedCIDRv6, err: err}

View File

@ -10,7 +10,7 @@ import (
func TestSetupFixedCIDRv6(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
config := &Configuration{}
config := &NetworkConfiguration{}
br := newInterface(config)
_, config.FixedCIDRv6, _ = net.ParseCIDR("2002:db8::/48")

View File

@ -10,10 +10,10 @@ const (
ipv4ForwardConfPerm = 0644
)
func setupIPForwarding(config *Configuration, i *bridgeInterface) error {
func setupIPForwarding(config *Configuration) error {
// Sanity Check
if config.EnableIPForwarding == false {
return (*ipForwardCfgError)(i)
return ErrIPFwdCfg
}
// Enable IPv4 forwarding

View File

@ -18,12 +18,10 @@ func TestSetupIPForwarding(t *testing.T) {
// Create test interface with ip forwarding setting enabled
config := &Configuration{
BridgeName: DefaultBridgeName,
EnableIPForwarding: true}
br := &bridgeInterface{}
// Set IP Forwarding
if err := setupIPForwarding(config, br); err != nil {
if err := setupIPForwarding(config); err != nil {
t.Fatalf("Failed to setup IP forwarding: %v", err)
}
@ -41,17 +39,15 @@ func TestUnexpectedSetupIPForwarding(t *testing.T) {
// Create test interface without ip forwarding setting enabled
config := &Configuration{
BridgeName: DefaultBridgeName,
EnableIPForwarding: false}
br := &bridgeInterface{}
// Attempt Set IP Forwarding
err := setupIPForwarding(config, br)
err := setupIPForwarding(config)
if err == nil {
t.Fatal("Setup IP forwarding was expected to fail")
}
if _, ok := err.(*ipForwardCfgError); !ok {
if err != ErrIPFwdCfg {
t.Fatalf("Setup IP forwarding failed with unexpected error: %v", err)
}
}

View File

@ -13,7 +13,7 @@ const (
DockerChain = "DOCKER"
)
func setupIPTables(config *Configuration, i *bridgeInterface) error {
func setupIPTables(config *NetworkConfiguration, i *bridgeInterface) error {
// Sanity check.
if config.EnableIPTables == false {
return ipTableCfgError(config.BridgeName)

View File

@ -58,14 +58,14 @@ func TestSetupIPTables(t *testing.T) {
assertBridgeConfig(config, br, t)
}
func getBasicTestConfig() *Configuration {
config := &Configuration{
func getBasicTestConfig() *NetworkConfiguration {
config := &NetworkConfiguration{
BridgeName: DefaultBridgeName,
AddressIPv4: &net.IPNet{IP: net.ParseIP(iptablesTestBridgeIP), Mask: net.CIDRMask(16, 32)}}
return config
}
func createTestBridge(config *Configuration, br *bridgeInterface, t *testing.T) {
func createTestBridge(config *NetworkConfiguration, br *bridgeInterface, t *testing.T) {
if err := setupDevice(config, br); err != nil {
t.Fatalf("Failed to create the testing Bridge: %s", err.Error())
}
@ -94,7 +94,7 @@ func assertIPTableChainProgramming(rule iptRule, descr string, t *testing.T) {
}
// Assert function which pushes chains based on bridge config parameters.
func assertBridgeConfig(config *Configuration, br *bridgeInterface, t *testing.T) {
func assertBridgeConfig(config *NetworkConfiguration, br *bridgeInterface, t *testing.T) {
// Attempt programming of ip tables.
err := setupIPTables(config, br)
if err != nil {

View File

@ -40,7 +40,7 @@ func init() {
}
}
func setupBridgeIPv4(config *Configuration, i *bridgeInterface) error {
func setupBridgeIPv4(config *NetworkConfiguration, i *bridgeInterface) error {
addrv4, _, err := i.addresses()
if err != nil {
return err
@ -77,12 +77,12 @@ func setupBridgeIPv4(config *Configuration, i *bridgeInterface) error {
return nil
}
func allocateBridgeIP(config *Configuration, i *bridgeInterface) error {
func allocateBridgeIP(config *NetworkConfiguration, i *bridgeInterface) error {
ipAllocator.RequestIP(i.bridgeIPv4, i.bridgeIPv4.IP)
return nil
}
func electBridgeIPv4(config *Configuration) (*net.IPNet, error) {
func electBridgeIPv4(config *NetworkConfiguration) (*net.IPNet, error) {
// Use the requested IPv4 CIDR when available.
if config.AddressIPv4 != nil {
return config.AddressIPv4, nil
@ -108,7 +108,7 @@ func electBridgeIPv4(config *Configuration) (*net.IPNet, error) {
return nil, IPv4AddrRangeError(config.BridgeName)
}
func setupGatewayIPv4(config *Configuration, i *bridgeInterface) error {
func setupGatewayIPv4(config *NetworkConfiguration, i *bridgeInterface) error {
if !i.bridgeIPv4.Contains(config.DefaultGatewayIPv4) {
return ErrInvalidGateway
}

View File

@ -8,8 +8,8 @@ import (
"github.com/vishvananda/netlink"
)
func setupTestInterface(t *testing.T) (*Configuration, *bridgeInterface) {
config := &Configuration{
func setupTestInterface(t *testing.T) (*NetworkConfiguration, *bridgeInterface) {
config := &NetworkConfiguration{
BridgeName: DefaultBridgeName}
br := &bridgeInterface{}
@ -84,7 +84,7 @@ func TestSetupGatewayIPv4(t *testing.T) {
nw.IP = ip
gw := net.ParseIP("192.168.0.254")
config := &Configuration{
config := &NetworkConfiguration{
BridgeName: DefaultBridgeName,
DefaultGatewayIPv4: gw}

View File

@ -22,7 +22,7 @@ func init() {
}
}
func setupBridgeIPv6(config *Configuration, i *bridgeInterface) error {
func setupBridgeIPv6(config *NetworkConfiguration, i *bridgeInterface) error {
// Enable IPv6 on the bridge
procFile := "/proc/sys/net/ipv6/conf/" + config.BridgeName + "/disable_ipv6"
if err := ioutil.WriteFile(procFile, []byte{'0', '\n'}, 0644); err != nil {
@ -47,7 +47,7 @@ func setupBridgeIPv6(config *Configuration, i *bridgeInterface) error {
return nil
}
func setupGatewayIPv6(config *Configuration, i *bridgeInterface) error {
func setupGatewayIPv6(config *NetworkConfiguration, i *bridgeInterface) error {
if config.FixedCIDRv6 == nil {
return ErrInvalidContainerSubnet
}

View File

@ -53,7 +53,7 @@ func TestSetupGatewayIPv6(t *testing.T) {
_, nw, _ := net.ParseCIDR("2001:db8:ea9:9abc:ffff::/80")
gw := net.ParseIP("2001:db8:ea9:9abc:ffff::254")
config := &Configuration{
config := &NetworkConfiguration{
BridgeName: DefaultBridgeName,
FixedCIDRv6: nw,
DefaultGatewayIPv6: gw}

View File

@ -2,7 +2,7 @@ package bridge
import "github.com/vishvananda/netlink"
func setupVerifyAndReconcile(config *Configuration, i *bridgeInterface) error {
func setupVerifyAndReconcile(config *NetworkConfiguration, i *bridgeInterface) error {
// Fetch a single IPv4 and a slice of IPv6 addresses from the bridge.
addrv4, addrsv6, err := i.addresses()
if err != nil {

View File

@ -27,7 +27,7 @@ func TestSetupVerify(t *testing.T) {
addrv4 := net.IPv4(192, 168, 1, 1)
inf := setupVerifyTest(t)
config := &Configuration{}
config := &NetworkConfiguration{}
config.AddressIPv4 = &net.IPNet{IP: addrv4, Mask: addrv4.DefaultMask()}
if err := netlink.AddrAdd(inf.Link, &netlink.Addr{IPNet: config.AddressIPv4}); err != nil {
@ -44,7 +44,7 @@ func TestSetupVerifyBad(t *testing.T) {
addrv4 := net.IPv4(192, 168, 1, 1)
inf := setupVerifyTest(t)
config := &Configuration{}
config := &NetworkConfiguration{}
config.AddressIPv4 = &net.IPNet{IP: addrv4, Mask: addrv4.DefaultMask()}
ipnet := &net.IPNet{IP: net.IPv4(192, 168, 1, 2), Mask: addrv4.DefaultMask()}
@ -62,7 +62,7 @@ func TestSetupVerifyMissing(t *testing.T) {
addrv4 := net.IPv4(192, 168, 1, 1)
inf := setupVerifyTest(t)
config := &Configuration{}
config := &NetworkConfiguration{}
config.AddressIPv4 = &net.IPNet{IP: addrv4, Mask: addrv4.DefaultMask()}
if err := setupVerifyAndReconcile(config, inf); err == nil {
@ -75,7 +75,7 @@ func TestSetupVerifyIPv6(t *testing.T) {
addrv4 := net.IPv4(192, 168, 1, 1)
inf := setupVerifyTest(t)
config := &Configuration{}
config := &NetworkConfiguration{}
config.AddressIPv4 = &net.IPNet{IP: addrv4, Mask: addrv4.DefaultMask()}
config.EnableIPv6 = true
@ -96,7 +96,7 @@ func TestSetupVerifyIPv6Missing(t *testing.T) {
addrv4 := net.IPv4(192, 168, 1, 1)
inf := setupVerifyTest(t)
config := &Configuration{}
config := &NetworkConfiguration{}
config.AddressIPv4 = &net.IPNet{IP: addrv4, Mask: addrv4.DefaultMask()}
config.EnableIPv6 = true

View File

@ -10,7 +10,7 @@ import (
"github.com/docker/docker/pkg/resolvconf"
"github.com/docker/libnetwork/driverapi"
"github.com/docker/libnetwork/netutils"
"github.com/docker/libnetwork/pkg/options"
"github.com/docker/libnetwork/pkg/netlabel"
"github.com/docker/libnetwork/sandbox"
"github.com/docker/libnetwork/types"
)
@ -409,7 +409,7 @@ func (ep *endpoint) setupDNS() error {
}
// replace any localhost/127.* but always discard IPv6 entries for now.
resolvConf, _ = resolvconf.FilterResolvDns(resolvConf, false)
resolvConf, _ = resolvconf.FilterResolvDns(resolvConf, ep.network.enableIPv6)
return ioutil.WriteFile(ep.container.config.resolvConfPath, resolvConf, 0644)
}
@ -504,7 +504,7 @@ func CreateOptionExposedPorts(exposedPorts []netutils.TransportPort) EndpointOpt
copy(eps, exposedPorts)
// Store endpoint label and in generic because driver needs it
ep.exposedPorts = eps
ep.generic[options.ExposedPorts] = eps
ep.generic[netlabel.ExposedPorts] = eps
}
}
@ -515,7 +515,7 @@ func CreateOptionPortMapping(portBindings []netutils.PortBinding) EndpointOption
// Store a copy of the bindings as generic data to pass to the driver
pbs := make([]netutils.PortBinding, len(portBindings))
copy(pbs, portBindings)
ep.generic[options.PortMap] = pbs
ep.generic[netlabel.PortMap] = pbs
}
}

View File

@ -1,6 +1,8 @@
package libnetwork_test
import (
"bytes"
"io/ioutil"
"net"
"os"
"testing"
@ -9,6 +11,7 @@ import (
"github.com/docker/docker/pkg/reexec"
"github.com/docker/libnetwork"
"github.com/docker/libnetwork/netutils"
"github.com/docker/libnetwork/pkg/netlabel"
"github.com/docker/libnetwork/pkg/options"
)
@ -24,17 +27,18 @@ func TestMain(m *testing.M) {
os.Exit(m.Run())
}
func createTestNetwork(networkType, networkName string, option options.Generic) (libnetwork.Network, error) {
func createTestNetwork(networkType, networkName string, option options.Generic, netOption options.Generic) (libnetwork.Network, error) {
controller := libnetwork.New()
genericOption := make(map[string]interface{})
genericOption[options.GenericData] = option
genericOption[netlabel.GenericData] = option
err := controller.ConfigureNetworkDriver(networkType, genericOption)
if err != nil {
return nil, err
}
network, err := controller.NewNetwork(networkType, networkName)
network, err := controller.NewNetwork(networkType, networkName,
libnetwork.NetworkOptionGeneric(netOption))
if err != nil {
return nil, err
}
@ -44,7 +48,7 @@ func createTestNetwork(networkType, networkName string, option options.Generic)
func getEmptyGenericOption() map[string]interface{} {
genericOption := make(map[string]interface{})
genericOption[options.GenericData] = options.Generic{}
genericOption[netlabel.GenericData] = options.Generic{}
return genericOption
}
@ -57,7 +61,8 @@ func getPortMapping() []netutils.PortBinding {
}
func TestNull(t *testing.T) {
network, err := createTestNetwork("null", "testnetwork", options.Generic{})
network, err := createTestNetwork("null", "testnetwork", options.Generic{},
options.Generic{})
if err != nil {
t.Fatal(err)
}
@ -90,7 +95,7 @@ func TestNull(t *testing.T) {
}
func TestHost(t *testing.T) {
network, err := createTestNetwork("host", "testnetwork", options.Generic{})
network, err := createTestNetwork("host", "testnetwork", options.Generic{}, options.Generic{})
if err != nil {
t.Fatal(err)
}
@ -145,6 +150,10 @@ func TestBridge(t *testing.T) {
log.Debug("Adding a bridge")
option := options.Generic{
"EnableIPForwarding": true,
}
netOption := options.Generic{
"BridgeName": bridgeName,
"AddressIPv4": subnet,
"FixedCIDR": cidr,
@ -153,10 +162,9 @@ func TestBridge(t *testing.T) {
"EnableIPTables": true,
"EnableIPMasquerade": true,
"EnableICC": true,
"EnableIPForwarding": true,
"AllowNonDefaultBridge": true}
network, err := createTestNetwork(bridgeNetType, "testnetwork", option)
network, err := createTestNetwork(bridgeNetType, "testnetwork", option, netOption)
if err != nil {
t.Fatal(err)
}
@ -170,7 +178,7 @@ func TestBridge(t *testing.T) {
if err != nil {
t.Fatal(err)
}
pmd, ok := epInfo[options.PortMap]
pmd, ok := epInfo[netlabel.PortMap]
if !ok {
t.Fatalf("Could not find expected info in endpoint data")
}
@ -194,7 +202,7 @@ func TestBridge(t *testing.T) {
func TestUnknownDriver(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
_, err := createTestNetwork("unknowndriver", "testnetwork", options.Generic{})
_, err := createTestNetwork("unknowndriver", "testnetwork", options.Generic{}, options.Generic{})
if err == nil {
t.Fatal("Expected to fail. But instead succeeded")
}
@ -237,14 +245,15 @@ func TestDuplicateNetwork(t *testing.T) {
controller := libnetwork.New()
genericOption := make(map[string]interface{})
genericOption[options.GenericData] = options.Generic{}
genericOption[netlabel.GenericData] = options.Generic{}
err := controller.ConfigureNetworkDriver(bridgeNetType, genericOption)
if err != nil {
t.Fatal(err)
}
_, err = controller.NewNetwork(bridgeNetType, "testnetwork", nil)
_, err = controller.NewNetwork(bridgeNetType, "testnetwork",
libnetwork.NetworkOptionGeneric(genericOption))
if err != nil {
t.Fatal(err)
}
@ -263,7 +272,7 @@ func TestNetworkName(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
networkName := "testnetwork"
n, err := createTestNetwork(bridgeNetType, networkName, options.Generic{})
n, err := createTestNetwork(bridgeNetType, networkName, options.Generic{}, options.Generic{})
if err != nil {
t.Fatal(err)
}
@ -275,7 +284,7 @@ func TestNetworkName(t *testing.T) {
func TestNetworkType(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{})
n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{}, options.Generic{})
if err != nil {
t.Fatal(err)
}
@ -288,7 +297,7 @@ func TestNetworkType(t *testing.T) {
func TestNetworkID(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{})
n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{}, options.Generic{})
if err != nil {
t.Fatal(err)
}
@ -304,7 +313,7 @@ func TestDeleteNetworkWithActiveEndpoints(t *testing.T) {
"BridgeName": bridgeName,
"AllowNonDefaultBridge": true}
network, err := createTestNetwork(bridgeNetType, "testnetwork", option)
network, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{}, option)
if err != nil {
t.Fatal(err)
}
@ -339,7 +348,7 @@ func TestUnknownNetwork(t *testing.T) {
"BridgeName": bridgeName,
"AllowNonDefaultBridge": true}
network, err := createTestNetwork(bridgeNetType, "testnetwork", option)
network, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{}, option)
if err != nil {
t.Fatal(err)
}
@ -372,7 +381,7 @@ func TestUnknownEndpoint(t *testing.T) {
"AddressIPv4": subnet,
"AllowNonDefaultBridge": true}
network, err := createTestNetwork(bridgeNetType, "testnetwork", option)
network, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{}, option)
if err != nil {
t.Fatal(err)
}
@ -579,7 +588,7 @@ const containerID = "valid_container"
func TestEndpointJoin(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{})
n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{}, options.Generic{})
if err != nil {
t.Fatal(err)
}
@ -606,7 +615,7 @@ func TestEndpointJoin(t *testing.T) {
func TestEndpointJoinInvalidContainerId(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{})
n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{}, options.Generic{})
if err != nil {
t.Fatal(err)
}
@ -629,7 +638,7 @@ func TestEndpointJoinInvalidContainerId(t *testing.T) {
func TestEndpointMultipleJoins(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{})
n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{}, options.Generic{})
if err != nil {
t.Fatal(err)
}
@ -666,7 +675,7 @@ func TestEndpointMultipleJoins(t *testing.T) {
func TestEndpointInvalidLeave(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{})
n, err := createTestNetwork(bridgeNetType, "testnetwork", options.Generic{}, options.Generic{})
if err != nil {
t.Fatal(err)
}
@ -721,7 +730,7 @@ func TestEndpointInvalidLeave(t *testing.T) {
func TestEndpointUpdateParent(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
n, err := createTestNetwork("bridge", "testnetwork", options.Generic{})
n, err := createTestNetwork("bridge", "testnetwork", options.Generic{}, options.Generic{})
if err != nil {
t.Fatal(err)
}
@ -765,3 +774,106 @@ func TestEndpointUpdateParent(t *testing.T) {
t.Fatal(err)
}
}
func TestEnableIPv6(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
tmpResolvConf := []byte("search pommesfrites.fr\nnameserver 12.34.56.78\nnameserver 2001:4860:4860::8888")
//take a copy of resolv.conf for restoring after test completes
resolvConfSystem, err := ioutil.ReadFile("/etc/resolv.conf")
if err != nil {
t.Fatal(err)
}
//cleanup
defer func() {
if err := ioutil.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
t.Fatal(err)
}
}()
netOption := options.Generic{
netlabel.EnableIPv6: true,
}
n, err := createTestNetwork("bridge", "testnetwork", options.Generic{}, netOption)
if err != nil {
t.Fatal(err)
}
ep1, err := n.CreateEndpoint("ep1", nil)
if err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile("/etc/resolv.conf", tmpResolvConf, 0644); err != nil {
t.Fatal(err)
}
resolvConfPath := "/tmp/libnetwork_test/resolv.conf"
_, err = ep1.Join(containerID,
libnetwork.JoinOptionResolvConfPath(resolvConfPath))
content, err := ioutil.ReadFile(resolvConfPath)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(content, tmpResolvConf) {
t.Fatalf("Expected %s, Got %s", string(tmpResolvConf), string(content))
}
if err != nil {
t.Fatal(err)
}
}
func TestNoEnableIPv6(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
tmpResolvConf := []byte("search pommesfrites.fr\nnameserver 12.34.56.78\nnameserver 2001:4860:4860::8888")
expectedResolvConf := []byte("search pommesfrites.fr\nnameserver 12.34.56.78\n")
//take a copy of resolv.conf for restoring after test completes
resolvConfSystem, err := ioutil.ReadFile("/etc/resolv.conf")
if err != nil {
t.Fatal(err)
}
//cleanup
defer func() {
if err := ioutil.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
t.Fatal(err)
}
}()
n, err := createTestNetwork("bridge", "testnetwork", options.Generic{}, options.Generic{})
if err != nil {
t.Fatal(err)
}
ep1, err := n.CreateEndpoint("ep1", nil)
if err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile("/etc/resolv.conf", tmpResolvConf, 0644); err != nil {
t.Fatal(err)
}
resolvConfPath := "/tmp/libnetwork_test/resolv.conf"
_, err = ep1.Join(containerID,
libnetwork.JoinOptionResolvConfPath(resolvConfPath))
content, err := ioutil.ReadFile(resolvConfPath)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(content, expectedResolvConf) {
t.Fatalf("Expected %s, Got %s", string(expectedResolvConf), string(content))
}
if err != nil {
t.Fatal(err)
}
}

View File

@ -5,6 +5,7 @@ import (
"github.com/docker/docker/pkg/stringid"
"github.com/docker/libnetwork/driverapi"
"github.com/docker/libnetwork/pkg/netlabel"
"github.com/docker/libnetwork/pkg/options"
"github.com/docker/libnetwork/types"
)
@ -52,6 +53,7 @@ type network struct {
networkType string
id types.UUID
driver driverapi.Driver
enableIPv6 bool
endpoints endpointTable
generic options.Generic
sync.Mutex
@ -83,6 +85,9 @@ type NetworkOption func(n *network)
func NetworkOptionGeneric(generic map[string]interface{}) NetworkOption {
return func(n *network) {
n.generic = generic
if _, ok := generic[netlabel.EnableIPv6]; ok {
n.enableIPv6 = generic[netlabel.EnableIPv6].(bool)
}
}
}

View File

@ -0,0 +1,18 @@
package netlabel
const (
// GenericData constant that helps to identify an option as a Generic constant
GenericData = "io.docker.network.generic"
// PortMap constant represents Port Mapping
PortMap = "io.docker.network.endpoint.portmap"
// MacAddress constant represents Mac Address config of a Container
MacAddress = "io.docker.network.endpoint.macaddress"
// ExposedPorts constant represents exposedports of a Container
ExposedPorts = "io.docker.network.endpoint.exposedports"
//EnableIPv6 constant represents enabling IPV6 at network level
EnableIPv6 = "io.docker.network.enable_ipv6"
)

View File

@ -7,17 +7,6 @@ import (
"reflect"
)
const (
// GenericData constant that helps to identify an option as a Generic constant
GenericData = "io.docker.network.generic"
// PortMap constant represents Port Mapping
PortMap = "io.docker.network.endpoint.portmap"
// MacAddress constant represents Mac Address config of a Container
MacAddress = "io.docker.network.endpoint.macaddress"
// ExposedPorts constant represents exposedports of a Container
ExposedPorts = "io.docker.network.endpoint.exposedports"
)
// NoSuchFieldError is the error returned when the generic parameters hold a
// value for a field absent from the destination structure.
type NoSuchFieldError struct {