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 Jul 3, 2023
1 parent 93fe90a commit f1f1a27
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 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,38 @@ 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, error) {
if fn, ok := newFn.(idmConstructor); ok {
return fn, nil
}
if fn, ok := newFn.(legacyConstructor); ok {
return func(id string, start, end uint64) (*idm.Idm, error) {
return fn(nil, id, start, end)
}, nil
}
return nil, fmt.Errorf("invalid constructor: %+v (%T)", newFn, newFn)
}

func newPortSpace(protocol api.PortConfig_Protocol) (*portSpace, error) {
fn, err := adaptConstructor(idm.New)
if err != nil {
return nil, err
}

masterName := fmt.Sprintf("%s-master-ports", protocol)
dynamicName := fmt.Sprintf("%s-dynamic-ports", protocol)

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

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

0 comments on commit f1f1a27

Please sign in to comment.