Skip to content

Commit 1001aac

Browse files
authored
Fixing Leader election failure (#468)
Signed-off-by: anishakj <anisha.kj@dell.com>
1 parent 06b7ddc commit 1001aac

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

charts/zookeeper-operator/templates/operator.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ spec:
3232
- name: {{ template "zookeeper-operator.fullname" . }}
3333
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
3434
imagePullPolicy: {{ .Values.image.pullPolicy }}
35+
ports:
36+
- containerPort: 6000
37+
name: metrics
3538
command:
3639
- zookeeper-operator
37-
args:
38-
- "--metrics-bind-address=127.0.0.1:6000"
3940
{{- if .Values.disableFinalizer }}
41+
args:
4042
- -disableFinalizer
4143
{{- end }}
4244
env:

config/manager/manager.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ spec:
1717
- name: zookeeper-operator
1818
# Replace this with the built image name
1919
image: pravega/zookeeper-operator:0.2.13
20-
args:
21-
- "--metrics-bind-address=127.0.0.1:60000"
20+
ports:
21+
- containerPort: 60000
22+
name: metrics
2223
command:
2324
- zookeeper-operator
2425
imagePullPolicy: Always

main.go

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,27 @@ package main
1212

1313
import (
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

Comments
 (0)