Files
moby/libnetwork/ipams/null/null.go
Cory Snider d478e13639 libnet: un-plumb datastores from IPAM inits
The datastore arguments to the IPAM driver Init() functions are always
nil, even in Swarmkit. The only IPAM driver which consumed the
datastores was builtin; all others (null, remote, windowsipam) simply
ignored it. As the signatures of the IPAM driver init functions cannot
be changed without breaking the Swarmkit build, they have to be left
with the same signatures for the time being. Assert that nil datastores
are always passed into the builtin IPAM driver's init function so that
there is no ambiguity the datastores are no longer respected.

Add new Register functions for the IPAM drivers which are free from the
legacy baggage of the Init functions. (The legacy Init functions can be
removed once Swarmkit is migrated to using the Register functions.) As
the remote IPAM driver is the only one which depends on a PluginGetter,
pass it in explicitly as an argument to Register. The other IPAM drivers
should not be forced to depend on a GetPluginGetter() method they do not
use (Interface Segregation Principle).

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-27 11:47:42 -05:00

83 lines
2.4 KiB
Go

// Package null implements the null ipam driver. Null ipam driver satisfies ipamapi contract,
// but does not effectively reserve/allocate any address pool or address
package null
import (
"fmt"
"net"
"github.com/docker/docker/libnetwork/discoverapi"
"github.com/docker/docker/libnetwork/ipamapi"
"github.com/docker/docker/libnetwork/types"
)
var (
defaultAS = "null"
defaultPool, _ = types.ParseCIDR("0.0.0.0/0")
defaultPoolID = fmt.Sprintf("%s/%s", defaultAS, defaultPool.String())
)
type allocator struct{}
func (a *allocator) GetDefaultAddressSpaces() (string, string, error) {
return defaultAS, defaultAS, nil
}
func (a *allocator) RequestPool(addressSpace, pool, subPool string, options map[string]string, v6 bool) (string, *net.IPNet, map[string]string, error) {
if addressSpace != defaultAS {
return "", nil, nil, types.BadRequestErrorf("unknown address space: %s", addressSpace)
}
if pool != "" {
return "", nil, nil, types.BadRequestErrorf("null ipam driver does not handle specific address pool requests")
}
if subPool != "" {
return "", nil, nil, types.BadRequestErrorf("null ipam driver does not handle specific address subpool requests")
}
if v6 {
return "", nil, nil, types.BadRequestErrorf("null ipam driver does not handle IPv6 address pool pool requests")
}
return defaultPoolID, defaultPool, nil, nil
}
func (a *allocator) ReleasePool(poolID string) error {
return nil
}
func (a *allocator) RequestAddress(poolID string, ip net.IP, opts map[string]string) (*net.IPNet, map[string]string, error) {
if poolID != defaultPoolID {
return nil, nil, types.BadRequestErrorf("unknown pool id: %s", poolID)
}
return nil, nil, nil
}
func (a *allocator) ReleaseAddress(poolID string, ip net.IP) error {
if poolID != defaultPoolID {
return types.BadRequestErrorf("unknown pool id: %s", poolID)
}
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
}
// Init registers the null ipam driver with ic.
//
// Deprecated: use [Register].
func Init(ic ipamapi.Callback, l, g interface{}) error {
return Register(ic)
}
// Register registers the null ipam driver with r.
func Register(r ipamapi.Registerer) error {
return r.RegisterIpamDriver(ipamapi.NullIPAM, &allocator{})
}