diff --git a/pkg/controller/exporter.go b/pkg/controller/exporter.go index 1e599459c20..1e86d8a395a 100644 --- a/pkg/controller/exporter.go +++ b/pkg/controller/exporter.go @@ -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) @@ -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) diff --git a/pkg/ipam/ipam.go b/pkg/ipam/ipam.go index e202049403a..b3c63c8d160 100644 --- a/pkg/ipam/ipam.go +++ b/pkg/ipam/ipam.go @@ -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 { @@ -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 } diff --git a/pkg/ipam/subnet.go b/pkg/ipam/subnet.go index 920c69bf24e..983e6ebdc7b 100644 --- a/pkg/ipam/subnet.go +++ b/pkg/ipam/subnet.go @@ -15,7 +15,7 @@ import ( type Subnet struct { Name string - mutex sync.RWMutex + Mutex sync.RWMutex CIDR string Protocol string V4CIDR *net.IPNet @@ -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 { @@ -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 @@ -543,8 +543,8 @@ 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) @@ -552,16 +552,16 @@ func (s *Subnet) ReleaseAddress(podName string) { } 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 @@ -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(), @@ -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 { @@ -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 {