Browse code

libnetwork: don't embed mutex in controller

Embedded structs are part of the exported surface of a struct type.
Boxing a struct value into an interface value does not erase that;
any code could gain access to the embedded struct value with a simple
type assertion. The mutex is supposed to be a private implementation
detail, but *controller implements sync.Locker because the mutex is
embedded.

c, _ := libnetwork.New()
c.(sync.Locker).Lock()

Change the mutex to an unexported field so *controller no longer
spuriously implements the sync.Locker interface.

Signed-off-by: Cory Snider <csnider@mirantis.com>

Cory Snider authored on 2023/01/12 05:56:50
Showing 9 changed files
... ...
@@ -108,7 +108,7 @@ func (c *controller) handleKeyChange(keys []*types.EncryptionKey) error {
108 108
 
109 109
 	// Find the deleted key. If the deleted key was the primary key,
110 110
 	// a new primary key should be set before removing if from keyring.
111
-	c.Lock()
111
+	c.mu.Lock()
112 112
 	added := []byte{}
113 113
 	deleted := []byte{}
114 114
 	j := len(c.keys)
... ...
@@ -157,7 +157,7 @@ func (c *controller) handleKeyChange(keys []*types.EncryptionKey) error {
157 157
 			}
158 158
 		}
159 159
 	}
160
-	c.Unlock()
160
+	c.mu.Unlock()
161 161
 
