Skip to content

Commit

Permalink
manager/allocator/cnmallocator: add temporary adaptor for constructor
Browse files Browse the repository at this point in the history
Add an adaptor to help with a signature change in the Idm constructor.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
  • Loading branch information
thaJeztah committed Jun 30, 2023
1 parent ad0f3ae commit 14602cf
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 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,35 @@ func newPortAllocator() (*portAllocator, error) {
return &portAllocator{portSpaces: portSpaces}, nil
}

type (
legacyConstructor func(ds datastore.DataStore, id string, start, end uint64) (*idm.Idm, error)
idmConstructor func(id string, start, end uint64) (*idm.Idm, error)
)

func adaptConstructor(newFn any) idmConstructor {
if fn, ok := newFn.(idmConstructor); ok {
return fn
} else if fn, ok := newFn.(legacyConstructor); ok {
return func(id string, start, end uint64) (*idm.Idm, error) {
return fn(nil, id, start, end)
}
} else {
return func(id string, start, end uint64) (*idm.Idm, error) {
return nil, fmt.Errorf("invalid constructor")
}
}
}

func newPortSpace(protocol api.PortConfig_Protocol) (*portSpace, error) {
masterName := fmt.Sprintf("%s-master-ports", protocol)
dynamicName := fmt.Sprintf("%s-dynamic-ports", protocol)

master, err := idm.New(nil, masterName, masterPortStart, masterPortEnd)
master, err := adaptConstructor(idm.New)(masterName, masterPortStart, masterPortEnd)
if err != nil {
return nil, err
}

dynamic, err := idm.New(nil, dynamicName, dynamicPortStart, dynamicPortEnd)
dynamic, err := adaptConstructor(idm.New)(dynamicName, dynamicPortStart, dynamicPortEnd)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 14602cf

Please sign in to comment.