mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Default IPAM to use ipamutils pkg
Signed-off-by: Alessandro Boch <aboch@docker.com>
This commit is contained in:
parent
fd00a53019
commit
94b6e5e18b
4 changed files with 19 additions and 42 deletions
|
@ -10,7 +10,7 @@ import (
|
||||||
"github.com/docker/libnetwork/bitseq"
|
"github.com/docker/libnetwork/bitseq"
|
||||||
"github.com/docker/libnetwork/datastore"
|
"github.com/docker/libnetwork/datastore"
|
||||||
"github.com/docker/libnetwork/ipamapi"
|
"github.com/docker/libnetwork/ipamapi"
|
||||||
"github.com/docker/libnetwork/netutils"
|
"github.com/docker/libnetwork/ipamutils"
|
||||||
"github.com/docker/libnetwork/types"
|
"github.com/docker/libnetwork/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -61,8 +61,8 @@ func NewAllocator(lcDs, glDs datastore.DataStore) (*Allocator, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
a.predefined = map[string][]*net.IPNet{
|
a.predefined = map[string][]*net.IPNet{
|
||||||
localAddressSpace: initLocalPredefinedPools(),
|
localAddressSpace: ipamutils.PredefinedBroadNetworks,
|
||||||
globalAddressSpace: initGlobalPredefinedPools(),
|
globalAddressSpace: ipamutils.PredefinedGranularNetworks,
|
||||||
}
|
}
|
||||||
|
|
||||||
a.addrSpace2Configs = map[string]*PoolsConfig{
|
a.addrSpace2Configs = map[string]*PoolsConfig{
|
||||||
|
@ -307,7 +307,8 @@ func (a *Allocator) getPredefinedPool(as string, ipV6 bool) (*net.IPNet, error)
|
||||||
|
|
||||||
if !cfg.contains(as, nw) {
|
if !cfg.contains(as, nw) {
|
||||||
if as == localAddressSpace {
|
if as == localAddressSpace {
|
||||||
if err := netutils.CheckRouteOverlaps(nw); err == nil {
|
// Check if nw overlap with system routes, name servers
|
||||||
|
if _, err := ipamutils.FindAvailableNetwork([]*net.IPNet{nw}); err == nil {
|
||||||
return nw, nil
|
return nw, nil
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -36,33 +36,6 @@ func getAddressRange(pool string) (*AddressRange, error) {
|
||||||
return &AddressRange{nw, ipToUint32(types.GetMinimalIP(lIP)), ipToUint32(types.GetMinimalIP(hIP))}, nil
|
return &AddressRange{nw, ipToUint32(types.GetMinimalIP(lIP)), ipToUint32(types.GetMinimalIP(hIP))}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func initLocalPredefinedPools() []*net.IPNet {
|
|
||||||
pl := make([]*net.IPNet, 0, 274)
|
|
||||||
mask := []byte{255, 255, 0, 0}
|
|
||||||
for i := 17; i < 32; i++ {
|
|
||||||
pl = append(pl, &net.IPNet{IP: []byte{172, byte(i), 0, 0}, Mask: mask})
|
|
||||||
}
|
|
||||||
for i := 0; i < 256; i++ {
|
|
||||||
pl = append(pl, &net.IPNet{IP: []byte{10, byte(i), 0, 0}, Mask: mask})
|
|
||||||
}
|
|
||||||
mask24 := []byte{255, 255, 255, 0}
|
|
||||||
for i := 42; i < 45; i++ {
|
|
||||||
pl = append(pl, &net.IPNet{IP: []byte{192, 168, byte(i), 0}, Mask: mask24})
|
|
||||||
}
|
|
||||||
return pl
|
|
||||||
}
|
|
||||||
|
|
||||||
func initGlobalPredefinedPools() []*net.IPNet {
|
|
||||||
pl := make([]*net.IPNet, 0, 256*256)
|
|
||||||
mask := []byte{255, 255, 255, 0}
|
|
||||||
for i := 0; i < 256; i++ {
|
|
||||||
for j := 0; j < 256; j++ {
|
|
||||||
pl = append(pl, &net.IPNet{IP: []byte{10, byte(i), byte(j), 0}, Mask: mask})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return pl
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check subnets size. In case configured subnet is v6 and host size is
|
// Check subnets size. In case configured subnet is v6 and host size is
|
||||||
// greater than 32 bits, adjust subnet to /96.
|
// greater than 32 bits, adjust subnet to /96.
|
||||||
func adjustAndCheckSubnetSize(subnet *net.IPNet) (*net.IPNet, error) {
|
func adjustAndCheckSubnetSize(subnet *net.IPNet) (*net.IPNet, error) {
|
||||||
|
|
|
@ -28,8 +28,12 @@ func init() {
|
||||||
// and returns its IPv4 and IPv6 addresses in CIDR form. If the interface does not exist,
|
// and returns its IPv4 and IPv6 addresses in CIDR form. If the interface does not exist,
|
||||||
// it chooses from a predifined list the first IPv4 address which does not conflict
|
// it chooses from a predifined list the first IPv4 address which does not conflict
|
||||||
// with other interfaces on the system.
|
// with other interfaces on the system.
|
||||||
func ElectInterfaceAddresses(name string) ([]*net.IPNet, []*net.IPNet, error) {
|
func ElectInterfaceAddresses(name string) (*net.IPNet, []*net.IPNet, error) {
|
||||||
var v4Nets, v6Nets []*net.IPNet
|
var (
|
||||||
|
v4Net *net.IPNet
|
||||||
|
v6Nets []*net.IPNet
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
link, _ := netlink.LinkByName(name)
|
link, _ := netlink.LinkByName(name)
|
||||||
if link != nil {
|
if link != nil {
|
||||||
|
@ -41,24 +45,23 @@ func ElectInterfaceAddresses(name string) ([]*net.IPNet, []*net.IPNet, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
for _, nlAddr := range v4addr {
|
if len(v4addr) > 0 {
|
||||||
v4Nets = append(v4Nets, nlAddr.IPNet)
|
v4Net = v4addr[0].IPNet
|
||||||
}
|
}
|
||||||
for _, nlAddr := range v6addr {
|
for _, nlAddr := range v6addr {
|
||||||
v6Nets = append(v6Nets, nlAddr.IPNet)
|
v6Nets = append(v6Nets, nlAddr.IPNet)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if link == nil || len(v4Nets) == 0 {
|
if link == nil || v4Net == nil {
|
||||||
// Choose from predifined broad networks
|
// Choose from predifined broad networks
|
||||||
v4Net, err := FindAvailableNetwork(PredefinedBroadNetworks)
|
v4Net, err = FindAvailableNetwork(PredefinedBroadNetworks)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
v4Nets = append(v4Nets, v4Net)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return v4Nets, v6Nets, nil
|
return v4Net, v6Nets, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindAvailableNetwork returns a network from the passed list which does not
|
// FindAvailableNetwork returns a network from the passed list which does not
|
||||||
|
|
|
@ -77,7 +77,7 @@ func TestElectInterfaceAddress(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(ipv4Nw) == 0 {
|
if ipv4Nw == nil {
|
||||||
t.Fatalf("unexpected empty ipv4 network addresses")
|
t.Fatalf("unexpected empty ipv4 network addresses")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,8 +85,8 @@ func TestElectInterfaceAddress(t *testing.T) {
|
||||||
t.Fatalf("unexpected empty ipv4 network addresses")
|
t.Fatalf("unexpected empty ipv4 network addresses")
|
||||||
}
|
}
|
||||||
|
|
||||||
if nws != ipv4Nw[0].String() {
|
if nws != ipv4Nw.String() {
|
||||||
t.Fatalf("expected %s. got %s", nws, ipv4Nw[0])
|
t.Fatalf("expected %s. got %s", nws, ipv4Nw)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue