builtin_unix.go 1.5 KB

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