Skip to content

Commit

Permalink
ipam: fix IPRangeList clone (#3979)
Browse files Browse the repository at this point in the history
Signed-off-by: zhangzujian <[email protected]>
  • Loading branch information
zhangzujian authored May 6, 2024
1 parent 8a38cfa commit 5fa123a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
9 changes: 6 additions & 3 deletions pkg/ipam/ip_range_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ipam
import (
"fmt"
"net"
"slices"
"sort"
"strings"

Expand Down Expand Up @@ -70,7 +71,9 @@ func NewIPRangeListFrom(x ...string) (*IPRangeList, error) {

func (r *IPRangeList) Clone() *IPRangeList {
ret := &IPRangeList{make([]*IPRange, r.Len())}
copy(ret.ranges, r.ranges)
for i := range r.ranges {
ret.ranges[i] = r.ranges[i].Clone()
}
return ret
}

Expand Down Expand Up @@ -120,7 +123,7 @@ func (r *IPRangeList) Add(ip IP) bool {
(n < r.Len() && r.ranges[n].Add(ip)) {
if n-1 >= 0 && n < r.Len() && r.ranges[n-1].End().Add(1).Equal(r.ranges[n].Start()) {
r.ranges[n-1].SetEnd(r.ranges[n].End())
r.ranges = append(r.ranges[:n], r.ranges[n+1:]...)
r.ranges = slices.Delete(r.ranges, n, n+1)
}
return true
}
Expand All @@ -143,7 +146,7 @@ func (r *IPRangeList) Remove(ip IP) bool {
v, _ := r.ranges[n].Remove(ip)
switch len(v) {
case 0:
r.ranges = append(r.ranges[:n], r.ranges[n+1:]...)
r.ranges = slices.Delete(r.ranges, n, n+1)
case 1:
r.ranges[n] = v[0]
case 2:
Expand Down
23 changes: 13 additions & 10 deletions pkg/ipam/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func NewIPAM() *IPAM {
}

func (ipam *IPAM) GetRandomAddress(podName, nicName string, mac *string, subnetName, poolName string, skippedAddrs []string, checkConflict bool) (string, string, string, error) {
ipam.mutex.RLock()
defer ipam.mutex.RUnlock()
ipam.mutex.Lock()
defer ipam.mutex.Unlock()
var v4, v6 string
subnet, ok := ipam.Subnets[subnetName]
if !ok {
Expand All @@ -65,8 +65,8 @@ func (ipam *IPAM) GetRandomAddress(podName, nicName string, mac *string, subnetN
}

func (ipam *IPAM) GetStaticAddress(podName, nicName, ip string, mac *string, subnetName string, checkConflict bool) (string, string, string, error) {
ipam.mutex.RLock()
defer ipam.mutex.RUnlock()
ipam.mutex.Lock()
defer ipam.mutex.Unlock()
var subnet *Subnet
var ok bool
klog.Infof("allocating static ip %s from subnet %s", ip, subnetName)
Expand Down Expand Up @@ -146,8 +146,8 @@ func checkAndAppendIpsForDual(ips []IP, mac, podName, nicName string, subnet *Su
}

func (ipam *IPAM) ReleaseAddressByPod(podName, subnetName string) {
ipam.mutex.RLock()
defer ipam.mutex.RUnlock()
ipam.mutex.Lock()
defer ipam.mutex.Unlock()
if subnetName != "" {
if subnet, ok := ipam.Subnets[subnetName]; ok {
subnet.ReleaseAddress(podName)
Expand Down Expand Up @@ -378,6 +378,9 @@ func (ipam *IPAM) IsIPAssignedToOtherPod(ip, subnetName, podName string) (string
}

func (ipam *IPAM) GetSubnetV4Mask(subnetName string) (string, error) {
ipam.mutex.RLock()
defer ipam.mutex.RUnlock()

subnet, ok := ipam.Subnets[subnetName]
if ok {
mask, _ := subnet.V4CIDR.Mask.Size()
Expand Down Expand Up @@ -409,8 +412,8 @@ func (ipam *IPAM) GetSubnetIPRangeString(subnetName string, excludeIps []string)
}

func (ipam *IPAM) AddOrUpdateIPPool(subnet, ippool string, ips []string) error {
ipam.mutex.RLock()
defer ipam.mutex.RUnlock()
ipam.mutex.Lock()
defer ipam.mutex.Unlock()

s := ipam.Subnets[subnet]
if s == nil {
Expand All @@ -421,11 +424,11 @@ func (ipam *IPAM) AddOrUpdateIPPool(subnet, ippool string, ips []string) error {
}

func (ipam *IPAM) RemoveIPPool(subnet, ippool string) {
ipam.mutex.RLock()
ipam.mutex.Lock()
if s := ipam.Subnets[subnet]; s != nil {
s.RemoveIPPool(ippool)
}
ipam.mutex.RUnlock()
ipam.mutex.Unlock()
}

func (ipam *IPAM) IPPoolStatistics(subnet, ippool string) (
Expand Down

0 comments on commit 5fa123a

Please sign in to comment.