github.skymusic.top/operator-framework/operator-sdk@v0.8.2/doc/proposals/leader-for-life.md (about)

     1  # Leader for Life
     2  
     3  ## Background
     4  
     5  Operators need leader election to ensure that if the same operator is running
     6  in two separate pods within the same namespace, only one of them will be
     7  active. The primary goal of leader election is to avoid contention between
     8  multiple active operators of the same type.
     9  
    10  High availability is not a goal of leader election for operators.
    11  
    12  Controller-runtime is adding leader election based on functionality present [in
    13  client-go](https://github.com/kubernetes/client-go/blob/master/tools/leaderelection/leaderelection.go). However that
    14  implementation allows for the possibility of brief periods during which
    15  multiple leaders are active.
    16  
    17  Requirements have been [discussed on
    18  GitHub](https://github.com/operator-framework/operator-sdk/issues/136).
    19  
    20  This proposal is to add leader election to the SDK that follows a "leader for
    21  life" model, which does not allow for multiple concurrent leaders.
    22  
    23  ## Goals
    24  
    25  * Provide leader election that is easy to use
    26  * Provide leader election that prohibits multiple leaders
    27  
    28  ## Non-Goals
    29  
    30  * Make operators highly available
    31  
    32  ## Solution
    33  
    34  The "leader for life" approach uses Kubernetes features to detect when a leader
    35  has disappeared and then automatically remove its lock.
    36  
    37  The approach and a PoC is detailed in [a separate
    38  repository](https://github.com/mhrivnak/leaderelection). This proposal is to move
    39  that implementation into `operator-sdk` and finish/modify it as appropriate.
    40  
    41  ### Usage
    42  
    43  ```golang
    44  func main() {
    45      // create a lock named "myapp-lock", retrying every 5 seconds until it succeeds
    46      err := leader.Become("myapp-lock", 5)
    47      if err != nil {
    48          log.Error(err, "")
    49          os.Exit(1)
    50      }
    51      ...
    52      // do whatever else your app does
    53  }
    54  ```
    55  
    56  ## Future
    57  
    58  Once accepted into operator-sdk, this would be valuable to contribute back to either
    59  controller-runtime directly or client-go.