github.com/docker/engine@v22.0.0-20211208180946-d456264580cf+incompatible/libnetwork/ipams/builtin/builtin_unix.go (about)

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