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

Support network options in rest api

- Also unexporting configuration structures in bridge
- Changes in dnet/network.go to set bridge name = network name

Signed-off-by: Alessandro Boch <aboch@docker.com>
This commit is contained in:
Alessandro Boch 2015-05-22 10:56:36 -07:00
parent 2ba2884981
commit 5d9c59e523
28 changed files with 325 additions and 156 deletions

View file

@ -16,7 +16,7 @@ var (
successResponse = responseStatus{Status: "Success", StatusCode: http.StatusOK}
createdResponse = responseStatus{Status: "Created", StatusCode: http.StatusCreated}
mismatchResponse = responseStatus{Status: "Body/URI parameter mismatch", StatusCode: http.StatusBadRequest}
badQueryresponse = responseStatus{Status: "Unsupported query", StatusCode: http.StatusBadRequest}
badQueryResponse = responseStatus{Status: "Unsupported query", StatusCode: http.StatusBadRequest}
)
const (
@ -170,9 +170,19 @@ func buildEndpointResource(ep libnetwork.Endpoint) *endpointResource {
return r
}
/**************
Options Parser
***************/
/****************
Options Parsers
*****************/
func (nc *networkCreate) parseOptions() []libnetwork.NetworkOption {
var setFctList []libnetwork.NetworkOption
if nc.Options != nil {
setFctList = append(setFctList, libnetwork.NetworkOptionGeneric(nc.Options))
}
return setFctList
}
func (ej *endpointJoin) parseOptions() []libnetwork.EndpointOption {
var setFctList []libnetwork.EndpointOption
@ -224,7 +234,7 @@ func procCreateNetwork(c libnetwork.NetworkController, vars map[string]string, b
return "", &responseStatus{Status: "Invalid body: " + err.Error(), StatusCode: http.StatusBadRequest}
}
nw, err := c.NewNetwork(create.NetworkType, create.Name, nil)
nw, err := c.NewNetwork(create.NetworkType, create.Name, create.parseOptions()...)
if err != nil {
return "", convertNetworkError(err)
}
@ -248,7 +258,7 @@ func procGetNetworks(c libnetwork.NetworkController, vars map[string]string, bod
name, queryByName := vars[urlNwName]
shortID, queryByPid := vars[urlNwPID]
if queryByName && queryByPid {
return nil, &badQueryresponse
return nil, &badQueryResponse
}
if queryByName {
@ -323,7 +333,7 @@ func procGetEndpoints(c libnetwork.NetworkController, vars map[string]string, bo
name, queryByName := vars[urlEpName]
shortID, queryByPid := vars[urlEpPID]
if queryByName && queryByPid {
return nil, &badQueryresponse
return nil, &badQueryResponse
}
nwT, nwBy := detectNetworkTarget(vars)

View file

@ -78,6 +78,25 @@ func i2nL(i interface{}) []*networkResource {
return s
}
func createTestNetwork(t *testing.T, network string) (libnetwork.NetworkController, libnetwork.Network) {
c, err := libnetwork.New()
if err != nil {
t.Fatal(err)
}
err = c.ConfigureNetworkDriver(bridgeNetType, nil)
if err != nil {
t.Fatal(err)
}
nw, err := c.NewNetwork(bridgeNetType, network, nil)
if err != nil {
t.Fatal(err)
}
return c, nw
}
func TestMain(m *testing.M) {
if reexec.Init() {
return
@ -184,8 +203,15 @@ func TestCreateDeleteNetwork(t *testing.T) {
t.Fatalf("Expected StatusBadRequest status code, got: %v", errRsp)
}
ops := make(map[string]interface{})
ops[netlabel.GenericData] = options.Generic{}
ops := options.Generic{
netlabel.EnableIPv6: true,
netlabel.GenericData: map[string]string{
"BridgeName": "abc",
"AllowNonDefaultBridge": "true",
"FixedCIDRv6": "fe80::1/64",
"AddressIP": "172.28.30.254/24",
},
}
nc := networkCreate{Name: "network_1", NetworkType: bridgeNetType, Options: ops}
goodBody, err := json.Marshal(nc)
if err != nil {
@ -498,19 +524,7 @@ func TestDetectGetNetworksInvalidQueryComposition(t *testing.T) {
func TestDetectGetEndpointsInvalidQueryComposition(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
c, err := libnetwork.New()
if err != nil {
t.Fatal(err)
}
err = c.ConfigureNetworkDriver(bridgeNetType, nil)
if err != nil {
t.Fatal(err)
}
_, err = c.NewNetwork(bridgeNetType, "network", nil)
if err != nil {
t.Fatal(err)
}
c, _ := createTestNetwork(t, "network")
vars := map[string]string{urlNwName: "network", urlEpName: "x", urlEpPID: "y"}
_, errRsp := procGetEndpoints(c, vars, nil)
@ -522,19 +536,7 @@ func TestDetectGetEndpointsInvalidQueryComposition(t *testing.T) {
func TestFindNetworkUtil(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
c, err := libnetwork.New()
if err != nil {
t.Fatal(err)
}
err = c.ConfigureNetworkDriver(bridgeNetType, nil)
if err != nil {
t.Fatal(err)
}
nw, err := c.NewNetwork(bridgeNetType, "network", nil)
if err != nil {
t.Fatal(err)
}
c, nw := createTestNetwork(t, "network")
nid := nw.ID()
defer checkPanic(t)
@ -880,19 +882,7 @@ func TestJoinLeave(t *testing.T) {
func TestFindEndpointUtil(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
c, err := libnetwork.New()
if err != nil {
t.Fatal(err)
}
err = c.ConfigureNetworkDriver(bridgeNetType, nil)
if err != nil {
t.Fatal(err)
}
nw, err := c.NewNetwork(bridgeNetType, "second", nil)
if err != nil {
t.Fatal(err)
}
c, nw := createTestNetwork(t, "network")
nid := nw.ID()
ep, err := nw.CreateEndpoint("secondEp", nil)
@ -1228,8 +1218,22 @@ func TestEndToEnd(t *testing.T) {
}
handleRequest := NewHTTPHandler(c)
ops := options.Generic{
netlabel.EnableIPv6: true,
netlabel.GenericData: map[string]string{
"BridgeName": "cdef",
"FixedCIDRv6": "fe80:2000::1/64",
"EnableIPv6": "true",
"Mtu": "1460",
"EnableIPTables": "true",
"AddressIP": "172.28.30.254/16",
"EnableUserlandProxy": "true",
"AllowNonDefaultBridge": "true",
},
}
// Create network
nc := networkCreate{Name: "network-fiftyfive", NetworkType: bridgeNetType}
nc := networkCreate{Name: "network-fiftyfive", NetworkType: bridgeNetType, Options: ops}
body, err := json.Marshal(nc)
if err != nil {
t.Fatal(err)

View file

@ -9,6 +9,7 @@ import (
flag "github.com/docker/docker/pkg/mflag"
"github.com/docker/docker/pkg/stringid"
"github.com/docker/libnetwork/netlabel"
)
const (
@ -54,7 +55,17 @@ func (cli *NetworkCli) CmdNetworkCreate(chain string, args ...string) error {
*flDriver = nullNetType
}
nc := networkCreate{Name: cmd.Arg(0), NetworkType: *flDriver}
// Construct network create request body
ops := make(map[string]interface{})
nc := networkCreate{Name: cmd.Arg(0), NetworkType: *flDriver, Options: ops}
// Set driver specific options
if *flDriver == "bridge" {
ops[netlabel.GenericData] = map[string]string{
"BridgeName": cmd.Arg(0),
"AllowNonDefaultBridge": "true",
}
}
obj, _, err := readBody(cli.call("POST", "/networks", nc, nil))
if err != nil {

View file

@ -3,6 +3,7 @@ package bridge
import (
"errors"
"net"
"strconv"
"strings"
"sync"
@ -31,13 +32,13 @@ var (
portMapper *portmapper.PortMapper
)
// Configuration info for the "bridge" driver.
type Configuration struct {
// configuration info for the "bridge" driver.
type configuration struct {
EnableIPForwarding bool
}
// NetworkConfiguration for network specific configuration
type NetworkConfiguration struct {
// networkConfiguration for network specific configuration
type networkConfiguration struct {
BridgeName string
AddressIPv4 *net.IPNet
FixedCIDR *net.IPNet
@ -54,15 +55,15 @@ type NetworkConfiguration struct {
EnableUserlandProxy bool
}
// EndpointConfiguration represents the user specified configuration for the sandbox endpoint
type EndpointConfiguration struct {
// endpointConfiguration represents the user specified configuration for the sandbox endpoint
type endpointConfiguration struct {
MacAddress net.HardwareAddr
PortBindings []types.PortBinding
ExposedPorts []types.TransportPort
}
// ContainerConfiguration represents the user specified configuration for a container
type ContainerConfiguration struct {
// containerConfiguration represents the user specified configuration for a container
type containerConfiguration struct {
ParentEndpoints []string
ChildEndpoints []string
}
@ -71,21 +72,21 @@ type bridgeEndpoint struct {
id types.UUID
intf *sandbox.Interface
macAddress net.HardwareAddr
config *EndpointConfiguration // User specified parameters
containerConfig *ContainerConfiguration
config *endpointConfiguration // User specified parameters
containerConfig *containerConfiguration
portMapping []types.PortBinding // Operation port bindings
}
type bridgeNetwork struct {
id types.UUID
bridge *bridgeInterface // The bridge's L3 interface
config *NetworkConfiguration
config *networkConfiguration
endpoints map[types.UUID]*bridgeEndpoint // key: endpoint id
sync.Mutex
}
type driver struct {
config *Configuration
config *configuration
network *bridgeNetwork
sync.Mutex
}
@ -107,7 +108,7 @@ func Init(dc driverapi.DriverCallback) error {
// Validate performs a static validation on the network configuration parameters.
// Whatever can be assessed a priori before attempting any programming.
func (c *NetworkConfiguration) Validate() error {
func (c *networkConfiguration) Validate() error {
if c.Mtu < 0 {
return ErrInvalidMtu(c.Mtu)
}
@ -145,6 +146,147 @@ func (c *NetworkConfiguration) Validate() error {
return nil
}
// FromMap retrieve the configuration data from the map form.
func (c *networkConfiguration) FromMap(data map[string]interface{}) error {
if i, ok := data["BridgeName"]; ok && i != nil {
if c.BridgeName, ok = i.(string); !ok {
return types.BadRequestErrorf("invalid type for BridgeName value")
}
}
var err error
if i, ok := data["Mtu"]; ok && i != nil {
if s, ok := i.(string); ok {
if c.Mtu, err = strconv.Atoi(s); err != nil {
return types.BadRequestErrorf("failed to parse Mtu value: %s", err.Error())
}
} else {
return types.BadRequestErrorf("invalid type for Mtu value")
}
}
if i, ok := data["EnableIPv6"]; ok && i != nil {
if s, ok := i.(string); ok {
if c.EnableIPv6, err = strconv.ParseBool(s); err != nil {
return types.BadRequestErrorf("failed to parse EnableIPv6 value: %s", err.Error())
}
} else {
return types.BadRequestErrorf("invalid type for EnableIPv6 value")
}
}
if i, ok := data["EnableIPTables"]; ok && i != nil {
if s, ok := i.(string); ok {
if c.EnableIPTables, err = strconv.ParseBool(s); err != nil {
return types.BadRequestErrorf("failed to parse EnableIPTables value: %s", err.Error())
}
} else {
return types.BadRequestErrorf("invalid type for EnableIPTables value")
}
}
if i, ok := data["EnableIPMasquerade"]; ok && i != nil {
if s, ok := i.(string); ok {
if c.EnableIPMasquerade, err = strconv.ParseBool(s); err != nil {
return types.BadRequestErrorf("failed to parse EnableIPMasquerade value: %s", err.Error())
}
} else {
return types.BadRequestErrorf("invalid type for EnableIPMasquerade value")
}
}
if i, ok := data["EnableICC"]; ok && i != nil {
if s, ok := i.(string); ok {
if c.EnableICC, err = strconv.ParseBool(s); err != nil {
return types.BadRequestErrorf("failed to parse EnableICC value: %s", err.Error())
}
} else {
return types.BadRequestErrorf("invalid type for EnableICC value")
}
}
if i, ok := data["AllowNonDefaultBridge"]; ok && i != nil {
if s, ok := i.(string); ok {
if c.AllowNonDefaultBridge, err = strconv.ParseBool(s); err != nil {
return types.BadRequestErrorf("failed to parse AllowNonDefaultBridge value: %s", err.Error())
}
} else {
return types.BadRequestErrorf("invalid type for AllowNonDefaultBridge value")
}
}
if i, ok := data["AddressIPv4"]; ok && i != nil {
if s, ok := i.(string); ok {
if ip, nw, e := net.ParseCIDR(s); e == nil {
nw.IP = ip
c.AddressIPv4 = nw
} else {
return types.BadRequestErrorf("failed to parse AddressIPv4 value")
}
} else {
return types.BadRequestErrorf("invalid type for AddressIPv4 value")
}
}
if i, ok := data["FixedCIDR"]; ok && i != nil {
if s, ok := i.(string); ok {
if ip, nw, e := net.ParseCIDR(s); e == nil {
nw.IP = ip
c.FixedCIDR = nw
} else {
return types.BadRequestErrorf("failed to parse FixedCIDR value")
}
} else {
return types.BadRequestErrorf("invalid type for FixedCIDR value")
}
}
if i, ok := data["FixedCIDRv6"]; ok && i != nil {
if s, ok := i.(string); ok {
if ip, nw, e := net.ParseCIDR(s); e == nil {
nw.IP = ip
c.FixedCIDRv6 = nw
} else {
return types.BadRequestErrorf("failed to parse FixedCIDRv6 value")
}
} else {
return types.BadRequestErrorf("invalid type for FixedCIDRv6 value")
}
}
if i, ok := data["DefaultGatewayIPv4"]; ok && i != nil {
if s, ok := i.(string); ok {
if c.DefaultGatewayIPv4 = net.ParseIP(s); c.DefaultGatewayIPv4 == nil {
return types.BadRequestErrorf("failed to parse DefaultGatewayIPv4 value")
}
} else {
return types.BadRequestErrorf("invalid type for DefaultGatewayIPv4 value")
}
}
if i, ok := data["DefaultGatewayIPv6"]; ok && i != nil {
if s, ok := i.(string); ok {
if c.DefaultGatewayIPv6 = net.ParseIP(s); c.DefaultGatewayIPv6 == nil {
return types.BadRequestErrorf("failed to parse DefaultGatewayIPv6 value")
}
} else {
return types.BadRequestErrorf("invalid type for DefaultGatewayIPv6 value")
}
}
if i, ok := data["DefaultBindingIP"]; ok && i != nil {
if s, ok := i.(string); ok {
if c.DefaultBindingIP = net.ParseIP(s); c.DefaultBindingIP == nil {
return types.BadRequestErrorf("failed to parse DefaultBindingIP value")
}
} else {
return types.BadRequestErrorf("invalid type for DefaultBindingIP value")
}
}
return nil
}
func (n *bridgeNetwork) getEndpoint(eid types.UUID) (*bridgeEndpoint, error) {
n.Lock()
defer n.Unlock()
@ -161,7 +303,7 @@ func (n *bridgeNetwork) getEndpoint(eid types.UUID) (*bridgeEndpoint, error) {
}
func (d *driver) Config(option map[string]interface{}) error {
var config *Configuration
var config *configuration
d.Lock()
defer d.Unlock()
@ -174,12 +316,12 @@ func (d *driver) Config(option map[string]interface{}) error {
if ok && genericData != nil {
switch opt := genericData.(type) {
case options.Generic:
opaqueConfig, err := options.GenerateFromModel(opt, &Configuration{})
opaqueConfig, err := options.GenerateFromModel(opt, &configuration{})
if err != nil {
return err
}
config = opaqueConfig.(*Configuration)
case *Configuration:
config = opaqueConfig.(*configuration)
case *configuration:
config = opt
default:
return &ErrInvalidDriverConfig{}
@ -187,7 +329,7 @@ func (d *driver) Config(option map[string]interface{}) error {
d.config = config
} else {
config = &Configuration{}
config = &configuration{}
}
if config.EnableIPForwarding {
@ -205,29 +347,29 @@ func (d *driver) getNetwork(id types.UUID) (*bridgeNetwork, error) {
return d.network, nil
}
func parseNetworkOptions(option options.Generic) (*NetworkConfiguration, error) {
var config *NetworkConfiguration
func parseNetworkOptions(option options.Generic) (*networkConfiguration, error) {
config := &networkConfiguration{}
if genData, ok := option[netlabel.GenericData]; ok && genData != nil {
switch opt := genData.(type) {
genericData, ok := option[netlabel.GenericData]
if ok && genericData != nil {
switch opt := genericData.(type) {
case map[string]interface{}:
if err := config.FromMap(opt); err != nil {
return nil, err
}
case options.Generic:
opaqueConfig, err := options.GenerateFromModel(opt, &NetworkConfiguration{})
opaqueConfig, err := options.GenerateFromModel(opt, &networkConfiguration{})
if err != nil {
return nil, err
}
config = opaqueConfig.(*NetworkConfiguration)
case *NetworkConfiguration:
config = opaqueConfig.(*networkConfiguration)
case *networkConfiguration:
config = opt
default:
return nil, &ErrInvalidNetworkConfig{}
return nil, types.BadRequestErrorf("do not recognize network configuration format: %T", opt)
}
if err := config.Validate(); err != nil {
return nil, err
}
} else {
config = &NetworkConfiguration{}
}
if err := config.Validate(); err != nil {
return nil, err
}
if _, ok := option[netlabel.EnableIPv6]; ok {
@ -754,7 +896,7 @@ func (d *driver) Leave(nid, eid types.UUID) error {
func (d *driver) link(network *bridgeNetwork, endpoint *bridgeEndpoint, options map[string]interface{}, enable bool) error {
var (
cc *ContainerConfiguration
cc *containerConfiguration
err error
)
@ -845,12 +987,12 @@ func (d *driver) Type() string {
return networkType
}
func parseEndpointOptions(epOptions map[string]interface{}) (*EndpointConfiguration, error) {
func parseEndpointOptions(epOptions map[string]interface{}) (*endpointConfiguration, error) {
if epOptions == nil {
return nil, nil
}
ec := &EndpointConfiguration{}
ec := &endpointConfiguration{}
if opt, ok := epOptions[netlabel.MacAddress]; ok {
if mac, ok := opt.(net.HardwareAddr); ok {
@ -879,7 +1021,7 @@ func parseEndpointOptions(epOptions map[string]interface{}) (*EndpointConfigurat
return ec, nil
}
func parseContainerOptions(cOptions map[string]interface{}) (*ContainerConfiguration, error) {
func parseContainerOptions(cOptions map[string]interface{}) (*containerConfiguration, error) {
if cOptions == nil {
return nil, nil
}
@ -889,19 +1031,19 @@ func parseContainerOptions(cOptions map[string]interface{}) (*ContainerConfigura
}
switch opt := genericData.(type) {
case options.Generic:
opaqueConfig, err := options.GenerateFromModel(opt, &ContainerConfiguration{})
opaqueConfig, err := options.GenerateFromModel(opt, &containerConfiguration{})
if err != nil {
return nil, err
}
return opaqueConfig.(*ContainerConfiguration), nil
case *ContainerConfiguration:
return opaqueConfig.(*containerConfiguration), nil
case *containerConfiguration:
return opt, nil
default:
return nil, nil
}
}
func electMacAddress(epConfig *EndpointConfiguration) net.HardwareAddr {
func electMacAddress(epConfig *endpointConfiguration) net.HardwareAddr {
if epConfig != nil && epConfig.MacAddress != nil {
return epConfig.MacAddress
}

View file

@ -19,11 +19,11 @@ func TestCreateFullOptions(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
d := newDriver()
config := &Configuration{
config := &configuration{
EnableIPForwarding: true,
}
netConfig := &NetworkConfiguration{
netConfig := &networkConfiguration{
BridgeName: DefaultBridgeName,
EnableIPv6: true,
FixedCIDR: bridgeNetworks[0],
@ -50,7 +50,7 @@ func TestCreate(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
d := newDriver()
config := &NetworkConfiguration{BridgeName: DefaultBridgeName}
config := &networkConfiguration{BridgeName: DefaultBridgeName}
genericOption := make(map[string]interface{})
genericOption[netlabel.GenericData] = config
@ -63,7 +63,7 @@ func TestCreateFail(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
d := newDriver()
config := &NetworkConfiguration{BridgeName: "dummy0"}
config := &networkConfiguration{BridgeName: "dummy0"}
genericOption := make(map[string]interface{})
genericOption[netlabel.GenericData] = config
@ -170,7 +170,7 @@ func testQueryEndpointInfo(t *testing.T, ulPxyEnabled bool) {
d := newDriver()
dd, _ := d.(*driver)
config := &NetworkConfiguration{
config := &networkConfiguration{
BridgeName: DefaultBridgeName,
EnableIPTables: true,
EnableICC: false,
@ -227,7 +227,7 @@ func TestCreateLinkWithOptions(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
d := newDriver()
config := &NetworkConfiguration{BridgeName: DefaultBridgeName}
config := &networkConfiguration{BridgeName: DefaultBridgeName}
netOptions := make(map[string]interface{})
netOptions[netlabel.GenericData] = config
@ -283,7 +283,7 @@ func TestLinkContainers(t *testing.T) {
d := newDriver()
config := &NetworkConfiguration{
config := &networkConfiguration{
BridgeName: DefaultBridgeName,
EnableIPTables: true,
EnableICC: false,
@ -323,7 +323,7 @@ func TestLinkContainers(t *testing.T) {
}
ce := []string{"ep1"}
cConfig := &ContainerConfiguration{ChildEndpoints: ce}
cConfig := &containerConfiguration{ChildEndpoints: ce}
genericOption = make(map[string]interface{})
genericOption[netlabel.GenericData] = cConfig
@ -371,7 +371,7 @@ func TestLinkContainers(t *testing.T) {
// Error condition test with an invalid endpoint-id "ep4"
ce = []string{"ep1", "ep4"}
cConfig = &ContainerConfiguration{ChildEndpoints: ce}
cConfig = &containerConfiguration{ChildEndpoints: ce}
genericOption = make(map[string]interface{})
genericOption[netlabel.GenericData] = cConfig
@ -400,7 +400,7 @@ func TestLinkContainers(t *testing.T) {
func TestValidateConfig(t *testing.T) {
// Test mtu
c := NetworkConfiguration{Mtu: -2}
c := networkConfiguration{Mtu: -2}
err := c.Validate()
if err == nil {
t.Fatalf("Failed to detect invalid MTU number")
@ -417,7 +417,7 @@ func TestValidateConfig(t *testing.T) {
// Test FixedCIDR
_, containerSubnet, _ := net.ParseCIDR("172.27.0.0/16")
c = NetworkConfiguration{
c = networkConfiguration{
AddressIPv4: network,
FixedCIDR: containerSubnet,
}
@ -463,7 +463,7 @@ func TestValidateConfig(t *testing.T) {
// Test v6 gw
_, containerSubnet, _ = net.ParseCIDR("2001:1234:ae:b004::/64")
c = NetworkConfiguration{
c = networkConfiguration{
EnableIPv6: true,
FixedCIDRv6: containerSubnet,
DefaultGatewayIPv6: net.ParseIP("2001:1234:ac:b004::bad:a55"),
@ -495,7 +495,7 @@ func TestSetDefaultGw(t *testing.T) {
gw4[3] = 254
gw6 := net.ParseIP("2001:db8:ea9:9abc:b0c4::254")
config := &NetworkConfiguration{
config := &networkConfiguration{
BridgeName: DefaultBridgeName,
EnableIPv6: true,
FixedCIDRv6: subnetv6,

View file

@ -22,10 +22,10 @@ type bridgeInterface struct {
}
// newInterface creates a new bridge interface structure. It attempts to find
// an already existing device identified by the Configuration BridgeName field,
// 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 *NetworkConfiguration) *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 := &NetworkConfiguration{}
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(&NetworkConfiguration{})
inf := newInterface(&networkConfiguration{})
addrv4, addrsv6, err := inf.addresses()
if err != nil {
t.Fatalf("Failed to get addresses of default interface: %v", err)

View file

@ -15,7 +15,7 @@ func TestLinkCreate(t *testing.T) {
dr := d.(*driver)
mtu := 1490
config := &NetworkConfiguration{
config := &networkConfiguration{
BridgeName: DefaultBridgeName,
Mtu: mtu,
EnableIPv6: true,
@ -105,7 +105,7 @@ func TestLinkCreateTwo(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
d := newDriver()
config := &NetworkConfiguration{
config := &networkConfiguration{
BridgeName: DefaultBridgeName,
EnableIPv6: true}
genericOption := make(map[string]interface{})
@ -137,7 +137,7 @@ func TestLinkCreateNoEnableIPv6(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
d := newDriver()
config := &NetworkConfiguration{
config := &networkConfiguration{
BridgeName: DefaultBridgeName}
genericOption := make(map[string]interface{})
genericOption[netlabel.GenericData] = config
@ -167,7 +167,7 @@ func TestLinkDelete(t *testing.T) {
defer netutils.SetupTestNetNS(t)()
d := newDriver()
config := &NetworkConfiguration{
config := &networkConfiguration{
BridgeName: DefaultBridgeName,
EnableIPv6: true}
genericOption := make(map[string]interface{})

View file

@ -15,7 +15,7 @@ var (
defaultBindingIP = net.IPv4(0, 0, 0, 0)
)
func allocatePorts(epConfig *EndpointConfiguration, intf *sandbox.Interface, reqDefBindIP net.IP, ulPxyEnabled bool) ([]types.PortBinding, error) {
func allocatePorts(epConfig *endpointConfiguration, intf *sandbox.Interface, reqDefBindIP net.IP, ulPxyEnabled bool) ([]types.PortBinding, error) {
if epConfig == nil || epConfig.PortBindings == nil {
return nil, nil
}

View file

@ -28,7 +28,7 @@ func TestPortMappingConfig(t *testing.T) {
epOptions := make(map[string]interface{})
epOptions[netlabel.PortMap] = portBindings
netConfig := &NetworkConfiguration{
netConfig := &networkConfiguration{
BridgeName: DefaultBridgeName,
EnableIPTables: true,
}

View file

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

View file

@ -4,11 +4,12 @@ import (
log "github.com/Sirupsen/logrus"
"github.com/docker/docker/pkg/parsers/kernel"
"github.com/docker/libnetwork/netutils"
"github.com/docker/libnetwork/types"
"github.com/vishvananda/netlink"
)
// SetupDevice create a new bridge interface/
func setupDevice(config *NetworkConfiguration, 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 {
@ -31,11 +32,14 @@ func setupDevice(config *NetworkConfiguration, i *bridgeInterface) error {
}
// Call out to netlink to create the device.
return netlink.LinkAdd(i.Link)
if err = netlink.LinkAdd(i.Link); err != nil {
return types.InternalErrorf("Failed to program bridge link: %s", err.Error())
}
return nil
}
// SetupDeviceUp ups the given bridge interface.
func setupDeviceUp(config *NetworkConfiguration, 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 := &NetworkConfiguration{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 := &NetworkConfiguration{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 := &NetworkConfiguration{BridgeName: DefaultBridgeName}
config := &networkConfiguration{BridgeName: DefaultBridgeName}
br := &bridgeInterface{}
if err := setupDevice(config, br); err != nil {

View file

@ -4,7 +4,7 @@ import (
log "github.com/Sirupsen/logrus"
)
func setupFixedCIDRv4(config *NetworkConfiguration, 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 := &NetworkConfiguration{
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 := &NetworkConfiguration{
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

@ -4,7 +4,7 @@ import (
log "github.com/Sirupsen/logrus"
)
func setupFixedCIDRv6(config *NetworkConfiguration, 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 := &NetworkConfiguration{}
config := &networkConfiguration{}
br := newInterface(config)
_, config.FixedCIDRv6, _ = net.ParseCIDR("2002:db8::/48")

View file

@ -10,7 +10,7 @@ const (
ipv4ForwardConfPerm = 0644
)
func setupIPForwarding(config *Configuration) error {
func setupIPForwarding(config *configuration) error {
// Sanity Check
if config.EnableIPForwarding == false {
return &ErrIPFwdCfg{}

View file

@ -17,7 +17,7 @@ func TestSetupIPForwarding(t *testing.T) {
}
// Create test interface with ip forwarding setting enabled
config := &Configuration{
config := &configuration{
EnableIPForwarding: true}
// Set IP Forwarding
@ -38,7 +38,7 @@ func TestUnexpectedSetupIPForwarding(t *testing.T) {
defer reconcileIPForwardingSetting(t, procSetting)
// Create test interface without ip forwarding setting enabled
config := &Configuration{
config := &configuration{
EnableIPForwarding: false}
// Attempt Set IP Forwarding

View file

@ -13,7 +13,7 @@ const (
DockerChain = "DOCKER"
)
func setupIPTables(config *NetworkConfiguration, 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() *NetworkConfiguration {
config := &NetworkConfiguration{
func getBasicTestConfig() *networkConfiguration {
config := &networkConfiguration{
BridgeName: DefaultBridgeName,
AddressIPv4: &net.IPNet{IP: net.ParseIP(iptablesTestBridgeIP), Mask: net.CIDRMask(16, 32)}}
return config
}
func createTestBridge(config *NetworkConfiguration, 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 *NetworkConfiguration, 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

@ -4,7 +4,6 @@ import (
"fmt"
"io/ioutil"
"net"
"path/filepath"
log "github.com/Sirupsen/logrus"
@ -44,7 +43,7 @@ func init() {
}
}
func setupBridgeIPv4(config *NetworkConfiguration, i *bridgeInterface) error {
func setupBridgeIPv4(config *networkConfiguration, i *bridgeInterface) error {
addrv4, _, err := i.addresses()
if err != nil {
return err
@ -81,12 +80,12 @@ func setupBridgeIPv4(config *NetworkConfiguration, i *bridgeInterface) error {
return nil
}
func allocateBridgeIP(config *NetworkConfiguration, i *bridgeInterface) error {
func allocateBridgeIP(config *networkConfiguration, i *bridgeInterface) error {
ipAllocator.RequestIP(i.bridgeIPv4, i.bridgeIPv4.IP)
return nil
}
func electBridgeIPv4(config *NetworkConfiguration) (*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
@ -112,7 +111,7 @@ func electBridgeIPv4(config *NetworkConfiguration) (*net.IPNet, error) {
return nil, IPv4AddrRangeError(config.BridgeName)
}
func setupGatewayIPv4(config *NetworkConfiguration, i *bridgeInterface) error {
func setupGatewayIPv4(config *networkConfiguration, i *bridgeInterface) error {
if !i.bridgeIPv4.Contains(config.DefaultGatewayIPv4) {
return &ErrInvalidGateway{}
}
@ -126,7 +125,7 @@ func setupGatewayIPv4(config *NetworkConfiguration, i *bridgeInterface) error {
return nil
}
func setupLoopbackAdressesRouting(config *NetworkConfiguration, i *bridgeInterface) error {
func setupLoopbackAdressesRouting(config *networkConfiguration, i *bridgeInterface) error {
// Enable loopback adresses routing
sysPath := filepath.Join("/proc/sys/net/ipv4/conf", config.BridgeName, "route_localnet")
if err := ioutil.WriteFile(sysPath, []byte{'1', '\n'}, 0644); err != nil {

View file

@ -8,8 +8,8 @@ import (
"github.com/vishvananda/netlink"
)
func setupTestInterface(t *testing.T) (*NetworkConfiguration, *bridgeInterface) {
config := &NetworkConfiguration{
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 := &NetworkConfiguration{
config := &networkConfiguration{
BridgeName: DefaultBridgeName,
DefaultGatewayIPv4: gw}

View file

@ -22,7 +22,7 @@ func init() {
}
}
func setupBridgeIPv6(config *NetworkConfiguration, 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 {
@ -48,7 +48,7 @@ func setupBridgeIPv6(config *NetworkConfiguration, i *bridgeInterface) error {
return nil
}
func setupGatewayIPv6(config *NetworkConfiguration, 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 := &NetworkConfiguration{
config := &networkConfiguration{
BridgeName: DefaultBridgeName,
FixedCIDRv6: nw,
DefaultGatewayIPv6: gw}

View file

@ -4,7 +4,7 @@ import (
"github.com/vishvananda/netlink"
)
func setupVerifyAndReconcile(config *NetworkConfiguration, 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 := &NetworkConfiguration{}
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 := &NetworkConfiguration{}
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 := &NetworkConfiguration{}
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 := &NetworkConfiguration{}
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 := &NetworkConfiguration{}
config := &networkConfiguration{}
config.AddressIPv4 = &net.IPNet{IP: addrv4, Mask: addrv4.DefaultMask()}
config.EnableIPv6 = true

View file

@ -314,8 +314,7 @@ func TestDuplicateNetwork(t *testing.T) {
t.Fatal(err)
}
_, err = controller.NewNetwork(bridgeNetType, "testnetwork",
libnetwork.NetworkOptionGeneric(genericOption))
_, err = controller.NewNetwork(bridgeNetType, "testnetwork", nil)
if err != nil {
t.Fatal(err)
}