builtin_unix.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. err := ipamutils.ConfigLocalScopeDefaultNetworks(GetDefaultIPAddressPool())
  31. if err != nil {
  32. return err
  33. }
  34. a, err := ipam.NewAllocator(localDs, globalDs)
  35. if err != nil {
  36. return err
  37. }
  38. cps := &ipamapi.Capability{RequiresRequestReplay: true}
  39. return ic.RegisterIpamDriverWithCapabilities(ipamapi.DefaultIPAM, a, cps)
  40. }
  41. // SetDefaultIPAddressPool stores default address pool.
  42. func SetDefaultIPAddressPool(addressPool []*ipamutils.NetworkToSplit) {
  43. defaultAddressPool = addressPool
  44. }
  45. // GetDefaultIPAddressPool returns default address pool.
  46. func GetDefaultIPAddressPool() []*ipamutils.NetworkToSplit {
  47. return defaultAddressPool
  48. }