Skip to content

Commit

Permalink
manager/allocator/cnmallocator: use a map for initializers
Browse files Browse the repository at this point in the history
Refactor the initializers slice to be a map (as driver-names should be
unique, and they were not dynamically set here either way).

Also inlining the code from initializeDrivers(), as it was only used
in a single location, and the code lived far from where it was used.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
  • Loading branch information
thaJeztah committed Jul 3, 2023
1 parent 470f0b5 commit 0b6b98e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 39 deletions.
6 changes: 3 additions & 3 deletions manager/allocator/cnmallocator/drivers_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"github.com/moby/swarmkit/v2/manager/allocator/networkallocator"
)

var initializers = []initializer{
{remote.Init, "remote"},
{ovmanager.Init, "overlay"},
var initializers = map[string]driverRegisterFn{
"remote": remote.Init,
"overlay": ovmanager.Init,
}

// PredefinedNetworks returns the list of predefined network structures
Expand Down
15 changes: 8 additions & 7 deletions manager/allocator/cnmallocator/drivers_network_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import (
"github.com/docker/docker/libnetwork/drivers/macvlan/mvmanager"
"github.com/docker/docker/libnetwork/drivers/overlay/ovmanager"
"github.com/docker/docker/libnetwork/drivers/remote"
"github.com/docker/docker/libnetwork/drvregistry"
"github.com/moby/swarmkit/v2/manager/allocator/networkallocator"
)

var initializers = []initializer{
{remote.Init, "remote"},
{ovmanager.Init, "overlay"},
{mvmanager.Init, "macvlan"},
{brmanager.Init, "bridge"},
{ivmanager.Init, "ipvlan"},
{host.Init, "host"},
var initializers = map[string]drvregistry.InitFunc{
"remote": remote.Init,
"overlay": ovmanager.Init,
"macvlan": mvmanager.Init,
"bridge": brmanager.Init,
"ipvlan": ivmanager.Init,
"host": host.Init,
}

// PredefinedNetworks returns the list of predefined network structures
Expand Down
13 changes: 7 additions & 6 deletions manager/allocator/cnmallocator/drivers_network_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package cnmallocator
import (
"github.com/docker/docker/libnetwork/drivers/overlay/ovmanager"
"github.com/docker/docker/libnetwork/drivers/remote"
"github.com/docker/docker/libnetwork/drvregistry"
"github.com/moby/swarmkit/v2/manager/allocator/networkallocator"
)

var initializers = []initializer{
{remote.Init, "remote"},
{ovmanager.Init, "overlay"},
{StubManagerInit("internal"), "internal"},
{StubManagerInit("l2bridge"), "l2bridge"},
{StubManagerInit("nat"), "nat"},
var initializers = map[string]drvregistry.InitFunc{
"remote": remote.Init,
"overlay": ovmanager.Init,
"internal": StubManagerInit("internal"),
"l2bridge": StubManagerInit("l2bridge"),
"nat": StubManagerInit("nat"),
}

// PredefinedNetworks returns the list of predefined network structures
Expand Down
29 changes: 6 additions & 23 deletions manager/allocator/cnmallocator/networkallocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ type networkDriver struct {
capability *driverapi.Capability
}

type initializer struct {
fn drvregistry.InitFunc
ntype string
}

// NetworkConfig is used to store network related cluster config in the Manager.
type NetworkConfig struct {
// DefaultAddrPool specifies default subnet pool for global scope networks
Expand Down Expand Up @@ -115,8 +110,10 @@ func New(pg plugingetter.PluginGetter, netConfig *NetworkConfig) (networkallocat
return nil, err
}

if err := initializeDrivers(reg); err != nil {
return nil, err
for _, init := range initializers {
if err := init(reg, nil); err != nil {
return nil, err
}
}

if err = initIPAMDrivers(reg, netConfig); err != nil {
Expand Down Expand Up @@ -978,15 +975,6 @@ func (na *cnmNetworkAllocator) allocatePools(n *api.Network) (map[string]string,
return pools, nil
}

func initializeDrivers(reg *drvregistry.DrvRegistry) error {
for _, i := range initializers {
if err := reg.AddDriver(i.ntype, i.fn, nil); err != nil {
return err
}
}
return nil
}

func serviceNetworks(s *api.Service) []*api.NetworkAttachmentConfig {
// Always prefer NetworkAttachmentConfig in the TaskSpec
if len(s.Spec.Task.Networks) == 0 && len(s.Spec.Networks) != 0 {
Expand All @@ -1010,13 +998,8 @@ func (na *cnmNetworkAllocator) IsVIPOnIngressNetwork(vip *api.Endpoint_VirtualIP

// IsBuiltInDriver returns whether the passed driver is an internal network driver
func IsBuiltInDriver(name string) bool {
n := strings.ToLower(name)
for _, d := range initializers {
if n == d.ntype {
return true
}
}
return false
_, ok := initializers[strings.ToLower(name)]
return ok
}

// setIPAMSerialAlloc sets the ipam allocation method to serial
Expand Down

0 comments on commit 0b6b98e

Please sign in to comment.