Skip to content

Commit

Permalink
Modified to check the external IP of the service when determining whe…
Browse files Browse the repository at this point in the history
…ther to update.
  • Loading branch information
backguynn committed Apr 19, 2024
1 parent 8813b02 commit ab28d07
Showing 1 changed file with 52 additions and 31 deletions.
83 changes: 52 additions & 31 deletions pkg/agent/manager/loadbalancer/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,37 +748,7 @@ func (m *Manager) addLoadBalancer(svc *corev1.Service) error {
}
}

for _, sp := range m.lbCache[cacheKey].LbServicePairs {
// Update external IP if has changed

// Update endpoint list if the list has changed
for _, lb := range sp.LbModelList {
if len(endpointIPs) == len(lb.Endpoints) {
nEps := 0
for _, ep := range endpointIPs {
found := false
for _, oldEp := range lb.Endpoints {
if ep == oldEp.EndpointIP {
found = true
nEps++
break
}
}
if !found {
break
}
}
if nEps != len(endpointIPs) {
update = true
}
} else {
update = true
}
}
if update {
klog.Infof("%s: Endpoint update", cacheKey)
}
}
update = m.checkUpdateEndpoints(cacheKey, endpointIPs) || m.checkUpdateExternalIP(ingSvcPairs, svc)

if !update {
// TODO: Some cloud providers(e.g: K3d) delete external IPs assigned by kube-loxilb, so you can reach this syntax:
Expand Down Expand Up @@ -1126,6 +1096,57 @@ func (m *Manager) getEndpointsForLB(nodes []*corev1.Node, addrType string) []str
return endpoints
}

func (m *Manager) checkUpdateExternalIP(ingSvcPairs []SvcPair, svc *corev1.Service) bool {
for _, ingSvcPair := range ingSvcPairs {
if ingSvcPair.InRange || ingSvcPair.StaticIP {
retIngress := corev1.LoadBalancerIngress{Hostname: "llb-" + ingSvcPair.IPString}
if !m.checkServiceIngressIPExists(svc, retIngress.Hostname) {
return true
}
}
}

return false
}

func (m *Manager) checkUpdateEndpoints(cacheKey string, endpointIPs []string) bool {
var update bool

for _, sp := range m.lbCache[cacheKey].LbServicePairs {
// Update external IP if has changed

// Update endpoint list if the list has changed
for _, lb := range sp.LbModelList {
if len(endpointIPs) == len(lb.Endpoints) {
nEps := 0
for _, ep := range endpointIPs {
found := false
for _, oldEp := range lb.Endpoints {
if ep == oldEp.EndpointIP {
found = true
nEps++
break
}
}
if !found {
break
}
}
if nEps != len(endpointIPs) {
update = true
}
} else {
update = true
}
}
if update {
klog.Infof("%s: Endpoint update", cacheKey)
}
}

return update
}

func (m *Manager) checkServiceIngressIPExists(service *corev1.Service, newIngress string) bool {
for _, ingress := range service.Status.LoadBalancer.Ingress {
if ingress.IP != "" {
Expand Down

0 comments on commit ab28d07

Please sign in to comment.