@@ -12,23 +12,27 @@ package main
1212
1313import (
1414 "context"
15+ "errors"
1516 "flag"
1617 "fmt"
17- "github.com/operator-framework/operator-lib/leader"
18+ "io/ioutil"
19+ "os"
20+ "runtime"
21+ "strings"
22+
1823 zkConfig "github.com/pravega/zookeeper-operator/pkg/controller/config"
24+ "github.com/pravega/zookeeper-operator/pkg/utils"
1925 "github.com/pravega/zookeeper-operator/pkg/version"
2026 zkClient "github.com/pravega/zookeeper-operator/pkg/zk"
2127 "github.com/sirupsen/logrus"
2228 apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
2329 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2430 clientgoscheme "k8s.io/client-go/kubernetes/scheme"
25- "os"
26- "runtime"
27- "sigs.k8s.io/controller-runtime/pkg/cache"
28- "strings"
29-
3031 _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
32+ _ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
3133 ctrl "sigs.k8s.io/controller-runtime"
34+ "sigs.k8s.io/controller-runtime/pkg/cache"
35+ "sigs.k8s.io/controller-runtime/pkg/client/config"
3236 "sigs.k8s.io/controller-runtime/pkg/log/zap"
3337
3438 api "github.com/pravega/zookeeper-operator/api/v1beta1"
@@ -69,11 +73,17 @@ func main() {
6973 log .Error (err , "unable to get WatchNamespace, " +
7074 "the manager will watch and manage resources in all namespaces" )
7175 }
76+
7277 printVersion ()
7378
7479 if versionFlag {
7580 os .Exit (0 )
7681 }
82+
83+ if zkConfig .DisableFinalizer {
84+ logrus .Warn ("----- Running with finalizer disabled. -----" )
85+ }
86+
7787 //When operator is started to watch resources in a specific set of namespaces, we use the MultiNamespacedCacheBuilder cache.
7888 //In this scenario, it is also suggested to restrict the provided authorization to this namespace by replacing the default
7989 //ClusterRole and ClusterRoleBinding to Role and RoleBinding respectively
@@ -87,12 +97,23 @@ func main() {
8797 }
8898 managerWatchCache = cache .MultiNamespacedCacheBuilder (ns )
8999 }
90- ctx := context .TODO ()
100+
101+ // Get a config to talk to the apiserver
102+ cfg , err := config .GetConfig ()
103+ if err != nil {
104+ logrus .Fatal (err )
105+ }
106+
107+ operatorNs , err := GetOperatorNamespace ()
108+ if err != nil {
109+ log .Error (err , "failed to get operator namespace" )
110+ os .Exit (1 )
111+ }
91112
92113 // Become the leader before proceeding
93- err = leader . Become ( ctx , "zookeeper-operator-lock" )
114+ err = utils . BecomeLeader ( context . TODO (), cfg , "zookeeper-operator-lock" , operatorNs )
94115 if err != nil {
95- logrus .Error (err )
116+ log .Error (err , "" )
96117 os .Exit (1 )
97118 }
98119
@@ -139,3 +160,15 @@ func getWatchNamespace() (string, error) {
139160 }
140161 return ns , nil
141162}
163+
164+ func GetOperatorNamespace () (string , error ) {
165+ nsBytes , err := ioutil .ReadFile ("/var/run/secrets/kubernetes.io/serviceaccount/namespace" )
166+ if err != nil {
167+ if os .IsNotExist (err ) {
168+ return "" , errors .New ("file does not exist" )
169+ }
170+ return "" , err
171+ }
172+ ns := strings .TrimSpace (string (nsBytes ))
173+ return ns , nil
174+ }
0 commit comments