libnet/ipam: default-address-pools as Register arg
Prior to this change, daemon's `default-address-pools` param would be passed to `SetDefaultIPAddressPool()` to set a global var named `defaultAddressPool`. This var would then be retrieved during the `default` IPAM driver registration. Both steps were executed in close succession during libnet's controller initialization. This change removes the global var and just pass the user-defined `default-address-pools` to the `default` driver's `Register` fn. Signed-off-by: Albin Kerouanton <albinker@gmail.com>
This commit is contained in:
parent
1854b22984
commit
dbc961a3d3
6 changed files with 28 additions and 48 deletions
|
@ -40,13 +40,11 @@ func initIPAMDrivers(r ipamapi.Registerer, netConfig *networkallocator.Config) e
|
|||
log.G(context.TODO()).Infof("Swarm initialized global default address pool to: " + str.String())
|
||||
}
|
||||
|
||||
for _, fn := range [](func(ipamapi.Registerer) error){
|
||||
builtinIpam.Register,
|
||||
nullIpam.Register,
|
||||
} {
|
||||
if err := fn(r); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := builtinIpam.Register(r, []*ipamutils.NetworkToSplit(nil)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := nullIpam.Register(r); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -10,21 +10,11 @@ import (
|
|||
)
|
||||
|
||||
func initIPAMDrivers(r ipamapi.Registerer, pg plugingetter.PluginGetter, addressPool []*ipamutils.NetworkToSplit) error {
|
||||
// TODO: pass address pools as arguments to builtinIpam.Init instead of
|
||||
// indirectly through global mutable state. Swarmkit references that
|
||||
// function so changing its signature breaks the build.
|
||||
if err := builtinIpam.SetDefaultIPAddressPool(addressPool); err != nil {
|
||||
if err := builtinIpam.Register(r, addressPool); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, fn := range [](func(ipamapi.Registerer) error){
|
||||
builtinIpam.Register,
|
||||
nullIpam.Register,
|
||||
} {
|
||||
if err := fn(r); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := nullIpam.Register(r); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return remoteIpam.Register(r, pg)
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
builtinIpam "github.com/docker/docker/libnetwork/ipams/builtin"
|
||||
nullIpam "github.com/docker/docker/libnetwork/ipams/null"
|
||||
remoteIpam "github.com/docker/docker/libnetwork/ipams/remote"
|
||||
"github.com/docker/docker/libnetwork/ipamutils"
|
||||
"gotest.tools/v3/assert"
|
||||
is "gotest.tools/v3/assert/cmp"
|
||||
)
|
||||
|
@ -16,7 +17,7 @@ import (
|
|||
func getNewIPAMs(t *testing.T) *IPAMs {
|
||||
r := &IPAMs{}
|
||||
|
||||
assert.Assert(t, builtinIpam.Register(r))
|
||||
assert.Assert(t, builtinIpam.Register(r, []*ipamutils.NetworkToSplit(nil)))
|
||||
assert.Assert(t, remoteIpam.Register(r, nil))
|
||||
assert.Assert(t, nullIpam.Register(r))
|
||||
|
||||
|
|
|
@ -1,26 +1,25 @@
|
|||
package builtin
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/docker/docker/libnetwork/ipam"
|
||||
"github.com/docker/docker/libnetwork/ipamapi"
|
||||
"github.com/docker/docker/libnetwork/ipamutils"
|
||||
)
|
||||
|
||||
// defaultAddressPool Stores user configured subnet list
|
||||
var defaultAddressPool []*net.IPNet
|
||||
|
||||
// registerBuiltin registers the built-in ipam driver with libnetwork.
|
||||
func registerBuiltin(ic ipamapi.Registerer) error {
|
||||
var localAddressPool []*net.IPNet
|
||||
if len(defaultAddressPool) > 0 {
|
||||
localAddressPool = append([]*net.IPNet(nil), defaultAddressPool...)
|
||||
} else {
|
||||
localAddressPool = ipamutils.GetLocalScopeDefaultNetworks()
|
||||
// registerBuiltin registers the built-in ipam driver with libnetwork. It takes
|
||||
// an optional addressPools containing the list of user-defined address pools
|
||||
// used by the local address space (ie. daemon's default-address-pools parameter).
|
||||
func registerBuiltin(ic ipamapi.Registerer, addressPools []*ipamutils.NetworkToSplit) error {
|
||||
localAddressPools := ipamutils.GetLocalScopeDefaultNetworks()
|
||||
if len(addressPools) > 0 {
|
||||
var err error
|
||||
localAddressPools, err = ipamutils.SplitNetworks(addressPools)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
a, err := ipam.NewAllocator(localAddressPool, ipamutils.GetGlobalScopeDefaultNetworks())
|
||||
a, err := ipam.NewAllocator(localAddressPools, ipamutils.GetGlobalScopeDefaultNetworks())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -29,13 +28,3 @@ func registerBuiltin(ic ipamapi.Registerer) error {
|
|||
|
||||
return ic.RegisterIpamDriverWithCapabilities(ipamapi.DefaultIPAM, a, cps)
|
||||
}
|
||||
|
||||
// SetDefaultIPAddressPool stores default address pool.
|
||||
func SetDefaultIPAddressPool(addressPool []*ipamutils.NetworkToSplit) error {
|
||||
nets, err := ipamutils.SplitNetworks(addressPool)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defaultAddressPool = nets
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -4,9 +4,10 @@ package builtin
|
|||
|
||||
import (
|
||||
"github.com/docker/docker/libnetwork/ipamapi"
|
||||
"github.com/docker/docker/libnetwork/ipamutils"
|
||||
)
|
||||
|
||||
// Register registers the built-in ipam service with libnetwork.
|
||||
func Register(r ipamapi.Registerer) error {
|
||||
return registerBuiltin(r)
|
||||
func Register(r ipamapi.Registerer, addressPools []*ipamutils.NetworkToSplit) error {
|
||||
return registerBuiltin(r, addressPools)
|
||||
}
|
||||
|
|
|
@ -5,11 +5,12 @@ package builtin
|
|||
import (
|
||||
"github.com/docker/docker/libnetwork/ipamapi"
|
||||
"github.com/docker/docker/libnetwork/ipams/windowsipam"
|
||||
"github.com/docker/docker/libnetwork/ipamutils"
|
||||
)
|
||||
|
||||
// Register registers the built-in ipam services with libnetwork.
|
||||
func Register(r ipamapi.Registerer) error {
|
||||
if err := registerBuiltin(r); err != nil {
|
||||
func Register(r ipamapi.Registerer, addressPools []*ipamutils.NetworkToSplit) error {
|
||||
if err := registerBuiltin(r, addressPools); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue