github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/libnetwork/ipams/builtin/builtin_unix.go (about)

     1  // +build linux freebsd solaris darwin
     2  
     3  package builtin
     4  
     5  import (
     6  	"fmt"
     7  
     8  	"github.com/docker/libnetwork/datastore"
     9  	"github.com/docker/libnetwork/ipam"
    10  	"github.com/docker/libnetwork/ipamapi"
    11  	"github.com/docker/libnetwork/ipamutils"
    12  )
    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  
    21  	if l != nil {
    22  		if localDs, ok = l.(datastore.DataStore); !ok {
    23  			return fmt.Errorf("incorrect local datastore passed to built-in ipam init")
    24  		}
    25  	}
    26  
    27  	if g != nil {
    28  		if globalDs, ok = g.(datastore.DataStore); !ok {
    29  			return fmt.Errorf("incorrect global datastore passed to built-in ipam init")
    30  		}
    31  	}
    32  
    33  	ipamutils.InitNetworks()
    34  
    35  	a, err := ipam.NewAllocator(localDs, globalDs)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	cps := &ipamapi.Capability{RequiresRequestReplay: true}
    41  
    42  	return ic.RegisterIpamDriverWithCapabilities(ipamapi.DefaultIPAM, a, cps)
    43  }