Skip to content

Commit b0265ad

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 b0265ad

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,3 +1388,38 @@ func TestIPRangeListToCIDRsEdgeCases(t *testing.T) {
13881388
require.Equal(t, []string{"::/128"}, result)
13891389
})
13901390
}
1391+
1392+
func TestIPRangeListLen_WithNilReceiver(t *testing.T) {
1393+
// Background: In GetSubnetIPRangeString, subnet.V4Available can be nil for IPv6-only subnets.
1394+
// Calling Len() on nil receiver must not panic.
1395+
var nilList *IPRangeList
1396+
require.NotPanics(t, func() {
1397+
require.Equal(t, 0, nilList.Len())
1398+
})
1399+
}
1400+
1401+
func TestIPRangeListSeparate_WithNilReceiver(t *testing.T) {
1402+
// Background: In GetSubnetIPRangeString, we call V4Available.Separate(V4Using).
1403+
// For IPv6-only subnets, V4Available is nil. This must not panic.
1404+
var nilList *IPRangeList
1405+
other, err := NewIPRangeListFrom("10.0.0.1..10.0.0.10")
1406+
require.NoError(t, err)
1407+
1408+
var result *IPRangeList
1409+
require.NotPanics(t, func() {
1410+
result = nilList.Separate(other)
1411+
})
1412+
require.NotNil(t, result)
1413+
require.Equal(t, 0, result.Len())
1414+
}
1415+
1416+
func TestIPRangeListSeparate_WithEmptyList(t *testing.T) {
1417+
// An empty but non-nil list should also return empty result
1418+
emptyList := NewEmptyIPRangeList()
1419+
other, err := NewIPRangeListFrom("10.0.0.1..10.0.0.10")
1420+
require.NoError(t, err)
1421+
1422+
result := emptyList.Separate(other)
1423+
require.NotNil(t, result)
1424+
require.Equal(t, 0, result.Len())
1425+
}

0 commit comments

Comments
 (0)