Skip to content

Commit 142b522

Browse files
committed
libnetwork/config: remove vestiges of global scope
Without (*Controller).ReloadConfiguration, the only way to configure datastore scopes would be by passing config.Options to libnetwork.New. The only options defined which relate to datastore scopes are limited to configuring the local-scope datastore. Furthermore, the default datastore config only defines configuration for the local-scope datastore. The local-scope datastore is therefore the only datastore scope possible in libnetwork. Start removing code which is only needed to support multiple datastore scopes. Signed-off-by: Cory Snider <csnider@mirantis.com>
1 parent 52d9883 commit 142b522

File tree

9 files changed

+53
-108
lines changed

9 files changed

+53
-108
lines changed

libnetwork/bitseq/sequence_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func randomLocalStore() (datastore.DataStore, error) {
2929
if err := tmp.Close(); err != nil {
3030
return nil, fmt.Errorf("Error closing temp file: %v", err)
3131
}
32-
return datastore.NewDataStore(datastore.LocalScope, &datastore.ScopeCfg{
32+
return datastore.NewDataStore(datastore.ScopeCfg{
3333
Client: datastore.ScopeClientCfg{
3434
Provider: "boltdb",
3535
Address: filepath.Join(defaultPrefix, filepath.Base(tmp.Name())),

libnetwork/config/config.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type Config struct {
2929
ClusterProvider cluster.Provider
3030
NetworkControlPlaneMTU int
3131
DefaultAddressPool []*ipamutils.NetworkToSplit
32-
Scopes map[string]*datastore.ScopeCfg
32+
Scope datastore.ScopeCfg
3333
ActiveSandboxes map[string]interface{}
3434
PluginGetter plugingetter.PluginGetter
3535
}
@@ -38,7 +38,6 @@ type Config struct {
3838
func New(opts ...Option) *Config {
3939
cfg := &Config{
4040
DriverCfg: make(map[string]interface{}),
41-
Scopes: make(map[string]*datastore.ScopeCfg),
4241
}
4342

4443
for _, opt := range opts {
@@ -48,10 +47,8 @@ func New(opts ...Option) *Config {
4847
}
4948

5049
// load default scope configs which don't have explicit user specified configs.
51-
for k, v := range datastore.DefaultScopes(cfg.DataDir) {
52-
if _, ok := cfg.Scopes[k]; !ok {
53-
cfg.Scopes[k] = v
54-
}
50+
if cfg.Scope == (datastore.ScopeCfg{}) {
51+
cfg.Scope = datastore.DefaultScope(cfg.DataDir)
5552
}
5653
return cfg
5754
}
@@ -147,32 +144,23 @@ func IsValidName(name string) bool {
147144
func OptionLocalKVProvider(provider string) Option {
148145
return func(c *Config) {
149146
logrus.Debugf("Option OptionLocalKVProvider: %s", provider)
150-
if _, ok := c.Scopes[datastore.LocalScope]; !ok {
151-
c.Scopes[datastore.LocalScope] = &datastore.ScopeCfg{}
152-
}
153-
c.Scopes[datastore.LocalScope].Client.Provider = strings.TrimSpace(provider)
147+
c.Scope.Client.Provider = strings.TrimSpace(provider)
154148
}
155149
}
156150

157151
// OptionLocalKVProviderURL function returns an option setter for kvstore url
158152
func OptionLocalKVProviderURL(url string) Option {
159153
return func(c *Config) {
160154
logrus.Debugf("Option OptionLocalKVProviderURL: %s", url)
161-
if _, ok := c.Scopes[datastore.LocalScope]; !ok {
162-
c.Scopes[datastore.LocalScope] = &datastore.ScopeCfg{}
163-
}
164-
c.Scopes[datastore.LocalScope].Client.Address = strings.TrimSpace(url)
155+
c.Scope.Client.Address = strings.TrimSpace(url)
165156
}
166157
}
167158

168159
// OptionLocalKVProviderConfig function returns an option setter for kvstore config
169160
func OptionLocalKVProviderConfig(config *store.Config) Option {
170161
return func(c *Config) {
171162
logrus.Debugf("Option OptionLocalKVProviderConfig: %v", config)
172-
if _, ok := c.Scopes[datastore.LocalScope]; !ok {
173-
c.Scopes[datastore.LocalScope] = &datastore.ScopeCfg{}
174-
}
175-
c.Scopes[datastore.LocalScope].Client.Config = config
163+
c.Scope.Client.Config = config
176164
}
177165
}
178166

libnetwork/controller.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -353,15 +353,14 @@ func (c *Controller) makeDriverConfig(ntype string) map[string]interface{} {
353353
}
354354
}
355355

356-
for k, v := range c.cfg.Scopes {
357-
if !v.IsValid() {
358-
continue
359-
}
360-
cfg[netlabel.MakeKVClient(k)] = discoverapi.DatastoreConfigData{
361-
Scope: k,
362-
Provider: v.Client.Provider,
363-
Address: v.Client.Address,
364-
Config: v.Client.Config,
356+
if c.cfg.Scope.IsValid() {
357+
// FIXME: every driver instance constructs a new DataStore
358+
// instance against the same database. Yikes!
359+
cfg[netlabel.LocalKVClient] = discoverapi.DatastoreConfigData{
360+
Scope: datastore.LocalScope,
361+
Provider: c.cfg.Scope.Client.Provider,
362+
Address: c.cfg.Scope.Client.Address,
363+
Config: c.cfg.Scope.Client.Config,
365364
}
366365
}
367366

libnetwork/datastore/datastore.go

Lines changed: 22 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -129,37 +129,28 @@ const (
129129
EndpointKeyPrefix = "endpoint"
130130
)
131131

132-
var (
133-
defaultScopes = makeDefaultScopes()
134-
)
132+
var defaultRootChain = []string{"docker", "network", "v1.0"}
133+
var rootChain = defaultRootChain
134+
135+
// DefaultScope returns a default scope config for clients to use.
136+
func DefaultScope(dataDir string) ScopeCfg {
137+
var dbpath string
138+
if dataDir == "" {
139+
dbpath = defaultPrefix + "/local-kv.db"
140+
} else {
141+
dbpath = dataDir + "/network/files/local-kv.db"
142+
}
135143

136-
func makeDefaultScopes() map[string]*ScopeCfg {
137-
def := make(map[string]*ScopeCfg)
138-
def[LocalScope] = &ScopeCfg{
144+
return ScopeCfg{
139145
Client: ScopeClientCfg{
140146
Provider: string(store.BOLTDB),
141-
Address: defaultPrefix + "/local-kv.db",
147+
Address: dbpath,
142148
Config: &store.Config{
143149
Bucket: "libnetwork",
144150
ConnectionTimeout: time.Minute,
145151
},
146152
},
147153
}
148-
149-
return def
150-
}
151-
152-
var defaultRootChain = []string{"docker", "network", "v1.0"}
153-
var rootChain = defaultRootChain
154-
155-
// DefaultScopes returns a map of default scopes and its config for clients to use.
156-
func DefaultScopes(dataDir string) map[string]*ScopeCfg {
157-
s := makeDefaultScopes()
158-
if dataDir != "" {
159-
s[LocalScope].Client.Address = dataDir + "/network/files/local-kv.db"
160-
}
161-
162-
return s
163154
}
164155

165156
// IsValid checks if the scope config has valid configuration.
@@ -192,16 +183,7 @@ func ParseKey(key string) ([]string, error) {
192183
}
193184

194185
// newClient used to connect to KV Store
195-
func newClient(scope string, kv string, addr string, config *store.Config, cached bool) (DataStore, error) {
196-
197-
if cached && scope != LocalScope {
198-
return nil, fmt.Errorf("caching supported only for scope %s", LocalScope)
199-
}
200-
sequential := false
201-
if scope == LocalScope {
202-
sequential = true
203-
}
204-
186+
func newClient(kv string, addr string, config *store.Config) (DataStore, error) {
205187
if config == nil {
206188
config = &store.Config{}
207189
}
@@ -227,31 +209,19 @@ func newClient(scope string, kv string, addr string, config *store.Config, cache
227209
return nil, err
228210
}
229211

230-
ds := &datastore{scope: scope, store: s, active: true, watchCh: make(chan struct{}), sequential: sequential}
231-
if cached {
232-
ds.cache = newCache(ds)
233-
}
212+
ds := &datastore{scope: LocalScope, store: s, active: true, watchCh: make(chan struct{}), sequential: true}
213+
ds.cache = newCache(ds)
234214

235215
return ds, nil
236216
}
237217

238218
// NewDataStore creates a new instance of LibKV data store
239-
func NewDataStore(scope string, cfg *ScopeCfg) (DataStore, error) {
240-
if cfg == nil || cfg.Client.Provider == "" || cfg.Client.Address == "" {
241-
c, ok := defaultScopes[scope]
242-
if !ok || c.Client.Provider == "" || c.Client.Address == "" {
243-
return nil, fmt.Errorf("unexpected scope %s without configuration passed", scope)
244-
}
245-
246-
cfg = c
247-
}
248-
249-
var cached bool
250-
if scope == LocalScope {
251-
cached = true
219+
func NewDataStore(cfg ScopeCfg) (DataStore, error) {
220+
if cfg.Client.Provider == "" || cfg.Client.Address == "" {
221+
cfg = DefaultScope("")
252222
}
253223

254-
return newClient(scope, cfg.Client.Provider, cfg.Client.Address, cfg.Client.Config, cached)
224+
return newClient(cfg.Client.Provider, cfg.Client.Address, cfg.Client.Config)
255225
}
256226

257227
// NewDataStoreFromConfig creates a new instance of LibKV data store starting from the datastore config data
@@ -266,15 +236,15 @@ func NewDataStoreFromConfig(dsc discoverapi.DatastoreConfigData) (DataStore, err
266236
return nil, fmt.Errorf("cannot parse store configuration: %v", dsc.Config)
267237
}
268238

269-
scopeCfg := &ScopeCfg{
239+
scopeCfg := ScopeCfg{
270240
Client: ScopeClientCfg{
271241
Address: dsc.Address,
272242
Provider: dsc.Provider,
273243
Config: sCfgP,
274244
},
275245
}
276246

277-
ds, err := NewDataStore(dsc.Scope, scopeCfg)
247+
ds, err := NewDataStore(scopeCfg)
278248
if err != nil {
279249
return nil, fmt.Errorf("failed to construct datastore client from datastore configuration %v: %v", dsc, err)
280250
}

libnetwork/datastore/datastore_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,13 @@ func TestParseKey(t *testing.T) {
3636
}
3737

3838
func TestInvalidDataStore(t *testing.T) {
39-
config := &ScopeCfg{}
40-
config.Client.Provider = "invalid"
41-
config.Client.Address = "localhost:8500"
42-
_, err := NewDataStore(GlobalScope, config)
39+
config := ScopeCfg{
40+
Client: ScopeClientCfg{
41+
Provider: "invalid",
42+
Address: "localhost:8500",
43+
},
44+
}
45+
_, err := NewDataStore(config)
4346
if err == nil {
4447
t.Fatal("Invalid Datastore connection configuration must result in a failure")
4548
}

libnetwork/ipam/allocator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func randomLocalStore(needStore bool) (datastore.DataStore, error) {
4545
if err := tmp.Close(); err != nil {
4646
return nil, fmt.Errorf("Error closing temp file: %v", err)
4747
}
48-
return datastore.NewDataStore(datastore.LocalScope, &datastore.ScopeCfg{
48+
return datastore.NewDataStore(datastore.ScopeCfg{
4949
Client: datastore.ScopeClientCfg{
5050
Provider: "boltdb",
5151
Address: filepath.Join(defaultPrefix, filepath.Base(tmp.Name())),

libnetwork/libnetwork_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestMain(m *testing.M) {
3737
}
3838

3939
// Cleanup local datastore file
40-
_ = os.Remove(datastore.DefaultScopes("")[datastore.LocalScope].Client.Address)
40+
_ = os.Remove(datastore.DefaultScope("").Client.Address)
4141

4242
os.Exit(m.Run())
4343
}

libnetwork/store.go

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,20 @@ func registerKVStores() {
1313
boltdb.Register()
1414
}
1515

16-
func (c *Controller) initScopedStore(scope string, scfg *datastore.ScopeCfg) error {
17-
store, err := datastore.NewDataStore(scope, scfg)
18-
if err != nil {
19-
return err
20-
}
21-
c.mu.Lock()
22-
c.stores = append(c.stores, store)
23-
c.mu.Unlock()
24-
25-
return nil
26-
}
27-
2816
func (c *Controller) initStores() error {
2917
registerKVStores()
3018

3119
c.mu.Lock()
20+
defer c.mu.Unlock()
21+
3222
if c.cfg == nil {
33-
c.mu.Unlock()
3423
return nil
3524
}
36-
scopeConfigs := c.cfg.Scopes
37-
c.stores = nil
38-
c.mu.Unlock()
39-
40-
for scope, scfg := range scopeConfigs {
41-
if err := c.initScopedStore(scope, scfg); err != nil {
42-
return err
43-
}
25+
store, err := datastore.NewDataStore(c.cfg.Scope)
26+
if err != nil {
27+
return err
4428
}
29+
c.stores = []datastore.DataStore{store}
4530

4631
c.startWatch()
4732
return nil

libnetwork/store_linux_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func TestBoltdbBackend(t *testing.T) {
12-
defer os.Remove(datastore.DefaultScopes("")[datastore.LocalScope].Client.Address)
12+
defer os.Remove(datastore.DefaultScope("").Client.Address)
1313
testLocalBackend(t, "", "", nil)
1414
defer os.Remove("/tmp/boltdb.db")
1515
config := &store.Config{Bucket: "testBackend"}

0 commit comments

Comments
 (0)