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

Code before:
	var prevAlloc netip.Prefix

	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 || (pdf.Base.Addr().Is4() && prefixSize > 32) || prefixSize > 128 {
				// The subnet size isn't valid for the pool
				continue
			} else {
				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.

Review comment (suggestion):
nit - the "else" isn't needed here ...

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

Code after:
	var prevAlloc netip.Prefix

	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.