Skip to content

Commit

Permalink
libnetwork/drvregistry: split up the registries
Browse files Browse the repository at this point in the history
There is no benefit to having a single registry for both IPAM drivers
and network drivers. IPAM drivers are registered in a separate namespace
from network drivers, have separate registration methods, separate
accessor methods and do not interact with network drivers within a
DrvRegistry in any way. The only point of commonality is

    interface { GetPluginGetter() plugingetter.PluginGetter }

which is only used by the respective remote drivers and therefore should
be outside of the scope of a driver registry.

Create new, separate registry types for network drivers and IPAM
drivers, respectively. These types are "legacy-free". Neither type has
GetPluginGetter methods. The IPAMs registry does not have an
IPAMDefaultAddressSpaces method as that information can be queried
directly from the driver using its GetDefaultAddressSpaces method.
The Networks registry does not have an AddDriver method as that method
is a trivial wrapper around calling one of its arguments with its other
arguments.

Refactor DrvRegistry in terms of the new IPAMs and Networks registries
so that existing code in libnetwork and Swarmkit will continue to work.

Signed-off-by: Cory Snider <csnider@mirantis.com>
  • Loading branch information
corhere committed Jan 27, 2023
1 parent d478e13 commit 5595311
Show file tree
Hide file tree
Showing 7 changed files with 315 additions and 238 deletions.
8 changes: 6 additions & 2 deletions libnetwork/driverapi/driverapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,16 @@ type JoinInfo interface {
AddTableEntry(tableName string, key string, value []byte) error
}

// Registerer provides a way for network drivers to be dynamically registered.
type Registerer interface {
RegisterDriver(name string, driver Driver, capability Capability) error
}

// DriverCallback provides a Callback interface for Drivers into LibNetwork
type DriverCallback interface {
Registerer
// GetPluginGetter returns the pluginv2 getter.
GetPluginGetter() plugingetter.PluginGetter
// RegisterDriver provides a way for Remote drivers to dynamically register new NetworkType and associate with a driver instance
RegisterDriver(name string, driver Driver, capability Capability) error
}

// Capability represents the high level capabilities of the drivers which libnetwork can make use of
Expand Down
198 changes: 24 additions & 174 deletions libnetwork/drvregistry/drvregistry.go
Original file line number Diff line number Diff line change
@@ -1,221 +1,71 @@
package drvregistry

import (
"errors"
"fmt"
"strings"
"sync"

"github.com/docker/docker/libnetwork/driverapi"
"github.com/docker/docker/libnetwork/ipamapi"
"github.com/docker/docker/libnetwork/types"
"github.com/docker/docker/pkg/plugingetter"
)

type driverData struct {
driver driverapi.Driver
capability driverapi.Capability
}

type ipamData struct {
driver ipamapi.Ipam
capability *ipamapi.Capability
// default address spaces are provided by ipam driver at registration time
defaultLocalAddressSpace, defaultGlobalAddressSpace string
}

type driverTable map[string]*driverData
type ipamTable map[string]*ipamData

// DrvRegistry holds the registry of all network drivers and IPAM drivers that it knows about.
type DrvRegistry struct {
sync.Mutex
drivers driverTable
ipamDrivers ipamTable
dfn DriverNotifyFunc
Networks
IPAMs
pluginGetter plugingetter.PluginGetter
}

// Functors definition
var _ driverapi.DriverCallback = (*DrvRegistry)(nil)
var _ ipamapi.Callback = (*DrvRegistry)(nil)

// InitFunc defines the driver initialization function signature.
type InitFunc func(driverapi.DriverCallback, map[string]interface{}) error

// IPAMWalkFunc defines the IPAM driver table walker function signature.
type IPAMWalkFunc func(name string, driver ipamapi.Ipam, cap *ipamapi.Capability) bool

// DriverWalkFunc defines the network driver table walker function signature.
type DriverWalkFunc func(name string, driver driverapi.Driver, capability driverapi.Capability) bool

// DriverNotifyFunc defines the notify function signature when a new network driver gets registered.
type DriverNotifyFunc func(name string, driver driverapi.Driver, capability driverapi.Capability) error

// Placeholder is a type for function arguments which need to be present for Swarmkit
// to compile, but for which the only acceptable value is nil.
type Placeholder *struct{}

