title: How to execute functions once pod is elected leader by using leader-with-lease in operator-framework?
body: <p>I am trying to run 3 pods/replicas of a container in which I want one of them to be run as the leader and if that particular pod is stopped or fails, immediately the other non-leader running pods should take the leadership to continue execution that is required from a leader pod. Just to be clear the other pods would be executing but the leader pod once it gains leadership executes some extra code which the other pods will not. Here is the link to the <a href="https://sdk.operatorframework.io/docs/building-operators/golang/advanced-topics/#leader-election" rel="nofollow noreferrer">Docs</a> I followed.</p>
  <p>Here is a test example I ran with which I am having problems running the actual code that the container is supposed to run.</p>
  <pre><code>package main
  import (
      &quot;fmt&quot;
      &quot;github.com/operator-framework/operator-lib/leader&quot;
      &quot;sigs.k8s.io/controller-runtime/pkg/manager&quot;
      &quot;sigs.k8s.io/controller-runtime/pkg/client/config&quot;
      &quot;sigs.k8s.io/controller-runtime/pkg/manager/signals&quot;
      &quot;context&quot;
      &quot;time&quot;
      &quot;os&quot;
  )
  
  func main() {
      fmt.Println(&quot;Program has started successfully&quot;)
  
      cfg, err := config.GetConfig()
      if err != nil {
          os.Exit(1)
      }
  
      opts := manager.Options{
          LeaderElection:     true,
          LeaderElectionID:   &quot;memcached-operator-lock&quot;,
      }
      fmt.Println(&quot;Creating a manager&quot;)
      mgr, err := manager.New(cfg, opts)
      if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
          fmt.Println(err, &quot;Manager exited non-zero&quot;)
          os.Exit(1)
      }
      fmt.Println(&quot;Competing to become the leader&quot;)
      err = leader.Become(context.TODO(), &quot;memcached-operator-lock&quot;)
      if err != nil {
          fmt.Println(&quot;Failed to retry for leader lock because : %v &quot;, err)
      }
      fmt.Println(&quot;I am the leader, i will pause for 4 minutes&quot;)
      time.Sleep(4 * time.Minute)
  }
  </code></pre>
  <p>The logs of the leader pod are as follows :</p>
  <pre><code>[diamanti@appserv41 leader_test]$ kubectl logs test-rs-qbm7k
  Program has started successfully
  Creating a manager
  I1007 00:14:10.785904       1 leaderelection.go:242] attempting to acquire leader lease  default/memcached-operator-lock...
  I1007 00:14:28.184916       1 leaderelection.go:252] successfully acquired lease default/memcached-operator-lock
  </code></pre>
  <p>I want the pod to be able to run till the &quot;I am the leader&quot; print statement and execute the tasks that I want the leader pod would do. Even if I can pass a function somewhere which will allow it to be executed once it gains leadership, is fine with me. Currently, I am making a mistake somewhere and not able to do anything after one of the 3 pods gain leadership.</p>