Browse code

libnetwork/driverapi: make discoverAPI an optional part of the interface

Most drivers do not implement this, so detect if a driver implements
the discoverAPI, and remove the implementation from drivers that do
not support it.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2023/07/27 19:36:25
Showing 17 changed files
... ...
@@ -182,8 +182,11 @@ func (c *Controller) handleKeyChange(keys []*types.EncryptionKey) error {
182 182
 	}
183 183
 
184 184
 	c.drvRegistry.WalkDrivers(func(name string, driver driverapi.Driver, capability driverapi.Capability) bool {
185
-		err := driver.DiscoverNew(discoverapi.EncryptionKeysUpdate, drvEnc)
186
-		if err != nil {
185
+		dr, ok := driver.(discoverapi.Discover)
186
+		if !ok {
187
+			return false
188
+		}
189
+		if err := dr.DiscoverNew(discoverapi.EncryptionKeysUpdate, drvEnc); err != nil {
187 190
 			log.G(context.TODO()).Warnf("Failed to update datapath keys in driver %s: %v", name, err)
188 191
 			// Attempt to reconfigure keys in case of a update failure
189 192
 			// which can arise due to a mismatch of keys
... ...
@@ -191,7 +194,7 @@ func (c *Controller) handleKeyChange(keys []*types.EncryptionKey) error {
191 191
 			log.G(context.TODO()).Warnf("Reconfiguring datapath keys for  %s", name)
192 192
 			drvCfgEnc := discoverapi.DriverEncryptionConfig{}
193 193
 			drvCfgEnc.Keys, drvCfgEnc.Tags = c.getKeys(subsysIPSec)
194
-			err = driver.DiscoverNew(discoverapi.EncryptionKeysConfig, drvCfgEnc)
194
+			err = dr.DiscoverNew(discoverapi.EncryptionKeysConfig, drvCfgEnc)
195 195
 			if err != nil {
196 196
 				log.G(context.TODO()).Warnf("Failed to reset datapath keys in driver %s: %v", name, err)
197 197
 			}
... ...
@@ -232,7 +235,9 @@ func (c *Controller) agentSetup(clusterProvider cluster.Provider) error {
232 232
 		}
233 233
 		c.drvRegistry.WalkDrivers(func(name string, driver driverapi.Driver, capability driverapi.Capability) bool {
234 234
 			if capability.ConnectivityScope == datastore.GlobalScope {
235
-				c.agentDriverNotify(driver)
235
+				if d, ok := driver.(discoverapi.Discover); ok {
236
+					c.agentDriverNotify(d)
237
+				}
236 238
 			}
237 239
 			return false
238 240
 		})
... ...
@@ -337,9 +342,10 @@ func (c *Controller) agentInit(listenAddr, bindAddrOrInterface, advertiseAddr, d
337 337
 	drvEnc.Tags = tags
338 338
 
339 339
 	c.drvRegistry.WalkDrivers(func(name string, driver driverapi.Driver, capability driverapi.Capability) bool {
340
-		err := driver.DiscoverNew(discoverapi.EncryptionKeysConfig, drvEnc)
341
-		if err != nil {
342
-			log.G(context.TODO()).Warnf("Failed to set datapath keys in driver %s: %v", name, err)
340
+		if dr, ok := driver.(discoverapi.Discover); ok {
341
+			if err := dr.DiscoverNew(discoverapi.EncryptionKeysConfig, drvEnc); err != nil {
342
+				log.G(context.TODO()).Warnf("Failed to set datapath keys in driver %s: %v", name, err)
343
+			}
343 344
 		}
344 345
 		return false
345 346
 	})
... ...
@@ -357,7 +363,7 @@ func (c *Controller) agentJoin(remoteAddrList []string) error {
357 357
 	return agent.networkDB.Join(remoteAddrList)
358 358
 }
359 359
 
360
-func (c *Controller) agentDriverNotify(d driverapi.Driver) {
360
+func (c *Controller) agentDriverNotify(d discoverapi.Discover) {
361 361
 	agent := c.getAgent()
362 362
 	if agent == nil {
363 363
 		return
... ...
@@ -385,12 +385,14 @@ func (c *Controller) BuiltinIPAMDrivers() []string {
385 385
 
386 386
 func (c *Controller) processNodeDiscovery(nodes []net.IP, add bool) {
387 387
 	c.drvRegistry.WalkDrivers(func(name string, driver driverapi.Driver, capability driverapi.Capability) bool {
388
-		c.pushNodeDiscovery(driver, capability, nodes, add)
388
+		if d, ok := driver.(discoverapi.Discover); ok {
389
+			c.pushNodeDiscovery(d, capability, nodes, add)
390
+		}
389 391
 		return false
390 392
 	})
391 393
 }
392 394
 
393
-func (c *Controller) pushNodeDiscovery(d driverapi.Driver, cap driverapi.Capability, nodes []net.IP, add bool) {
395
+func (c *Controller) pushNodeDiscovery(d discoverapi.Discover, cap driverapi.Capability, nodes []net.IP, add bool) {
394 396
 	var self net.IP
395 397
 	// try swarm-mode config
396 398
 	if agent := c.getAgent(); agent != nil {
... ...
@@ -452,7 +454,9 @@ func (c *Controller) GetPluginGetter() plugingetter.PluginGetter {
452 452
 }
453 453
 
454 454
 func (c *Controller) RegisterDriver(networkType string, driver driverapi.Driver, capability driverapi.Capability) error {
455
-	c.agentDriverNotify(driver)
455
+	if d, ok := driver.(discoverapi.Discover); ok {
456
+		c.agentDriverNotify(d)
457
+	}
456 458
 	return nil
457 459
 }
458 460
 
... ...
@@ -1,18 +1,12 @@
1 1
 package driverapi
2 2
 
3
-import (
4
-	"net"
5
-
6
-	"github.com/docker/docker/libnetwork/discoverapi"
7
-)
3
+import "net"
8 4
 
9 5
 // NetworkPluginEndpointType represents the Endpoint Type used by Plugin system
10 6
 const NetworkPluginEndpointType = "NetworkDriver"
11 7
 
12 8
 // Driver is an interface that every plugin driver needs to implement.
13 9
 type Driver interface {
14
-	discoverapi.Discover
15
-
16 10
 	// NetworkAllocate invokes the driver method to allocate network
17 11
 	// specific resources passing network id and network specific config.
18 12
 	// It returns a key,value pair of network specific driver allocations
... ...
@@ -14,7 +14,6 @@ import (
14 14
 
15 15
 	"github.com/containerd/containerd/log"
16 16
 	"github.com/docker/docker/libnetwork/datastore"
17
-	"github.com/docker/docker/libnetwork/discoverapi"
18 17
 	"github.com/docker/docker/libnetwork/driverapi"
19 18
 	"github.com/docker/docker/libnetwork/iptables"
20 19
 	"github.com/docker/docker/libnetwork/netlabel"
... ...
@@ -1435,16 +1434,6 @@ func (d *driver) IsBuiltIn() bool {
1435 1435
 	return true
1436 1436
 }
1437 1437
 
1438
-// DiscoverNew is a notification for a new discovery event, such as a new node joining a cluster
1439
-func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
1440
-	return nil
1441
-}
1442
-
1443
-// DiscoverDelete is a notification for a discovery delete event, such as a node leaving a cluster
1444
-func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
1445
-	return nil
1446
-}
1447
-
1448 1438
 func parseEndpointOptions(epOptions map[string]interface{}) (*endpointConfiguration, error) {
1449 1439
 	if epOptions == nil {
1450 1440
 		return nil, nil
... ...
@@ -2,7 +2,6 @@ package brmanager
2 2
 
3 3
 import (
4 4
 	"github.com/docker/docker/libnetwork/datastore"
5
-	"github.com/docker/docker/libnetwork/discoverapi"
6 5
 	"github.com/docker/docker/libnetwork/driverapi"
7 6
 	"github.com/docker/docker/libnetwork/types"
8 7
 )
... ...
@@ -70,14 +69,6 @@ func (d *driver) IsBuiltIn() bool {
70 70
 	return true
71 71
 }
72 72
 
73
-func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
74
-	return types.NotImplementedErrorf("not implemented")
75
-}
76
-
77
-func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
78
-	return types.NotImplementedErrorf("not implemented")
79
-}
80
-
81 73
 func (d *driver) ProgramExternalConnectivity(nid, eid string, options map[string]interface{}) error {
82 74
 	return types.NotImplementedErrorf("not implemented")
83 75
 }
... ...
@@ -4,7 +4,6 @@ import (
4 4
 	"sync"
5 5
 
6 6
 	"github.com/docker/docker/libnetwork/datastore"
7
-	"github.com/docker/docker/libnetwork/discoverapi"
8 7
 	"github.com/docker/docker/libnetwork/driverapi"
9 8
 	"github.com/docker/docker/libnetwork/types"
10 9
 )
... ...
@@ -92,13 +91,3 @@ func (d *driver) Type() string {
92 92
 func (d *driver) IsBuiltIn() bool {
93 93
 	return true
94 94
 }
95
-
96
-// DiscoverNew is a notification for a new discovery event, such as a new node joining a cluster
97
-func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
98
-	return nil
99
-}
100
-
101
-// DiscoverDelete is a notification for a discovery delete event, such as a node leaving a cluster
102
-func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
103
-	return nil
104
-}
... ...
@@ -8,7 +8,6 @@ import (
8 8
 	"sync"
9 9
 
10 10
 	"github.com/docker/docker/libnetwork/datastore"
11
-	"github.com/docker/docker/libnetwork/discoverapi"
12 11
 	"github.com/docker/docker/libnetwork/driverapi"
13 12
 	"github.com/docker/docker/libnetwork/types"
14 13
 )
... ...
@@ -104,16 +103,6 @@ func (d *driver) RevokeExternalConnectivity(nid, eid string) error {
104 104
 	return nil
105 105
 }
106 106
 
107
-// DiscoverNew is a notification for a new discovery event.
108
-func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
109
-	return nil
110
-}
111
-
112
-// DiscoverDelete is a notification for a discovery delete event.
113
-func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
114
-	return nil
115
-}
116
-
117 107
 func (d *driver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
118 108
 }
119 109
 
... ...
@@ -2,7 +2,6 @@ package ivmanager
2 2
 
3 3
 import (
4 4
 	"github.com/docker/docker/libnetwork/datastore"
5
-	"github.com/docker/docker/libnetwork/discoverapi"
6 5
 	"github.com/docker/docker/libnetwork/driverapi"
7 6
 	"github.com/docker/docker/libnetwork/types"
8 7
 )
... ...
@@ -70,14 +69,6 @@ func (d *driver) IsBuiltIn() bool {
70 70
 	return true
71 71
 }
72 72
 
73
-func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
74
-	return types.NotImplementedErrorf("not implemented")
75
-}
76
-
77
-func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
78
-	return types.NotImplementedErrorf("not implemented")
79
-}
80
-
81 73
 func (d *driver) ProgramExternalConnectivity(nid, eid string, options map[string]interface{}) error {
82 74
 	return types.NotImplementedErrorf("not implemented")
83 75
 }
... ...
@@ -8,7 +8,6 @@ import (
8 8
 	"sync"
9 9
 
10 10
 	"github.com/docker/docker/libnetwork/datastore"
11
-	"github.com/docker/docker/libnetwork/discoverapi"
12 11
 	"github.com/docker/docker/libnetwork/driverapi"
13 12
 	"github.com/docker/docker/libnetwork/types"
14 13
 )
... ...
@@ -98,16 +97,6 @@ func (d *driver) RevokeExternalConnectivity(nid, eid string) error {
98 98
 	return nil
99 99
 }
100 100
 
101
-// DiscoverNew is a notification for a new discovery event
102
-func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
103
-	return nil
104
-}
105
-
106
-// DiscoverDelete is a notification for a discovery delete event
107
-func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
108
-	return nil
109
-}
110
-
111 101
 func (d *driver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
112 102
 }
113 103
 
... ...
@@ -2,7 +2,6 @@ package mvmanager
2 2
 
3 3
 import (
4 4
 	"github.com/docker/docker/libnetwork/datastore"
5
-	"github.com/docker/docker/libnetwork/discoverapi"
6 5
 	"github.com/docker/docker/libnetwork/driverapi"
7 6
 	"github.com/docker/docker/libnetwork/types"
8 7
 )
... ...
@@ -70,14 +69,6 @@ func (d *driver) IsBuiltIn() bool {
70 70
 	return true
71 71
 }
72 72
 
73
-func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
74
-	return types.NotImplementedErrorf("not implemented")
75
-}
76
-
77
-func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
78
-	return types.NotImplementedErrorf("not implemented")
79
-}
80
-
81 73
 func (d *driver) ProgramExternalConnectivity(nid, eid string, options map[string]interface{}) error {
82 74
 	return types.NotImplementedErrorf("not implemented")
83 75
 }
... ...
@@ -4,7 +4,6 @@ import (
4 4
 	"sync"
5 5
 
6 6
 	"github.com/docker/docker/libnetwork/datastore"
7
-	"github.com/docker/docker/libnetwork/discoverapi"
8 7
 	"github.com/docker/docker/libnetwork/driverapi"
9 8
 	"github.com/docker/docker/libnetwork/types"
10 9
 )
... ...
@@ -92,13 +91,3 @@ func (d *driver) Type() string {
92 92
 func (d *driver) IsBuiltIn() bool {
93 93
 	return true
94 94
 }
95
-
96
-// DiscoverNew is a notification for a new discovery event, such as a new node joining a cluster
97
-func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
98
-	return nil
99
-}
100
-
101
-// DiscoverDelete is a notification for a discovery delete event, such as a node leaving a cluster
102
-func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
103
-	return nil
104
-}
... ...
@@ -23,6 +23,9 @@ const (
23 23
 	secureOption = "encrypted"
24 24
 )
25 25
 
26
+// overlay driver must implement the discover-API.
27
+var _ discoverapi.Discover = (*driver)(nil)
28
+
26 29
 type driver struct {
27 30
 	bindAddress      string
28 31
 	advertiseAddress string
... ...
@@ -10,7 +10,6 @@ import (
10 10
 	"github.com/containerd/containerd/log"
11 11
 	"github.com/docker/docker/libnetwork/bitmap"
12 12
 	"github.com/docker/docker/libnetwork/datastore"
13
-	"github.com/docker/docker/libnetwork/discoverapi"
14 13
 	"github.com/docker/docker/libnetwork/driverapi"
15 14
 	"github.com/docker/docker/libnetwork/drivers/overlay/overlayutils"
16 15
 	"github.com/docker/docker/libnetwork/netlabel"
... ...
@@ -208,16 +207,6 @@ func (d *driver) IsBuiltIn() bool {
208 208
 	return true
209 209
 }
210 210
 
211
-// DiscoverNew is a notification for a new discovery event, such as a new node joining a cluster
212
-func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
213
-	return types.NotImplementedErrorf("not implemented")
214
-}
215
-
216
-// DiscoverDelete is a notification for a discovery delete event, such as a node leaving a cluster
217
-func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
218
-	return types.NotImplementedErrorf("not implemented")
219
-}
220
-
221 211
 func (d *driver) ProgramExternalConnectivity(nid, eid string, options map[string]interface{}) error {
222 212
 	return types.NotImplementedErrorf("not implemented")
223 213
 }
... ...
@@ -16,6 +16,9 @@ import (
16 16
 	"github.com/pkg/errors"
17 17
 )
18 18
 
19
+// remote driver must implement the discover-API.
20
+var _ discoverapi.Discover = (*driver)(nil)
21
+
19 22
 type driver struct {
20 23
 	endpoint    *plugins.Client
21 24
 	networkType string
... ...
@@ -6,7 +6,6 @@ import (
6 6
 	"fmt"
7 7
 	"net"
8 8
 
9
-	"github.com/docker/docker/libnetwork/discoverapi"
10 9
 	"github.com/docker/docker/libnetwork/ipamapi"
11 10
 	"github.com/docker/docker/libnetwork/types"
12 11
 )
... ...
@@ -57,14 +56,6 @@ func (a *allocator) ReleaseAddress(poolID string, ip net.IP) error {
57 57
 	return nil
58 58
 }
59 59
 
60
-func (a *allocator) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
61
-	return nil
62
-}
63
-
64
-func (a *allocator) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
65
-	return nil
66
-}
67
-
68 60
 func (a *allocator) IsBuiltIn() bool {
69 61
 	return true
70 62
 }
... ...
@@ -6,7 +6,6 @@ import (
6 6
 	"net"
7 7
 
8 8
 	"github.com/containerd/containerd/log"
9
-	"github.com/docker/docker/libnetwork/discoverapi"
10 9
 	"github.com/docker/docker/libnetwork/ipamapi"
11 10
 	"github.com/docker/docker/libnetwork/ipams/remote/api"
12 11
 	"github.com/docker/docker/libnetwork/types"
... ...
@@ -168,16 +167,6 @@ func (a *allocator) ReleaseAddress(poolID string, address net.IP) error {
168 168
 	return a.call("ReleaseAddress", req, res)
169 169
 }
170 170
 
171
-// DiscoverNew is a notification for a new discovery event, such as a new global datastore
172
-func (a *allocator) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
173
-	return nil
174
-}
175
-
176
-// DiscoverDelete is a notification for a discovery delete event, such as a node leaving a cluster
177
-func (a *allocator) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
178
-	return nil
179
-}
180
-
181 171
 func (a *allocator) IsBuiltIn() bool {
182 172
 	return false
183 173
 }
... ...
@@ -10,7 +10,6 @@ import (
10 10
 
11 11
 	"github.com/docker/docker/internal/testutils/netnsutils"
12 12
 	"github.com/docker/docker/libnetwork/datastore"
13
-	"github.com/docker/docker/libnetwork/discoverapi"
14 13
 	"github.com/docker/docker/libnetwork/driverapi"
15 14
 	"github.com/docker/docker/libnetwork/ipamapi"
16 15
 	"github.com/docker/docker/libnetwork/netlabel"
... ...
@@ -674,14 +673,6 @@ func (b *badDriver) Leave(nid, eid string) error {
674 674
 	return nil
675 675
 }
676 676
 
677
-func (b *badDriver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
678
-	return nil
679
-}
680
-
681
-func (b *badDriver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
682
-	return nil
683
-}
684
-
685 677
 func (b *badDriver) Type() string {
686 678
 	return badDriverName
687 679
 }