From 99a0015ed8c4a4e65222c0a5bb2aba01cbfdccb7 Mon Sep 17 00:00:00 2001 From: Trekkie Coder Date: Fri, 8 Dec 2023 22:50:25 +0900 Subject: [PATCH] faster detection of loxilb instance liveness --- cmd/loxilb-agent/agent.go | 25 ++++++++++--------- .../manager/loadbalancer/loadbalancer.go | 4 +-- pkg/api/client.go | 6 ++--- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/cmd/loxilb-agent/agent.go b/cmd/loxilb-agent/agent.go index bca2f1a..ddb3619 100644 --- a/cmd/loxilb-agent/agent.go +++ b/cmd/loxilb-agent/agent.go @@ -155,7 +155,7 @@ func run(o *Options) error { loxiLBLiveCh := make(chan *api.LoxiClient, 50) loxiLBPurgeCh := make(chan *api.LoxiClient, 5) loxiLBSelMasterEvent := make(chan bool) - loxiLBDeadCh := make(chan bool, 64) + loxiLBDeadCh := make(chan struct{}, 64) if len(networkConfig.LoxilbURLs) > 0 { for _, lbURL := range networkConfig.LoxilbURLs { @@ -179,22 +179,23 @@ func run(o *Options) error { informerFactory, ) - go wait.Until(func() { - select { - case <-loxiLBDeadCh: + roleEvents := func() { + for range loxiLBDeadCh { if networkConfig.SetRoles != "" { klog.Infof("Running select-roles") lbManager.SelectLoxiLBRoles(true, loxiLBSelMasterEvent) } - default: - if len(networkConfig.LoxilbURLs) <= 0 { - lbManager.DiscoverLoxiLBServices(loxiLBLiveCh, loxiLBDeadCh, loxiLBPurgeCh) - } - lbManager.DiscoverLoxiLBPeerServices(loxiLBLiveCh, loxiLBDeadCh, loxiLBPurgeCh) + } + } + go roleEvents() + go wait.Until(func() { + if len(networkConfig.LoxilbURLs) <= 0 { + lbManager.DiscoverLoxiLBServices(loxiLBLiveCh, loxiLBDeadCh, loxiLBPurgeCh) + } + lbManager.DiscoverLoxiLBPeerServices(loxiLBLiveCh, loxiLBDeadCh, loxiLBPurgeCh) - if networkConfig.SetRoles != "" { - lbManager.SelectLoxiLBRoles(true, loxiLBSelMasterEvent) - } + if networkConfig.SetRoles != "" { + lbManager.SelectLoxiLBRoles(true, loxiLBSelMasterEvent) } }, time.Second*20, stopCh) diff --git a/pkg/agent/manager/loadbalancer/loadbalancer.go b/pkg/agent/manager/loadbalancer/loadbalancer.go index a9127df..43f7449 100644 --- a/pkg/agent/manager/loadbalancer/loadbalancer.go +++ b/pkg/agent/manager/loadbalancer/loadbalancer.go @@ -1243,7 +1243,7 @@ func (m *Manager) addIngress(service *corev1.Service, newIP net.IP) { append(service.Status.LoadBalancer.Ingress, corev1.LoadBalancerIngress{IP: newIP.String()}) } -func (m *Manager) DiscoverLoxiLBServices(loxiLBAliveCh chan *api.LoxiClient, loxiLBDeadCh chan bool, loxiLBPurgeCh chan *api.LoxiClient) { +func (m *Manager) DiscoverLoxiLBServices(loxiLBAliveCh chan *api.LoxiClient, loxiLBDeadCh chan struct{}, loxiLBPurgeCh chan *api.LoxiClient) { var tmploxilbClients []*api.LoxiClient // DNS lookup (not used now) // ips, err := net.LookupIP("loxilb-lb-service") @@ -1293,7 +1293,7 @@ func (m *Manager) DiscoverLoxiLBServices(loxiLBAliveCh chan *api.LoxiClient, lox m.LoxiClients = tmp } -func (m *Manager) DiscoverLoxiLBPeerServices(loxiLBAliveCh chan *api.LoxiClient, loxiLBDeadCh chan bool, loxiLBPurgeCh chan *api.LoxiClient) { +func (m *Manager) DiscoverLoxiLBPeerServices(loxiLBAliveCh chan *api.LoxiClient, loxiLBDeadCh chan struct{}, loxiLBPurgeCh chan *api.LoxiClient) { var tmploxilbPeerClients []*api.LoxiClient ips, err := k8s.GetServiceEndPoints(m.kubeClient, "loxilb-peer-service", "kube-system") if len(ips) > 0 { diff --git a/pkg/api/client.go b/pkg/api/client.go index 9000377..a2e6ea7 100644 --- a/pkg/api/client.go +++ b/pkg/api/client.go @@ -27,7 +27,7 @@ type LoxiClient struct { } // apiServer is string. what format? http://10.0.0.1 or 10.0.0.1 -func NewLoxiClient(apiServer string, aliveCh chan *LoxiClient, deadCh chan bool, peerOnly bool) (*LoxiClient, error) { +func NewLoxiClient(apiServer string, aliveCh chan *LoxiClient, deadCh chan struct{}, peerOnly bool) (*LoxiClient, error) { client := &http.Client{} @@ -68,7 +68,7 @@ func NewLoxiClient(apiServer string, aliveCh chan *LoxiClient, deadCh chan bool, return lc, nil } -func (l *LoxiClient) StartLoxiHealthCheckChan(aliveCh chan *LoxiClient, deadCh chan bool) { +func (l *LoxiClient) StartLoxiHealthCheckChan(aliveCh chan *LoxiClient, deadCh chan struct{}) { l.IsAlive = false go wait.Until(func() { @@ -79,7 +79,7 @@ func (l *LoxiClient) StartLoxiHealthCheckChan(aliveCh chan *LoxiClient, deadCh c if time.Duration(time.Since(l.DeadSigTs).Seconds()) >= 3 && l.MasterLB { klog.Infof("LoxiHealthCheckChan: master down") l.DeadSigTs = time.Now() - deadCh <- true + deadCh <- struct{}{} } else { l.DeadSigTs = time.Now() }