// New returns a new driver registry handle.
// New returns a new legacy driver registry.
//
// Deprecated: use the separate [Networks] and [IPAMs] registries.
func New(lDs, gDs Placeholder, dfn DriverNotifyFunc, ifn Placeholder, pg plugingetter.PluginGetter) (*DrvRegistry, error) {
r := &DrvRegistry{
drivers: make(driverTable),
ipamDrivers: make(ipamTable),
dfn: dfn,
return &DrvRegistry{
Networks: Networks{Notify: dfn},
pluginGetter: pg,
}

return r, nil
}, nil
}

// AddDriver adds a network driver to the registry.
func (r *DrvRegistry) AddDriver(ntype string, fn InitFunc, config map[string]interface{}) error {
//
// Deprecated: call fn(r, config) directly.
func (r *DrvRegistry) AddDriver(_ string, fn InitFunc, config map[string]interface{}) error {
return fn(r, config)
}

// WalkIPAMs walks the IPAM drivers registered in the registry and invokes the passed walk function and each one of them.
func (r *DrvRegistry) WalkIPAMs(ifn IPAMWalkFunc) {
type ipamVal struct {
name string
data *ipamData
}

r.Lock()
ivl := make([]ipamVal, 0, len(r.ipamDrivers))
for k, v := range r.ipamDrivers {
ivl = append(ivl, ipamVal{name: k, data: v})
}
r.Unlock()

for _, iv := range ivl {
if ifn(iv.name, iv.data.driver, iv.data.capability) {
break
}
}
}

// WalkDrivers walks the network drivers registered in the registry and invokes the passed walk function and each one of them.
func (r *DrvRegistry) WalkDrivers(dfn DriverWalkFunc) {
type driverVal struct {
name string
data *driverData
}

r.Lock()
dvl := make([]driverVal, 0, len(r.drivers))
for k, v := range r.drivers {
dvl = append(dvl, driverVal{name: k, data: v})
}
r.Unlock()

for _, dv := range dvl {
if dfn(dv.name, dv.data.driver, dv.data.capability) {
break
}
}
}

// Driver returns the actual network driver instance and its capability which registered with the passed name.
func (r *DrvRegistry) Driver(name string) (driverapi.Driver, *driverapi.Capability) {
r.Lock()
defer r.Unlock()

d, ok := r.drivers[name]
if !ok {
return nil, nil
}

return d.driver, &d.capability
}

// IPAM returns the actual IPAM driver instance and its capability which registered with the passed name.
func (r *DrvRegistry) IPAM(name string) (ipamapi.Ipam, *ipamapi.Capability) {
r.Lock()
defer r.Unlock()

i, ok := r.ipamDrivers[name]
if !ok {
return nil, nil
}

return i.driver, i.capability
}

// IPAMDefaultAddressSpaces returns the default address space strings for the passed IPAM driver name.
//
// Deprecated: call GetDefaultAddressSpaces() on the IPAM driver.
func (r *DrvRegistry) IPAMDefaultAddressSpaces(name string) (string, string, error) {
r.Lock()
defer r.Unlock()
d, _ := r.IPAM(name)

i, ok := r.ipamDrivers[name]
if !ok {
if d == nil {
return "", "", fmt.Errorf("ipam %s not found", name)
}

return i.defaultLocalAddressSpace, i.defaultGlobalAddressSpace, nil
return d.GetDefaultAddressSpaces()
}

// GetPluginGetter returns the plugingetter
func (r *DrvRegistry) GetPluginGetter() plugingetter.PluginGetter {
return r.pluginGetter
}

// RegisterDriver registers the network driver when it gets discovered.
func (r *DrvRegistry) RegisterDriver(ntype string, driver driverapi.Driver, capability driverapi.Capability) error {
if strings.TrimSpace(ntype) == "" {
return errors.New("network type string cannot be empty")
}

r.Lock()
dd, ok := r.drivers[ntype]
r.Unlock()

if ok && dd.driver.IsBuiltIn() {
return driverapi.ErrActiveRegistration(ntype)
}

if r.dfn != nil {
if err := r.dfn(ntype, driver, capability); err != nil {
return err
}
}

dData := &driverData{driver, capability}

r.Lock()
r.drivers[ntype] = dData
r.Unlock()

return nil
}

func (r *DrvRegistry) registerIpamDriver(name string, driver ipamapi.Ipam, caps *ipamapi.Capability) error {
if strings.TrimSpace(name) == "" {
return errors.New("ipam driver name string cannot be empty")
}

r.Lock()
dd, ok := r.ipamDrivers[name]
r.Unlock()
if ok && dd.driver.IsBuiltIn() {
return types.ForbiddenErrorf("ipam driver %q already registered", name)
}
// Driver returns the network driver instance registered under name, and its capability.
func (r *DrvRegistry) Driver(name string) (driverapi.Driver, *driverapi.Capability) {
d, c := r.Networks.Driver(name)

locAS, glbAS, err := driver.GetDefaultAddressSpaces()
if err != nil {
return types.InternalErrorf("ipam driver %q failed to return default address spaces: %v", name, err)
if c == (driverapi.Capability{}) {
return d, nil
}

r.Lock()
r.ipamDrivers[name] = &ipamData{driver: driver, defaultLocalAddressSpace: locAS, defaultGlobalAddressSpace: glbAS, capability: caps}
r.Unlock()

return nil
}

// RegisterIpamDriver registers the IPAM driver discovered with default capabilities.
func (r *DrvRegistry) RegisterIpamDriver(name string, driver ipamapi.Ipam) error {
return r.registerIpamDriver(name, driver, &ipamapi.Capability{})
}

// RegisterIpamDriverWithCapabilities registers the IPAM driver discovered with specified capabilities.
func (r *DrvRegistry) RegisterIpamDriverWithCapabilities(name string, driver ipamapi.Ipam, caps *ipamapi.Capability) error {
return r.registerIpamDriver(name, driver, caps)
return d, &c
}
68 changes: 6 additions & 62 deletions libnetwork/drvregistry/drvregistry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"

"github.com/docker/docker/libnetwork/datastore"
"github.com/docker/docker/libnetwork/discoverapi"
"github.com/docker/docker/libnetwork/driverapi"
"github.com/docker/docker/libnetwork/ipamapi"
builtinIpam "github.com/docker/docker/libnetwork/ipams/builtin"
Expand All @@ -18,48 +17,16 @@ import (

const mockDriverName = "mock-driver"

type mockDriver struct{}

var md = mockDriver{}

func mockDriverInit(reg driverapi.DriverCallback, opt map[string]interface{}) error {
return reg.RegisterDriver(mockDriverName, &md, driverapi.Capability{DataScope: datastore.LocalScope})
}

func (m *mockDriver) CreateNetwork(nid string, options map[string]interface{}, nInfo driverapi.NetworkInfo, ipV4Data, ipV6Data []driverapi.IPAMData) error {
return nil
}

func (m *mockDriver) DeleteNetwork(nid string) error {
return nil
}

func (m *mockDriver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo, options map[string]interface{}) error {
return nil
}

func (m *mockDriver) DeleteEndpoint(nid, eid string) error {
return nil
type mockDriver struct {
driverapi.Driver
}

func (m *mockDriver) EndpointOperInfo(nid, eid string) (map[string]interface{}, error) {
return nil, nil
}
var mockDriverCaps = driverapi.Capability{DataScope: datastore.LocalScope}

func (m *mockDriver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo, options map[string]interface{}) error {
return nil
}

func (m *mockDriver) Leave(nid, eid string) error {
return nil
}

func (m *mockDriver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
return nil
}
var md = mockDriver{}

func (m *mockDriver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
return nil
func mockDriverInit(reg driverapi.DriverCallback, opt map[string]interface{}) error {
return reg.RegisterDriver(mockDriverName, &md, mockDriverCaps)
}

func (m *mockDriver) Type() string {
Expand All @@ -70,29 +37,6 @@ func (m *mockDriver) IsBuiltIn() bool {
return true
}

func (m *mockDriver) ProgramExternalConnectivity(nid, eid string, options map[string]interface{}) error {
return nil
}

func (m *mockDriver) RevokeExternalConnectivity(nid, eid string) error {
return nil
}

func (m *mockDriver) NetworkAllocate(id string, option map[string]string, ipV4Data, ipV6Data []driverapi.IPAMData) (map[string]string, error) {
return nil, nil
}

func (m *mockDriver) NetworkFree(id string) error {
return nil
}

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

func (m *mockDriver) DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string) {
return "", nil
}

func getNew(t *testing.T) *DrvRegistry {
reg, err := New(nil, nil, nil, nil, nil)
if err != nil {
Expand Down
Loading

0 comments on commit 5595311

Please sign in to comment.