mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
e797f80ad4
- Added api enhancement to pass driver specific config - Refactored simple bridge driver code for driver specific config - Added an undocumented option to add non-default bridges without manual pre-provisioning to help libnetwork testing - Reenabled libnetwork test to do api testing - Updated README.md Signed-off-by: Jana Radhakrishnan <mrjana@docker.com>
61 lines
1.6 KiB
Go
61 lines
1.6 KiB
Go
package bridge
|
|
|
|
import (
|
|
"net"
|
|
|
|
"github.com/vishvananda/netlink"
|
|
)
|
|
|
|
const (
|
|
// DefaultBridgeName is the default name for the bridge interface managed
|
|
// by the driver when unspecified by the caller.
|
|
DefaultBridgeName = "docker0"
|
|
)
|
|
|
|
// Interface models the bridge network device.
|
|
type bridgeInterface struct {
|
|
Link netlink.Link
|
|
bridgeIPv4 *net.IPNet
|
|
bridgeIPv6 *net.IPNet
|
|
}
|
|
|
|
// NewInterface creates a new bridge interface structure. It attempts to find
|
|
// an already existing device identified by the Configuration BridgeName field,
|
|
// or the default bridge name when unspecified), but doesn't attempt to create
|
|
// on when missing
|
|
func newInterface(config *Configuration) *bridgeInterface {
|
|
i := &bridgeInterface{}
|
|
|
|
// Initialize the bridge name to the default if unspecified.
|
|
if config.BridgeName == "" {
|
|
config.BridgeName = DefaultBridgeName
|
|
}
|
|
|
|
// Attempt to find an existing bridge named with the specified name.
|
|
i.Link, _ = netlink.LinkByName(config.BridgeName)
|
|
return i
|
|
}
|
|
|
|
// Exists indicates if the existing bridge interface exists on the system.
|
|
func (i *bridgeInterface) exists() bool {
|
|
return i.Link != nil
|
|
}
|
|
|
|
// Addresses returns a single IPv4 address and all IPv6 addresses for the
|
|
// bridge interface.
|
|
func (i *bridgeInterface) addresses() (netlink.Addr, []netlink.Addr, error) {
|
|
v4addr, err := netlink.AddrList(i.Link, netlink.FAMILY_V4)
|
|
if err != nil {
|
|
return netlink.Addr{}, nil, err
|
|
}
|
|
|
|
v6addr, err := netlink.AddrList(i.Link, netlink.FAMILY_V6)
|
|
if err != nil {
|
|
return netlink.Addr{}, nil, err
|
|
}
|
|
|
|
if len(v4addr) == 0 {
|
|
return netlink.Addr{}, v6addr, nil
|
|
}
|
|
return v4addr[0], v6addr, nil
|
|
}
|