Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

manager/allocator/cnmallocator: add temporary adaptor for constructor #3139

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 19 additions & 3 deletions manager/allocator/cnmallocator/networkallocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,23 @@ 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 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 +125,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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're damn right there is no way to pass a PluginGetter when constructing the non-deprecated drvregistry types. That's because they don't need one. See also moby/moby@28edc8e

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only consumers of libnetwork/idm are this file and libnetwork's ovmanager driver. Notably, ovmanager initializes its idm with start=0, which means the idm transparently delegates everything to the underlying bitmap and can therefore be trivially replaced with a bitmap.Sequence. That leaves this file as the only consumer of idm which actually needs it. Therefore I propose moving the idm package into swarmkit, which would make the adapters unnecessary.

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
idmConstructor = func(id string, start, end uint64) (*idm.Idm, error)
idmConstructor = func(start, end uint64) (*idm.Idm, error)

Without the datastore, id would also be unused.

)

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