Skip to content

Commit

Permalink
libnetwork/driverapi: make discoverAPI an optional part of the interface
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
thaJeztah committed Jul 28, 2023
1 parent 70e620f commit fca38bc
Show file tree
Hide file tree
Showing 17 changed files with 28 additions and 140 deletions.
22 changes: 14 additions & 8 deletions libnetwork/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,19 @@ func (c *Controller) handleKeyChange(keys []*types.EncryptionKey) error {
}

c.drvRegistry.WalkDrivers(func(name string, driver driverapi.Driver, capability driverapi.Capability) bool {
err := driver.DiscoverNew(discoverapi.EncryptionKeysUpdate, drvEnc)
if err != nil {
dr, ok := driver.(discoverapi.Discover)
if !ok {
return false
}
if err := dr.DiscoverNew(discoverapi.EncryptionKeysUpdate, drvEnc); err != nil {
log.G(context.TODO()).Warnf("Failed to update datapath keys in driver %s: %v", name, err)
// Attempt to reconfigure keys in case of a update failure
// which can arise due to a mismatch of keys
// if worker nodes get temporarily disconnected
log.G(context.TODO()).Warnf("Reconfiguring datapath keys for %s", name)
drvCfgEnc := discoverapi.DriverEncryptionConfig{}
drvCfgEnc.Keys, drvCfgEnc.Tags = c.getKeys(subsysIPSec)
err = driver.DiscoverNew(discoverapi.EncryptionKeysConfig, drvCfgEnc)
err = dr.DiscoverNew(discoverapi.EncryptionKeysConfig, drvCfgEnc)
if err != nil {
log.G(context.TODO()).Warnf("Failed to reset datapath keys in driver %s: %v", name, err)
}
Expand Down Expand Up @@ -232,7 +235,9 @@ func (c *Controller) agentSetup(clusterProvider cluster.Provider) error {
}
c.drvRegistry.WalkDrivers(func(name string, driver driverapi.Driver, capability driverapi.Capability) bool {
if capability.ConnectivityScope == datastore.GlobalScope {
c.agentDriverNotify(driver)
if d, ok := driver.(discoverapi.Discover); ok {
c.agentDriverNotify(d)
}
}
return false
})
Expand Down Expand Up @@ -337,9 +342,10 @@ func (c *Controller) agentInit(listenAddr, bindAddrOrInterface, advertiseAddr, d
drvEnc.Tags = tags

c.drvRegistry.WalkDrivers(func(name string, driver driverapi.Driver, capability driverapi.Capability) bool {
err := driver.DiscoverNew(discoverapi.EncryptionKeysConfig, drvEnc)
if err != nil {
log.G(context.TODO()).Warnf("Failed to set datapath keys in driver %s: %v", name, err)
if dr, ok := driver.(discoverapi.Discover); ok {
if err := dr.DiscoverNew(discoverapi.EncryptionKeysConfig, drvEnc); err != nil {
log.G(context.TODO()).Warnf("Failed to set datapath keys in driver %s: %v", name, err)
}
}
return false
})
Expand All @@ -357,7 +363,7 @@ func (c *Controller) agentJoin(remoteAddrList []string) error {
return agent.networkDB.Join(remoteAddrList)
}

func (c *Controller) agentDriverNotify(d driverapi.Driver) {
func (c *Controller) agentDriverNotify(d discoverapi.Discover) {
agent := c.getAgent()
if agent == nil {
return
Expand Down
10 changes: 7 additions & 3 deletions libnetwork/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,14 @@ func (c *Controller) BuiltinIPAMDrivers() []string {

func (c *Controller) processNodeDiscovery(nodes []net.IP, add bool) {
c.drvRegistry.WalkDrivers(func(name string, driver driverapi.Driver, capability driverapi.Capability) bool {
c.pushNodeDiscovery(driver, capability, nodes, add)
if d, ok := driver.(discoverapi.Discover); ok {
c.pushNodeDiscovery(d, capability, nodes, add)
}
return false
})
}

func (c *Controller) pushNodeDiscovery(d driverapi.Driver, cap driverapi.Capability, nodes []net.IP, add bool) {
func (c *Controller) pushNodeDiscovery(d discoverapi.Discover, cap driverapi.Capability, nodes []net.IP, add bool) {
var self net.IP
// try swarm-mode config
if agent := c.getAgent(); agent != nil {
Expand Down Expand Up @@ -452,7 +454,9 @@ func (c *Controller) GetPluginGetter() plugingetter.PluginGetter {
}

func (c *Controller) RegisterDriver(networkType string, driver driverapi.Driver, capability driverapi.Capability) error {
c.agentDriverNotify(driver)
if d, ok := driver.(discoverapi.Discover); ok {
c.agentDriverNotify(d)
}
return nil
}

Expand Down
8 changes: 1 addition & 7 deletions libnetwork/driverapi/driverapi.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package driverapi

import (
"net"

"github.com/docker/docker/libnetwork/discoverapi"
)
import "net"

// NetworkPluginEndpointType represents the Endpoint Type used by Plugin system
const NetworkPluginEndpointType = "NetworkDriver"

// Driver is an interface that every plugin driver needs to implement.
type Driver interface {
discoverapi.Discover

// NetworkAllocate invokes the driver method to allocate network
// specific resources passing network id and network specific config.
// It returns a key,value pair of network specific driver allocations
Expand Down
11 changes: 0 additions & 11 deletions libnetwork/drivers/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

"github.com/containerd/containerd/log"
"github.com/docker/docker/libnetwork/datastore"
"github.com/docker/docker/libnetwork/discoverapi"
"github.com/docker/docker/libnetwork/driverapi"
"github.com/docker/docker/libnetwork/iptables"
"github.com/docker/docker/libnetwork/netlabel"
Expand Down Expand Up @@ -1435,16 +1434,6 @@ func (d *driver) IsBuiltIn() bool {
return true
}

// DiscoverNew is a notification for a new discovery event, such as a new node joining a cluster
func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
return nil
}

// DiscoverDelete is a notification for a discovery delete event, such as a node leaving a cluster
func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
return nil
}

func parseEndpointOptions(epOptions map[string]interface{}) (*endpointConfiguration, error) {
if epOptions == nil {
return nil, nil
Expand Down
9 changes: 0 additions & 9 deletions libnetwork/drivers/bridge/brmanager/brmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package brmanager

import (
"github.com/docker/docker/libnetwork/datastore"
"github.com/docker/docker/libnetwork/discoverapi"
"github.com/docker/docker/libnetwork/driverapi"
"github.com/docker/docker/libnetwork/types"
)
Expand Down Expand Up @@ -70,14 +69,6 @@ func (d *driver) IsBuiltIn() bool {
return true
}

func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
return types.NotImplementedErrorf("not implemented")
}

func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
return types.NotImplementedErrorf("not implemented")
}

func (d *driver) ProgramExternalConnectivity(nid, eid string, options map[string]interface{}) error {
return types.NotImplementedErrorf("not implemented")
}
Expand Down
11 changes: 0 additions & 11 deletions libnetwork/drivers/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"sync"

"github.com/docker/docker/libnetwork/datastore"
"github.com/docker/docker/libnetwork/discoverapi"
"github.com/docker/docker/libnetwork/driverapi"
"github.com/docker/docker/libnetwork/types"
)
Expand Down Expand Up @@ -92,13 +91,3 @@ func (d *driver) Type() string {
func (d *driver) IsBuiltIn() bool {
return true
}

// DiscoverNew is a notification for a new discovery event, such as a new node joining a cluster
func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
return nil
}

// DiscoverDelete is a notification for a discovery delete event, such as a node leaving a cluster
func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
return nil
}
11 changes: 0 additions & 11 deletions libnetwork/drivers/ipvlan/ipvlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"sync"

"github.com/docker/docker/libnetwork/datastore"
"github.com/docker/docker/libnetwork/discoverapi"
"github.com/docker/docker/libnetwork/driverapi"
"github.com/docker/docker/libnetwork/types"
)
Expand Down Expand Up @@ -104,16 +103,6 @@ func (d *driver) RevokeExternalConnectivity(nid, eid string) error {
return nil
}

// DiscoverNew is a notification for a new discovery event.
func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
return nil
}

// DiscoverDelete is a notification for a discovery delete event.
func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
return nil
}

func (d *driver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
}

Expand Down
9 changes: 0 additions & 9 deletions libnetwork/drivers/ipvlan/ivmanager/ivmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ivmanager

import (
"github.com/docker/docker/libnetwork/datastore"
"github.com/docker/docker/libnetwork/discoverapi"
"github.com/docker/docker/libnetwork/driverapi"
"github.com/docker/docker/libnetwork/types"
)
Expand Down Expand Up @@ -70,14 +69,6 @@ func (d *driver) IsBuiltIn() bool {
return true
}

func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
return types.NotImplementedErrorf("not implemented")
}

func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
return types.NotImplementedErrorf("not implemented")
}

func (d *driver) ProgramExternalConnectivity(nid, eid string, options map[string]interface{}) error {
return types.NotImplementedErrorf("not implemented")
}
Expand Down
11 changes: 0 additions & 11 deletions libnetwork/drivers/macvlan/macvlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"sync"

"github.com/docker/docker/libnetwork/datastore"
"github.com/docker/docker/libnetwork/discoverapi"
"github.com/docker/docker/libnetwork/driverapi"
"github.com/docker/docker/libnetwork/types"
)
Expand Down Expand Up @@ -98,16 +97,6 @@ func (d *driver) RevokeExternalConnectivity(nid, eid string) error {
return nil
}

// DiscoverNew is a notification for a new discovery event
func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
return nil
}

// DiscoverDelete is a notification for a discovery delete event
func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
return nil
}

func (d *driver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
}

Expand Down
9 changes: 0 additions & 9 deletions libnetwork/drivers/macvlan/mvmanager/mvmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package mvmanager

import (
"github.com/docker/docker/libnetwork/datastore"
"github.com/docker/docker/libnetwork/discoverapi"
"github.com/docker/docker/libnetwork/driverapi"
"github.com/docker/docker/libnetwork/types"
)
Expand Down Expand Up @@ -70,14 +69,6 @@ func (d *driver) IsBuiltIn() bool {
return true
}

func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
return types.NotImplementedErrorf("not implemented")
}

func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
return types.NotImplementedErrorf("not implemented")
}

