Skip to content

Commit d7d23d7

Browse files
authored
Merge pull request #2732 from selansen/default_addr_pool
Default Address Pool Code Refactor
2 parents ee0ba51 + e32e960 commit d7d23d7

File tree

10 files changed

+73
-63
lines changed

10 files changed

+73
-63
lines changed

manager/allocator/allocator.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package allocator
22

33
import (
4-
"net"
54
"sync"
65

76
"github.com/docker/docker/pkg/plugingetter"
87
"github.com/docker/go-events"
98
"github.com/docker/swarmkit/api"
9+
"github.com/docker/swarmkit/manager/allocator/cnmallocator"
1010
"github.com/docker/swarmkit/manager/state"
1111
"github.com/docker/swarmkit/manager/state/store"
1212
"golang.org/x/net/context"
@@ -34,12 +34,8 @@ type Allocator struct {
3434
// pluginGetter provides access to docker's plugin inventory.
3535
pluginGetter plugingetter.PluginGetter
3636

37-
// DefaultAddrPool specifies default subnet pool for global scope networks
38-
defaultAddrPool []*net.IPNet
39-
40-
// SubnetSize specifies the subnet size of the networks created from
41-
// the default subnet pool
42-
subnetSize int
37+
// networkConfig stores network related config for the cluster
38+
networkConfig *cnmallocator.NetworkConfig
4339
}
4440

4541
// taskBallot controls how the voting for task allocation is
@@ -73,17 +69,16 @@ type allocActor struct {
7369

7470
// New returns a new instance of Allocator for use during allocation
7571
// stage of the manager.
76-
func New(store *store.MemoryStore, pg plugingetter.PluginGetter, defaultAddrPool []*net.IPNet, subnetSize int) (*Allocator, error) {
72+
func New(store *store.MemoryStore, pg plugingetter.PluginGetter, netConfig *cnmallocator.NetworkConfig) (*Allocator, error) {
7773
a := &Allocator{
7874
store: store,
7975
taskBallot: &taskBallot{
8076
votes: make(map[string][]string),
8177
},
82-
stopChan: make(chan struct{}),
83-
doneChan: make(chan struct{}),
84-
pluginGetter: pg,
85-
defaultAddrPool: defaultAddrPool,
86-
subnetSize: subnetSize,
78+
stopChan: make(chan struct{}),
79+
doneChan: make(chan struct{}),
80+
pluginGetter: pg,
81+
networkConfig: netConfig,
8782
}
8883

8984
return a, nil

manager/allocator/allocator_linux_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestIPAMNotNil(t *testing.T) {
1616
assert.NotNil(t, s)
1717
defer s.Close()
1818

19-
a, err := New(s, nil, nil, 0)
19+
a, err := New(s, nil, nil)
2020
assert.NoError(t, err)
2121
assert.NotNil(t, a)
2222

manager/allocator/allocator_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestAllocator(t *testing.T) {
2727
assert.NotNil(t, s)
2828
defer s.Close()
2929

30-
a, err := New(s, nil, nil, 0)
30+
a, err := New(s, nil, nil)
3131
assert.NoError(t, err)
3232
assert.NotNil(t, a)
3333

@@ -668,7 +668,7 @@ func TestNoDuplicateIPs(t *testing.T) {
668668

669669
return nil
670670
}))
671-
a, err := New(s, nil, nil, 0)
671+
a, err := New(s, nil, nil)
672672
assert.NoError(t, err)
673673
assert.NotNil(t, a)
674674

@@ -800,7 +800,7 @@ func TestAllocatorRestoreForDuplicateIPs(t *testing.T) {
800800
return true
801801
}
802802

803-
a, err := New(s, nil, nil, 0)
803+
a, err := New(s, nil, nil)
804804
assert.NoError(t, err)
805805
assert.NotNil(t, a)
806806
// Start allocator
@@ -950,7 +950,7 @@ func TestAllocatorRestartNoEndpointSpec(t *testing.T) {
950950
return true
951951
}
952952

953-
a, err := New(s, nil, nil, 0)
953+
a, err := New(s, nil, nil)
954954
assert.NoError(t, err)
955955
assert.NotNil(t, a)
956956
// Start allocator
@@ -1154,7 +1154,7 @@ func TestAllocatorRestoreForUnallocatedNetwork(t *testing.T) {
11541154
return true
11551155
}
11561156

1157-
a, err := New(s, nil, nil, 0)
1157+
a, err := New(s, nil, nil)
11581158
assert.NoError(t, err)
11591159
assert.NotNil(t, a)
11601160
// Start allocator
@@ -1181,7 +1181,7 @@ func TestNodeAllocator(t *testing.T) {
11811181
assert.NotNil(t, s)
11821182
defer s.Close()
11831183

1184-
a, err := New(s, nil, nil, 0)
1184+
a, err := New(s, nil, nil)
11851185
assert.NoError(t, err)
11861186
assert.NotNil(t, a)
11871187

manager/allocator/cnmallocator/drivers_ipam.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cnmallocator
22

33
import (
4-
"net"
54
"strconv"
65
"strings"
76

@@ -14,24 +13,24 @@ import (
1413
"github.com/sirupsen/logrus"
1514
)
1615

17-
func initIPAMDrivers(r *drvregistry.DrvRegistry, defaultAddrPool []*net.IPNet, subnetSize int) error {
16+
func initIPAMDrivers(r *drvregistry.DrvRegistry, netConfig *NetworkConfig) error {
1817
var addressPool []*ipamutils.NetworkToSplit
1918
var str strings.Builder
2019
str.WriteString("Subnetlist - ")
2120
// Extract defaultAddrPool param info and construct ipamutils.NetworkToSplit
2221
// from the info. We will be using it to call Libnetwork API
2322
// We also need to log new address pool info whenever swarm init
2423
// happens with default address pool option
25-
if defaultAddrPool != nil {
26-
for _, p := range defaultAddrPool {
24+
if netConfig != nil {
25+
for _, p := range netConfig.DefaultAddrPool {
2726
addressPool = append(addressPool, &ipamutils.NetworkToSplit{
28-
Base: p.String(),
29-
Size: subnetSize,
27+
Base: p,
28+
Size: int(netConfig.SubnetSize),
3029
})
31-
str.WriteString(p.String() + ",")
30+
str.WriteString(p + ",")
3231
}
3332
str.WriteString(": Size ")
34-
str.WriteString(strconv.Itoa(subnetSize))
33+
str.WriteString(strconv.Itoa(int(netConfig.SubnetSize)))
3534
}
3635
if err := ipamutils.ConfigGlobalScopeDefaultNetworks(addressPool); err != nil {
3736
return err

manager/allocator/cnmallocator/networkallocator.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,18 @@ type initializer struct {
8686
ntype string
8787
}
8888

89+
// NetworkConfig is used to store network related cluster config in the Manager.
90+
type NetworkConfig struct {
91+
// DefaultAddrPool specifies default subnet pool for global scope networks
92+
DefaultAddrPool []string
93+
94+
// SubnetSize specifies the subnet size of the networks created from
95+
// the default subnet pool
96+
SubnetSize uint32
97+
}
98+
8999
// New returns a new NetworkAllocator handle
90-
func New(pg plugingetter.PluginGetter, defaultAddrPool []*net.IPNet, subnetSize int) (networkallocator.NetworkAllocator, error) {
100+
func New(pg plugingetter.PluginGetter, netConfig *NetworkConfig) (networkallocator.NetworkAllocator, error) {
91101
na := &cnmNetworkAllocator{
92102
networks: make(map[string]*network),
93103
services: make(map[string]struct{}),
@@ -106,7 +116,7 @@ func New(pg plugingetter.PluginGetter, defaultAddrPool []*net.IPNet, subnetSize
106116
return nil, err
107117
}
108118

109-
if err = initIPAMDrivers(reg, defaultAddrPool, subnetSize); err != nil {
119+
if err = initIPAMDrivers(reg, netConfig); err != nil {
110120
return nil, err
111121
}
112122

manager/allocator/cnmallocator/networkallocator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
func newNetworkAllocator(t *testing.T) networkallocator.NetworkAllocator {
16-
na, err := New(nil, nil, 0)
16+
na, err := New(nil, nil)
1717
assert.NoError(t, err)
1818
assert.NotNil(t, na)
1919
return na

manager/allocator/network.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,15 @@ type networkContext struct {
6868
}
6969

7070
func (a *Allocator) doNetworkInit(ctx context.Context) (err error) {
71-
na, err := cnmallocator.New(a.pluginGetter, a.defaultAddrPool, a.subnetSize)
71+
var netConfig *cnmallocator.NetworkConfig
72+
if a.networkConfig != nil && a.networkConfig.DefaultAddrPool != nil {
73+
netConfig = &cnmallocator.NetworkConfig{
74+
DefaultAddrPool: a.networkConfig.DefaultAddrPool,
75+
SubnetSize: a.networkConfig.SubnetSize,
76+
}
77+
}
78+
79+
na, err := cnmallocator.New(a.pluginGetter, netConfig)
7280
if err != nil {
7381
return err
7482
}

manager/controlapi/cluster.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ const (
1919
// expiredCertGrace is the amount of time to keep a node in the
2020
// blacklist beyond its certificate expiration timestamp.
2121
expiredCertGrace = 24 * time.Hour * 7
22+
// inbuilt default subnet size
23+
inbuiltSubnetSize = 24
24+
)
25+
26+
var (
27+
// inbuilt default address pool
28+
inbuiltDefaultAddressPool = []string{"10.0.0.0/8"}
2229
)
2330

2431
func validateClusterSpec(spec *api.ClusterSpec) error {
@@ -268,6 +275,12 @@ func redactClusters(clusters []*api.Cluster) []*api.Cluster {
268275
DefaultAddressPool: cluster.DefaultAddressPool,
269276
SubnetSize: cluster.SubnetSize,
270277
}
278+
if newCluster.DefaultAddressPool == nil {
279+
// This is just for CLI display. Set the inbuilt default pool for
280+
// user reference.
281+
newCluster.DefaultAddressPool = inbuiltDefaultAddressPool
282+
newCluster.SubnetSize = inbuiltSubnetSize
283+
}
271284
redactedClusters = append(redactedClusters, newCluster)
272285
}
273286

manager/manager.go

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/docker/swarmkit/identity"
2121
"github.com/docker/swarmkit/log"
2222
"github.com/docker/swarmkit/manager/allocator"
23+
"github.com/docker/swarmkit/manager/allocator/cnmallocator"
2324
"github.com/docker/swarmkit/manager/allocator/networkallocator"
2425
"github.com/docker/swarmkit/manager/controlapi"
2526
"github.com/docker/swarmkit/manager/dispatcher"
@@ -127,12 +128,8 @@ type Config struct {
127128
// FIPS setting.
128129
FIPS bool
129130

130-
// DefaultAddrPool specifies default subnet pool for global scope networks
131-
DefaultAddrPool []*net.IPNet
132-
133-
// SubnetSize specifies the subnet size of the networks created from
134-
// the default subnet pool
135-
SubnetSize int
131+
// NetworkConfig stores network related config for the cluster
132+
NetworkConfig *cnmallocator.NetworkConfig
136133
}
137134

138135
// Manager is the cluster manager for Swarm.
@@ -947,14 +944,10 @@ func (m *Manager) becomeLeader(ctx context.Context) {
947944
nil,
948945
0)
949946

950-
var defaultAddrPool []string
951-
for _, p := range m.config.DefaultAddrPool {
952-
defaultAddrPool = append(defaultAddrPool, p.String())
953-
}
954947
// If defaultAddrPool is valid we update cluster object with new value
955-
if defaultAddrPool != nil {
956-
clusterObj.DefaultAddressPool = defaultAddrPool
957-
clusterObj.SubnetSize = uint32(m.config.SubnetSize)
948+
if m.config.NetworkConfig != nil && m.config.NetworkConfig.DefaultAddrPool != nil {
949+
clusterObj.DefaultAddressPool = m.config.NetworkConfig.DefaultAddrPool
950+
clusterObj.SubnetSize = m.config.NetworkConfig.SubnetSize
958951
}
959952

960953
err := store.CreateCluster(tx, clusterObj)
@@ -1003,24 +996,20 @@ func (m *Manager) becomeLeader(ctx context.Context) {
1003996

1004997
// If DefaultAddrPool is null, Read from store and check if
1005998
// DefaultAddrPool info is stored in cluster object
1006-
if m.config.DefaultAddrPool == nil {
999+
if m.config.NetworkConfig == nil || m.config.NetworkConfig.DefaultAddrPool == nil {
10071000
var cluster *api.Cluster
10081001
s.View(func(tx store.ReadTx) {
10091002
cluster = store.GetCluster(tx, clusterID)
10101003
})
10111004
if cluster.DefaultAddressPool != nil {
10121005
for _, address := range cluster.DefaultAddressPool {
1013-
_, b, err := net.ParseCIDR(address)
1014-
if err != nil {
1015-
log.G(ctx).WithError(err).Error("Default Address Pool reading failed for cluster object %s", address)
1016-
}
1017-
m.config.DefaultAddrPool = append(m.config.DefaultAddrPool, b)
1006+
m.config.NetworkConfig.DefaultAddrPool = append(m.config.NetworkConfig.DefaultAddrPool, address)
10181007
}
1008+
m.config.NetworkConfig.SubnetSize = cluster.SubnetSize
10191009
}
1020-
m.config.SubnetSize = int(cluster.SubnetSize)
10211010
}
10221011

1023-
m.allocator, err = allocator.New(s, m.config.PluginGetter, m.config.DefaultAddrPool, m.config.SubnetSize)
1012+
m.allocator, err = allocator.New(s, m.config.PluginGetter, m.config.NetworkConfig)
10241013
if err != nil {
10251014
log.G(ctx).WithError(err).Error("failed to create allocator")
10261015
// TODO(stevvooe): It doesn't seem correct here to fail
@@ -1144,7 +1133,7 @@ func defaultClusterObject(
11441133
rootCA *ca.RootCA,
11451134
fips bool,
11461135
defaultAddressPool []string,
1147-
subnetSize int) *api.Cluster {
1136+
subnetSize uint32) *api.Cluster {
11481137
var caKey []byte
11491138
if rcaSigner, err := rootCA.Signer(); err == nil {
11501139
caKey = rcaSigner.Key
@@ -1178,7 +1167,7 @@ func defaultClusterObject(
11781167
UnlockKeys: initialUnlockKeys,
11791168
FIPS: fips,
11801169
DefaultAddressPool: defaultAddressPool,
1181-
SubnetSize: uint32(subnetSize),
1170+
SubnetSize: subnetSize,
11821171
}
11831172
}
11841173

node/node.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/docker/swarmkit/ioutils"
2929
"github.com/docker/swarmkit/log"
3030
"github.com/docker/swarmkit/manager"
31+
"github.com/docker/swarmkit/manager/allocator/cnmallocator"
3132
"github.com/docker/swarmkit/manager/encryption"
3233
"github.com/docker/swarmkit/remotes"
3334
"github.com/docker/swarmkit/xnet"
@@ -105,12 +106,8 @@ type Config struct {
105106
// for connections to the remote API (including the raft service).
106107
AdvertiseRemoteAPI string
107108

108-
// DefaultAddrPool specifies default subnet pool for global scope networks
109-
DefaultAddrPool []*net.IPNet
110-
111-
// SubnetSize specifies the subnet size of the networks created from
112-
// the default subnet pool
113-
SubnetSize int
109+
// NetworkConfig stores network related config for the cluster
110+
NetworkConfig *cnmallocator.NetworkConfig
114111

115112
// Executor specifies the executor to use for the agent.
116113
Executor exec.Executor
@@ -1002,8 +999,7 @@ func (n *Node) runManager(ctx context.Context, securityConfig *ca.SecurityConfig
1002999
PluginGetter: n.config.PluginGetter,
10031000
RootCAPaths: rootPaths,
10041001
FIPS: n.config.FIPS,
1005-
DefaultAddrPool: n.config.DefaultAddrPool,
1006-
SubnetSize: n.config.SubnetSize,
1002+
NetworkConfig: n.config.NetworkConfig,
10071003
})
10081004
if err != nil {
10091005
return false, err

0 commit comments

Comments
 (0)