From d9e48bb6c3c00cc3dd02a257619cc601b85dfb5b Mon Sep 17 00:00:00 2001 From: Trekkie Coder Date: Thu, 26 Sep 2024 21:58:44 +0900 Subject: [PATCH] secondary-pool: fix for multi-port services --- .../manager/loadbalancer/loadbalancer.go | 39 ++++++++----------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/pkg/agent/manager/loadbalancer/loadbalancer.go b/pkg/agent/manager/loadbalancer/loadbalancer.go index 0b4cd3a..8327d23 100644 --- a/pkg/agent/manager/loadbalancer/loadbalancer.go +++ b/pkg/agent/manager/loadbalancer/loadbalancer.go @@ -425,6 +425,7 @@ func (m *Manager) addLoadBalancer(svc *corev1.Service) error { return errors.New("secondary pool not found") } } + numSecondarySvc = len(spools) } // Check for loxilb specific annotations - Secondary IPs (user specified) @@ -444,8 +445,6 @@ func (m *Manager) addLoadBalancer(svc *corev1.Service) error { } } - numSecondarySvc = len(sipPools) - if addrType != "ipv4" && len(sipPools) != 0 { klog.Infof("SecondaryIP Svc not possible for %v", addrType) return errors.New("SecondaryIP Svc not possible") @@ -1556,35 +1555,31 @@ func (m *Manager) getIngressSecSvcPairs(service *corev1.Service, numSecondary in } cacheKey := GenKey(service.Namespace, service.Name) + poolReserved := false for i := 0; i < numSecondary; i++ { - checkServicePortLoop: + poolReserved = false for _, port := range service.Spec.Ports { pool := sipPools[i] proto := strings.ToLower(string(port.Protocol)) portNum := port.Port - for _, sp := range lbCacheEntry.LbServicePairs { - if sp.Port == uint16(portNum) && proto == sp.Protocol { - oldsp := SvcPair{sp.ExternalIP, int32(sp.Port), sp.Protocol, sp.InRange, sp.StaticIP, sp.IdentIPAM, false, port} - sPairs = append(sPairs, oldsp) - continue checkServicePortLoop - } - } - - newIP, identIPAM := pool.GetNewIPAddr(cacheKey, uint32(portNum), proto) - if newIP == nil { - for j := 0; j < i; j++ { - rpool := sipPools[j] - rpool.ReturnIPAddr(sPairs[j].IPString, sPairs[j].IdentIPAM) + if !poolReserved { + newIP, identIPAM := pool.GetNewIPAddr(cacheKey, uint32(portNum), proto) + if newIP == nil { + for j := 0; j < i; j++ { + rpool := sipPools[j] + rpool.ReturnIPAddr(sPairs[j].IPString, sPairs[j].IdentIPAM) + } + errMsg := fmt.Sprintf("failed to generate secondary external IP. %s:%d:%s already used for %s", cacheKey, portNum, proto, identIPAM) + klog.Errorf(errMsg) + klog.Exit("kube-loxilb cant run optimally anymore") + return nil, errors.New(errMsg) } - errMsg := fmt.Sprintf("failed to generate secondary external IP. %s:%d:%s already used for %s", cacheKey, portNum, proto, identIPAM) - klog.Errorf(errMsg) - klog.Exit("kube-loxilb cant run optimally anymore") - return nil, errors.New(errMsg) + newsp := SvcPair{newIP.String(), portNum, proto, true, false, identIPAM, true, port} + sPairs = append(sPairs, newsp) + poolReserved = true } - newsp := SvcPair{newIP.String(), portNum, proto, true, false, identIPAM, true, port} - sPairs = append(sPairs, newsp) } }