Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 0 additions & 46 deletions internal/net/allocator/allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,29 +146,6 @@ func (a *Allocator) IsAllocated(cidr string) bool {
return allocated
}

// ContainsCIDR returns true if the given CIDR falls within any of the allocator's pools.
func (a *Allocator) ContainsCIDR(cidr string) bool {
_, ipNet, err := net.ParseCIDR(cidr)
if err != nil {
return false
}

ip := ipNet.IP
for _, pool := range a.ipv4Pools {
if pool.Contains(ip) {
return true
}
}

for _, pool := range a.ipv6Pools {
if pool.Contains(ip) {
return true
}
}

return false
}

// AllocateIPv4 allocates the next available IPv4 CIDR from the pools.
// Returns ErrPoolExhausted if no CIDRs are available.
func (a *Allocator) AllocateIPv4() (string, error) {
Expand Down Expand Up @@ -406,26 +383,3 @@ func (a *Allocator) DebugState() AllocatorDebugState {

return state
}

// ParseCIDRs parses a slice of CIDR strings into net.IPNet objects.
func ParseCIDRs(cidrs []string) ([]*net.IPNet, error) {
klog.V(3).Infof("ParseCIDRs: parsing %d CIDR strings", len(cidrs))

result := make([]*net.IPNet, 0, len(cidrs))
for i, cidr := range cidrs {
klog.V(4).Infof("ParseCIDRs: parsing CIDR[%d]: %q", i, cidr)

_, ipNet, err := net.ParseCIDR(cidr)
if err != nil {
klog.Errorf("ParseCIDRs: failed to parse CIDR[%d] %q: %v", i, cidr, err)
return nil, fmt.Errorf("invalid CIDR %q: %w", cidr, err)
}

klog.V(4).Infof("ParseCIDRs: successfully parsed CIDR[%d]: %s", i, ipNet.String())
result = append(result, ipNet)
}

klog.V(3).Infof("ParseCIDRs: successfully parsed %d CIDRs", len(result))

return result, nil
}
55 changes: 40 additions & 15 deletions internal/net/allocator/allocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,32 @@

package allocator

import "testing"
import (
"fmt"
"net"
"testing"
)

// parseCIDRs turns CIDR strings into the net.IPNet slices NewAllocator takes.
//
// It used to be an exported helper on the allocator itself. Nothing in
// production ever called it - the live callers in site_controller.go and the
// webhook build their pools with splitCIDRBlocks - so it moved here, where its
// only callers already were.
func parseCIDRs(cidrs []string) ([]*net.IPNet, error) {
result := make([]*net.IPNet, 0, len(cidrs))

for _, cidr := range cidrs {
_, ipNet, err := net.ParseCIDR(cidr)
if err != nil {
return nil, fmt.Errorf("invalid CIDR %q: %w", cidr, err)
}

result = append(result, ipNet)
}

return result, nil
}

// TestNewAllocator tests new allocator.
func TestNewAllocator(t *testing.T) {
Expand Down Expand Up @@ -63,8 +88,8 @@ func TestNewAllocator(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ipv4Pools, _ := ParseCIDRs(tt.ipv4Pools)
ipv6Pools, _ := ParseCIDRs(tt.ipv6Pools)
ipv4Pools, _ := parseCIDRs(tt.ipv4Pools)
ipv6Pools, _ := parseCIDRs(tt.ipv6Pools)

_, err := NewAllocator(ipv4Pools, ipv6Pools, tt.ipv4MaskSize, tt.ipv6MaskSize)
if (err != nil) != tt.wantErr {
Expand All @@ -77,7 +102,7 @@ func TestNewAllocator(t *testing.T) {
// TestAllocateIPv4 tests allocate ipv4.
func TestAllocateIPv4(t *testing.T) {
// Test with /24 from /22 (4 possible /24s)
ipv4Pools, _ := ParseCIDRs([]string{"10.0.0.0/22"})
ipv4Pools, _ := parseCIDRs([]string{"10.0.0.0/22"})

alloc, err := NewAllocator(ipv4Pools, nil, 24, 0)
if err != nil {
Expand Down Expand Up @@ -114,7 +139,7 @@ func TestAllocateIPv4(t *testing.T) {
// TestAllocateIPv6 tests allocate ipv6.
func TestAllocateIPv6(t *testing.T) {
// Test with /64 from /62 (4 possible /64s)
ipv6Pools, _ := ParseCIDRs([]string{"fd00::/62"})
ipv6Pools, _ := parseCIDRs([]string{"fd00::/62"})

alloc, err := NewAllocator(nil, ipv6Pools, 0, 64)
if err != nil {
Expand Down Expand Up @@ -150,7 +175,7 @@ func TestAllocateIPv6(t *testing.T) {

// TestMarkAllocated tests mark allocated.
func TestMarkAllocated(t *testing.T) {
ipv4Pools, _ := ParseCIDRs([]string{"10.0.0.0/22"})
ipv4Pools, _ := parseCIDRs([]string{"10.0.0.0/22"})

alloc, err := NewAllocator(ipv4Pools, nil, 24, 0)
if err != nil {
Expand All @@ -175,7 +200,7 @@ func TestMarkAllocated(t *testing.T) {
// TestMultiplePools tests multiple pools.
func TestMultiplePools(t *testing.T) {
// Test with multiple pools
ipv4Pools, _ := ParseCIDRs([]string{"10.0.0.0/24", "10.1.0.0/24"})
ipv4Pools, _ := parseCIDRs([]string{"10.0.0.0/24", "10.1.0.0/24"})

alloc, err := NewAllocator(ipv4Pools, nil, 26, 0)
if err != nil {
Expand Down Expand Up @@ -224,7 +249,7 @@ func TestMultiplePools(t *testing.T) {
}
}

// TestParseCIDRs tests parse cidrs.
// TestParseCIDRs tests the local CIDR parsing helper.
func TestParseCIDRs(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -255,17 +280,17 @@ func TestParseCIDRs(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := ParseCIDRs(tt.cidrs)
_, err := parseCIDRs(tt.cidrs)
if (err != nil) != tt.wantErr {
t.Errorf("ParseCIDRs() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("parseCIDRs() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

// TestIsAllocated tests is allocated.
func TestIsAllocated(t *testing.T) {
ipv4Pools, _ := ParseCIDRs([]string{"10.0.0.0/16"})
ipv4Pools, _ := parseCIDRs([]string{"10.0.0.0/16"})
alloc, _ := NewAllocator(ipv4Pools, nil, 24, 0)

cidr := "10.0.5.0/24"
Expand All @@ -287,7 +312,7 @@ func TestIsAllocated(t *testing.T) {
// TestHasPools tests has pools.
func TestHasPools(t *testing.T) {
t.Run("IPv4 only", func(t *testing.T) {
ipv4Pools, _ := ParseCIDRs([]string{"10.0.0.0/16"})
ipv4Pools, _ := parseCIDRs([]string{"10.0.0.0/16"})
alloc, _ := NewAllocator(ipv4Pools, nil, 24, 0)

if !alloc.HasIPv4Pools() {
Expand All @@ -300,7 +325,7 @@ func TestHasPools(t *testing.T) {
})

t.Run("IPv6 only", func(t *testing.T) {
ipv6Pools, _ := ParseCIDRs([]string{"fd00::/48"})
ipv6Pools, _ := parseCIDRs([]string{"fd00::/48"})
alloc, _ := NewAllocator(nil, ipv6Pools, 0, 64)

if alloc.HasIPv4Pools() {
Expand All @@ -313,8 +338,8 @@ func TestHasPools(t *testing.T) {
})

t.Run("dual-stack", func(t *testing.T) {
ipv4Pools, _ := ParseCIDRs([]string{"10.0.0.0/16"})
ipv6Pools, _ := ParseCIDRs([]string{"fd00::/48"})
ipv4Pools, _ := parseCIDRs([]string{"10.0.0.0/16"})
ipv6Pools, _ := parseCIDRs([]string{"fd00::/48"})
alloc, _ := NewAllocator(ipv4Pools, ipv6Pools, 24, 64)

if !alloc.HasIPv4Pools() {
Expand Down
Loading
Loading