libnw/ipamutils: make local defaults immutable

ConfigLocalScopeDefaultNetworks is now dead code, thank goodness! Make
sure it stays dead by deleting the function. Refactor package ipamutils
to simplify things given its newly-reduced (ahem) scope.

Signed-off-by: Cory Snider <csnider@mirantis.com>
This commit is contained in:
Cory Snider 2023-01-16 20:09:42 -05:00
parent 540d1e0561
commit e8011d7872
3 changed files with 36 additions and 66 deletions

View file

@ -8,12 +8,12 @@ import (
)
var (
// PredefinedLocalScopeDefaultNetworks contains a list of 31 IPv4 private networks with host size 16 and 12
// predefinedLocalScopeDefaultNetworks contains a list of 31 IPv4 private networks with host size 16 and 12
// (172.17-31.x.x/16, 192.168.x.x/20) which do not overlap with the networks in `PredefinedGlobalScopeDefaultNetworks`
PredefinedLocalScopeDefaultNetworks []*net.IPNet
// PredefinedGlobalScopeDefaultNetworks contains a list of 64K IPv4 private networks with host size 8
predefinedLocalScopeDefaultNetworks []*net.IPNet
// predefinedGlobalScopeDefaultNetworks contains a list of 64K IPv4 private networks with host size 8
// (10.x.x.x/24) which do not overlap with the networks in `PredefinedLocalScopeDefaultNetworks`
PredefinedGlobalScopeDefaultNetworks []*net.IPNet
predefinedGlobalScopeDefaultNetworks []*net.IPNet
mutex sync.Mutex
localScopeDefaultNetworks = []*NetworkToSplit{{"172.17.0.0/16", 16}, {"172.18.0.0/16", 16}, {"172.19.0.0/16", 16},
{"172.20.0.0/14", 16}, {"172.24.0.0/14", 16}, {"172.28.0.0/14", 16},
@ -33,57 +33,41 @@ type NetworkToSplit struct {
func init() {
var err error
if PredefinedGlobalScopeDefaultNetworks, err = SplitNetworks(globalScopeDefaultNetworks); err != nil {
if predefinedGlobalScopeDefaultNetworks, err = SplitNetworks(globalScopeDefaultNetworks); err != nil {
panic("failed to initialize the global scope default address pool: " + err.Error())
}
if PredefinedLocalScopeDefaultNetworks, err = SplitNetworks(localScopeDefaultNetworks); err != nil {
if predefinedLocalScopeDefaultNetworks, err = SplitNetworks(localScopeDefaultNetworks); err != nil {
panic("failed to initialize the local scope default address pool: " + err.Error())
}
}
// configDefaultNetworks configures local as well global default pool based on input
func configDefaultNetworks(defaultAddressPool []*NetworkToSplit, result *[]*net.IPNet) error {
mutex.Lock()
defer mutex.Unlock()
defaultNetworks, err := SplitNetworks(defaultAddressPool)
if err != nil {
return err
}
*result = defaultNetworks
return nil
}
// GetGlobalScopeDefaultNetworks returns PredefinedGlobalScopeDefaultNetworks
func GetGlobalScopeDefaultNetworks() []*net.IPNet {
mutex.Lock()
defer mutex.Unlock()
return PredefinedGlobalScopeDefaultNetworks
}
// GetLocalScopeDefaultNetworks returns PredefinedLocalScopeDefaultNetworks
func GetLocalScopeDefaultNetworks() []*net.IPNet {
mutex.Lock()
defer mutex.Unlock()
return PredefinedLocalScopeDefaultNetworks
}
// ConfigGlobalScopeDefaultNetworks configures global default pool.
// Ideally this will be called from SwarmKit as part of swarm init
func ConfigGlobalScopeDefaultNetworks(defaultAddressPool []*NetworkToSplit) error {
if defaultAddressPool == nil {
defaultAddressPool = globalScopeDefaultNetworks
}
return configDefaultNetworks(defaultAddressPool, &PredefinedGlobalScopeDefaultNetworks)
}
// ConfigLocalScopeDefaultNetworks configures local default pool.
// Ideally this will be called during libnetwork init
func ConfigLocalScopeDefaultNetworks(defaultAddressPool []*NetworkToSplit) error {
if defaultAddressPool == nil {
return nil
}
return configDefaultNetworks(defaultAddressPool, &PredefinedLocalScopeDefaultNetworks)
mutex.Lock()
defer mutex.Unlock()
defaultNetworks, err := SplitNetworks(defaultAddressPool)
if err != nil {
return err
}
predefinedGlobalScopeDefaultNetworks = defaultNetworks
return nil
}
// GetGlobalScopeDefaultNetworks returns a copy of the global-sopce network list.
func GetGlobalScopeDefaultNetworks() []*net.IPNet {
mutex.Lock()
defer mutex.Unlock()
return append([]*net.IPNet(nil), predefinedGlobalScopeDefaultNetworks...)
}
// GetLocalScopeDefaultNetworks returns a copy of the default local-scope network list.
func GetLocalScopeDefaultNetworks() []*net.IPNet {
return append([]*net.IPNet(nil), predefinedLocalScopeDefaultNetworks...)
}
// SplitNetworks takes a slice of networks, split them accordingly and returns them

View file

@ -44,13 +44,13 @@ func initGlobalScopeNetworks() []*net.IPNet {
}
func TestDefaultNetwork(t *testing.T) {
for _, nw := range PredefinedGlobalScopeDefaultNetworks {
for _, nw := range GetGlobalScopeDefaultNetworks() {
if ones, bits := nw.Mask.Size(); bits != 32 || ones != 24 {
t.Fatalf("Unexpected size for network in granular list: %v", nw)
}
}
for _, nw := range PredefinedLocalScopeDefaultNetworks {
for _, nw := range GetLocalScopeDefaultNetworks() {
if ones, bits := nw.Mask.Size(); bits != 32 || (ones != 20 && ones != 16) {
t.Fatalf("Unexpected size for network in broad list: %v", nw)
}
@ -61,7 +61,7 @@ func TestDefaultNetwork(t *testing.T) {
for _, v := range originalBroadNets {
m[v.String()] = true
}
for _, nw := range PredefinedLocalScopeDefaultNetworks {
for _, nw := range GetLocalScopeDefaultNetworks() {
_, ok := m[nw.String()]
assert.Check(t, ok)
delete(m, nw.String())
@ -75,7 +75,7 @@ func TestDefaultNetwork(t *testing.T) {
for _, v := range originalGranularNets {
m[v.String()] = true
}
for _, nw := range PredefinedGlobalScopeDefaultNetworks {
for _, nw := range GetGlobalScopeDefaultNetworks() {
_, ok := m[nw.String()]
assert.Check(t, ok)
delete(m, nw.String())
@ -93,7 +93,7 @@ func TestConfigGlobalScopeDefaultNetworks(t *testing.T) {
for _, v := range originalGlobalScopeNetworks {
m[v.String()] = true
}
for _, nw := range PredefinedGlobalScopeDefaultNetworks {
for _, nw := range GetGlobalScopeDefaultNetworks() {
_, ok := m[nw.String()]
assert.Check(t, ok)
delete(m, nw.String())
@ -101,17 +101,3 @@ func TestConfigGlobalScopeDefaultNetworks(t *testing.T) {
assert.Check(t, is.Len(m, 0))
}
func TestInitAddressPools(t *testing.T) {
err := ConfigLocalScopeDefaultNetworks([]*NetworkToSplit{{"172.80.0.0/16", 24}, {"172.90.0.0/16", 24}})
assert.NilError(t, err)
// Check for Random IPAddresses in PredefinedLocalScopeDefaultNetworks ex: first , last and middle
assert.Check(t, is.Len(PredefinedLocalScopeDefaultNetworks, 512), "Failed to find PredefinedLocalScopeDefaultNetworks")
assert.Check(t, is.Equal(PredefinedLocalScopeDefaultNetworks[0].String(), "172.80.0.0/24"))
assert.Check(t, is.Equal(PredefinedLocalScopeDefaultNetworks[127].String(), "172.80.127.0/24"))
assert.Check(t, is.Equal(PredefinedLocalScopeDefaultNetworks[255].String(), "172.80.255.0/24"))
assert.Check(t, is.Equal(PredefinedLocalScopeDefaultNetworks[256].String(), "172.90.0.0/24"))
assert.Check(t, is.Equal(PredefinedLocalScopeDefaultNetworks[383].String(), "172.90.127.0/24"))
assert.Check(t, is.Equal(PredefinedLocalScopeDefaultNetworks[511].String(), "172.90.255.0/24"))
}

View file

@ -249,13 +249,13 @@ func TestUtilGenerateRandomMAC(t *testing.T) {
func TestNetworkRequest(t *testing.T) {
defer testutils.SetupTestOSContext(t)()
nw, err := FindAvailableNetwork(ipamutils.PredefinedLocalScopeDefaultNetworks)
nw, err := FindAvailableNetwork(ipamutils.GetLocalScopeDefaultNetworks())
if err != nil {
t.Fatal(err)
}
var found bool
for _, exp := range ipamutils.PredefinedLocalScopeDefaultNetworks {
for _, exp := range ipamutils.GetLocalScopeDefaultNetworks() {
if types.CompareIPNet(exp, nw) {
found = true
break
@ -266,13 +266,13 @@ func TestNetworkRequest(t *testing.T) {
t.Fatalf("Found unexpected broad network %s", nw)
}
nw, err = FindAvailableNetwork(ipamutils.PredefinedGlobalScopeDefaultNetworks)
nw, err = FindAvailableNetwork(ipamutils.GetGlobalScopeDefaultNetworks())
if err != nil {
t.Fatal(err)
}
found = false
for _, exp := range ipamutils.PredefinedGlobalScopeDefaultNetworks {
for _, exp := range ipamutils.GetGlobalScopeDefaultNetworks() {
if types.CompareIPNet(exp, nw) {
found = true
break
@ -290,7 +290,7 @@ func TestNetworkRequest(t *testing.T) {
if err != nil {
t.Fatal(err)
}
nw, err = FindAvailableNetwork(ipamutils.PredefinedLocalScopeDefaultNetworks)
nw, err = FindAvailableNetwork(ipamutils.GetLocalScopeDefaultNetworks())
if err != nil {
t.Fatal(err)
}