Skip to content

Commit

Permalink
libnet/ipam: default-address-pools as Register arg
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
akerouanton committed Apr 26, 2024
1 parent 1d5a12d commit e8644c3
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 48 deletions.
12 changes: 5 additions & 7 deletions libnetwork/cnmallocator/drivers_ipam.go
Expand Up @@ -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
Expand Down
16 changes: 3 additions & 13 deletions libnetwork/drivers_ipam.go
Expand Up @@ -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)
}
3 changes: 2 additions & 1 deletion libnetwork/drvregistry/ipams_test.go
Expand Up @@ -9,14 +9,15 @@ 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"
)

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))

Expand Down
35 changes: 12 additions & 23 deletions libnetwork/ipams/builtin/builtin.go
@@ -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
}
Expand All @@ -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
}
5 changes: 3 additions & 2 deletions libnetwork/ipams/builtin/builtin_unix.go
Expand Up @@ -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 changes: 3 additions & 2 deletions libnetwork/ipams/builtin/builtin_windows.go
Expand Up @@ -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
}

Expand Down

0 comments on commit e8644c3

Please sign in to comment.