Bladeren bron

libnetwork/ipam: Allocator.RequestPool: name args, output vars

network.requestPoolHelper and Allocator.RequestPool have many args and
output vars with generic types. Add names for them to make it easier to
grasp what's what.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 2 jaren geleden
bovenliggende
commit
87fc8c772b

+ 11 - 11
libnetwork/ipam/allocator.go

@@ -64,14 +64,14 @@ func (a *Allocator) GetDefaultAddressSpaces() (string, string, error) {
 
 // RequestPool returns an address pool along with its unique id.
 // addressSpace must be a valid address space name and must not be the empty string.
-// If pool is the empty string then the default predefined pool for addressSpace will be used, otherwise pool must be a valid IP address and length in CIDR notation.
-// If subPool is not empty, it must be a valid IP address and length in CIDR notation which is a sub-range of pool.
-// subPool must be empty if pool is empty.
-func (a *Allocator) RequestPool(addressSpace, pool, subPool string, _ map[string]string, v6 bool) (string, *net.IPNet, map[string]string, error) {
-	log.G(context.TODO()).Debugf("RequestPool(%s, %s, %s, _, %t)", addressSpace, pool, subPool, v6)
+// If requestedPool is the empty string then the default predefined pool for addressSpace will be used, otherwise pool must be a valid IP address and length in CIDR notation.
+// If requestedSubPool is not empty, it must be a valid IP address and length in CIDR notation which is a sub-range of requestedPool.
+// requestedSubPool must be empty if requestedPool is empty.
+func (a *Allocator) RequestPool(addressSpace, requestedPool, requestedSubPool string, _ map[string]string, v6 bool) (poolID string, pool *net.IPNet, meta map[string]string, err error) {
+	log.G(context.TODO()).Debugf("RequestPool(%s, %s, %s, _, %t)", addressSpace, requestedPool, requestedSubPool, v6)
 
 	parseErr := func(err error) error {
-		return types.InternalErrorf("failed to parse pool request for address space %q pool %q subpool %q: %v", addressSpace, pool, subPool, err)
+		return types.InternalErrorf("failed to parse pool request for address space %q pool %q subpool %q: %v", addressSpace, requestedPool, requestedSubPool, err)
 	}
 
 	if addressSpace == "" {
@@ -81,12 +81,12 @@ func (a *Allocator) RequestPool(addressSpace, pool, subPool string, _ map[string
 	if err != nil {
 		return "", nil, nil, err
 	}
-	if pool == "" && subPool != "" {
+	if requestedPool == "" && requestedSubPool != "" {
 		return "", nil, nil, parseErr(ipamapi.ErrInvalidSubPool)
 	}
 
 	k := PoolID{AddressSpace: addressSpace}
-	if pool == "" {
+	if requestedPool == "" {
 		k.Subnet, err = aSpace.allocatePredefinedPool(v6)
 		if err != nil {
 			return "", nil, nil, err
@@ -94,12 +94,12 @@ func (a *Allocator) RequestPool(addressSpace, pool, subPool string, _ map[string
 		return k.String(), toIPNet(k.Subnet), nil, nil
 	}
 
-	if k.Subnet, err = netip.ParsePrefix(pool); err != nil {
+	if k.Subnet, err = netip.ParsePrefix(requestedPool); err != nil {
 		return "", nil, nil, parseErr(ipamapi.ErrInvalidPool)
 	}
 
-	if subPool != "" {
-		k.ChildSubnet, err = netip.ParsePrefix(subPool)
+	if requestedSubPool != "" {
+		k.ChildSubnet, err = netip.ParsePrefix(requestedSubPool)
 		if err != nil {
 			return "", nil, nil, parseErr(ipamapi.ErrInvalidSubPool)
 		}

+ 4 - 4
libnetwork/ipamapi/contract.go

@@ -51,12 +51,12 @@ type Ipam interface {
 	// GetDefaultAddressSpaces returns the default local and global address spaces for this ipam
 	GetDefaultAddressSpaces() (string, string, error)
 	// RequestPool returns an address pool along with its unique id. Address space is a mandatory field
-	// which denotes a set of non-overlapping pools. pool describes the pool of addresses in CIDR notation.
-	// subpool indicates a smaller range of addresses from the pool, for now it is specified in CIDR notation.
-	// Both pool and subpool are non mandatory fields. When they are not specified, Ipam driver may choose to
+	// which denotes a set of non-overlapping pools. requestedPool describes the pool of addresses in CIDR notation.
+	// requestedSubPool indicates a smaller range of addresses from the pool, for now it is specified in CIDR notation.
+	// Both requestedPool and requestedSubPool are non-mandatory fields. When they are not specified, Ipam driver may choose to
 	// return a self chosen pool for this request. In such case the v6 flag needs to be set appropriately so
 	// that the driver would return the expected ip version pool.
-	RequestPool(addressSpace, pool, subPool string, options map[string]string, v6 bool) (string, *net.IPNet, map[string]string, error)
+	RequestPool(addressSpace, requestedPool, requestedSubPool string, options map[string]string, v6 bool) (poolID string, pool *net.IPNet, meta map[string]string, err error)
 	// ReleasePool releases the address pool identified by the passed id
 	ReleasePool(poolID string) error
 	// RequestAddress request an address from the specified pool ID. Input options or required IP can be passed.

+ 3 - 3
libnetwork/ipams/null/null.go

@@ -23,14 +23,14 @@ func (a *allocator) GetDefaultAddressSpaces() (string, string, error) {
 	return defaultAddressSpace, defaultAddressSpace, nil
 }
 
-func (a *allocator) RequestPool(addressSpace, pool, subPool string, _ map[string]string, v6 bool) (string, *net.IPNet, map[string]string, error) {
+func (a *allocator) RequestPool(addressSpace, requestedPool, requestedSubPool string, _ map[string]string, v6 bool) (string, *net.IPNet, map[string]string, error) {
 	if addressSpace != defaultAddressSpace {
 		return "", nil, nil, types.BadRequestErrorf("unknown address space: %s", addressSpace)
 	}
-	if pool != "" {
+	if requestedPool != "" {
 		return "", nil, nil, types.BadRequestErrorf("null ipam driver does not handle specific address pool requests")
 	}
-	if subPool != "" {
+	if requestedSubPool != "" {
 		return "", nil, nil, types.BadRequestErrorf("null ipam driver does not handle specific address subpool requests")
 	}
 	if v6 {

+ 2 - 2
libnetwork/ipams/remote/remote.go

@@ -116,8 +116,8 @@ func (a *allocator) GetDefaultAddressSpaces() (string, string, error) {
 }
 
 // RequestPool requests an address pool in the specified address space
-func (a *allocator) RequestPool(addressSpace, pool, subPool string, options map[string]string, v6 bool) (string, *net.IPNet, map[string]string, error) {
-	req := &api.RequestPoolRequest{AddressSpace: addressSpace, Pool: pool, SubPool: subPool, Options: options, V6: v6}
+func (a *allocator) RequestPool(addressSpace, requestedPool, requestedSubPool string, options map[string]string, v6 bool) (string, *net.IPNet, map[string]string, error) {
+	req := &api.RequestPoolRequest{AddressSpace: addressSpace, Pool: requestedPool, SubPool: requestedSubPool, Options: options, V6: v6}
 	res := &api.RequestPoolResponse{}
 	if err := a.call("RequestPool", req, res); err != nil {
 		return "", nil, nil, err

+ 5 - 5
libnetwork/ipams/windowsipam/windowsipam.go

@@ -32,17 +32,17 @@ func (a *allocator) GetDefaultAddressSpaces() (string, string, error) {
 
 // RequestPool returns an address pool along with its unique id. This is a null ipam driver. It allocates the
 // subnet user asked and does not validate anything. Doesn't support subpool allocation
-func (a *allocator) RequestPool(addressSpace, pool, subPool string, options map[string]string, v6 bool) (string, *net.IPNet, map[string]string, error) {
-	log.G(context.TODO()).Debugf("RequestPool(%s, %s, %s, %v, %t)", addressSpace, pool, subPool, options, v6)
-	if subPool != "" || v6 {
+func (a *allocator) RequestPool(addressSpace, requestedPool, requestedSubPool string, options map[string]string, v6 bool) (string, *net.IPNet, map[string]string, error) {
+	log.G(context.TODO()).Debugf("RequestPool(%s, %s, %s, %v, %t)", addressSpace, requestedPool, requestedSubPool, options, v6)
+	if requestedSubPool != "" || v6 {
 		return "", nil, nil, types.InternalErrorf("This request is not supported by null ipam driver")
 	}
 
 	var ipNet *net.IPNet
 	var err error
 
-	if pool != "" {
-		_, ipNet, err = net.ParseCIDR(pool)
+	if requestedPool != "" {
+		_, ipNet, err = net.ParseCIDR(requestedPool)
 		if err != nil {
 			return "", nil, nil, err
 		}

+ 5 - 5
libnetwork/network.go

@@ -1524,16 +1524,16 @@ func (n *Network) ipamAllocate() error {
 	return err
 }
 
-func (n *Network) requestPoolHelper(ipam ipamapi.Ipam, addressSpace, preferredPool, subPool string, options map[string]string, v6 bool) (string, *net.IPNet, map[string]string, error) {
+func (n *Network) requestPoolHelper(ipam ipamapi.Ipam, addressSpace, requestedPool, requestedSubPool string, options map[string]string, v6 bool) (poolID string, pool *net.IPNet, meta map[string]string, err error) {
 	for {
-		poolID, pool, meta, err := ipam.RequestPool(addressSpace, preferredPool, subPool, options, v6)
+		poolID, pool, meta, err = ipam.RequestPool(addressSpace, requestedPool, requestedSubPool, options, v6)
 		if err != nil {
 			return "", nil, nil, err
 		}
 
 		// If the network belongs to global scope or the pool was
 		// explicitly chosen or it is invalid, do not perform the overlap check.
-		if n.Scope() == scope.Global || preferredPool != "" || !types.IsIPNetValid(pool) {
+		if n.Scope() == scope.Global || requestedPool != "" || !types.IsIPNetValid(pool) {
 			return poolID, pool, meta, nil
 		}
 
@@ -1559,8 +1559,8 @@ func (n *Network) requestPoolHelper(ipam ipamapi.Ipam, addressSpace, preferredPo
 		// is local scope and there is an overlap, we fail the
 		// network creation right here. The pool will be
 		// released in the defer.
-		if preferredPool != "" {
-			return "", nil, nil, fmt.Errorf("requested subnet %s overlaps in the host", preferredPool)
+		if requestedPool != "" {
+			return "", nil, nil, fmt.Errorf("requested subnet %s overlaps in the host", requestedPool)
 		}
 	}
 }