mirror of
https://github.com/kubernetes/client-go.git
synced 2025-07-18 09:11:10 +00:00
chore(leader-election): modify leader-election comment to OnStoppedLeading callback is always called when the LeaderElector exits, even if it did not start leading.
Kubernetes-commit: 7d2fbc7cfd49bca7d58160ed25dfd1f838629bb9
This commit is contained in:
parent
d680385f61
commit
1b8aded77b
@ -21,6 +21,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"sync/atomic"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -57,6 +58,7 @@ func main() {
|
|||||||
var leaseLockName string
|
var leaseLockName string
|
||||||
var leaseLockNamespace string
|
var leaseLockNamespace string
|
||||||
var id string
|
var id string
|
||||||
|
var startedLeading atomic.Bool
|
||||||
|
|
||||||
flag.StringVar(&kubeconfig, "kubeconfig", "", "absolute path to the kubeconfig file")
|
flag.StringVar(&kubeconfig, "kubeconfig", "", "absolute path to the kubeconfig file")
|
||||||
flag.StringVar(&id, "id", uuid.New().String(), "the holder identity name")
|
flag.StringVar(&id, "id", uuid.New().String(), "the holder identity name")
|
||||||
@ -135,11 +137,24 @@ func main() {
|
|||||||
OnStartedLeading: func(ctx context.Context) {
|
OnStartedLeading: func(ctx context.Context) {
|
||||||
// we're notified when we start - this is where you would
|
// we're notified when we start - this is where you would
|
||||||
// usually put your code
|
// usually put your code
|
||||||
|
startedLeading.Store(true)
|
||||||
run(ctx)
|
run(ctx)
|
||||||
},
|
},
|
||||||
OnStoppedLeading: func() {
|
OnStoppedLeading: func() {
|
||||||
// we can do cleanup here
|
// we can do cleanup here, but note that this callback is always called
|
||||||
|
// when the LeaderElector exits, even if it did not start leading.
|
||||||
|
// Therefore, we should check if we actually started leading before
|
||||||
|
// performing any cleanup operations to avoid unexpected behavior.
|
||||||
klog.Infof("leader lost: %s", id)
|
klog.Infof("leader lost: %s", id)
|
||||||
|
|
||||||
|
// Example check to ensure we only perform cleanup if we actually started leading
|
||||||
|
if startedLeading.Load() {
|
||||||
|
// Perform cleanup operations here
|
||||||
|
// For example, releasing resources, closing connections, etc.
|
||||||
|
klog.Info("Performing cleanup operations...")
|
||||||
|
} else {
|
||||||
|
klog.Info("No cleanup needed as we never started leading.")
|
||||||
|
}
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
},
|
},
|
||||||
OnNewLeader: func(identity string) {
|
OnNewLeader: func(identity string) {
|
||||||
|
@ -173,7 +173,10 @@ type LeaderElectionConfig struct {
|
|||||||
type LeaderCallbacks struct {
|
type LeaderCallbacks struct {
|
||||||
// OnStartedLeading is called when a LeaderElector client starts leading
|
// OnStartedLeading is called when a LeaderElector client starts leading
|
||||||
OnStartedLeading func(context.Context)
|
OnStartedLeading func(context.Context)
|
||||||
// OnStoppedLeading is called when a LeaderElector client stops leading
|
// OnStoppedLeading is called when a LeaderElector client stops leading.
|
||||||
|
// This callback is always called when the LeaderElector exits, even if it did not start leading.
|
||||||
|
// Users should not assume that OnStoppedLeading is only called after OnStartedLeading.
|
||||||
|
// see: https://github.com/kubernetes/kubernetes/pull/127675#discussion_r1780059887
|
||||||
OnStoppedLeading func()
|
OnStoppedLeading func()
|
||||||
// OnNewLeader is called when the client observes a leader that is
|
// OnNewLeader is called when the client observes a leader that is
|
||||||
// not the previously observed leader. This includes the first observed
|
// not the previously observed leader. This includes the first observed
|
||||||
|
Loading…
Reference in New Issue
Block a user