func (d *driver) ProgramExternalConnectivity(nid, eid string, options map[string]interface{}) error {
return types.NotImplementedErrorf("not implemented")
}
Expand Down
11 changes: 0 additions & 11 deletions libnetwork/drivers/null/null.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"sync"

"github.com/docker/docker/libnetwork/datastore"
"github.com/docker/docker/libnetwork/discoverapi"
"github.com/docker/docker/libnetwork/driverapi"
"github.com/docker/docker/libnetwork/types"
)
Expand Down Expand Up @@ -92,13 +91,3 @@ func (d *driver) Type() string {
func (d *driver) IsBuiltIn() bool {
return true
}

// DiscoverNew is a notification for a new discovery event, such as a new node joining a cluster
func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
return nil
}

// DiscoverDelete is a notification for a discovery delete event, such as a node leaving a cluster
func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
return nil
}
3 changes: 3 additions & 0 deletions libnetwork/drivers/overlay/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const (
secureOption = "encrypted"
)

// overlay driver must implement the discover-API.
var _ discoverapi.Discover = (*driver)(nil)

type driver struct {
bindAddress string
advertiseAddress string
Expand Down
11 changes: 0 additions & 11 deletions libnetwork/drivers/overlay/ovmanager/ovmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/containerd/containerd/log"
"github.com/docker/docker/libnetwork/bitmap"
"github.com/docker/docker/libnetwork/datastore"
"github.com/docker/docker/libnetwork/discoverapi"
"github.com/docker/docker/libnetwork/driverapi"
"github.com/docker/docker/libnetwork/drivers/overlay/overlayutils"
"github.com/docker/docker/libnetwork/netlabel"
Expand Down Expand Up @@ -208,16 +207,6 @@ func (d *driver) IsBuiltIn() bool {
return true
}

// DiscoverNew is a notification for a new discovery event, such as a new node joining a cluster
func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
return types.NotImplementedErrorf("not implemented")
}

