Skip to content

Commit ddebbfd

Browse files
committed
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.)
1 parent a017253 commit ddebbfd

File tree

2 files changed

+11
-81
lines changed

2 files changed

+11
-81
lines changed

pkg/registry/core/service/ipallocator/allocator.go

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,17 @@ type Range struct {
8282

8383
// NewAllocatorCIDRRange creates a Range over a net.IPNet, calling allocatorFactory to construct the backing store.
8484
func NewAllocatorCIDRRange(cidr *net.IPNet, allocatorFactory allocator.AllocatorFactory) (*Range, error) {
85-
max := RangeSize(cidr)
86-
base := bigForIP(cidr.IP)
85+
max := utilnet.RangeSize(cidr)
86+
base := utilnet.BigForIP(cidr.IP)
8787
rangeSpec := cidr.String()
8888

89+
if utilnet.IsIPv6CIDR(cidr) {
90+
// Limit the max size, since the allocator keeps a bitmap of that size.
91+
if max > 65536 {
92+
max = 65536
93+
}
94+
}
95+
8996
r := Range{
9097
net: cidr,
9198
base: base.Add(base, big.NewInt(1)), // don't use the network base
@@ -171,7 +178,7 @@ func (r *Range) AllocateNext() (net.IP, error) {
171178
if !ok {
172179
return nil, ErrFull
173180
}
174-
return addIPOffset(r.base, offset), nil
181+
return utilnet.AddIPOffset(r.base, offset), nil
175182
}
176183

177184
// Release releases the IP back to the pool. Releasing an
@@ -247,40 +254,8 @@ func (r *Range) contains(ip net.IP) (bool, int) {
247254
return true, offset
248255
}
249256

250-
// bigForIP creates a big.Int based on the provided net.IP
251-
func bigForIP(ip net.IP) *big.Int {
252-
b := ip.To4()
253-
if b == nil {
254-
b = ip.To16()
255-
}
256-
return big.NewInt(0).SetBytes(b)
257-
}
258-
259-
// addIPOffset adds the provided integer offset to a base big.Int representing a
260-
// net.IP
261-
func addIPOffset(base *big.Int, offset int) net.IP {
262-
return net.IP(big.NewInt(0).Add(base, big.NewInt(int64(offset))).Bytes())
263-
}
264-
265257
// calculateIPOffset calculates the integer offset of ip from base such that
266258
// base + offset = ip. It requires ip >= base.
267259
func calculateIPOffset(base *big.Int, ip net.IP) int {
268-
return int(big.NewInt(0).Sub(bigForIP(ip), base).Int64())
269-
}
270-
271-
// RangeSize returns the size of a range in valid addresses.
272-
func RangeSize(subnet *net.IPNet) int64 {
273-
ones, bits := subnet.Mask.Size()
274-
if bits == 32 && (bits-ones) >= 31 || bits == 128 && (bits-ones) >= 127 {
275-
return 0
276-
}
277-
// For IPv6, the max size will be limited to 65536
278-
// This is due to the allocator keeping track of all the
279-
// allocated IP's in a bitmap. This will keep the size of
280-
// the bitmap to 64k.
281-
if bits == 128 && (bits-ones) >= 16 {
282-
return int64(1) << uint(16)
283-
} else {
284-
return int64(1) << uint(bits-ones)
285-
}
260+
return int(big.NewInt(0).Sub(utilnet.BigForIP(ip), base).Int64())
286261
}

pkg/registry/core/service/ipallocator/allocator_test.go

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -213,51 +213,6 @@ func TestAllocateSmall(t *testing.T) {
213213
t.Logf("allocated: %v", found)
214214
}
215215

216-
func TestRangeSize(t *testing.T) {
217-
testCases := []struct {
218-
name string
219-
cidr string
220-
addrs int64
221-
}{
222-
{
223-
name: "supported IPv4 cidr",
224-
cidr: "192.168.1.0/24",
225-
addrs: 256,
226-
},
227-
{
228-
name: "supported large IPv4 cidr",
229-
cidr: "10.96.0.0/12",
230-
addrs: 1048576,
231-
},
232-
{
233-
name: "unsupported IPv4 cidr",
234-
cidr: "192.168.1.0/1",
235-
addrs: 0,
236-
},
237-
{
238-
name: "supported IPv6 cidr",
239-
cidr: "2001:db8::/48",
240-
addrs: 65536,
241-
},
242-
{
243-
name: "unsupported IPv6 mask",
244-
cidr: "2001:db8::/1",
245-
addrs: 0,
246-
},
247-
}
248-
249-
for _, tc := range testCases {
250-
_, cidr, err := net.ParseCIDR(tc.cidr)
251-
if err != nil {
252-
t.Errorf("failed to parse cidr for test %s, unexpected error: '%s'", tc.name, err)
253-
}
254-
if size := RangeSize(cidr); size != tc.addrs {
255-
t.Errorf("test %s failed. %s should have a range size of %d, got %d",
256-
tc.name, tc.cidr, tc.addrs, size)
257-
}
258-
}
259-
}
260-
261216
func TestForEach(t *testing.T) {
262217
_, cidr, err := net.ParseCIDR("192.168.1.0/24")
263218
if err != nil {

0 commit comments

Comments
 (0)