libnetwork/netutils: drop ElectInterfaceAddresses
The function references global shared, mutable state and is no longer needed. Deleting it brings us one step closer to getting rid of that pesky shared state. Signed-off-by: Cory Snider <csnider@mirantis.com>
This commit is contained in:
parent
cc19eba579
commit
48ad9e19e4
3 changed files with 13 additions and 106 deletions
|
@ -13,6 +13,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/docker/docker/libnetwork/driverapi"
|
"github.com/docker/docker/libnetwork/driverapi"
|
||||||
|
"github.com/docker/docker/libnetwork/ipamutils"
|
||||||
"github.com/docker/docker/libnetwork/iptables"
|
"github.com/docker/docker/libnetwork/iptables"
|
||||||
"github.com/docker/docker/libnetwork/netlabel"
|
"github.com/docker/docker/libnetwork/netlabel"
|
||||||
"github.com/docker/docker/libnetwork/netutils"
|
"github.com/docker/docker/libnetwork/netutils"
|
||||||
|
@ -169,13 +170,13 @@ func compareBindings(a, b []types.PortBinding) bool {
|
||||||
|
|
||||||
func getIPv4Data(t *testing.T, iface string) []driverapi.IPAMData {
|
func getIPv4Data(t *testing.T, iface string) []driverapi.IPAMData {
|
||||||
ipd := driverapi.IPAMData{AddressSpace: "full"}
|
ipd := driverapi.IPAMData{AddressSpace: "full"}
|
||||||
nws, _, err := netutils.ElectInterfaceAddresses(iface)
|
nw, err := netutils.FindAvailableNetwork(ipamutils.GetLocalScopeDefaultNetworks())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
ipd.Pool = nws[0]
|
ipd.Pool = nw
|
||||||
// Set network gateway to X.X.X.1
|
// Set network gateway to X.X.X.1
|
||||||
ipd.Gateway = types.GetIPNetCopy(nws[0])
|
ipd.Gateway = types.GetIPNetCopy(nw)
|
||||||
ipd.Gateway.IP[len(ipd.Gateway.IP)-1] = 1
|
ipd.Gateway.IP[len(ipd.Gateway.IP)-1] = 1
|
||||||
return []driverapi.IPAMData{ipd}
|
return []driverapi.IPAMData{ipd}
|
||||||
}
|
}
|
||||||
|
@ -1049,7 +1050,15 @@ func TestCreateWithExistingBridge(t *testing.T) {
|
||||||
genericOption := make(map[string]interface{})
|
genericOption := make(map[string]interface{})
|
||||||
genericOption[netlabel.GenericData] = netconfig
|
genericOption[netlabel.GenericData] = netconfig
|
||||||
|
|
||||||
if err := d.CreateNetwork(brName, genericOption, nil, getIPv4Data(t, brName), nil); err != nil {
|
ipv4Data := []driverapi.IPAMData{{
|
||||||
|
AddressSpace: "full",
|
||||||
|
Pool: types.GetIPNetCopy(addr.IPNet),
|
||||||
|
Gateway: types.GetIPNetCopy(addr.IPNet),
|
||||||
|
}}
|
||||||
|
// Set network gateway to X.X.X.1
|
||||||
|
ipv4Data[0].Gateway.IP[len(ipv4Data[0].Gateway.IP)-1] = 1
|
||||||
|
|
||||||
|
if err := d.CreateNetwork(brName, genericOption, nil, ipv4Data, nil); err != nil {
|
||||||
t.Fatalf("Failed to create bridge network: %v", err)
|
t.Fatalf("Failed to create bridge network: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,6 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/docker/docker/libnetwork/ipamutils"
|
|
||||||
"github.com/docker/docker/libnetwork/ns"
|
"github.com/docker/docker/libnetwork/ns"
|
||||||
"github.com/docker/docker/libnetwork/resolvconf"
|
"github.com/docker/docker/libnetwork/resolvconf"
|
||||||
"github.com/docker/docker/libnetwork/types"
|
"github.com/docker/docker/libnetwork/types"
|
||||||
|
@ -61,46 +60,6 @@ func GenerateIfaceName(nlh *netlink.Handle, prefix string, len int) (string, err
|
||||||
return "", types.InternalErrorf("could not generate interface name")
|
return "", types.InternalErrorf("could not generate interface name")
|
||||||
}
|
}
|
||||||
|
|
||||||
// ElectInterfaceAddresses looks for an interface on the OS with the
|
|
||||||
// specified name and returns returns all its IPv4 and IPv6 addresses in CIDR notation.
|
|
||||||
// If a failure in retrieving the addresses or no IPv4 address is found, an error is returned.
|
|
||||||
// If the interface does not exist, it chooses from a predefined
|
|
||||||
// list the first IPv4 address which does not conflict with other
|
|
||||||
// interfaces on the system.
|
|
||||||
func ElectInterfaceAddresses(name string) ([]*net.IPNet, []*net.IPNet, error) {
|
|
||||||
var v4Nets, v6Nets []*net.IPNet
|
|
||||||
|
|
||||||
link, _ := ns.NlHandle().LinkByName(name)
|
|
||||||
if link != nil {
|
|
||||||
v4addr, err := ns.NlHandle().AddrList(link, netlink.FAMILY_V4)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
v6addr, err := ns.NlHandle().AddrList(link, netlink.FAMILY_V6)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
for _, nlAddr := range v4addr {
|
|
||||||
v4Nets = append(v4Nets, nlAddr.IPNet)
|
|
||||||
}
|
|
||||||
for _, nlAddr := range v6addr {
|
|
||||||
v6Nets = append(v6Nets, nlAddr.IPNet)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if link == nil || len(v4Nets) == 0 {
|
|
||||||
// Choose from predefined local scope networks
|
|
||||||
v4Net, err := FindAvailableNetwork(ipamutils.PredefinedLocalScopeDefaultNetworks)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, errors.Wrapf(err, "PredefinedLocalScopeDefaultNetworks List: %+v",
|
|
||||||
ipamutils.PredefinedLocalScopeDefaultNetworks)
|
|
||||||
}
|
|
||||||
v4Nets = append(v4Nets, v4Net)
|
|
||||||
}
|
|
||||||
|
|
||||||
return v4Nets, v6Nets, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindAvailableNetwork returns a network from the passed list which does not
|
// FindAvailableNetwork returns a network from the passed list which does not
|
||||||
// overlap with existing interfaces in the system
|
// overlap with existing interfaces in the system
|
||||||
func FindAvailableNetwork(list []*net.IPNet) (*net.IPNet, error) {
|
func FindAvailableNetwork(list []*net.IPNet) (*net.IPNet, error) {
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"sort"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -300,66 +299,6 @@ func TestNetworkRequest(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestElectInterfaceAddressMultipleAddresses(t *testing.T) {
|
|
||||||
defer testutils.SetupTestOSContext(t)()
|
|
||||||
|
|
||||||
nws := []string{"172.101.202.254/16", "172.102.202.254/16"}
|
|
||||||
createInterface(t, "test", nws...)
|
|
||||||
|
|
||||||
ipv4NwList, ipv6NwList, err := ElectInterfaceAddresses("test")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(ipv4NwList) == 0 {
|
|
||||||
t.Fatal("unexpected empty ipv4 network addresses")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(ipv6NwList) == 0 {
|
|
||||||
t.Fatal("unexpected empty ipv6 network addresses")
|
|
||||||
}
|
|
||||||
|
|
||||||
nwList := []string{}
|
|
||||||
for _, ipv4Nw := range ipv4NwList {
|
|
||||||
nwList = append(nwList, ipv4Nw.String())
|
|
||||||
}
|
|
||||||
sort.Strings(nws)
|
|
||||||
sort.Strings(nwList)
|
|
||||||
|
|
||||||
if len(nws) != len(nwList) {
|
|
||||||
t.Fatalf("expected %v. got %v", nws, nwList)
|
|
||||||
}
|
|
||||||
for i, nw := range nws {
|
|
||||||
if nw != nwList[i] {
|
|
||||||
t.Fatalf("expected %v. got %v", nw, nwList[i])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestElectInterfaceAddress(t *testing.T) {
|
|
||||||
defer testutils.SetupTestOSContext(t)()
|
|
||||||
|
|
||||||
nws := "172.101.202.254/16"
|
|
||||||
createInterface(t, "test", nws)
|
|
||||||
|
|
||||||
ipv4Nw, ipv6Nw, err := ElectInterfaceAddresses("test")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(ipv4Nw) == 0 {
|
|
||||||
t.Fatal("unexpected empty ipv4 network addresses")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(ipv6Nw) == 0 {
|
|
||||||
t.Fatal("unexpected empty ipv6 network addresses")
|
|
||||||
}
|
|
||||||
|
|
||||||
if nws != ipv4Nw[0].String() {
|
|
||||||
t.Fatalf("expected %s. got %s", nws, ipv4Nw[0])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func createInterface(t *testing.T, name string, nws ...string) {
|
func createInterface(t *testing.T, name string, nws ...string) {
|
||||||
// Add interface
|
// Add interface
|
||||||
link := &netlink.Bridge{
|
link := &netlink.Bridge{
|
||||||
|
|
Loading…
Add table
Reference in a new issue