From f6dcc1c07e0a2d3c583cb90e1cdb7ec4718625ce Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Wed, 8 Apr 2020 12:44:38 -0400 Subject: [PATCH] Minor tweak to IPv6 service IP allocation The service allocator skips the "broadcast address" in the service CIDR, but that concept only applies to IPv4 addressing. --- pkg/registry/core/service/ipallocator/allocator.go | 11 +++++++++-- .../core/service/ipallocator/allocator_test.go | 11 +++++------ .../service/ipallocator/controller/repair_test.go | 2 +- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/pkg/registry/core/service/ipallocator/allocator.go b/pkg/registry/core/service/ipallocator/allocator.go index 4ff99757d18..634569f767a 100644 --- a/pkg/registry/core/service/ipallocator/allocator.go +++ b/pkg/registry/core/service/ipallocator/allocator.go @@ -91,12 +91,19 @@ func NewAllocatorCIDRRange(cidr *net.IPNet, allocatorFactory allocator.Allocator if max > 65536 { max = 65536 } + } else { + // Don't use the IPv4 network's broadcast address. + max-- } + // Don't use the network's ".0" address. + base.Add(base, big.NewInt(1)) + max-- + r := Range{ net: cidr, - base: base.Add(base, big.NewInt(1)), // don't use the network base - max: maximum(0, int(max-2)), // don't use the network broadcast, + base: base, + max: maximum(0, int(max)), } var err error r.alloc, err = allocatorFactory(r.max, rangeSpec) diff --git a/pkg/registry/core/service/ipallocator/allocator_test.go b/pkg/registry/core/service/ipallocator/allocator_test.go index e1f93dee2cd..a66b1f51575 100644 --- a/pkg/registry/core/service/ipallocator/allocator_test.go +++ b/pkg/registry/core/service/ipallocator/allocator_test.go @@ -49,14 +49,13 @@ func TestAllocate(t *testing.T) { { name: "IPv6", cidr: "2001:db8:1::/48", - free: 65534, + free: 65535, released: "2001:db8:1::5", outOfRange: []string{ - "2001:db8::1", // not in 2001:db8:1::/48 - "2001:db8:1::", // reserved (base address) - "2001:db8:1::ffff", // reserved (broadcast address) - "2001:db8:1::1:0", // not in the low 16 bits of 2001:db8:1::/48 - "2001:db8:2::2", // not in 2001:db8:1::/48 + "2001:db8::1", // not in 2001:db8:1::/48 + "2001:db8:1::", // reserved (base address) + "2001:db8:1::1:0", // not in the low 16 bits of 2001:db8:1::/48 + "2001:db8:2::2", // not in 2001:db8:1::/48 }, alreadyAllocated: "2001:db8:1::1", }, diff --git a/pkg/registry/core/service/ipallocator/controller/repair_test.go b/pkg/registry/core/service/ipallocator/controller/repair_test.go index 26ef1bd34c7..9b83282e5c5 100644 --- a/pkg/registry/core/service/ipallocator/controller/repair_test.go +++ b/pkg/registry/core/service/ipallocator/controller/repair_test.go @@ -529,7 +529,7 @@ func TestRepairWithExistingDualStack(t *testing.T) { if !secondaryAfter.Has(net.ParseIP("2000::1")) || !secondaryAfter.Has(net.ParseIP("2000::2")) { t.Errorf("unexpected ipallocator state: %#v", secondaryAfter) } - if free := secondaryAfter.Free(); free != 65532 { + if free := secondaryAfter.Free(); free != 65533 { t.Errorf("unexpected ipallocator state: %d free (number of free ips is not 65532)", free) }