mirror of
https://github.com/kubernetes/client-go.git
synced 2025-09-10 03:19:44 +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:
committed by
Kubernetes Publisher
parent
d680385f61
commit
1b8aded77b
@@ -21,6 +21,7 @@ import (
|
||||
"flag"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@@ -57,6 +58,7 @@ func main() {
|
||||
var leaseLockName string
|
||||
var leaseLockNamespace string
|
||||
var id string
|
||||
var startedLeading atomic.Bool
|
||||
|
||||
flag.StringVar(&kubeconfig, "kubeconfig", "", "absolute path to the kubeconfig file")
|
||||
flag.StringVar(&id, "id", uuid.New().String(), "the holder identity name")
|
||||
@@ -135,11 +137,24 @@ func main() {
|
||||
OnStartedLeading: func(ctx context.Context) {
|
||||
// we're notified when we start - this is where you would
|
||||
// usually put your code
|
||||
startedLeading.Store(true)
|
||||
run(ctx)
|
||||
},
|
||||
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)
|
||||
|
||||
// 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)
|
||||
},
|
||||
OnNewLeader: func(identity string) {
|
||||
|
Reference in New Issue
Block a user