Browse code

libnetwork: don't embed mutex in network

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 *network implements sync.Locker because the mutex is
embedded. Change the mutex to an unexported field so *network no
longer spuriously implements the sync.Locker interface.

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

Cory Snider authored on 2023/01/12 08:00:34
Showing 2 changed files
... ...
@@ -234,7 +234,7 @@ type network struct {
234 234
 	configFrom       string
235 235
 	loadBalancerIP   net.IP
236 236
 	loadBalancerMode string
237
-	sync.Mutex
237
+	mu               sync.Mutex
238 238
 }
239 239
 
240 240
 const (
... ...
@@ -244,36 +244,36 @@ const (
244 244
 )
245 245
 
246 246
 func (n *network) Name() string {
247
-	n.Lock()
248
-	defer n.Unlock()
247
+	n.mu.Lock()
248
+	defer n.mu.Unlock()
249 249
 
250 250
 	return n.name
251 251
 }
252 252
 
253 253
 func (n *network) ID() string {
254
-	n.Lock()
255
-	defer n.Unlock()
254
+	n.mu.Lock()
255
+	defer n.mu.Unlock()
256 256
 
257 257
 	return n.id
258 258
 }
259 259
 
260 260
 func (n *network) Created() time.Time {
261
-	n.Lock()
262
-	defer n.Unlock()
261
+	n.mu.Lock()
262
+	defer n.mu.Unlock()
263 263
 
264 264
 	return n.created
265 265
 }
266 266
 
267 267
 func (n *network) Type() string {
268
-	n.Lock()
269
-	defer n.Unlock()
268
+	n.mu.Lock()
269
+	defer n.mu.Unlock()
270 270
 
271 271
 	return n.networkType
272 272
 }
273 273
 
274 274
 func (n *network) Key() []string {
275
-	n.Lock()
276
-	defer n.Unlock()
275
+	n.mu.Lock()
276
+	defer n.mu.Unlock()
277 277
 	return []string{datastore.NetworkKeyPrefix, n.id}
278 278
 }
279 279
 
... ...
@@ -282,8 +282,8 @@ func (n *network) KeyPrefix() []string {
282 282
 }
283 283
 
284 284
 func (n *network) Value() []byte {
285
-	n.Lock()
286
-	defer n.Unlock()
285
+	n.mu.Lock()
286
+	defer n.mu.Unlock()
287 287
 	b, err := json.Marshal(n)
288 288
 	if err != nil {
289 289
 		return nil
... ...
@@ -296,33 +296,33 @@ func (n *network) SetValue(value []byte) error {
296 296
 }
297 297
 
298 298
 func (n *network) Index() uint64 {
299
-	n.Lock()
300
-	defer n.Unlock()
299
+	n.mu.Lock()
300
+	defer n.mu.Unlock()
301 301
 	return n.dbIndex
302 302
 }
303 303
 
304 304
 func (n *network) SetIndex(index uint64) {
305
-	n.Lock()
305
+	n.mu.Lock()
306 306
 	n.dbIndex = index
307 307
 	n.dbExists = true
308
-	n.Unlock()
308
+	n.mu.Unlock()
309 309
 }
310 310
 
311 311
 func (n *network) Exists() bool {
312
-	n.Lock()
313
-	defer n.Unlock()
312
+	n.mu.Lock()
313
+	defer n.mu.Unlock()
314 314
 	return n.dbExists
315 315
 }
316 316
 
317 317
 func (n *network) Skip() bool {
318
-	n.Lock()
319
-	defer n.Unlock()
318
+	n.mu.Lock()
319
+	defer n.mu.Unlock()
320 320
 	return !n.persist
321 321
 }
322 322
 
323 323
 func (n *network) New() datastore.KVObject {
324
-	n.Lock()
325
-	defer n.Unlock()
324
+	n.mu.Lock()
325
+	defer n.mu.Unlock()
326 326
 
327 327
 	return &network{
328 328
 		ctrlr:   n.ctrlr,
... ...
@@ -456,8 +456,8 @@ func (n *network) applyConfigurationTo(to *network) error {
456 456
 }
457 457
 
458 458
 func (n *network) CopyTo(o datastore.KVObject) error {
459
-	n.Lock()
460
-	defer n.Unlock()
459
+	n.mu.Lock()
460
+	defer n.mu.Unlock()
461 461
 
462 462
 	dstN := o.(*network)
463 463
 	dstN.name = n.name
... ...
@@ -547,8 +547,8 @@ func (n *network) DataScope() string {
547 547
 }
548 548
 
549 549
 func (n *network) getEpCnt() *endpointCnt {
550
-	n.Lock()
551
-	defer n.Unlock()
550
+	n.mu.Lock()
551
+	defer n.mu.Unlock()
552 552
 
553 553
 	return n.epCnt
554 554
 }
... ...
@@ -955,7 +955,7 @@ func (n *network) driver(load bool) (driverapi.Driver, error) {
955 955
 		return nil, err
956 956
 	}
957 957
 
958
-	n.Lock()
958
+	n.mu.Lock()
959 959
 	// If load is not required, driver, cap and err may all be nil
960 960
 	if n.scope == "" && cap != nil {
961 961
 		n.scope = cap.DataScope
... ...
@@ -965,7 +965,7 @@ func (n *network) driver(load bool) (driverapi.Driver, error) {
965 965
 		// scoped regardless of the backing driver.
966 966
 		n.scope = datastore.SwarmScope
967 967
 	}
968
-	n.Unlock()
968
+	n.mu.Unlock()
969 969
 	return d, nil
970 970
 }
971 971
 
... ...
@@ -986,11 +986,11 @@ func (n *network) Delete(options ...NetworkDeleteOption) error {
986 986
 //   - controller.networkCleanup() -- (true, true)
987 987
 //     remove the network no matter what
988 988
 func (n *network) delete(force bool, rmLBEndpoint bool) error {
989
-	n.Lock()
989
+	n.mu.Lock()
990 990
 	c := n.ctrlr
991 991
 	name := n.name
992 992
 	id := n.id
993
-	n.Unlock()
993
+	n.mu.Unlock()
994 994
 
995 995
 	c.networkLocker.Lock(id)
996 996
 	defer c.networkLocker.Unlock(id) //nolint:errcheck
... ...
@@ -1466,8 +1466,8 @@ func (n *network) deleteSvcRecords(eID, name, serviceID string, epIP net.IP, epI
1466 1466
 }
1467 1467
 
1468 1468
 func (n *network) getSvcRecords(ep *Endpoint) []etchosts.Record {
1469
-	n.Lock()
1470
-	defer n.Unlock()
1469
+	n.mu.Lock()
1470
+	defer n.mu.Unlock()
1471 1471
 
1472 1472
 	if ep == nil {
1473 1473
 		return nil
... ...
@@ -1511,8 +1511,8 @@ func (n *network) getSvcRecords(ep *Endpoint) []etchosts.Record {
1511 1511
 }
1512 1512
 
1513 1513
 func (n *network) getController() *Controller {
1514
-	n.Lock()
1515
-	defer n.Unlock()
1514
+	n.mu.Lock()
1515
+	defer n.mu.Unlock()
1516 1516
 	return n.ctrlr
1517 1517
 }
1518 1518
 
... ...
@@ -1746,9 +1746,9 @@ func (n *network) getIPInfo(ipVer int) []*IpamInfo {
1746 1746
 		return nil
1747 1747
 	}
1748 1748
 	l := make([]*IpamInfo, 0, len(info))
1749
-	n.Lock()
1749
+	n.mu.Lock()
1750 1750
 	l = append(l, info...)
1751
-	n.Unlock()
1751
+	n.mu.Unlock()
1752 1752
 	return l
1753 1753
 }
1754 1754
 
... ...
@@ -1763,11 +1763,11 @@ func (n *network) getIPData(ipVer int) []driverapi.IPAMData {
1763 1763
 		return nil
1764 1764
 	}
1765 1765
 	l := make([]driverapi.IPAMData, 0, len(info))
1766
-	n.Lock()
1766
+	n.mu.Lock()
1767 1767
 	for _, d := range info {
1768 1768
 		l = append(l, d.IPAMData)
1769 1769
 	}
1770
-	n.Unlock()
1770
+	n.mu.Unlock()
1771 1771
 	return l
1772 1772
 }
1773 1773
 
... ...
@@ -1800,8 +1800,8 @@ func (n *network) Peers() []networkdb.PeerInfo {
1800 1800
 }
1801 1801
 
1802 1802
 func (n *network) DriverOptions() map[string]string {
1803
-	n.Lock()
1804
-	defer n.Unlock()
1803
+	n.mu.Lock()
1804
+	defer n.mu.Unlock()
1805 1805
 	if n.generic != nil {
1806 1806
 		if m, ok := n.generic[netlabel.GenericData]; ok {
1807 1807
 			return m.(map[string]string)
... ...
@@ -1811,14 +1811,14 @@ func (n *network) DriverOptions() map[string]string {
1811 1811
 }
1812 1812
 
1813 1813
 func (n *network) Scope() string {
1814
-	n.Lock()
1815
-	defer n.Unlock()
1814
+	n.mu.Lock()
1815
+	defer n.mu.Unlock()
1816 1816
 	return n.scope
1817 1817
 }
1818 1818
 
1819 1819
 func (n *network) IpamConfig() (string, map[string]string, []*IpamConf, []*IpamConf) {
1820
-	n.Lock()
1821
-	defer n.Unlock()
1820
+	n.mu.Lock()
1821
+	defer n.mu.Unlock()
1822 1822
 
1823 1823
 	v4L := make([]*IpamConf, len(n.ipamV4Config))
1824 1824
 	v6L := make([]*IpamConf, len(n.ipamV6Config))
... ...
@@ -1843,8 +1843,8 @@ func (n *network) IpamConfig() (string, map[string]string, []*IpamConf, []*IpamC
1843 1843
 }
1844 1844
 
1845 1845
 func (n *network) IpamInfo() ([]*IpamInfo, []*IpamInfo) {
1846
-	n.Lock()
1847
-	defer n.Unlock()
1846
+	n.mu.Lock()
1847
+	defer n.mu.Unlock()
1848 1848
 
1849 1849
 	v4Info := make([]*IpamInfo, len(n.ipamV4Info))
1850 1850
 	v6Info := make([]*IpamInfo, len(n.ipamV6Info))
... ...
@@ -1869,57 +1869,57 @@ func (n *network) IpamInfo() ([]*IpamInfo, []*IpamInfo) {
1869 1869
 }
1870 1870
 
1871 1871
 func (n *network) Internal() bool {
1872
-	n.Lock()
1873
-	defer n.Unlock()
1872
+	n.mu.Lock()
1873
+	defer n.mu.Unlock()
1874 1874
 
1875 1875
 	return n.internal
1876 1876
 }
1877 1877
 
1878 1878
 func (n *network) Attachable() bool {
1879
-	n.Lock()
1880
-	defer n.Unlock()
1879
+	n.mu.Lock()
1880
+	defer n.mu.Unlock()
1881 1881
 
1882 1882
 	return n.attachable
1883 1883
 }
1884 1884
 
1885 1885
 func (n *network) Ingress() bool {
1886
-	n.Lock()
1887
-	defer n.Unlock()
1886
+	n.mu.Lock()
1887
+	defer n.mu.Unlock()
1888 1888
 
1889 1889
 	return n.ingress
1890 1890
 }
1891 1891
 
1892 1892
 func (n *network) Dynamic() bool {
1893
-	n.Lock()
1894
-	defer n.Unlock()
1893
+	n.mu.Lock()
1894
+	defer n.mu.Unlock()
1895 1895
 
1896 1896
 	return n.dynamic
1897 1897
 }
1898 1898
 
1899 1899
 func (n *network) IPv6Enabled() bool {
1900
-	n.Lock()
1901
-	defer n.Unlock()
1900
+	n.mu.Lock()
1901
+	defer n.mu.Unlock()
1902 1902
 
1903 1903
 	return n.enableIPv6
1904 1904
 }
1905 1905
 
1906 1906
 func (n *network) ConfigFrom() string {
1907
-	n.Lock()
1908
-	defer n.Unlock()
1907
+	n.mu.Lock()
1908
+	defer n.mu.Unlock()
1909 1909
 
1910 1910
 	return n.configFrom
1911 1911
 }
1912 1912
 
1913 1913
 func (n *network) ConfigOnly() bool {
1914
-	n.Lock()
1915
-	defer n.Unlock()
1914
+	n.mu.Lock()
1915
+	defer n.mu.Unlock()
1916 1916
 
1917 1917
 	return n.configOnly
1918 1918
 }
1919 1919
 
1920 1920
 func (n *network) Labels() map[string]string {
1921
-	n.Lock()
1922
-	defer n.Unlock()
1921
+	n.mu.Lock()
1922
+	defer n.mu.Unlock()
1923 1923
 
1924 1924
 	var lbls = make(map[string]string, len(n.labels))
1925 1925
 	for k, v := range n.labels {
... ...
@@ -1938,8 +1938,8 @@ func (n *network) TableEventRegister(tableName string, objType driverapi.ObjectT
1938 1938
 		name:    tableName,
1939 1939
 		objType: objType,
1940 1940
 	}
1941
-	n.Lock()
1942
-	defer n.Unlock()
1941
+	n.mu.Lock()
1942
+	defer n.mu.Unlock()
1943 1943
 	n.driverTables = append(n.driverTables, t)
1944 1944
 	return nil
1945 1945
 }
... ...
@@ -1954,8 +1954,8 @@ func (n *network) UpdateIpamConfig(ipV4Data []driverapi.IPAMData) {
1954 1954
 		ipamV4Config[i] = ic
1955 1955
 	}
1956 1956
 
1957
-	n.Lock()
1958
-	defer n.Unlock()
1957
+	n.mu.Lock()
1958
+	defer n.mu.Unlock()
1959 1959
 	n.ipamV4Config = ipamV4Config
1960 1960
 }
1961 1961
 
... ...
@@ -2212,10 +2212,10 @@ func (n *network) createLoadBalancerSandbox() (retErr error) {
2212 2212
 }
2213 2213
 
2214 2214
 func (n *network) deleteLoadBalancerSandbox() error {
2215
-	n.Lock()
2215
+	n.mu.Lock()
2216 2216
 	c := n.ctrlr
2217 2217
 	name := n.name
2218
-	n.Unlock()
2218
+	n.mu.Unlock()
2219 2219
 
2220 2220
 	sandboxName := n.lbSandboxName()
2221 2221
 	endpointName := n.lbEndpointName()
... ...
@@ -138,7 +138,7 @@ func (c *Controller) getNetworksFromStore() []*network {
138 138
 
139 139
 		for _, kvo := range kvol {
140 140
 			n := kvo.(*network)
141
-			n.Lock()
141
+			n.mu.Lock()
142 142
 			n.ctrlr = c
143 143
 			ec := &endpointCnt{n: n}
144 144
 			// Trim the leading & trailing "/" to make it consistent across all stores
... ...
@@ -150,7 +150,7 @@ func (c *Controller) getNetworksFromStore() []*network {
150 150
 			if n.scope == "" {
151 151
 				n.scope = store.Scope()
152 152
 			}
153
-			n.Unlock()
153
+			n.mu.Unlock()
154 154
 			nl = append(nl, n)
155 155
 		}
156 156
 	}