2021-08-23 13:14:53 +00:00
|
|
|
//go:build linux || freebsd || darwin
|
2017-10-25 13:33:06 +00:00
|
|
|
// +build linux freebsd darwin
|
2016-01-30 01:08:11 +00:00
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
package builtin
|
|
|
|
|
|
|
|
import (
|
2016-11-22 01:29:53 +00:00
|
|
|
"errors"
|
2015-09-22 20:20:55 +00:00
|
|
|
|
2021-04-06 00:24:47 +00:00
|
|
|
"github.com/docker/docker/libnetwork/datastore"
|
|
|
|
"github.com/docker/docker/libnetwork/ipam"
|
|
|
|
"github.com/docker/docker/libnetwork/ipamapi"
|
|
|
|
"github.com/docker/docker/libnetwork/ipamutils"
|
2015-09-22 20:20:55 +00:00
|
|
|
)
|
|
|
|
|
2018-01-18 22:30:51 +00:00
|
|
|
var (
|
|
|
|
// defaultAddressPool Stores user configured subnet list
|
|
|
|
defaultAddressPool []*ipamutils.NetworkToSplit
|
|
|
|
)
|
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
// Init registers the built-in ipam service with libnetwork
|
|
|
|
func Init(ic ipamapi.Callback, l, g interface{}) error {
|
|
|
|
var (
|
|
|
|
ok bool
|
|
|
|
localDs, globalDs datastore.DataStore
|
|
|
|
)
|
|
|
|
|
|
|
|
if l != nil {
|
|
|
|
if localDs, ok = l.(datastore.DataStore); !ok {
|
2016-11-22 01:29:53 +00:00
|
|
|
return errors.New("incorrect local datastore passed to built-in ipam init")
|
2015-09-22 20:20:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if g != nil {
|
|
|
|
if globalDs, ok = g.(datastore.DataStore); !ok {
|
2016-11-22 01:29:53 +00:00
|
|
|
return errors.New("incorrect global datastore passed to built-in ipam init")
|
2015-09-22 20:20:55 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-04 13:50:26 +00:00
|
|
|
|
2020-03-17 16:09:10 +00:00
|
|
|
err := ipamutils.ConfigLocalScopeDefaultNetworks(GetDefaultIPAddressPool())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-04 13:50:26 +00:00
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
a, err := ipam.NewAllocator(localDs, globalDs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-18 22:11:36 +00:00
|
|
|
cps := &ipamapi.Capability{RequiresRequestReplay: true}
|
|
|
|
|
|
|
|
return ic.RegisterIpamDriverWithCapabilities(ipamapi.DefaultIPAM, a, cps)
|
2015-09-22 20:20:55 +00:00
|
|
|
}
|
2018-01-18 22:30:51 +00:00
|
|
|
|
|
|
|
// SetDefaultIPAddressPool stores default address pool.
|
|
|
|
func SetDefaultIPAddressPool(addressPool []*ipamutils.NetworkToSplit) {
|
|
|
|
defaultAddressPool = addressPool
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDefaultIPAddressPool returns default address pool.
|
|
|
|
func GetDefaultIPAddressPool() []*ipamutils.NetworkToSplit {
|
|
|
|
return defaultAddressPool
|
|
|
|
}
|