-
Notifications
You must be signed in to change notification settings - Fork 18.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libnetwork/drvregistry: split up the registries
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
Showing
7 changed files
with
315 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.