builtin_unix.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // +build linux freebsd darwin
  2. package builtin
  3. import (
  4. "errors"
  5. "github.com/docker/libnetwork/datastore"
  6. "github.com/docker/libnetwork/ipam"
  7. "github.com/docker/libnetwork/ipamapi"
  8. "github.com/docker/libnetwork/ipamutils"
  9. )
  10. var (
  11. // defaultAddressPool Stores user configured subnet list
  12. defaultAddressPool []*ipamutils.NetworkToSplit
  13. )
  14. // Init registers the built-in ipam service with libnetwork
  15. func Init(ic ipamapi.Callback, l, g interface{}) error {
  16. var (
  17. ok bool
  18. localDs, globalDs datastore.DataStore
  19. )
  20. if l != nil {
  21. if localDs, ok = l.(datastore.DataStore); !ok {
  22. return errors.New("incorrect local datastore passed to built-in ipam init")
  23. }
  24. }
  25. if g != nil {
  26. if globalDs, ok = g.(datastore.DataStore); !ok {
  27. return errors.New("incorrect global datastore passed to built-in ipam init")
  28. }
  29. }
  30. ipamutils.ConfigLocalScopeDefaultNetworks(GetDefaultIPAddressPool())
  31. a, err := ipam.NewAllocator(localDs, globalDs)
  32. if err != nil {
  33. return err
  34. }
  35. cps := &ipamapi.Capability{RequiresRequestReplay: true}
  36. return ic.RegisterIpamDriverWithCapabilities(ipamapi.DefaultIPAM, a, cps)
  37. }
  38. // SetDefaultIPAddressPool stores default address pool.
  39. func SetDefaultIPAddressPool(addressPool []*ipamutils.NetworkToSplit) {
  40. defaultAddressPool = addressPool
  41. }
  42. // GetDefaultIPAddressPool returns default address pool.
  43. func GetDefaultIPAddressPool() []*ipamutils.NetworkToSplit {
  44. return defaultAddressPool
  45. }