Kaynağa Gözat

Merge pull request #46755 from corhere/libn/netip-overlaps

libnetwork/ipam: refactor prefix-overlap checks
Sebastiaan van Stijn 1 yıl önce
ebeveyn
işleme
fb3cc5e716
2 değiştirilmiş dosya ile 6 ekleme ve 5 silme
  1. 1 1
      libnetwork/ipam/allocator.go
  2. 5 4
      libnetwork/ipam/structures.go

+ 1 - 1
libnetwork/ipam/allocator.go

@@ -214,7 +214,7 @@ func (aSpace *addrSpace) allocatePredefinedPool(ipV6 bool) (netip.Prefix, error)
 		}
 		// Shouldn't be necessary, but check prevents IP collisions should
 		// predefined pools overlap for any reason.
-		if !aSpace.contains(nw) {
+		if !aSpace.overlaps(nw) {
 			aSpace.updatePredefinedStartIndex(i + 1)
 			err := aSpace.allocateSubnetL(nw, netip.Prefix{})
 			if err != nil {

+ 5 - 4
libnetwork/ipam/structures.go

@@ -107,7 +107,7 @@ func (aSpace *addrSpace) allocateSubnet(nw, sub netip.Prefix) error {
 func (aSpace *addrSpace) allocateSubnetL(nw, sub netip.Prefix) error {
 	// If master pool, check for overlap
 	if sub == (netip.Prefix{}) {
-		if aSpace.contains(nw) {
+		if aSpace.overlaps(nw) {
 			return ipamapi.ErrPoolOverlap
 		}
 		// This is a new master pool, add it along with corresponding bitmask
@@ -157,10 +157,11 @@ func (aSpace *addrSpace) releaseSubnet(nw, sub netip.Prefix) error {
 	return nil
 }
 
-// contains checks whether nw is a superset or subset of any of the existing subnets in this address space.
-func (aSpace *addrSpace) contains(nw netip.Prefix) bool {
+// overlaps reports whether nw contains any IP addresses in common with any of
+// the existing subnets in this address space.
+func (aSpace *addrSpace) overlaps(nw netip.Prefix) bool {
 	for pool := range aSpace.subnets {
-		if nw.Contains(pool.Addr()) || pool.Contains(nw.Addr()) {
+		if pool.Overlaps(nw) {
 			return true
 		}
 	}