Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions manager/allocator/cnmallocator/networkallocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,24 @@ type NetworkConfig struct {
VXLANUDPPort uint32
}

type (
registryConstructor = func(drvregistry.DriverNotifyFunc, plugingetter.PluginGetter) (*drvregistry.DrvRegistry, error)
legacyConstructor = func(drvregistry.Placeholder, drvregistry.Placeholder, drvregistry.DriverNotifyFunc, drvregistry.Placeholder, plugingetter.PluginGetter) (*drvregistry.DrvRegistry, error)
)

func newDriverRegistry(newFn any, pg plugingetter.PluginGetter) (*drvregistry.DrvRegistry, error) {
switch fn := newFn.(type) {
case registryConstructor:
// There are no driver configurations and notification
// functions as of now.
return fn(nil, pg)
case legacyConstructor:
return fn(nil, nil, nil, nil, pg)
default:
return nil, fmt.Errorf("invalid constructor signature: %T", newFn)
}
}

// New returns a new NetworkAllocator handle
func New(pg plugingetter.PluginGetter, netConfig *NetworkConfig) (networkallocator.NetworkAllocator, error) {
na := &cnmNetworkAllocator{
Expand All @@ -108,9 +126,8 @@ func New(pg plugingetter.PluginGetter, netConfig *NetworkConfig) (networkallocat
nodes: make(map[string]map[string]struct{}),
}

// There are no driver configurations and notification
// functions as of now.
reg, err := drvregistry.New(nil, nil, nil, nil, pg)
// FIXME(thaJeztah): drvregistry.New was deprecated in https://github.com/moby/moby/commit/5595311209cc915e8b0ace0a1bbd8b52a7baecb0, but there's no other way to pass a PluginGetter to it.
reg, err := newDriverRegistry(drvregistry.New, pg)
if err != nil {
return nil, err
}
Expand Down
25 changes: 20 additions & 5 deletions manager/allocator/cnmallocator/portallocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cnmallocator
import (
"fmt"

"github.com/docker/docker/libnetwork/datastore"
"github.com/docker/docker/libnetwork/idm"
"github.com/moby/swarmkit/v2/api"
)
Expand Down Expand Up @@ -117,16 +118,30 @@ func newPortAllocator() (*portAllocator, error) {
return &portAllocator{portSpaces: portSpaces}, nil
}

func newPortSpace(protocol api.PortConfig_Protocol) (*portSpace, error) {
masterName := fmt.Sprintf("%s-master-ports", protocol)
dynamicName := fmt.Sprintf("%s-dynamic-ports", protocol)
type (
// these are deliberately aliases, otherwise we'd only match the same type, not signature.
legacyIdmConstructor = func(ds datastore.DataStore, id string, start, end uint64) (*idm.Idm, error)
idmConstructor = func(id string, start, end uint64) (*idm.Idm, error)
)

func newIdm(newFn any, id string, start, end uint64) (*idm.Idm, error) {
switch fn := newFn.(type) {
case idmConstructor:
return fn(id, start, end)
case legacyIdmConstructor:
return fn(nil, id, start, end)
default:
return nil, fmt.Errorf("invalid constructor signature: %T", newFn)
}
}

master, err := idm.New(nil, masterName, masterPortStart, masterPortEnd)
func newPortSpace(protocol api.PortConfig_Protocol) (*portSpace, error) {
master, err := newIdm(idm.New, protocol.String()+"-master-ports", masterPortStart, masterPortEnd)
if err != nil {
return nil, err
}

dynamic, err := idm.New(nil, dynamicName, dynamicPortStart, dynamicPortEnd)
dynamic, err := newIdm(idm.New, protocol.String()+"-dynamic-ports", dynamicPortStart, dynamicPortEnd)
if err != nil {
return nil, err
}
Expand Down