Merge pull request #115322 from aojea/hotfix
Fix panic on ClusterIP allocation for /28 subnets
This commit is contained in:
@@ -68,6 +68,7 @@ func NewAllocationMap(max int, rangeSpec string) *AllocationBitmap {
|
||||
// allows to pass an offset that divides the allocation bitmap in two blocks.
|
||||
// The first block of values will not be used for random value assigned by the AllocateNext()
|
||||
// method until the second block of values has been exhausted.
|
||||
// The offset value must be always smaller than the bitmap size.
|
||||
func NewAllocationMapWithOffset(max int, rangeSpec string, offset int) *AllocationBitmap {
|
||||
a := AllocationBitmap{
|
||||
strategy: randomScanStrategyWithOffset{
|
||||
|
@@ -83,6 +83,11 @@ func New(cidr *net.IPNet, allocatorFactory allocator.AllocatorWithOffsetFactory)
|
||||
base.Add(base, big.NewInt(1))
|
||||
max--
|
||||
|
||||
// cidr with whole mask can be negative
|
||||
if max < 0 {
|
||||
max = 0
|
||||
}
|
||||
|
||||
r := Range{
|
||||
net: cidr,
|
||||
base: base,
|
||||
@@ -357,7 +362,10 @@ func calculateRangeOffset(cidr *net.IPNet) int {
|
||||
)
|
||||
|
||||
cidrSize := netutils.RangeSize(cidr)
|
||||
if cidrSize < min {
|
||||
// available addresses are always less than the cidr size
|
||||
// A /28 CIDR returns 16 addresses, but 2 of them, the network
|
||||
// and broadcast addresses are not available.
|
||||
if cidrSize <= min {
|
||||
return 0
|
||||
}
|
||||
|
||||
|
@@ -798,8 +798,18 @@ func Test_calculateRangeOffset(t *testing.T) {
|
||||
{
|
||||
name: "small mask IPv4",
|
||||
cidr: "192.168.1.1/28",
|
||||
want: 0,
|
||||
},
|
||||
{
|
||||
name: "small mask IPv4",
|
||||
cidr: "192.168.1.1/27",
|
||||
want: 16,
|
||||
},
|
||||
{
|
||||
name: "small mask IPv6",
|
||||
cidr: "fd00::1/124",
|
||||
want: 0,
|
||||
},
|
||||
{
|
||||
name: "small mask IPv6",
|
||||
cidr: "fd00::1/122",
|
||||
|
Reference in New Issue
Block a user