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

     1  package windowsipam
     2  
     3  import (
     4  	"net"
     5  
     6  	log "github.com/Sirupsen/logrus"
     7  	"github.com/docker/libnetwork/discoverapi"
     8  	"github.com/docker/libnetwork/ipamapi"
     9  	"github.com/docker/libnetwork/netlabel"
    10  	"github.com/docker/libnetwork/types"
    11  )
    12  
    13  const (
    14  	localAddressSpace  = "LocalDefault"
    15  	globalAddressSpace = "GlobalDefault"
    16  )
    17  
    18  var (
    19  	defaultPool, _ = types.ParseCIDR("0.0.0.0/0")
    20  )
    21  
    22  type allocator struct {
    23  }
    24  
    25  // GetInit registers the built-in ipam service with libnetwork
    26  func GetInit(ipamName string) func(ic ipamapi.Callback, l, g interface{}) error {
    27  	return func(ic ipamapi.Callback, l, g interface{}) error {
    28  		return ic.RegisterIpamDriver(ipamName, &allocator{})
    29  	}
    30  }
    31  
    32  func (a *allocator) GetDefaultAddressSpaces() (string, string, error) {
    33  	return localAddressSpace, globalAddressSpace, nil
    34  }
    35  
    36  // RequestPool returns an address pool along with its unique id. This is a null ipam driver. It allocates the
    37  // subnet user asked and does not validate anything. Doesn't support subpool allocation
    38  func (a *allocator) RequestPool(addressSpace, pool, subPool string, options map[string]string, v6 bool) (string, *net.IPNet, map[string]string, error) {
    39  	log.Debugf("RequestPool(%s, %s, %s, %v, %t)", addressSpace, pool, subPool, options, v6)
    40  	if subPool != "" || v6 {
    41  		return "", nil, nil, types.InternalErrorf("This request is not supported by null ipam driver")
    42  	}
    43  
    44  	var ipNet *net.IPNet
    45  	var err error
    46  
    47  	if pool != "" {
    48  		_, ipNet, err = net.ParseCIDR(pool)
    49  		if err != nil {
    50  			return "", nil, nil, err
    51  		}
    52  	} else {
    53  		ipNet = defaultPool
    54  	}
    55  
    56  	return ipNet.String(), ipNet, nil, nil
    57  }
    58  
    59  // ReleasePool releases the address pool - always succeeds
    60  func (a *allocator) ReleasePool(poolID string) error {
    61  	log.Debugf("ReleasePool(%s)", poolID)
    62  	return nil
    63  }
    64  
    65  // RequestAddress returns an address from the specified pool ID.
    66  // Always allocate the 0.0.0.0/32 ip if no preferred address was specified
    67  func (a *allocator) RequestAddress(poolID string, prefAddress net.IP, opts map[string]string) (*net.IPNet, map[string]string, error) {
    68  	log.Debugf("RequestAddress(%s, %v, %v)", poolID, prefAddress, opts)
    69  	_, ipNet, err := net.ParseCIDR(poolID)
    70  
    71  	if err != nil {
    72  		return nil, nil, err
    73  	}
    74  
    75  	// TODO Windows: Remove this once the bug in docker daemon is fixed
    76  	// that causes it to throw an exception on nil gateway
    77  	if prefAddress != nil {
    78  		return &net.IPNet{IP: prefAddress, Mask: ipNet.Mask}, nil, nil
    79  	} else if opts[ipamapi.RequestAddressType] == netlabel.Gateway {
    80  		return ipNet, nil, nil
    81  	} else {
    82  		return nil, nil, nil
    83  	}
    84  }
    85  
    86  // ReleaseAddress releases the address - always succeeds
    87  func (a *allocator) ReleaseAddress(poolID string, address net.IP) error {
    88  	log.Debugf("ReleaseAddress(%s, %v)", poolID, address)
    89  	return nil
    90  }
    91  
    92  // DiscoverNew informs the allocator about a new global scope datastore
    93  func (a *allocator) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
    94  	return nil
    95  }
    96  
    97  // DiscoverDelete is a notification of no interest for the allocator
    98  func (a *allocator) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
    99  	return nil
   100  }