update for APIs being moved to utilnet

Several of the functions in pkg/registry/core/service/ipallocator were
moved to k8s.io/utils/net, but then the original code was never
updated to used to the vendored versions.

(utilnet's version of RangeSize does not have the IPv6 special case
that the original code did, so we need to move that to
NewAllocatorCIDRRange now.)
This commit is contained in:
Dan Winship 2020-03-12 11:27:48 -04:00 committed by Dan Winship
parent a017253ace
commit ddebbfd806
2 changed files with 11 additions and 81 deletions

View File

@ -82,10 +82,17 @@ type Range struct {
// NewAllocatorCIDRRange creates a Range over a net.IPNet, calling allocatorFactory to construct the backing store.
func NewAllocatorCIDRRange(cidr *net.IPNet, allocatorFactory allocator.AllocatorFactory) (*Range, error) {
max := RangeSize(cidr)
base := bigForIP(cidr.IP)
max := utilnet.RangeSize(cidr)
base := utilnet.BigForIP(cidr.IP)
rangeSpec := cidr.String()
if utilnet.IsIPv6CIDR(cidr) {
// Limit the max size, since the allocator keeps a bitmap of that size.
if max > 65536 {
max = 65536
}
}
r := Range{
net: cidr,
base: base.Add(base, big.NewInt(1)), // don't use the network base
@ -171,7 +178,7 @@ func (r *Range) AllocateNext() (net.IP, error) {
if !ok {
return nil, ErrFull
}
return addIPOffset(r.base, offset), nil
return utilnet.AddIPOffset(r.base, offset), nil
}
// Release releases the IP back to the pool. Releasing an
@ -247,40 +254,8 @@ func (r *Range) contains(ip net.IP) (bool, int) {
return true, offset
}
// bigForIP creates a big.Int based on the provided net.IP
func bigForIP(ip net.IP) *big.Int {
b := ip.To4()
if b == nil {
b = ip.To16()
}
return big.NewInt(0).SetBytes(b)
}
// addIPOffset adds the provided integer offset to a base big.Int representing a
// net.IP
func addIPOffset(base *big.Int, offset int) net.IP {
return net.IP(big.NewInt(0).Add(base, big.NewInt(int64(offset))).Bytes())
}
// calculateIPOffset calculates the integer offset of ip from base such that
// base + offset = ip. It requires ip >= base.
func calculateIPOffset(base *big.Int, ip net.IP) int {
return int(big.NewInt(0).Sub(bigForIP(ip), base).Int64())
}
// RangeSize returns the size of a range in valid addresses.
func RangeSize(subnet *net.IPNet) int64 {
ones, bits := subnet.Mask.Size()
if bits == 32 && (bits-ones) >= 31 || bits == 128 && (bits-ones) >= 127 {
return 0
}
// For IPv6, the max size will be limited to 65536
// This is due to the allocator keeping track of all the
// allocated IP's in a bitmap. This will keep the size of
// the bitmap to 64k.
if bits == 128 && (bits-ones) >= 16 {
return int64(1) << uint(16)
} else {
return int64(1) << uint(bits-ones)
}
return int(big.NewInt(0).Sub(utilnet.BigForIP(ip), base).Int64())
}

View File

@ -213,51 +213,6 @@ func TestAllocateSmall(t *testing.T) {
t.Logf("allocated: %v", found)
}
func TestRangeSize(t *testing.T) {
testCases := []struct {
name string
cidr string
addrs int64
}{
{
name: "supported IPv4 cidr",
cidr: "192.168.1.0/24",
addrs: 256,
},
{
name: "supported large IPv4 cidr",
cidr: "10.96.0.0/12",
addrs: 1048576,
},
{
name: "unsupported IPv4 cidr",
cidr: "192.168.1.0/1",
addrs: 0,
},
{
name: "supported IPv6 cidr",
cidr: "2001:db8::/48",
addrs: 65536,
},
{
name: "unsupported IPv6 mask",
cidr: "2001:db8::/1",
addrs: 0,
},
}
for _, tc := range testCases {
_, cidr, err := net.ParseCIDR(tc.cidr)
if err != nil {
t.Errorf("failed to parse cidr for test %s, unexpected error: '%s'", tc.name, err)
}
if size := RangeSize(cidr); size != tc.addrs {
t.Errorf("test %s failed. %s should have a range size of %d, got %d",
tc.name, tc.cidr, tc.addrs, size)
}
}
}
func TestForEach(t *testing.T) {
_, cidr, err := net.ParseCIDR("192.168.1.0/24")
if err != nil {