Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix ipam subnet concurrent map iteration and map write #4126

Merged
merged 1 commit into from
Jun 6, 2024
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: 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 @@ -331,7 +331,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 @@ -344,7 +344,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 @@ -16,7 +16,7 @@ import (

type Subnet struct {
Name string
mutex sync.RWMutex
Mutex sync.RWMutex
CIDR string
Protocol string
V4CIDR *net.IPNet
Expand Down Expand Up @@ -173,10 +173,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 @@ -318,8 +318,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 @@ -544,25 +544,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 @@ -606,8 +606,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 @@ -701,8 +701,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 @@ -728,8 +728,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
Loading