mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-04 04:08:16 +00:00 
			
		
		
		
	Merge pull request #127743 from googs1025/cleanup/leaderelection
chore(leader-election): modify leader-election comment to OnStoppedLeading callback is always called when the LeaderElector exits, even if it did not start leading.
This commit is contained in:
		@@ -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
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user