From bafdeae81c3bd8de38c1efb8bbaa6a6dc7f84216 Mon Sep 17 00:00:00 2001 From: bobz965 Date: Fri, 12 Jan 2024 09:01:06 +0800 Subject: [PATCH] fix: empty loop (#3615) * fix: empty top loop * fix: no need to compare gateway ip with pod ip --------- Signed-off-by: bobz965 --- pkg/controller/subnet.go | 42 ++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/pkg/controller/subnet.go b/pkg/controller/subnet.go index 70f5c7a21cf..2d4df9033ac 100644 --- a/pkg/controller/subnet.go +++ b/pkg/controller/subnet.go @@ -2040,12 +2040,25 @@ func (c *Controller) calcDualSubnetStatusIP(subnet *kubeovnv1.Subnet) (*kubeovnv var lenIP, lenVip, lenIptablesEip, lenOvnEip int lenIP = len(podUsedIPs) usingIPNums := lenIP + + // TODO:// replace ExcludeIps with ip pool and gw to avoid later loop + noGWExcludeIPs := []string{} + v4gw, v6gw := util.SplitStringIP(subnet.Spec.Gateway) for _, excludeIP := range subnet.Spec.ExcludeIps { + if v4gw == excludeIP || v6gw == excludeIP { + // no need to compair gateway ip with pod ip + continue + } + noGWExcludeIPs = append(noGWExcludeIPs, excludeIP) + } + if noGWExcludeIPs != nil { for _, podUsedIP := range podUsedIPs { - if util.ContainsIPs(excludeIP, podUsedIP.Spec.V4IPAddress) || util.ContainsIPs(excludeIP, podUsedIP.Spec.V6IPAddress) { - // This ip cr is allocated from subnet.spec.excludeIPs, do not count it as usingIPNums - usingIPNums-- - break + for _, excludeIP := range noGWExcludeIPs { + if util.ContainsIPs(excludeIP, podUsedIP.Spec.V4IPAddress) || util.ContainsIPs(excludeIP, podUsedIP.Spec.V6IPAddress) { + // This ip cr is allocated from subnet.spec.excludeIPs, do not count it as usingIPNums + usingIPNums-- + break + } } } } @@ -2157,12 +2170,25 @@ func (c *Controller) calcSubnetStatusIP(subnet *kubeovnv1.Subnet) (*kubeovnv1.Su } lenIP = len(podUsedIPs) usingIPNums := lenIP + + // TODO:// replace ExcludeIps with ip pool and gw to avoid later loop + noGWExcludeIPs := []string{} + v4gw, v6gw := util.SplitStringIP(subnet.Spec.Gateway) for _, excludeIP := range subnet.Spec.ExcludeIps { + if v4gw == excludeIP || v6gw == excludeIP { + // no need to compair gateway ip with pod ip + continue + } + noGWExcludeIPs = append(noGWExcludeIPs, excludeIP) + } + if noGWExcludeIPs != nil { for _, podUsedIP := range podUsedIPs { - if util.ContainsIPs(excludeIP, podUsedIP.Spec.V4IPAddress) || util.ContainsIPs(excludeIP, podUsedIP.Spec.V6IPAddress) { - // This ip cr is allocated from subnet.spec.excludeIPs, do not count it as usingIPNums - usingIPNums-- - break + for _, excludeIP := range noGWExcludeIPs { + if util.ContainsIPs(excludeIP, podUsedIP.Spec.V4IPAddress) || util.ContainsIPs(excludeIP, podUsedIP.Spec.V6IPAddress) { + // This ip cr is allocated from subnet.spec.excludeIPs, do not count it as usingIPNums + usingIPNums-- + break + } } } }