162 162
 	if len(added) > 0 {
163 163
 		a.networkDB.SetKey(added)
... ...
@@ -249,8 +249,8 @@ func (c *controller) agentSetup(clusterProvider cluster.Provider) error {
249 249
 // For a given subsystem getKeys sorts the keys by lamport time and returns
250 250
 // slice of keys and lamport time which can used as a unique tag for the keys
251 251
 func (c *controller) getKeys(subsys string) ([][]byte, []uint64) {
252
-	c.Lock()
253
-	defer c.Unlock()
252
+	c.mu.Lock()
253
+	defer c.mu.Unlock()
254 254
 
255 255
 	sort.Sort(ByTime(c.keys))
256 256
 
... ...
@@ -271,8 +271,8 @@ func (c *controller) getKeys(subsys string) ([][]byte, []uint64) {
271 271
 // getPrimaryKeyTag returns the primary key for a given subsystem from the
272 272
 // list of sorted key and the associated tag
273 273
 func (c *controller) getPrimaryKeyTag(subsys string) ([]byte, uint64, error) {
274
-	c.Lock()
275
-	defer c.Unlock()
274
+	c.mu.Lock()
275
+	defer c.mu.Unlock()
276 276
 	sort.Sort(ByTime(c.keys))
277 277
 	keys := []*types.EncryptionKey{}
278 278
 	for _, key := range c.keys {
... ...
@@ -316,7 +316,7 @@ func (c *controller) agentInit(listenAddr, bindAddrOrInterface, advertiseAddr, d
316 316
 	nodeCh, cancel := nDB.Watch(networkdb.NodeTable, "", "")
317 317
 	cancelList = append(cancelList, cancel)
318 318
 
319
-	c.Lock()
319
+	c.mu.Lock()
320 320
 	c.agent = &agent{
321 321
 		networkDB:         nDB,
322 322
 		bindAddr:          bindAddr,
... ...
@@ -325,7 +325,7 @@ func (c *controller) agentInit(listenAddr, bindAddrOrInterface, advertiseAddr, d
325 325
 		coreCancelFuncs:   cancelList,
326 326
 		driverCancelFuncs: make(map[string][]func()),
327 327
 	}
328
-	c.Unlock()
328
+	c.mu.Unlock()
329 329
 
330 330
 	go c.handleTableEvents(ch, c.handleEpTableEvent)
331 331
 	go c.handleTableEvents(nodeCh, c.handleNodeTableEvent)
... ...
@@ -383,10 +383,10 @@ func (c *controller) agentDriverNotify(d driverapi.Driver) {
383 383
 func (c *controller) agentClose() {
384 384
 	// Acquire current agent instance and reset its pointer
385 385
 	// then run closing functions
386
-	c.Lock()
386
+	c.mu.Lock()
387 387
 	agent := c.agent
388 388
 	c.agent = nil
389
-	c.Unlock()
389
+	c.mu.Unlock()
390 390
 
391 391
 	// when the agent is closed the cluster provider should be cleaned up
392 392
 	c.SetClusterProvider(nil)
... ...
@@ -174,7 +174,7 @@ type controller struct {
174 174
 	agentStopDone    chan struct{}
175 175
 	keys             []*types.EncryptionKey
176 176
 	DiagnosticServer *diagnostic.Server
177
-	sync.Mutex
177
+	mu               sync.Mutex
178 178
 }
179 179
 
180 180
 type initializer struct {
... ...
@@ -247,7 +247,7 @@ func New(cfgOptions ...config.Option) (NetworkController, error) {
247 247
 
248 248
 func (c *controller) SetClusterProvider(provider cluster.Provider) {
249 249
 	var sameProvider bool
250
-	c.Lock()
250
+	c.mu.Lock()
251 251
 	// Avoids to spawn multiple goroutine for the same cluster provider
252 252
 	if c.cfg.ClusterProvider == provider {
253 253
 		// If the cluster provider is already set, there is already a go routine spawned
... ...
@@ -256,7 +256,7 @@ func (c *controller) SetClusterProvider(provider cluster.Provider) {
256 256
 	} else {
257 257
 		c.cfg.ClusterProvider = provider
258 258
 	}
259
-	c.Unlock()
259
+	c.mu.Unlock()
260 260
 
261 261
 	if provider == nil || sameProvider {
262 262
 		return
... ...
@@ -284,17 +284,17 @@ func (c *controller) SetKeys(keys []*types.EncryptionKey) error {
284 284
 	}
285 285
 
286 286
 	if c.getAgent() == nil {
287
-		c.Lock()
287
+		c.mu.Lock()
288 288
 		c.keys = keys
289
-		c.Unlock()
289
+		c.mu.Unlock()
290 290
 		return nil
291 291
 	}
292 292
 	return c.handleKeyChange(keys)
293 293
 }
294 294
 
295 295
 func (c *controller) getAgent() *agent {
296
-	c.Lock()
297
-	defer c.Unlock()
296
+	c.mu.Lock()
297
+	defer c.mu.Unlock()
298 298
 	return c.agent
299 299
 }
300 300
 
... ...
@@ -309,9 +309,9 @@ func (c *controller) clusterAgentInit() {
309 309
 		case cluster.EventNetworkKeysAvailable:
310 310
 			// Validates that the keys are actually available before starting the initialization
311 311
 			// This will handle old spurious messages left on the channel
312
-			c.Lock()
312
+			c.mu.Lock()
313 313
 			keysAvailable = c.keys != nil
314
-			c.Unlock()
314
+			c.mu.Unlock()
315 315
 			fallthrough
316 316
 		case cluster.EventSocketChange, cluster.EventNodeReady:
317 317
 			if keysAvailable && !c.isDistributedControl() {
... ...
@@ -324,9 +324,9 @@ func (c *controller) clusterAgentInit() {
324 324
 			}
325 325
 		case cluster.EventNodeLeave:
326 326
 			c.agentOperationStart()
327
-			c.Lock()
327
+			c.mu.Lock()
328 328
 			c.keys = nil
329
-			c.Unlock()
329
+			c.mu.Unlock()
330 330
 
331 331
 			// We are leaving the cluster. Make sure we
332 332
 			// close the gossip so that we stop all
... ...
@@ -348,9 +348,9 @@ func (c *controller) clusterAgentInit() {
348 348
 
349 349
 // AgentInitWait waits for agent initialization to be completed in the controller.
350 350
 func (c *controller) AgentInitWait() {
351
-	c.Lock()
351
+	c.mu.Lock()
352 352
 	agentInitDone := c.agentInitDone
353
-	c.Unlock()
353
+	c.mu.Unlock()
354 354
 
355 355
 	if agentInitDone != nil {
356 356
 		<-agentInitDone
... ...
@@ -359,9 +359,9 @@ func (c *controller) AgentInitWait() {
359 359
 
360 360
 // AgentStopWait waits for the Agent stop to be completed in the controller
361 361
 func (c *controller) AgentStopWait() {
362
-	c.Lock()
362
+	c.mu.Lock()
363 363
 	agentStopDone := c.agentStopDone
364
-	c.Unlock()
364
+	c.mu.Unlock()
365 365
 	if agentStopDone != nil {
366 366
 		<-agentStopDone
367 367
 	}
... ...
@@ -369,34 +369,34 @@ func (c *controller) AgentStopWait() {
369 369
 
370 370
 // agentOperationStart marks the start of an Agent Init or Agent Stop
371 371
 func (c *controller) agentOperationStart() {
372
-	c.Lock()
372
+	c.mu.Lock()
373 373
 	if c.agentInitDone == nil {
374 374
 		c.agentInitDone = make(chan struct{})
375 375
 	}
376 376
 	if c.agentStopDone == nil {
377 377
 		c.agentStopDone = make(chan struct{})
378 378
 	}
379
-	c.Unlock()
379
+	c.mu.Unlock()
380 380
 }
381 381
 
382 382
 // agentInitComplete notifies the successful completion of the Agent initialization
383 383
 func (c *controller) agentInitComplete() {
384
-	c.Lock()
384
+	c.mu.Lock()
385 385
 	if c.agentInitDone != nil {
386 386
 		close(c.agentInitDone)
387 387
 		c.agentInitDone = nil
388 388
 	}
389
-	c.Unlock()
389
+	c.mu.Unlock()
390 390
 }
391 391
 
392 392
 // agentStopComplete notifies the successful completion of the Agent stop
393 393
 func (c *controller) agentStopComplete() {
394
-	c.Lock()
394
+	c.mu.Lock()
395 395
 	if c.agentStopDone != nil {
396 396
 		close(c.agentStopDone)
397 397
 		c.agentStopDone = nil
398 398
 	}
399
-	c.Unlock()
399
+	c.mu.Unlock()
400 400
 }
401 401
 
402 402
 func (c *controller) makeDriverConfig(ntype string) map[string]interface{} {
... ...
@@ -469,9 +469,9 @@ func (c *controller) ReloadConfiguration(cfgOptions ...config.Option) error {
469 469
 		return nil
470 470
 	}
471 471
 
472
-	c.Lock()
472
+	c.mu.Lock()
473 473
 	c.cfg = cfg
474
-	c.Unlock()
474
+	c.mu.Unlock()
475 475
 
476 476
 	var dsConfig *discoverapi.DatastoreConfigData
477 477
 	for scope, sCfg := range cfg.Scopes {
... ...
@@ -567,8 +567,8 @@ func (c *controller) pushNodeDiscovery(d driverapi.Driver, cap driverapi.Capabil
567 567
 }
568 568
 
569 569
 func (c *controller) Config() config.Config {
570
-	c.Lock()
571
-	defer c.Unlock()
570
+	c.mu.Lock()
571
+	defer c.mu.Unlock()
572 572
 	if c.cfg == nil {
573 573
 		return config.Config{}
574 574
 	}
... ...
@@ -576,8 +576,8 @@ func (c *controller) Config() config.Config {
576 576
 }
577 577
 
578 578
 func (c *controller) isManager() bool {
579
-	c.Lock()
580
-	defer c.Unlock()
579
+	c.mu.Lock()
580
+	defer c.mu.Unlock()
581 581
 	if c.cfg == nil || c.cfg.ClusterProvider == nil {
582 582
 		return false
583 583
 	}
... ...
@@ -585,8 +585,8 @@ func (c *controller) isManager() bool {
585 585
 }
586 586
 
587 587
 func (c *controller) isAgent() bool {
588
-	c.Lock()
589
-	defer c.Unlock()
588
+	c.mu.Lock()
589
+	defer c.mu.Unlock()
590 590
 	if c.cfg == nil || c.cfg.ClusterProvider == nil {
591 591
 		return false
592 592
 	}
... ...
@@ -811,9 +811,9 @@ addToStore:
811 811
 	}
812 812
 
813 813
 	if !c.isDistributedControl() {
814
-		c.Lock()
814
+		c.mu.Lock()
815 815
 		arrangeIngressFilterRule()
816
-		c.Unlock()
816
+		c.mu.Unlock()
817 817
 	}
818 818
 	arrangeUserFilterRule()
819 819
 
... ...
@@ -985,13 +985,13 @@ func (c *controller) NewSandbox(containerID string, options ...SandboxOption) (S
985 985
 	}
986 986
 
987 987
 	var sb *sandbox
988
-	c.Lock()
988
+	c.mu.Lock()
989 989
 	for _, s := range c.sandboxes {
990 990
 		if s.containerID == containerID {
991 991
 			// If not a stub, then we already have a complete sandbox.
992 992
 			if !s.isStub {
993 993
 				sbID := s.ID()
994
-				c.Unlock()
994
+				c.mu.Unlock()
995 995
 				return nil, types.ForbiddenErrorf("container %s is already present in sandbox %s", containerID, sbID)
996 996
 			}
997 997
 
... ...
@@ -1004,7 +1004,7 @@ func (c *controller) NewSandbox(containerID string, options ...SandboxOption) (S
1004 1004
 			break
1005 1005
 		}
1006 1006
 	}
1007
-	c.Unlock()
1007
+	c.mu.Unlock()
1008 1008
 
1009 1009
 	sandboxID := stringid.GenerateRandomID()
1010 1010
 	if runtime.GOOS == "windows" {
... ...
@@ -1027,9 +1027,9 @@ func (c *controller) NewSandbox(containerID string, options ...SandboxOption) (S
1027 1027
 
1028 1028
 	sb.processOptions(options...)
1029 1029
 
1030
-	c.Lock()
1030
+	c.mu.Lock()
1031 1031
 	if sb.ingress && c.ingressSandbox != nil {
1032
-		c.Unlock()
1032
+		c.mu.Unlock()
1033 1033
 		return nil, types.ForbiddenErrorf("ingress sandbox already present")
1034 1034
 	}
1035 1035
 
... ...
@@ -1041,16 +1041,16 @@ func (c *controller) NewSandbox(containerID string, options ...SandboxOption) (S
1041 1041
 	} else if sb.loadBalancerNID != "" {
1042 1042
 		sb.id = "lb_" + sb.loadBalancerNID
1043 1043
 	}
1044
-	c.Unlock()
1044
+	c.mu.Unlock()
1045 1045
 
1046 1046
 	var err error
1047 1047
 	defer func() {
1048 1048
 		if err != nil {
1049
-			c.Lock()
1049
+			c.mu.Lock()
1050 1050
 			if sb.ingress {
1051 1051
 				c.ingressSandbox = nil
1052 1052
 			}
1053
-			c.Unlock()
1053
+			c.mu.Unlock()
1054 1054
 		}
1055 1055
 	}()
1056 1056
 
... ...
@@ -1090,14 +1090,14 @@ func (c *controller) NewSandbox(containerID string, options ...SandboxOption) (S
1090 1090
 		sb.osSbox.ApplyOSTweaks(sb.oslTypes)
1091 1091
 	}
1092 1092
 
1093
-	c.Lock()
1093
+	c.mu.Lock()
1094 1094
 	c.sandboxes[sb.id] = sb
1095
-	c.Unlock()
1095
+	c.mu.Unlock()
1096 1096
 	defer func() {
1097 1097
 		if err != nil {
1098
-			c.Lock()
1098
+			c.mu.Lock()
1099 1099
 			delete(c.sandboxes, sb.id)
1100
-			c.Unlock()
1100
+			c.mu.Unlock()
1101 1101
 		}
1102 1102
 	}()
1103 1103
 
... ...
@@ -1110,8 +1110,8 @@ func (c *controller) NewSandbox(containerID string, options ...SandboxOption) (S
1110 1110
 }
1111 1111
 
1112 1112
 func (c *controller) Sandboxes() []Sandbox {
1113
-	c.Lock()
1114
-	defer c.Unlock()
1113
+	c.mu.Lock()
1114
+	defer c.mu.Unlock()
1115 1115
 
1116 1116
 	list := make([]Sandbox, 0, len(c.sandboxes))
1117 1117
 	for _, s := range c.sandboxes {
... ...
@@ -1138,9 +1138,9 @@ func (c *controller) SandboxByID(id string) (Sandbox, error) {
1138 1138
 	if id == "" {
1139 1139
 		return nil, ErrInvalidID(id)
1140 1140
 	}
1141
-	c.Lock()
1141
+	c.mu.Lock()
1142 1142
 	s, ok := c.sandboxes[id]
1143
-	c.Unlock()
1143
+	c.mu.Unlock()
1144 1144
 	if !ok {
1145 1145
 		return nil, types.NotFoundErrorf("sandbox %s not found", id)
1146 1146
 	}
... ...
@@ -1150,14 +1150,14 @@ func (c *controller) SandboxByID(id string) (Sandbox, error) {
1150 1150
 // SandboxDestroy destroys a sandbox given a container ID
1151 1151
 func (c *controller) SandboxDestroy(id string) error {
1152 1152
 	var sb *sandbox
1153
-	c.Lock()
1153
+	c.mu.Lock()
1154 1154
 	for _, s := range c.sandboxes {
1155 1155
 		if s.containerID == id {
1156 1156
 			sb = s
1157 1157
 			break
1158 1158
 		}
1159 1159
 	}
1160
-	c.Unlock()
1160
+	c.mu.Unlock()
1161 1161
 
1162 1162
 	// It is not an error if sandbox is not available
1163 1163
 	if sb == nil {
... ...
@@ -1253,32 +1253,32 @@ func (c *controller) Stop() {
1253 1253
 
1254 1254
 // StartDiagnostic start the network dias mode
1255 1255
 func (c *controller) StartDiagnostic(port int) {
1256
-	c.Lock()
1256
+	c.mu.Lock()
1257 1257
 	if !c.DiagnosticServer.IsDiagnosticEnabled() {
1258 1258
 		c.DiagnosticServer.EnableDiagnostic("127.0.0.1", port)
1259 1259
 	}
1260
-	c.Unlock()
1260
+	c.mu.Unlock()
1261 1261
 }
1262 1262
 
1263 1263
 // StopDiagnostic start the network dias mode
1264 1264
 func (c *controller) StopDiagnostic() {
1265
-	c.Lock()
1265
+	c.mu.Lock()
1266 1266
 	if c.DiagnosticServer.IsDiagnosticEnabled() {
1267 1267
 		c.DiagnosticServer.DisableDiagnostic()
1268 1268
 	}
1269
-	c.Unlock()
1269
+	c.mu.Unlock()
1270 1270
 }
1271 1271
 
1272 1272
 // IsDiagnosticEnabled returns true if the dias is enabled
1273 1273
 func (c *controller) IsDiagnosticEnabled() bool {
1274
-	c.Lock()
1275
-	defer c.Unlock()
1274
+	c.mu.Lock()
1275
+	defer c.mu.Unlock()
1276 1276
 	return c.DiagnosticServer.IsDiagnosticEnabled()
1277 1277
 }
1278 1278
 
1279 1279
 func (c *controller) iptablesEnabled() bool {
1280
-	c.Lock()
1281
-	defer c.Unlock()
1280
+	c.mu.Lock()
1281
+	defer c.mu.Unlock()
1282 1282
 
1283 1283
 	if c.cfg == nil {
1284 1284
 		return false
... ...
@@ -616,9 +616,9 @@ func (ep *endpoint) rename(name string) error {
616 616
 			return types.InternalErrorf("Could not delete service state for endpoint %s from cluster on rename: %v", ep.Name(), err)
617 617
 		}
618 618
 	} else {
619
-		c.Lock()
619
+		c.mu.Lock()
620 620
 		netWatch, ok = c.nmap[n.ID()]
621
-		c.Unlock()
621
+		c.mu.Unlock()
622 622
 		if !ok {
623 623
 			return fmt.Errorf("watch null for network %q", n.Name())
624 624
 		}
... ...
@@ -898,9 +898,9 @@ func (ep *endpoint) getSandbox() (*sandbox, bool) {
898 898
 	sid := ep.sandboxID
899 899
 	ep.Unlock()
900 900
 
901
-	c.Lock()
901
+	c.mu.Lock()
902 902
 	ps, ok := c.sandboxes[sid]
903
-	c.Unlock()
903
+	c.mu.Unlock()
904 904
 
905 905
 	return ps, ok
906 906
 }
... ...
@@ -1049,9 +1049,9 @@ func JoinOptionPriority(prio int) EndpointOption {
1049 1049
 	return func(ep *endpoint) {
1050 1050
 		// ep lock already acquired
1051 1051
 		c := ep.network.getController()
1052
-		c.Lock()
1052
+		c.mu.Lock()
1053 1053
 		sb, ok := c.sandboxes[ep.sandboxID]
1054
-		c.Unlock()
1054
+		c.mu.Unlock()
1055 1055
 		if !ok {
1056 1056
 			logrus.Errorf("Could not set endpoint priority value during Join to endpoint %s: No sandbox id present in endpoint", ep.id)
1057 1057
 			return
... ...
@@ -1413,8 +1413,8 @@ func (n *network) addSvcRecords(eID, name, serviceID string, epIP, epIPv6 net.IP
1413 1413
 	logrus.Debugf("%s (%.7s).addSvcRecords(%s, %s, %s, %t) %s sid:%s", eID, networkID, name, epIP, epIPv6, ipMapUpdate, method, serviceID)
1414 1414
 
1415 1415
 	c := n.getController()
1416
-	c.Lock()
1417
-	defer c.Unlock()
1416
+	c.mu.Lock()
1417
+	defer c.mu.Unlock()
1418 1418
 
1419 1419
 	sr, ok := c.svcRecords[networkID]
1420 1420
 	if !ok {
... ...
@@ -1449,8 +1449,8 @@ func (n *network) deleteSvcRecords(eID, name, serviceID string, epIP net.IP, epI
1449 1449
 	logrus.Debugf("%s (%.7s).deleteSvcRecords(%s, %s, %s, %t) %s sid:%s ", eID, networkID, name, epIP, epIPv6, ipMapUpdate, method, serviceID)
1450 1450
 
1451 1451
 	c := n.getController()
1452
-	c.Lock()
1453
-	defer c.Unlock()
1452
+	c.mu.Lock()
1453
+	defer c.mu.Unlock()
1454 1454
 
1455 1455
 	sr, ok := c.svcRecords[networkID]
1456 1456
 	if !ok {
... ...
@@ -1484,8 +1484,8 @@ func (n *network) getSvcRecords(ep *endpoint) []etchosts.Record {
1484 1484
 
1485 1485
 	epName := ep.Name()
1486 1486
 
1487
-	n.ctrlr.Lock()
1488
-	defer n.ctrlr.Unlock()
1487
+	n.ctrlr.mu.Lock()
1488
+	defer n.ctrlr.mu.Unlock()
1489 1489
 	sr, ok := n.ctrlr.svcRecords[n.id]
1490 1490
 	if !ok || sr.svcMap == nil {
1491 1491
 		return nil
... ...
@@ -1980,8 +1980,8 @@ func (n *network) ResolveName(req string, ipType int) ([]net.IP, bool) {
1980 1980
 
1981 1981
 	c := n.getController()
1982 1982
 	networkID := n.ID()
1983
-	c.Lock()
1984
-	defer c.Unlock()
1983
+	c.mu.Lock()
1984
+	defer c.mu.Unlock()
1985 1985
 	sr, ok := c.svcRecords[networkID]
1986 1986
 
1987 1987
 	if !ok {
... ...
@@ -2022,8 +2022,8 @@ func (n *network) ResolveName(req string, ipType int) ([]net.IP, bool) {
2022 2022
 func (n *network) HandleQueryResp(name string, ip net.IP) {
2023 2023
 	networkID := n.ID()
2024 2024
 	c := n.getController()
2025
-	c.Lock()
2026
-	defer c.Unlock()
2025
+	c.mu.Lock()
2026
+	defer c.mu.Unlock()
2027 2027
 	sr, ok := c.svcRecords[networkID]
2028 2028
 
2029 2029
 	if !ok {
... ...
@@ -2042,8 +2042,8 @@ func (n *network) HandleQueryResp(name string, ip net.IP) {
2042 2042
 func (n *network) ResolveIP(ip string) string {
2043 2043
 	networkID := n.ID()
2044 2044
 	c := n.getController()
2045
-	c.Lock()
2046
-	defer c.Unlock()
2045
+	c.mu.Lock()
2046
+	defer c.mu.Unlock()
2047 2047
 	sr, ok := c.svcRecords[networkID]
2048 2048
 
2049 2049
 	if !ok {
... ...
@@ -2096,8 +2096,8 @@ func (n *network) ResolveService(name string) ([]*net.SRV, []net.IP) {
2096 2096
 	svcName := strings.Join(parts[2:], ".")
2097 2097
 
2098 2098
 	networkID := n.ID()
2099
-	c.Lock()
2100
-	defer c.Unlock()
2099
+	c.mu.Lock()
2100
+	defer c.mu.Unlock()
2101 2101
 	sr, ok := c.svcRecords[networkID]
2102 2102
 
2103 2103
 	if !ok {
... ...
@@ -253,12 +253,12 @@ func (sb *sandbox) delete(force bool) error {
253 253
 		logrus.Warnf("Failed to delete sandbox %s from store: %v", sb.ID(), err)
254 254
 	}
255 255
 
256
-	c.Lock()
256
+	c.mu.Lock()
257 257
 	if sb.ingress {
258 258
 		c.ingressSandbox = nil
259 259
 	}
260 260
 	delete(c.sandboxes, sb.ID())
261
-	c.Unlock()
261
+	c.mu.Unlock()
262 262
 
263 263
 	return nil
264 264
 }
... ...
@@ -131,9 +131,9 @@ func (c *controller) startExternalKeyListener() error {
131 131
 		l.Close()
132 132
 		return err
133 133
 	}
134
-	c.Lock()
134
+	c.mu.Lock()
135 135
 	c.extKeyListener = l
136
-	c.Unlock()
136
+	c.mu.Unlock()
137 137
 
138 138
 	go c.acceptClientConnections(uds, l)
139 139
 	return nil
... ...
@@ -248,9 +248,9 @@ func (c *controller) sandboxCleanup(activeSandboxes map[string]interface{}) {
248 248
 			continue
249 249
 		}
250 250
 
251
-		c.Lock()
251
+		c.mu.Lock()
252 252
 		c.sandboxes[sb.id] = sb
253
-		c.Unlock()
253
+		c.mu.Unlock()
254 254
 
255 255
 		for _, eps := range sbs.Eps {
256 256
 			n, err := c.getNetworkFromStore(eps.Nid)
... ...
@@ -153,9 +153,9 @@ func (c *controller) getLBIndex(sid, nid string, ingressPorts []*PortConfig) int
153 153
 		id:    sid,
154 154
 		ports: portConfigs(ingressPorts).String(),
155 155
 	}
156
-	c.Lock()
156
+	c.mu.Lock()
157 157
 	s, ok := c.serviceBindings[skey]
158
-	c.Unlock()
158
+	c.mu.Unlock()
159 159
 
160 160
 	if !ok {
161 161
 		return 0
... ...
@@ -170,8 +170,8 @@ func (c *controller) getLBIndex(sid, nid string, ingressPorts []*PortConfig) int
170 170
 
171 171
 // cleanupServiceDiscovery when the network is being deleted, erase all the associated service discovery records
172 172
 func (c *controller) cleanupServiceDiscovery(cleanupNID string) {
173
-	c.Lock()
174
-	defer c.Unlock()
173
+	c.mu.Lock()
174
+	defer c.mu.Unlock()
175 175
 	if cleanupNID == "" {
176 176
 		logrus.Debugf("cleanupServiceDiscovery for all networks")
177 177
 		c.svcRecords = make(map[string]svcInfo)
... ...
@@ -185,12 +185,12 @@ func (c *controller) cleanupServiceBindings(cleanupNID string) {
185 185
 	var cleanupFuncs []func()
186 186
 
187 187
 	logrus.Debugf("cleanupServiceBindings for %s", cleanupNID)
188
-	c.Lock()
188
+	c.mu.Lock()
189 189
 	services := make([]*service, 0, len(c.serviceBindings))
190 190
 	for _, s := range c.serviceBindings {
191 191
 		services = append(services, s)
192 192
 	}
193
-	c.Unlock()
193
+	c.mu.Unlock()
194 194
 
195 195
 	for _, s := range services {
196 196
 		s.Lock()
... ...
@@ -248,7 +248,7 @@ func (c *controller) addServiceBinding(svcName, svcID, nID, eID, containerName s
248 248
 
249 249
 	var s *service
250 250
 	for {
251
-		c.Lock()
251
+		c.mu.Lock()
252 252
 		var ok bool
253 253
 		s, ok = c.serviceBindings[skey]
254 254
 		if !ok {
... ...
@@ -257,7 +257,7 @@ func (c *controller) addServiceBinding(svcName, svcID, nID, eID, containerName s
257 257
 			s = newService(svcName, svcID, ingressPorts, serviceAliases)
258 258
 			c.serviceBindings[skey] = s
259 259
 		}
260
-		c.Unlock()
260
+		c.mu.Unlock()
261 261
 		s.Lock()
262 262
 		if !s.deleted {
263 263
 			// ok the object is good to be used
... ...
@@ -321,9 +321,9 @@ func (c *controller) rmServiceBinding(svcName, svcID, nID, eID, containerName st
321 321
 		ports: portConfigs(ingressPorts).String(),
322 322
 	}
323 323
 
324
-	c.Lock()
324
+	c.mu.Lock()
325 325
 	s, ok := c.serviceBindings[skey]
326
-	c.Unlock()
326
+	c.mu.Unlock()
327 327
 	if !ok {
328 328
 		logrus.Warnf("rmServiceBinding %s %s %s aborted c.serviceBindings[skey] !ok", method, svcName, eID)
329 329
 		return nil
... ...
@@ -398,14 +398,14 @@ func (c *controller) rmServiceBinding(svcName, svcID, nID, eID, containerName st
398 398
 	if len(s.loadBalancers) == 0 {
399 399
 		// All loadbalancers for the service removed. Time to
400 400
 		// remove the service itself.
401
-		c.Lock()
401
+		c.mu.Lock()
402 402
 
403 403
 		// Mark the object as deleted so that the add won't use it wrongly
404 404
 		s.deleted = true
405 405
 		// NOTE The delete from the serviceBindings map has to be the last operation else we are allowing a race between this service
406 406
 		// that is getting deleted and a new service that will be created if the entry is not anymore there
407 407
 		delete(c.serviceBindings, skey)
408
-		c.Unlock()
408
+		c.mu.Unlock()
409 409
 	}
410 410
 
411 411
 	logrus.Debugf("rmServiceBinding from %s END for %s %s", method, svcName, eID)
... ...
@@ -18,9 +18,9 @@ func (c *controller) initScopedStore(scope string, scfg *datastore.ScopeCfg) err
18 18
 	if err != nil {
19 19
 		return err
20 20
 	}
21
-	c.Lock()
21
+	c.mu.Lock()
22 22
 	c.stores = append(c.stores, store)
23
-	c.Unlock()
23
+	c.mu.Unlock()
24 24
 
25 25
 	return nil
26 26
 }
... ...
@@ -28,14 +28,14 @@ func (c *controller) initScopedStore(scope string, scfg *datastore.ScopeCfg) err
28 28
 func (c *controller) initStores() error {
29 29
 	registerKVStores()
30 30
 
31
-	c.Lock()
31
+	c.mu.Lock()
32 32
 	if c.cfg == nil {
33
-		c.Unlock()
33
+		c.mu.Unlock()
34 34
 		return nil
35 35
 	}
36 36
 	scopeConfigs := c.cfg.Scopes
37 37
 	c.stores = nil
38
-	c.Unlock()
38
+	c.mu.Unlock()
39 39
 
40 40
 	for scope, scfg := range scopeConfigs {
41 41
 		if err := c.initScopedStore(scope, scfg); err != nil {
... ...
@@ -54,8 +54,8 @@ func (c *controller) closeStores() {
54 54
 }
55 55
 
56 56
 func (c *controller) getStore(scope string) datastore.DataStore {
57
-	c.Lock()
58
-	defer c.Unlock()
57
+	c.mu.Lock()
58
+	defer c.mu.Unlock()
59 59
 
60 60
 	for _, store := range c.stores {
61 61
 		if store.Scope() == scope {
... ...
@@ -67,8 +67,8 @@ func (c *controller) getStore(scope string) datastore.DataStore {
67 67
 }
68 68
 
69 69
 func (c *controller) getStores() []datastore.DataStore {
70
-	c.Lock()
71
-	defer c.Unlock()
70
+	c.mu.Lock()
71
+	defer c.mu.Unlock()
72 72
 
73 73
 	return c.stores
74 74
 }
... ...
@@ -244,8 +244,8 @@ type netWatch struct {
244 244
 }
245 245
 
246 246
 func (c *controller) getLocalEps(nw *netWatch) []*endpoint {
247
-	c.Lock()
248
-	defer c.Unlock()
247
+	c.mu.Lock()
248
+	defer c.mu.Unlock()
249 249
 
250 250
 	var epl []*endpoint
251 251
 	for _, ep := range nw.localEps {
... ...
@@ -276,7 +276,7 @@ func (c *controller) networkWatchLoop(nw *netWatch, ep *endpoint, ecCh <-chan da
276 276
 				break
277 277
 			}
278 278
 
279
-			c.Lock()
279
+			c.mu.Lock()
280 280
 			var addEp []*endpoint
281 281
 
282 282
 			delEpMap := make(map[string]*endpoint)
... ...
@@ -315,7 +315,7 @@ func (c *controller) networkWatchLoop(nw *netWatch, ep *endpoint, ecCh <-chan da
315 315
 					delete(nw.remoteEps, lEp.ID())
316 316
 				}
317 317
 			}
318
-			c.Unlock()
318
+			c.mu.Unlock()
319 319
 
320 320
 			for _, lEp := range delEpMap {
321 321
 				ep.getNetwork().updateSvcRecord(lEp, c.getLocalEps(nw), false)
... ...
@@ -336,22 +336,22 @@ func (c *controller) processEndpointCreate(nmap map[string]*netWatch, ep *endpoi
336 336
 	networkID := n.ID()
337 337
 	endpointID := ep.ID()
338 338
 
339
-	c.Lock()
339
+	c.mu.Lock()
340 340
 	nw, ok := nmap[networkID]
341
-	c.Unlock()
341
+	c.mu.Unlock()
342 342
 
343 343
 	if ok {
344 344
 		// Update the svc db for the local endpoint join right away
345 345
 		n.updateSvcRecord(ep, c.getLocalEps(nw), true)
346 346
 
347
-		c.Lock()
347
+		c.mu.Lock()
348 348
 		nw.localEps[endpointID] = ep
349 349
 
350 350
 		// If we had learned that from the kv store remove it
351 351
 		// from remote ep list now that we know that this is
352 352
 		// indeed a local endpoint
353 353
 		delete(nw.remoteEps, endpointID)
354
-		c.Unlock()
354
+		c.mu.Unlock()
355 355
 		return
356 356
 	}
357 357
 
... ...
@@ -365,11 +365,11 @@ func (c *controller) processEndpointCreate(nmap map[string]*netWatch, ep *endpoi
365 365
 	// try to update this ep's container's svc records
366 366
 	n.updateSvcRecord(ep, c.getLocalEps(nw), true)
367 367
 
368
-	c.Lock()
368
+	c.mu.Lock()
369 369
 	nw.localEps[endpointID] = ep
370 370
 	nmap[networkID] = nw
371 371
 	nw.stopCh = make(chan struct{})
372
-	c.Unlock()
372
+	c.mu.Unlock()
373 373
 
374 374
 	store := c.getStore(n.DataScope())
375 375
 	if store == nil {
... ...
@@ -398,19 +398,19 @@ func (c *controller) processEndpointDelete(nmap map[string]*netWatch, ep *endpoi
398 398
 	networkID := n.ID()
399 399
 	endpointID := ep.ID()
400 400
 
401
-	c.Lock()
401
+	c.mu.Lock()
402 402
 	nw, ok := nmap[networkID]
403 403
 
404 404
 	if ok {
405 405
 		delete(nw.localEps, endpointID)
406
-		c.Unlock()
406
+		c.mu.Unlock()
407 407
 
408 408
 		// Update the svc db about local endpoint leave right away
409 409
 		// Do this after we remove this ep from localEps so that we
410 410
 		// don't try to remove this svc record from this ep's container.
411 411
 		n.updateSvcRecord(ep, c.getLocalEps(nw), false)
412 412
 
413
-		c.Lock()
413
+		c.mu.Lock()
414 414
 		if len(nw.localEps) == 0 {
415 415
 			close(nw.stopCh)
416 416
 
... ...
@@ -421,7 +421,7 @@ func (c *controller) processEndpointDelete(nmap map[string]*netWatch, ep *endpoi
421 421
 			delete(nmap, networkID)
422 422
 		}
423 423
 	}
424
-	c.Unlock()
424
+	c.mu.Unlock()
425 425
 }
426 426
 
427 427
 func (c *controller) watchLoop() {