|
| 1 | +// Copyright (c) 2024-2025 Zededa, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//go:build kubevirt |
| 5 | + |
| 6 | +package zedkube |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/lf-edge/eve/pkg/pillar/kubeapi" |
| 13 | + "github.com/lf-edge/eve/pkg/pillar/types" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "k8s.io/client-go/tools/leaderelection" |
| 16 | + "k8s.io/client-go/tools/leaderelection/resourcelock" |
| 17 | +) |
| 18 | + |
| 19 | +func (z *zedkube) handleLeaderElection() { |
| 20 | + var cancelFunc context.CancelFunc |
| 21 | + // If we can not perform the leader election, due to kubernetes connection issues |
| 22 | + // at the moment, we will retry in 5 minutes |
| 23 | + retryTimer := time.NewTimer(0) |
| 24 | + retryTimer.Stop() // Ensure the timer is stopped initially |
| 25 | + retryTimerStarted := false |
| 26 | + for { |
| 27 | + log.Noticef("handleLeaderElection: Waiting for signal") // XXX |
| 28 | + select { |
| 29 | + case <-z.electionStartCh: |
| 30 | + |
| 31 | + // Create a cancellable context |
| 32 | + baseCtx, cancel := context.WithCancel(context.Background()) |
| 33 | + cancelFunc = cancel |
| 34 | + |
| 35 | + clientset, err := getKubeClientSet() |
| 36 | + if err != nil { |
| 37 | + z.inKubeLeaderElection.Store(false) |
| 38 | + z.publishLeaderElectionChange() |
| 39 | + log.Errorf("handleLeaderElection: can't get clientset %v, retry in 5 min", err) |
| 40 | + retryTimer.Reset(5 * time.Minute) |
| 41 | + retryTimerStarted = true |
| 42 | + continue |
| 43 | + } |
| 44 | + |
| 45 | + // Create a new lease lock |
| 46 | + lock := &resourcelock.LeaseLock{ |
| 47 | + LeaseMeta: metav1.ObjectMeta{ |
| 48 | + Name: "eve-kube-stats-leader", |
| 49 | + Namespace: kubeapi.EVEKubeNameSpace, |
| 50 | + }, |
| 51 | + Client: clientset.CoordinationV1(), |
| 52 | + LockConfig: resourcelock.ResourceLockConfig{ |
| 53 | + Identity: z.nodeName, |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + // Define the leader election configuration |
| 58 | + lec := leaderelection.LeaderElectionConfig{ |
| 59 | + Lock: lock, |
| 60 | + LeaseDuration: 300 * time.Second, |
| 61 | + RenewDeadline: 180 * time.Second, |
| 62 | + RetryPeriod: 15 * time.Second, |
| 63 | + ReleaseOnCancel: true, |
| 64 | + Callbacks: leaderelection.LeaderCallbacks{ |
| 65 | + OnStartedLeading: func(baseCtx context.Context) { |
| 66 | + z.isKubeStatsLeader.Store(true) |
| 67 | + z.publishLeaderElectionChange() |
| 68 | + log.Noticef("handleLeaderElection: Callback Started leading") |
| 69 | + }, |
| 70 | + OnStoppedLeading: func() { |
| 71 | + z.isKubeStatsLeader.Store(false) |
| 72 | + z.publishLeaderElectionChange() |
| 73 | + log.Noticef("handleLeaderElection: Callback Stopped leading") |
| 74 | + }, |
| 75 | + OnNewLeader: func(identity string) { |
| 76 | + z.leaderIdentity = identity |
| 77 | + z.publishLeaderElectionChange() |
| 78 | + log.Noticef("handleLeaderElection: Callback New leader elected: %s", identity) |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + // Start the leader election in a separate goroutine |
| 84 | + go func() { |
| 85 | + leaderelection.RunOrDie(baseCtx, lec) |
| 86 | + z.electionFuncRunning.Store(false) |
| 87 | + log.Noticef("handleLeaderElection: Leader election routine exited") |
| 88 | + if z.inKubeLeaderElection.Load() { |
| 89 | + retryTimer.Reset(5 * time.Minute) |
| 90 | + retryTimerStarted = true |
| 91 | + log.Noticef("handleLeaderElection: We should be inElection, retry in 5 min") |
| 92 | + } |
| 93 | + z.publishLeaderElectionChange() |
| 94 | + }() |
| 95 | + z.electionFuncRunning.Store(true) |
| 96 | + z.publishLeaderElectionChange() |
| 97 | + log.Noticef("handleLeaderElection: Started leader election routine for %s", z.nodeName) |
| 98 | + |
| 99 | + case <-z.electionStopCh: |
| 100 | + z.isKubeStatsLeader.Store(false) |
| 101 | + z.inKubeLeaderElection.Store(false) |
| 102 | + z.leaderIdentity = "" |
| 103 | + z.publishLeaderElectionChange() |
| 104 | + log.Noticef("handleLeaderElection: Stopped leading signal received") |
| 105 | + if retryTimerStarted { |
| 106 | + retryTimer.Stop() |
| 107 | + retryTimerStarted = false |
| 108 | + } |
| 109 | + |
| 110 | + if cancelFunc != nil { |
| 111 | + log.Noticef("handleLeaderElection: Stopped. cancelling leader election") |
| 112 | + cancelFunc() |
| 113 | + cancelFunc = nil |
| 114 | + } |
| 115 | + |
| 116 | + case <-retryTimer.C: |
| 117 | + log.Noticef("Retrying failed leader election") |
| 118 | + sub := z.subZedAgentStatus |
| 119 | + items := sub.GetAll() |
| 120 | + for _, item := range items { |
| 121 | + status := item.(types.ZedAgentStatus) |
| 122 | + z.handleControllerStatusChange(&status) |
| 123 | + break |
| 124 | + } |
| 125 | + retryTimerStarted = false |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// SignalStartLeaderElection - Function to signal the start of leader election |
| 131 | +func (z *zedkube) SignalStartLeaderElection() { |
| 132 | + z.inKubeLeaderElection.Store(true) |
| 133 | + select { |
| 134 | + case z.electionStartCh <- struct{}{}: |
| 135 | + log.Noticef("SignalStartLeaderElection: Signal sent successfully") |
| 136 | + default: |
| 137 | + log.Warningf("SignalStartLeaderElection: Channel is full, signal not sent") |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +// SignalStopLeaderElection - Function to signal the stop of leader election |
| 142 | +func (z *zedkube) SignalStopLeaderElection() { |
| 143 | + select { |
| 144 | + case z.electionStopCh <- struct{}{}: |
| 145 | + log.Noticef("SignalStopLeaderElection: Signal sent successfully") |
| 146 | + default: |
| 147 | + log.Warningf("SignalStopLeaderElection: Channel is full, signal not sent") |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func (z *zedkube) handleControllerStatusChange(status *types.ZedAgentStatus) { |
| 152 | + configStatus := status.ConfigGetStatus |
| 153 | + |
| 154 | + log.Noticef("handleControllerStatusChange: Leader enter, status %v", configStatus) |
| 155 | + switch configStatus { |
| 156 | + case types.ConfigGetSuccess, types.ConfigGetReadSaved: // either read success or read from saved config |
| 157 | + if !z.inKubeLeaderElection.Load() { |
| 158 | + z.SignalStartLeaderElection() |
| 159 | + } else { |
| 160 | + log.Noticef("handleControllerStatusChange: start. Already in leader election, skip") |
| 161 | + } |
| 162 | + default: |
| 163 | + if z.inKubeLeaderElection.Load() { |
| 164 | + z.SignalStopLeaderElection() |
| 165 | + } else { |
| 166 | + log.Noticef("handleControllerStatusChange: default stop. Not in leader election, skip") |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +func (z *zedkube) publishLeaderElectionChange() { |
| 172 | + // Publish the change in leader |
| 173 | + leaderElectinfo := types.KubeLeaderElectInfo{ |
| 174 | + InLeaderElection: z.inKubeLeaderElection.Load(), |
| 175 | + IsStatsLeader: z.isKubeStatsLeader.Load(), |
| 176 | + ElectionRunning: z.electionFuncRunning.Load(), |
| 177 | + LeaderIdentity: z.leaderIdentity, |
| 178 | + LatestChange: time.Now(), |
| 179 | + } |
| 180 | + z.pubLeaderElectInfo.Publish("global", leaderElectinfo) |
| 181 | +} |
0 commit comments