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

Code before:
	it := newMergeIter(aSpace.allocated, reserved, netiputil.PrefixCompare)

	makeAlloc := func(subnet netip.Prefix) netip.Prefix {
		// it.ia tracks the position of the mergeIter within aSpace.allocated.
		aSpace.allocated = slices.Insert(aSpace.allocated, it.ia, subnet)
		aSpace.subnets[subnet] = newPoolData(subnet)
		return subnet
	}

	// Filter the pools to only those that match the requested subnet size (if one is specified).
	var predefined []*ipamutils.NetworkToSplit
	for _, pdf := range aSpace.predefined {
		if subnetSize > 0 {
			if pdf.Base.Bits() > subnetSize || (pdf.Base.Addr().Is4() && subnetSize > 32) || subnetSize > 128 {
				// The subnet size isn't valid for the pool
				continue
			} else {
				predefined = append(predefined, &ipamutils.NetworkToSplit{
					Base: pdf.Base,
					Size: subnetSize,
				})
			}
		} else {
			predefined = append(predefined, pdf)
		}
	}

	if len(predefined) == 0 {
		// If we don't have any valid predefined networks
		return netip.Prefix{}, ipamapi.ErrInvalidPool
	}

	for {
		allocated := it.Get()
		if allocated == (netip.Prefix{}) {
			// We reached the end of both 'aSpace.allocated' and 'reserved'.
			break
		}

		if pdfID >= len(predefined) {
			// We ran out of predefined networks.
			return netip.Prefix{}, ipamapi.ErrNoMoreSubnets
		}
		pdf := predefined[pdfID]

		if pdf.Size < pdf.Base.Bits() {
			// The requested subnet size is larger than the network pool size.
			pdfID++
			continue
		} else if (pdf.Base.Addr().Is4() && pdf.Size > 32) || pdf.Size > 128 {
			pdfID++

Review comment (suggestion):
I don't think the slice needs to be copied when subnetSize is zero? (And the "else" isn't needed.) So, maybe ...

```
	// Filter the pools to only those that match the requested subnet size (if one is specified).
	var predefined []*ipamutils.NetworkToSplit
	if subnetSize == 0 {
		predefined = aSpace.predefined
	} else {
		for _, pdf := range aSpace.predefined {
			if pdf.Base.Bits() > subnetSize || (pdf.Base.Addr().Is4() && subnetSize > 32) || subnetSize > 128 {
				// The subnet size isn't valid for the pool
				continue
			}
			predefined = append(predefined, &ipamutils.NetworkToSplit{
				Base: pdf.Base,
				Size: subnetSize,
			})
		}
	}
```

Code after:
	it := newMergeIter(aSpace.allocated, reserved, netiputil.PrefixCompare)

	makeAlloc := func(subnet netip.Prefix) netip.Prefix {
		// it.ia tracks the position of the mergeIter within aSpace.allocated.
		aSpace.allocated = slices.Insert(aSpace.allocated, it.ia, subnet)
		aSpace.subnets[subnet] = newPoolData(subnet)
		return subnet
	}

	// Filter the pools to only those that match the requested subnet size (if one is specified).
	var predefined []*ipamutils.NetworkToSplit
	if prefixSize == 0 {
		predefined = aSpace.predefined
	} else {
		for _, pdf := range aSpace.predefined {
			if pdf.Base.Bits() > prefixSize || prefixSize > pdf.Base.Addr().BitLen() {
				// The subnet size isn't valid for the pool
				continue
			}

			predefined = append(predefined, &ipamutils.NetworkToSplit{
				Base: pdf.Base,
				Size: prefixSize,
			})
		}
	}

	if len(predefined) == 0 {
		// If we don't have any valid predefined networks
		return netip.Prefix{}, ipamapi.ErrInvalidPool
	}

	for {
		allocated := it.Get()
		if allocated == (netip.Prefix{}) {
			// We reached the end of both 'aSpace.allocated' and 'reserved'.
			break
		}

		if pdfID >= len(predefined) {
			// We ran out of predefined networks.
			return netip.Prefix{}, ipamapi.ErrNoMoreSubnets
		}
		pdf := predefined[pdfID]

		if allocated.Overlaps(pdf.Base) {
			if allocated.Bits() <= pdf.Base.Bits() {
				// The current 'allocated' prefix is bigger than the 'pdf'
				// network, thus the block is fully overlapped.
				partialOverlap = false
				prevAlloc = netip.Prefix{}