mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Fixed a bug in bridge driver when docker0 has no IP
address it doesn't select and configure a proper IP address. Signed-off-by: Jana Radhakrishnan <mrjana@docker.com>
This commit is contained in:
parent
def2a1192f
commit
68cae04fe9
3 changed files with 54 additions and 26 deletions
|
@ -226,9 +226,11 @@ func (d *driver) CreateNetwork(id types.UUID, option map[string]interface{}) err
|
|||
bridgeAlreadyExists := bridgeIface.exists()
|
||||
if !bridgeAlreadyExists {
|
||||
bridgeSetup.queueStep(setupDevice)
|
||||
bridgeSetup.queueStep(setupBridgeIPv4)
|
||||
}
|
||||
|
||||
// Even if a bridge exists try to setup IPv4.
|
||||
bridgeSetup.queueStep(setupBridgeIPv4)
|
||||
|
||||
// Conditionnally queue setup steps depending on configuration values.
|
||||
for _, step := range []struct {
|
||||
Condition bool
|
||||
|
@ -269,6 +271,8 @@ func (d *driver) CreateNetwork(id types.UUID, option map[string]interface{}) err
|
|||
}
|
||||
}
|
||||
|
||||
// Block bridge IP from being allocated.
|
||||
bridgeSetup.queueStep(allocateBridgeIP)
|
||||
// Apply the prepared list of steps, and abort at the first error.
|
||||
bridgeSetup.queueStep(setupDeviceUp)
|
||||
if err = bridgeSetup.apply(); err != nil {
|
||||
|
|
|
@ -14,6 +14,31 @@ import (
|
|||
"github.com/vishvananda/netlink"
|
||||
)
|
||||
|
||||
func TestCreateFullOptions(t *testing.T) {
|
||||
defer netutils.SetupTestNetNS(t)()
|
||||
_, d := New()
|
||||
|
||||
config := &Configuration{
|
||||
BridgeName: DefaultBridgeName,
|
||||
EnableIPv6: true,
|
||||
FixedCIDR: bridgeNetworks[0],
|
||||
EnableIPTables: true,
|
||||
EnableIPForwarding: true,
|
||||
}
|
||||
_, config.FixedCIDRv6, _ = net.ParseCIDR("2001:db8::/48")
|
||||
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)
|
||||
}
|
||||
|
||||
err := d.CreateNetwork("dummy", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create bridge: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
defer netutils.SetupTestNetNS(t)()
|
||||
_, d := New()
|
||||
|
@ -48,31 +73,6 @@ func TestCreateFail(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestCreateFullOptions(t *testing.T) {
|
||||
defer netutils.SetupTestNetNS(t)()
|
||||
_, d := New()
|
||||
|
||||
config := &Configuration{
|
||||
BridgeName: DefaultBridgeName,
|
||||
EnableIPv6: true,
|
||||
FixedCIDR: bridgeNetworks[0],
|
||||
EnableIPTables: true,
|
||||
EnableIPForwarding: true,
|
||||
}
|
||||
_, config.FixedCIDRv6, _ = net.ParseCIDR("2001:db8::/48")
|
||||
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)
|
||||
}
|
||||
|
||||
err := d.CreateNetwork("dummy", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create bridge: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueryEndpointInfo(t *testing.T) {
|
||||
defer netutils.SetupTestNetNS(t)()
|
||||
|
||||
|
|
|
@ -41,6 +41,25 @@ func init() {
|
|||
}
|
||||
|
||||
func setupBridgeIPv4(config *Configuration, i *bridgeInterface) error {
|
||||
addrv4, _, err := i.addresses()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Check if we have an IP address already on the bridge.
|
||||
if addrv4.IPNet != nil {
|
||||
// Make sure to store bridge network and default gateway before getting out.
|
||||
i.bridgeIPv4 = addrv4.IPNet
|
||||
i.gatewayIPv4 = addrv4.IPNet.IP
|
||||
return nil
|
||||
}
|
||||
|
||||
// Do not try to configure IPv4 on a non-default bridge unless you are
|
||||
// specifically asked to do so.
|
||||
if config.BridgeName != DefaultBridgeName && !config.AllowNonDefaultBridge {
|
||||
return NonDefaultBridgeExistError(config.BridgeName)
|
||||
}
|
||||
|
||||
bridgeIPv4, err := electBridgeIPv4(config)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -58,6 +77,11 @@ func setupBridgeIPv4(config *Configuration, i *bridgeInterface) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func allocateBridgeIP(config *Configuration, i *bridgeInterface) error {
|
||||
ipAllocator.RequestIP(i.bridgeIPv4, i.bridgeIPv4.IP)
|
||||
return nil
|
||||
}
|
||||
|
||||
func electBridgeIPv4(config *Configuration) (*net.IPNet, error) {
|
||||
// Use the requested IPv4 CIDR when available.
|
||||
if config.AddressIPv4 != nil {
|
||||
|
|
Loading…
Reference in a new issue