diff --git a/pkg/ipam/ipam.go b/pkg/ipam/ipam.go index 537f99c8322..a35f24f354e 100644 --- a/pkg/ipam/ipam.go +++ b/pkg/ipam/ipam.go @@ -207,6 +207,7 @@ func (ipam *IPAM) GetPodAddress(podName string) []*SubnetAddress { defer ipam.mutex.RUnlock() addresses := []*SubnetAddress{} for _, subnet := range ipam.Subnets { + subnet.mutex.RLock() for _, nicName := range subnet.PodToNicList[podName] { v4IP, v6IP, mac, protocol := subnet.GetPodAddress(podName, nicName) switch protocol { @@ -219,6 +220,7 @@ func (ipam *IPAM) GetPodAddress(podName string) []*SubnetAddress { addresses = append(addresses, &SubnetAddress{Subnet: subnet, Ip: string(v6IP), Mac: mac}) } } + subnet.mutex.RUnlock() } return addresses } diff --git a/pkg/ipam/subnet.go b/pkg/ipam/subnet.go index 83b7693c04e..47ad9668e48 100644 --- a/pkg/ipam/subnet.go +++ b/pkg/ipam/subnet.go @@ -520,10 +520,8 @@ func (subnet *Subnet) joinFreeWithReserve() { } } +// This func is only called in ipam.GetPodAddress, move mutex to caller func (subnet *Subnet) GetPodAddress(podName, nicName string) (IP, IP, string, string) { - subnet.mutex.RLock() - defer subnet.mutex.RUnlock() - if subnet.Protocol == kubeovnv1.ProtocolIPv4 { ip, mac := subnet.V4NicToIP[nicName], subnet.NicToMac[nicName] return ip, "", mac, kubeovnv1.ProtocolIPv4