Skip to content

Instantly share code, notes, and snippets.

@mayankshah1607
Created October 25, 2021 06:39
Show Gist options
  • Save mayankshah1607/b72b876026a84e62aa37043e7e91e9ed to your computer and use it in GitHub Desktop.
Save mayankshah1607/b72b876026a84e62aa37043e7e91e9ed to your computer and use it in GitHub Desktop.
runLeaderElection
func runLeaderElection(lock *resourcelock.LeaseLock, ctx context.Context, id string) {
leaderelection.RunOrDie(ctx, leaderelection.LeaderElectionConfig{
Lock: lock,
ReleaseOnCancel: true,
LeaseDuration: 15 * time.Second,
RenewDeadline: 10 * time.Second,
RetryPeriod: 2 * time.Second,
Callbacks: leaderelection.LeaderCallbacks{
OnStartedLeading: func(c context.Context) {
doStuff()
},
OnStoppedLeading: func() {
klog.Info("no longer the leader, staying inactive.")
},
OnNewLeader: func(current_id string) {
if current_id == id {
klog.Info("still the leader!")
return
}
klog.Info("new leader is %s", current_id)
},
},
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment