Repository: moby/moby
File: daemon/libnetwork/ipams/defaultipam/allocator.go (Go)

Code before:
	return localAddressSpace, globalAddressSpace, nil
}

// 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 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(req ipamapi.PoolRequest) (ipamapi.AllocatedPool, error) {
	log.G(context.TODO()).Debugf("RequestPool: %+v", req)

	parseErr := func(err error) error {
		return types.InternalErrorf("failed to parse pool request for address space %q pool %q subpool %q: %v", req.AddressSpace, req.Pool, req.SubPool, err)
	}

	if req.AddressSpace == "" {
		return ipamapi.AllocatedPool{}, parseErr(ipamapi.ErrInvalidAddressSpace)
	}
	aSpace, err := a.getAddrSpace(req.AddressSpace, req.V6)
	if err != nil {
		return ipamapi.AllocatedPool{}, err
	}

	k := PoolID{AddressSpace: req.AddressSpace}

	subnetSize := 0

	if req.Pool != "" {
		if strings.HasPrefix(req.Pool, "/") {
			if req.V6 {
				req.Pool = "::" + req.Pool
			} else {
				req.Pool = "0.0.0.0" + req.Pool
			}
		}

		prefix, err := netip.ParsePrefix(req.Pool)
		if err != nil {
			return ipamapi.AllocatedPool{}, parseErr(ipamapi.ErrInvalidPool)
		}

		if prefix.Addr().IsUnspecified() {
			// If the prefix is unspecified, we're only interested in the prefix size.
			// We'll attempt to use the specified size to allocate a subnet from the
			// predefined pools.
			req.Pool = ""

			if req.Options == nil {
				req.Options = make(map[string]string)
			}


Review comment (suggestion):
Maybe just me, but here and elsewhere ... `prefixLen` might be a better name than `subnetSize`? (It's not the number of addresses in the subnet.)

Code after:
}

// 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 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(req ipamapi.PoolRequest) (ipamapi.AllocatedPool, error) {
	log.G(context.TODO()).Debugf("RequestPool: %+v", req)

	parseErr := func(err error) error {
		return types.InternalErrorf("failed to parse pool request for address space %q pool %q subpool %q: %v", req.AddressSpace, req.Pool, req.SubPool, err)
	}

	if req.AddressSpace == "" {
		return ipamapi.AllocatedPool{}, parseErr(ipamapi.ErrInvalidAddressSpace)
	}
	aSpace, err := a.getAddrSpace(req.AddressSpace, req.V6)
	if err != nil {
		return ipamapi.AllocatedPool{}, err
	}

	k := PoolID{AddressSpace: req.AddressSpace}

	prefixLength := 0

	if req.Pool != "" {
		prefix, err := netip.ParsePrefix(req.Pool)
		if err != nil {
			return ipamapi.AllocatedPool{}, parseErr(ipamapi.ErrInvalidPool)
		}

		if prefix.Addr().IsUnspecified() {
			// If the prefix is unspecified, we're only interested in the prefix size.
			// We'll attempt to use the specified size to allocate a subnet from the
			// predefined pools.
			req.Pool = ""

			if prefix.Bits() > 0 {
				prefixLength = prefix.Bits()
			}
		} else {
			k.Subnet = prefix
		}
	}

	if req.Pool == "" && req.SubPool != "" {
		return ipamapi.AllocatedPool{}, parseErr(ipamapi.ErrInvalidSubPool)
	}

	if req.Pool == "" {