Skip to content

Commit 7d2fbc7

Browse files
committed
chore(leader-election): modify leader-election comment to OnStoppedLeading callback is always called when the LeaderElector exits, even if it did not start leading.
1 parent 1bbe775 commit 7d2fbc7

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

staging/src/k8s.io/client-go/examples/leader-election/main.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"flag"
2222
"os"
2323
"os/signal"
24+
"sync/atomic"
2425
"syscall"
2526
"time"
2627

@@ -57,6 +58,7 @@ func main() {
5758
var leaseLockName string
5859
var leaseLockNamespace string
5960
var id string
61+
var startedLeading atomic.Bool
6062

6163
flag.StringVar(&kubeconfig, "kubeconfig", "", "absolute path to the kubeconfig file")
6264
flag.StringVar(&id, "id", uuid.New().String(), "the holder identity name")
@@ -135,11 +137,24 @@ func main() {
135137
OnStartedLeading: func(ctx context.Context) {
136138
// we're notified when we start - this is where you would
137139
// usually put your code
140+
startedLeading.Store(true)
138141
run(ctx)
139142
},
140143
OnStoppedLeading: func() {
141-
// we can do cleanup here
144+
// we can do cleanup here, but note that this callback is always called
145+
// when the LeaderElector exits, even if it did not start leading.
146+
// Therefore, we should check if we actually started leading before
147+
// performing any cleanup operations to avoid unexpected behavior.
142148
klog.Infof("leader lost: %s", id)
149+
150+
// Example check to ensure we only perform cleanup if we actually started leading
151+
if startedLeading.Load() {
152+
// Perform cleanup operations here
153+
// For example, releasing resources, closing connections, etc.
154+
klog.Info("Performing cleanup operations...")
155+
} else {
156+
klog.Info("No cleanup needed as we never started leading.")
157+
}
143158
os.Exit(0)
144159
},
145160
OnNewLeader: func(identity string) {

staging/src/k8s.io/client-go/tools/leaderelection/leaderelection.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,10 @@ type LeaderElectionConfig struct {
173173
type LeaderCallbacks struct {
174174
// OnStartedLeading is called when a LeaderElector client starts leading
175175
OnStartedLeading func(context.Context)
176-
// OnStoppedLeading is called when a LeaderElector client stops leading
176+
// OnStoppedLeading is called when a LeaderElector client stops leading.
177+
// This callback is always called when the LeaderElector exits, even if it did not start leading.
178+
// Users should not assume that OnStoppedLeading is only called after OnStartedLeading.
179+
// see: https://github.com/kubernetes/kubernetes/pull/127675#discussion_r1780059887
177180
OnStoppedLeading func()
178181
// OnNewLeader is called when the client observes a leader that is
179182
// not the previously observed leader. This includes the first observed

0 commit comments

Comments
 (0)