Skip to content

Commit 0dbc2a6

Browse files
committed
fix: skip IPAM init for empty IP annotation and handle nil IPRangeList
- Skip IPAM initialization when pod IP annotation is empty, log warning instead - Make IPRangeList.Len() nil-safe to prevent panic in GetSubnetIPRangeString - Add unit test for nil IPRangeList.Separate() behavior Signed-off-by: zbb88888 <jmdxjsjgcxy@gmail.com>
1 parent 8978c18 commit 0dbc2a6

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

pkg/controller/init.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,10 @@ func (c *Controller) InitIPAM() error {
452452
portName := ovs.PodNameToPortName(podName, pod.Namespace, podNet.ProviderName)
453453
ip := pod.Annotations[fmt.Sprintf(util.IPAddressAnnotationTemplate, podNet.ProviderName)]
454454
mac := pod.Annotations[fmt.Sprintf(util.MacAddressAnnotationTemplate, podNet.ProviderName)]
455+
if ip == "" {
456+
klog.Warningf("pod %s/%s has empty IP annotation for provider %s, skip IPAM init", pod.Namespace, podName, podNet.ProviderName)
457+
continue
458+
}
455459
_, _, _, err := c.ipam.GetStaticAddress(key, portName, ip, &mac, podNet.Subnet.Name, true)
456460
if err != nil {
457461
klog.Errorf("failed to init pod %s.%s address %s: %v", podName, pod.Namespace, pod.Annotations[fmt.Sprintf(util.IPAddressAnnotationTemplate, podNet.ProviderName)], err)

pkg/ipam/ip_range_list.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ func (r *IPRangeList) Clone() *IPRangeList {
8585
}
8686

8787
func (r *IPRangeList) Len() int {
88+
if r == nil {
89+
return 0
90+
}
8891
return len(r.ranges)
8992
}
9093

pkg/ipam/ip_range_list_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,3 +1388,40 @@ func TestIPRangeListToCIDRsEdgeCases(t *testing.T) {
13881388
require.Equal(t, []string{"::/128"}, result)
13891389
})
13901390
}
1391+
1392+
func TestNilIPRangeListLen(t *testing.T) {
1393+
// Verify Len() returns 0 for nil receiver without panic
1394+
var nilList *IPRangeList
1395+
require.NotPanics(t, func() {
1396+
require.Equal(t, 0, nilList.Len())
1397+
})
1398+
}
1399+
1400+
func TestNilIPRangeListSeparate(t *testing.T) {
1401+
// This test verifies that calling Separate on a nil IPRangeList does not panic.
1402+
// In GetSubnetIPRangeString, subnet.V4Available or V6Available can be nil
1403+
// (e.g., for IPv6-only or IPv4-only subnets), and calling Separate on
1404+
// a nil receiver should not cause a crash.
1405+
var nilList *IPRangeList
1406+
other, err := NewIPRangeListFrom("10.0.0.1..10.0.0.10")
1407+
require.NoError(t, err)
1408+
1409+
// Explicitly verify no panic and returns empty list
1410+
var result *IPRangeList
1411+
require.NotPanics(t, func() {
1412+
result = nilList.Separate(other)
1413+
})
1414+
require.NotNil(t, result)
1415+
require.Equal(t, 0, result.Len())
1416+
}
1417+
1418+
func TestEmptyIPRangeListSeparate(t *testing.T) {
1419+
// Verify Separate on empty (non-nil) list also returns empty list
1420+
emptyList := NewEmptyIPRangeList()
1421+
other, err := NewIPRangeListFrom("10.0.0.1..10.0.0.10")
1422+
require.NoError(t, err)
1423+
1424+
result := emptyList.Separate(other)
1425+
require.NotNil(t, result)
1426+
require.Equal(t, 0, result.Len())
1427+
}

0 commit comments

Comments
 (0)