1111package main
1212
1313import (
14- "context"
15- "errors"
14+ "crypto/md5"
1615 "flag"
1716 "fmt"
17+ "io"
1818 "os"
1919 "runtime"
2020 "strings"
2121
2222 zkConfig "github.com/pravega/zookeeper-operator/pkg/controller/config"
23- "github.com/pravega/zookeeper-operator/pkg/utils"
2423 "github.com/pravega/zookeeper-operator/pkg/version"
2524 zkClient "github.com/pravega/zookeeper-operator/pkg/zk"
2625 "github.com/sirupsen/logrus"
@@ -31,7 +30,6 @@ import (
3130 _ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
3231 ctrl "sigs.k8s.io/controller-runtime"
3332 "sigs.k8s.io/controller-runtime/pkg/cache"
34- "sigs.k8s.io/controller-runtime/pkg/client/config"
3533 "sigs.k8s.io/controller-runtime/pkg/log/zap"
3634
3735 api "github.com/pravega/zookeeper-operator/api/v1beta1"
@@ -62,7 +60,10 @@ func printVersion() {
6260
6361func main () {
6462 var metricsAddr string
63+ var enableLeaderElection bool
6564 flag .StringVar (& metricsAddr , "metrics-bind-address" , "127.0.0.1:6000" , "The address the metric endpoint binds to." )
65+ flag .BoolVar (& enableLeaderElection , "enable-leader-election" , true ,
66+ "Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager." )
6667 opts := zap.Options {}
6768 opts .BindFlags (flag .CommandLine )
6869 flag .Parse ()
@@ -99,29 +100,14 @@ func main() {
99100 managerNamespaces = ns
100101 }
101102
102- // Get a config to talk to the apiserver
103- cfg , err := config .GetConfig ()
104- if err != nil {
105- logrus .Fatal (err )
106- }
107-
108- operatorNs , err := GetOperatorNamespace ()
109- if err != nil {
110- log .Error (err , "failed to get operator namespace" )
111- os .Exit (1 )
112- }
113-
114- // Become the leader before proceeding
115- err = utils .BecomeLeader (context .TODO (), cfg , "zookeeper-operator-lock" , operatorNs )
116- if err != nil {
117- log .Error (err , "" )
118- os .Exit (1 )
119- }
120-
103+ // create uniq leaderElectionID per deployment. a deployment watches a uniq set of namespaces
104+ leaderElectionID := fmt .Sprintf ("%s-%s" , "zookeeper-operator-lock" , StringMd5Hash (namespaces ))
121105 mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
122106 Scheme : scheme ,
123107 Cache : cache.Options {Namespaces : managerNamespaces },
124108 MetricsBindAddress : metricsAddr ,
109+ LeaderElection : enableLeaderElection ,
110+ LeaderElectionID : leaderElectionID ,
125111 })
126112 if err != nil {
127113 log .Error (err , "unable to start manager" )
@@ -160,14 +146,8 @@ func getWatchNamespace() (string, error) {
160146 return ns , nil
161147}
162148
163- func GetOperatorNamespace () (string , error ) {
164- nsBytes , err := os .ReadFile ("/var/run/secrets/kubernetes.io/serviceaccount/namespace" )
165- if err != nil {
166- if os .IsNotExist (err ) {
167- return "" , errors .New ("file does not exist" )
168- }
169- return "" , err
170- }
171- ns := strings .TrimSpace (string (nsBytes ))
172- return ns , nil
149+ func StringMd5Hash (s string ) string {
150+ h := md5 .New ()
151+ io .WriteString (h , s )
152+ return fmt .Sprintf ("%x" , h .Sum (nil ))
173153}
0 commit comments