diff --git a/pkg/ipam/ip_range_list.go b/pkg/ipam/ip_range_list.go index 17f8195f1c3..2a8d5d8faa1 100644 --- a/pkg/ipam/ip_range_list.go +++ b/pkg/ipam/ip_range_list.go @@ -3,6 +3,7 @@ package ipam import ( "fmt" "net" + "slices" "sort" "strings" @@ -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 } @@ -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 } @@ -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: diff --git a/pkg/ipam/ipam.go b/pkg/ipam/ipam.go index c50d7eabe10..e202049403a 100644 --- a/pkg/ipam/ipam.go +++ b/pkg/ipam/ipam.go @@ -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 { @@ -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) @@ -140,8 +140,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) @@ -372,6 +372,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() @@ -403,8 +406,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 { @@ -415,11 +418,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) (