github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/libnetwork/ipams/windowsipam/windowsipam.go (about)

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