Skip to content

Commit

Permalink
bugfix: fix the wrong bridge gateway
Browse files Browse the repository at this point in the history
Before default nat network is 172.17.0.1/24, commit: 7e7f607 has
changed it to 192.168.5.1/24. If "p0" is exist, pouchd start will check
gateway is error.

Signed-off-by: Rudy Zhang <[email protected]>
  • Loading branch information
rudyfly committed Jun 8, 2018
1 parent 4782518 commit b5e4299
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions network/mode/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/docker/libnetwork/drivers/bridge"
"github.com/docker/libnetwork/netlabel"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
)

Expand All @@ -41,30 +42,36 @@ func New(ctx context.Context, config network.BridgeConfig, manager mgr.NetworkMg
return err
}

var (
bridgeIPv4Address string
)
Addrs, err := netlink.AddrList(br, netlink.FAMILY_V4)
if err != nil {
return errors.Wrap(err, "failed to get bridge addr")
}
for _, addr := range Addrs {
cidr := addr.String()
if strings.Contains(cidr, ":") {
continue
}

parts := strings.Split(cidr, " ")
if len(parts) != 2 {
continue
}

bridgeIPv4Address = parts[0]
break
}

// get subnet
subnet := DefaultSubnet
if config.IP != "" {
subnet = config.IP
} else {
addrs, err := netlink.AddrList(br, netlink.FAMILY_V4)
if err != nil {
return errors.Wrap(err, "failed to get bridge addr")
}
for _, addr := range addrs {
cidr := addr.String()
if strings.Contains(cidr, ":") {
continue
}

parts := strings.Split(cidr, " ")
if len(parts) != 2 {
continue
}

subnet = parts[0]
break
}
} else if bridgeIPv4Address != "" {
subnet = bridgeIPv4Address
}
logrus.Debugf("initialize bridge network, subnet: %s", subnet)

// get ip range
ipRange := DefaultIPRange
Expand All @@ -73,12 +80,14 @@ func New(ctx context.Context, config network.BridgeConfig, manager mgr.NetworkMg
} else {
ipRange = subnet
}
logrus.Debugf("initialize bridge network, bridge ip range in subnet: %s", ipRange)

// get gateway
gateway := DefaultGateway
if config.GatewayIPv4 != "" {
gateway = config.GatewayIPv4
} else {
// get the default route set as gateway.
routes, err := netlink.RouteList(br, netlink.FAMILY_V4)
if err != nil {
return errors.Wrap(err, "failed to get route list")
Expand All @@ -90,7 +99,13 @@ func New(ctx context.Context, config network.BridgeConfig, manager mgr.NetworkMg
break
}
}

// nat mode bridge have no default route, so let the bridge ip as gateway.
if bridgeIPv4Address != "" {
gateway = strings.Split(bridgeIPv4Address, "/")[0]
}
}
logrus.Debugf("initialize bridge network, gateway: %s", gateway)

ipamV4Conf := types.IPAMConfig{
AuxAddress: make(map[string]string),
Expand Down

0 comments on commit b5e4299

Please sign in to comment.