Skip to content

Commit eb34207

Browse files
committed
make logic even simpler
1 parent d0ee275 commit eb34207

File tree

2 files changed

+13
-24
lines changed

2 files changed

+13
-24
lines changed

cloud/nodeipam/ipam/cloud_allocator.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -361,20 +361,12 @@ func getIPv6RangeFromLinodeInterface(iface linodego.LinodeInterface) string {
361361
// - For a /64 base, return {base64}:0:c::/112
362362
// - Only applies when desiredMask is /112 and the result is fully contained
363363
// in the base range. Otherwise, returns (nil, false) to signal fallback.
364-
func getIPv6PodCIDR(base *net.IPNet, desiredMask int) (*net.IPNet, bool) {
364+
func getIPv6PodCIDR(ip net.IP, desiredMask int) (*net.IPNet, bool) {
365365
// Some validation checks
366-
if base == nil || desiredMask != 112 {
367-
return nil, false
368-
}
369-
prefixLen, addrBits := base.Mask.Size()
370-
if addrBits != ipv6BitLen || prefixLen != ipv6PrefixLen64 { // must be a /64
366+
if ip == nil || desiredMask != 112 {
371367
return nil, false
372368
}
373369

374-
// create a copy to avoid modifying the original
375-
ip := make(net.IP, len(base.IP))
376-
copy(ip, base.IP)
377-
378370
// Keep first 64 bits (bytes 0..7) and set hextets 5..7 to 0, c, 0 respectively
379371
// Hextet index to byte mapping: h5->[8,9], h6->[10,11], h7->[12,13]
380372
ip[8], ip[9] = 0x00, 0x00 // :0
@@ -387,10 +379,6 @@ func getIPv6PodCIDR(base *net.IPNet, desiredMask int) (*net.IPNet, bool) {
387379
ip = ip.Mask(podMask)
388380
podCIDR := &net.IPNet{IP: ip, Mask: podMask}
389381

390-
if !base.Contains(ip) {
391-
return nil, false
392-
}
393-
394382
return podCIDR, true
395383
}
396384

@@ -464,16 +452,19 @@ func (c *cloudAllocator) allocateIPv6CIDR(ctx context.Context, node *v1.Node) (*
464452
return nil, fmt.Errorf("failed parsing ipv6 range %s: %w", ipv6Range, err)
465453
}
466454

467-
// Try stable subprefix selection first.
468-
if podCIDR, ok := getIPv6PodCIDR(base, c.nodeCIDRMaskSizeIPv6); ok {
469-
logger.V(4).Info("Using stable IPv6 PodCIDR subprefix :0:c::/112", "base", base, "podCIDR", podCIDR)
455+
// get pod cidr using stable mnemonic subprefix :0:c::/112
456+
if podCIDR, ok := getIPv6PodCIDR(ip, c.nodeCIDRMaskSizeIPv6); ok {
457+
logger.V(4).Info("Using stable IPv6 PodCIDR subprefix :0:c::/112", "ip", ip, "podCIDR", podCIDR)
458+
if !base.Contains(podCIDR.IP) {
459+
return nil, fmt.Errorf("stable IPv6 PodCIDR %s is not contained in base range %s", podCIDR, base)
460+
}
470461
return podCIDR, nil
471462
}
472463

473464
// Fallback to the original behavior: mask base IP directly to desired size
474465
podMask := net.CIDRMask(c.nodeCIDRMaskSizeIPv6, 128)
475466
fallbackPodCIDR := &net.IPNet{IP: ip.Mask(podMask), Mask: podMask}
476-
logger.V(4).Info("Falling back to start-of-range IPv6 PodCIDR", "base", base, "podCIDR", fallbackPodCIDR)
467+
logger.V(4).Info("Falling back to start-of-range IPv6 PodCIDR", "ip", ip, "podCIDR", fallbackPodCIDR)
477468
return fallbackPodCIDR, nil
478469
}
479470

cloud/nodeipam/ipam/cloud_allocator_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,18 @@ func TestComputeStableIPv6PodCIDR(t *testing.T) {
6060
}{
6161
{name: "nil base", baseCIDR: "", desiredMask: 112, wantOK: false},
6262
{name: "non-/112 desired", baseCIDR: "2300:5800:2:1::/64", desiredMask: 120, wantOK: false},
63-
{name: "ipv4 base", baseCIDR: "10.0.0.0/24", desiredMask: 112, wantOK: false},
64-
{name: "non-/64 ipv6 base (/56)", baseCIDR: "2300:5800:2::/56", desiredMask: 112, wantOK: false},
6563
{name: "success /64 -> mnemonic /112", baseCIDR: "2300:5800:2:1::/64", desiredMask: 112, wantCIDR: "2300:5800:2:1:0:c::/112", wantOK: true},
6664
} {
6765
t.Run(tc.name, func(t *testing.T) {
68-
var baseIPNet *net.IPNet
66+
var baseIP net.IP
6967
if tc.baseCIDR != "" {
70-
_, base, err := net.ParseCIDR(tc.baseCIDR)
68+
ip, _, err := net.ParseCIDR(tc.baseCIDR)
7169
if err != nil {
7270
t.Fatalf("parse base cidr: %v", err)
7371
}
74-
baseIPNet = base
72+
baseIP = ip
7573
}
76-
got, ok := getIPv6PodCIDR(baseIPNet, tc.desiredMask)
74+
got, ok := getIPv6PodCIDR(baseIP, tc.desiredMask)
7775
if ok != tc.wantOK {
7876
t.Fatalf("ok mismatch: got %v want %v (gotCIDR=%v)", ok, tc.wantOK, func() string {
7977
if got != nil {

0 commit comments

Comments
 (0)