moby/libnetwork/drvregistry/drvregistry.go
Cory Snider 5595311209 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>
2023-01-27 11:47:42 -05:00

71 lines
2.1 KiB
Go

package drvregistry
import (
"fmt"
"github.com/docker/docker/libnetwork/driverapi"
"github.com/docker/docker/libnetwork/ipamapi"
"github.com/docker/docker/pkg/plugingetter"
)
// DrvRegistry holds the registry of all network drivers and IPAM drivers that it knows about.
type DrvRegistry struct {
Networks
IPAMs
pluginGetter plugingetter.PluginGetter
}
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
// 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 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) {
return &DrvRegistry{
Networks: Networks{Notify: dfn},
pluginGetter: pg,
}, nil
}
// AddDriver adds a network driver to the registry.
//
// Deprecated: call fn(r, config) directly.
func (r *DrvRegistry) AddDriver(_ string, fn InitFunc, config map[string]interface{}) error {
return fn(r, config)
}
// 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) {
d, _ := r.IPAM(name)
if d == nil {
return "", "", fmt.Errorf("ipam %s not found", name)
}
return d.GetDefaultAddressSpaces()
}
// GetPluginGetter returns the plugingetter
func (r *DrvRegistry) GetPluginGetter() plugingetter.PluginGetter {
return r.pluginGetter
}
// 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)
if c == (driverapi.Capability{}) {
return d, nil
}
return d, &c
}