Skip to content

Commit

Permalink
fix ipam subnet concurrent map iteration and map write (#4126)
Browse files Browse the repository at this point in the history
Signed-off-by: 马洪贞 <hzma@alauda.io>
  • Loading branch information
hongzhen-ma committed Jun 6, 2024
1 parent 9e8c5ee commit e53db6e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
6 changes: 6 additions & 0 deletions pkg/controller/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ func (c *Controller) exportSubnetIPAMInfo(subnet *kubeovnv1.Subnet) {
return
}

ipamSubnet.Mutex.RLock()
defer ipamSubnet.Mutex.RUnlock()

switch subnet.Spec.Protocol {
case kubeovnv1.ProtocolIPv4:
metricSubnetIPAMInfo.WithLabelValues(subnet.Name, subnet.Spec.CIDRBlock, ipamSubnet.V4Free.String(), ipamSubnet.V4Reserved.String(), ipamSubnet.V4Available.String(), ipamSubnet.V4Using.String()).Set(1)
Expand All @@ -115,6 +118,9 @@ func (c *Controller) exportSubnetIPAssignedInfo(subnet *kubeovnv1.Subnet) {
return
}

ipamSubnet.Mutex.RLock()
defer ipamSubnet.Mutex.RUnlock()

if subnet.Spec.Protocol == kubeovnv1.ProtocolIPv4 || subnet.Spec.Protocol == kubeovnv1.ProtocolDual {
for ip, pod := range ipamSubnet.V4IPToPod {
metricSubnetIPAssignedInfo.WithLabelValues(subnet.Name, ip, pod).Set(1)
Expand Down
4 changes: 2 additions & 2 deletions pkg/ipam/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func (ipam *IPAM) GetPodAddress(podName string) []*SubnetAddress {
defer ipam.mutex.RUnlock()
addresses := []*SubnetAddress{}
for _, subnet := range ipam.Subnets {
subnet.mutex.RLock()
subnet.Mutex.RLock()
for _, nicName := range subnet.PodToNicList[podName] {
v4IP, v6IP, mac, protocol := subnet.GetPodAddress(podName, nicName)
switch protocol {
Expand All @@ -338,7 +338,7 @@ func (ipam *IPAM) GetPodAddress(podName string) []*SubnetAddress {
addresses = append(addresses, &SubnetAddress{Subnet: subnet, IP: v6IP.String(), Mac: mac})
}
}
subnet.mutex.RUnlock()
subnet.Mutex.RUnlock()
}
return addresses
}
Expand Down
34 changes: 17 additions & 17 deletions pkg/ipam/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

type Subnet struct {
Name string
mutex sync.RWMutex
Mutex sync.RWMutex
CIDR string
Protocol string
V4CIDR *net.IPNet
Expand Down Expand Up @@ -172,10 +172,10 @@ func (s *Subnet) popPodNic(podName, nicName string) {
}

func (s *Subnet) GetRandomAddress(poolName, podName, nicName string, mac *string, skippedAddrs []string, checkConflict bool) (IP, IP, string, error) {
s.mutex.Lock()
s.Mutex.Lock()
defer func() {
s.pushPodNic(podName, nicName)
s.mutex.Unlock()
s.Mutex.Unlock()
}()

switch s.Protocol {
Expand Down Expand Up @@ -317,8 +317,8 @@ func (s *Subnet) getV6RandomAddress(ippoolName, podName, nicName string, mac *st
func (s *Subnet) GetStaticAddress(podName, nicName string, ip IP, mac *string, force, checkConflict bool) (IP, string, error) {
var v4, v6 bool
isAllocated := false
s.mutex.Lock()
defer s.mutex.Unlock()
s.Mutex.Lock()
defer s.Mutex.Unlock()

if ip.To4() != nil {
v4 = s.V4CIDR != nil
Expand Down Expand Up @@ -543,25 +543,25 @@ func (s *Subnet) releaseAddr(podName, nicName string) {
}

func (s *Subnet) ReleaseAddress(podName string) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.Mutex.Lock()
defer s.Mutex.Unlock()
for _, nicName := range s.PodToNicList[podName] {
s.releaseAddr(podName, nicName)
s.popPodNic(podName, nicName)
}
}

func (s *Subnet) ReleaseAddressWithNicName(podName, nicName string) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.Mutex.Lock()
defer s.Mutex.Unlock()

s.releaseAddr(podName, nicName)
s.popPodNic(podName, nicName)
}

func (s *Subnet) ContainAddress(address IP) bool {
s.mutex.RLock()
defer s.mutex.RUnlock()
s.Mutex.RLock()
defer s.Mutex.RUnlock()

if _, ok := s.V4IPToPod[address.String()]; ok {
return true
Expand Down Expand Up @@ -605,8 +605,8 @@ func (s *Subnet) isIPAssignedToOtherPod(ip, podName string) (string, bool) {
}

func (s *Subnet) AddOrUpdateIPPool(name string, ips []string) error {
s.mutex.Lock()
defer s.mutex.Unlock()
s.Mutex.Lock()
defer s.Mutex.Unlock()

pool := &IPPool{
V4IPs: NewEmptyIPRangeList(),
Expand Down Expand Up @@ -700,8 +700,8 @@ func (s *Subnet) AddOrUpdateIPPool(name string, ips []string) error {
}

func (s *Subnet) RemoveIPPool(name string) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.Mutex.Lock()
defer s.Mutex.Unlock()

p := s.IPPools[name]
if p == nil {
Expand All @@ -727,8 +727,8 @@ func (s *Subnet) IPPoolStatistics(ippool string) (
v4Available, v4Using, v6Available, v6Using internal.BigInt,
v4AvailableRange, v4UsingRange, v6AvailableRange, v6UsingRange string,
) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.Mutex.Lock()
defer s.Mutex.Unlock()

p := s.IPPools[ippool]
if p == nil {
Expand Down

0 comments on commit e53db6e

Please sign in to comment.