// DiscoverDelete is a notification for a discovery delete event, such as a node leaving a cluster
func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
return types.NotImplementedErrorf("not implemented")
}

func (d *driver) ProgramExternalConnectivity(nid, eid string, options map[string]interface{}) error {
return types.NotImplementedErrorf("not implemented")
}
Expand Down
3 changes: 3 additions & 0 deletions libnetwork/drivers/remote/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import (
"github.com/pkg/errors"
)

// remote driver must implement the discover-API.
var _ discoverapi.Discover = (*driver)(nil)

type driver struct {
endpoint *plugins.Client
networkType string
Expand Down
9 changes: 0 additions & 9 deletions libnetwork/ipams/null/null.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"net"

"github.com/docker/docker/libnetwork/discoverapi"
"github.com/docker/docker/libnetwork/ipamapi"
"github.com/docker/docker/libnetwork/types"
)
Expand Down Expand Up @@ -57,14 +56,6 @@ func (a *allocator) ReleaseAddress(poolID string, ip net.IP) error {
return nil
}

func (a *allocator) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
return nil
}

func (a *allocator) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
return nil
}

func (a *allocator) IsBuiltIn() bool {
return true
}
Expand Down
11 changes: 0 additions & 11 deletions libnetwork/ipams/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net"

"github.com/containerd/containerd/log"
"github.com/docker/docker/libnetwork/discoverapi"
"github.com/docker/docker/libnetwork/ipamapi"
"github.com/docker/docker/libnetwork/ipams/remote/api"
"github.com/docker/docker/libnetwork/types"
Expand Down Expand Up @@ -168,16 +167,6 @@ func (a *allocator) ReleaseAddress(poolID string, address net.IP) error {
return a.call("ReleaseAddress", req, res)
}

// DiscoverNew is a notification for a new discovery event, such as a new global datastore
func (a *allocator) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
return nil
}

// DiscoverDelete is a notification for a discovery delete event, such as a node leaving a cluster
func (a *allocator) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
return nil
}

func (a *allocator) IsBuiltIn() bool {
return false
}
Loading

0 comments on commit fca38bc

Please sign in to comment.