Skip to content
Merged
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
6 changes: 5 additions & 1 deletion pkg/controller/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,13 @@ func (c *Controller) InitIPAM() error {
portName := ovs.PodNameToPortName(podName, pod.Namespace, podNet.ProviderName)
ip := pod.Annotations[fmt.Sprintf(util.IPAddressAnnotationTemplate, podNet.ProviderName)]
mac := pod.Annotations[fmt.Sprintf(util.MacAddressAnnotationTemplate, podNet.ProviderName)]
if ip == "" {
klog.Warningf("pod %s/%s has empty IP annotation for provider %s, skip IPAM init", pod.Namespace, podName, podNet.ProviderName)
continue
}
_, _, _, err := c.ipam.GetStaticAddress(key, portName, ip, &mac, podNet.Subnet.Name, true)
if err != nil {
klog.Errorf("failed to init pod %s.%s address %s: %v", podName, pod.Namespace, pod.Annotations[fmt.Sprintf(util.IPAddressAnnotationTemplate, podNet.ProviderName)], err)
klog.Errorf("failed to init pod %s.%s address %s: %v", podName, pod.Namespace, ip, err)
} else {
err = c.createOrUpdateIPCR(portName, podName, ip, mac, podNet.Subnet.Name, pod.Namespace, pod.Spec.NodeName, podType)
if err != nil {
Expand Down
24 changes: 21 additions & 3 deletions pkg/ipam/ip_range_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ func NewIPRangeListFrom(x ...string) (*IPRangeList, error) {
}

func (r *IPRangeList) Clone() *IPRangeList {
if r == nil {
return NewEmptyIPRangeList()
}
ret := &IPRangeList{make([]*IPRange, r.Len())}
for i := range r.ranges {
ret.ranges[i] = r.ranges[i].Clone()
Expand All @@ -85,10 +88,16 @@ func (r *IPRangeList) Clone() *IPRangeList {
}

func (r *IPRangeList) Len() int {
if r == nil {
return 0
}
return len(r.ranges)
}

func (r *IPRangeList) Count() internal.BigInt {
if r == nil {
return internal.BigInt{}
}
var count internal.BigInt
for _, v := range r.ranges {
count = count.Add(v.Count())
Expand All @@ -97,13 +106,16 @@ func (r *IPRangeList) Count() internal.BigInt {
}

func (r *IPRangeList) At(i int) *IPRange {
if i < len(r.ranges) {
return r.ranges[i]
if r == nil || i < 0 || i >= len(r.ranges) {
return nil
}
return nil
return r.ranges[i]
}

func (r *IPRangeList) Find(ip IP) (int, bool) {
if r == nil {
return 0, false
}
return sort.Find(len(r.ranges), func(i int) int {
if r.At(i).Start().GreaterThan(ip) {
return -1
Expand All @@ -121,6 +133,9 @@ func (r *IPRangeList) Contains(ip IP) bool {
}

func (r *IPRangeList) Add(ip IP) bool {
if r == nil {
return false
}
n, ok := r.Find(ip)
if ok {
return false
Expand All @@ -145,6 +160,9 @@ func (r *IPRangeList) Add(ip IP) bool {
}

func (r *IPRangeList) Remove(ip IP) bool {
if r == nil {
return false
}
n, ok := r.Find(ip)
if !ok {
return false
Expand Down
92 changes: 92 additions & 0 deletions pkg/ipam/ip_range_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1388,3 +1388,95 @@ func TestIPRangeListToCIDRsEdgeCases(t *testing.T) {
require.Equal(t, []string{"::/128"}, result)
})
}

func TestIPRangeList_NilReceiverSafety(t *testing.T) {
// Background: In single-stack subnets, V4Available or V6Available can be nil.
// All IPRangeList methods must handle nil receiver without panic.
var nilList *IPRangeList
ip, _ := NewIP("10.0.0.1")

t.Run("Len", func(t *testing.T) {
require.NotPanics(t, func() {
require.Equal(t, 0, nilList.Len())
})
})

t.Run("Count", func(t *testing.T) {
require.NotPanics(t, func() {
require.Equal(t, "0", nilList.Count().String())
})
})

t.Run("Clone", func(t *testing.T) {
var result *IPRangeList
require.NotPanics(t, func() {
result = nilList.Clone()
})
require.NotNil(t, result)
require.Equal(t, 0, result.Len())
})

t.Run("At", func(t *testing.T) {
require.NotPanics(t, func() {
require.Nil(t, nilList.At(0))
})
})

t.Run("Find", func(t *testing.T) {
require.NotPanics(t, func() {
idx, found := nilList.Find(ip)
require.Equal(t, 0, idx)
require.False(t, found)
})
})

t.Run("Contains", func(t *testing.T) {
require.NotPanics(t, func() {
require.False(t, nilList.Contains(ip))
})
})

t.Run("Add", func(t *testing.T) {
require.NotPanics(t, func() {
require.False(t, nilList.Add(ip))
})
})

t.Run("Remove", func(t *testing.T) {
require.NotPanics(t, func() {
require.False(t, nilList.Remove(ip))
})
})

t.Run("Separate", func(t *testing.T) {
var result *IPRangeList
require.NotPanics(t, func() {
result = nilList.Separate(nil)
})
require.NotNil(t, result)
require.Equal(t, 0, result.Len())
})

t.Run("String", func(t *testing.T) {
require.NotPanics(t, func() {
require.Equal(t, "", nilList.String())
})
})

t.Run("ToCIDRs", func(t *testing.T) {
require.NotPanics(t, func() {
cidrs, err := nilList.ToCIDRs()
require.NoError(t, err)
require.Nil(t, cidrs)
})
})
}

func TestIPRangeListSeparate_WithEmptyList(t *testing.T) {
// An empty but non-nil list returns empty result immediately without using 'other'
emptyList := NewEmptyIPRangeList()

result := emptyList.Separate(nil)
require.NotNil(t, result)
require.Equal(t, 0, result.Len())
}
Loading