Skip to content

Commit e21d93d

Browse files
committed
manager/allocator/cnmallocator: add temporary adaptor for constructor
Add an adaptor to help with a signature change in the Idm constructor. Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 93fe90a commit e21d93d

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

manager/allocator/cnmallocator/portallocator.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cnmallocator
33
import (
44
"fmt"
55

6+
"github.com/docker/docker/libnetwork/datastore"
67
"github.com/docker/docker/libnetwork/idm"
78
"github.com/moby/swarmkit/v2/api"
89
)
@@ -117,16 +118,40 @@ func newPortAllocator() (*portAllocator, error) {
117118
return &portAllocator{portSpaces: portSpaces}, nil
118119
}
119120

121+
type (
122+
// these are deliberately aliases, otherwise we'd only match the same type, not signature.
123+
legacyConstructor = func(ds datastore.DataStore, id string, start, end uint64) (*idm.Idm, error)
124+
idmConstructor = func(id string, start, end uint64) (*idm.Idm, error)
125+
)
126+
127+
func adaptConstructor(newFn any) (idmConstructor, error) {
128+
switch fn := newFn.(type) {
129+
case idmConstructor:
130+
return fn, nil
131+
case legacyConstructor:
132+
return func(id string, start, end uint64) (*idm.Idm, error) {
133+
return fn(nil, id, start, end)
134+
}, nil
135+
default:
136+
return nil, fmt.Errorf("invalid constructor signature:%T", newFn)
137+
}
138+
}
139+
120140
func newPortSpace(protocol api.PortConfig_Protocol) (*portSpace, error) {
141+
fn, err := adaptConstructor(idm.New)
142+
if err != nil {
143+
return nil, err
144+
}
145+
121146
masterName := fmt.Sprintf("%s-master-ports", protocol)
122147
dynamicName := fmt.Sprintf("%s-dynamic-ports", protocol)
123148

124-
master, err := idm.New(nil, masterName, masterPortStart, masterPortEnd)
149+
master, err := fn(masterName, masterPortStart, masterPortEnd)
125150
if err != nil {
126151
return nil, err
127152
}
128153

129-
dynamic, err := idm.New(nil, dynamicName, dynamicPortStart, dynamicPortEnd)
154+
dynamic, err := fn(dynamicName, dynamicPortStart, dynamicPortEnd)
130155
if err != nil {
131156
return nil, err
132157
}

0 commit comments

Comments
 (0)