Skip to content

Commit

Permalink
Fixing Leader election failure (#468)
Browse files Browse the repository at this point in the history
Signed-off-by: anishakj <[email protected]>
  • Loading branch information
anishakj authored Apr 26, 2022
1 parent 06b7ddc commit 1001aac
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
6 changes: 4 additions & 2 deletions charts/zookeeper-operator/templates/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ spec:
- name: {{ template "zookeeper-operator.fullname" . }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- containerPort: 6000
name: metrics
command:
- zookeeper-operator
args:
- "--metrics-bind-address=127.0.0.1:6000"
{{- if .Values.disableFinalizer }}
args:
- -disableFinalizer
{{- end }}
env:
Expand Down
5 changes: 3 additions & 2 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ spec:
- name: zookeeper-operator
# Replace this with the built image name
image: pravega/zookeeper-operator:0.2.13
args:
- "--metrics-bind-address=127.0.0.1:60000"
ports:
- containerPort: 60000
name: metrics
command:
- zookeeper-operator
imagePullPolicy: Always
Expand Down
51 changes: 42 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,27 @@ package main

import (
"context"
"errors"
"flag"
"fmt"
"github.com/operator-framework/operator-lib/leader"
"io/ioutil"
"os"
"runtime"
"strings"

zkConfig "github.com/pravega/zookeeper-operator/pkg/controller/config"
"github.com/pravega/zookeeper-operator/pkg/utils"
"github.com/pravega/zookeeper-operator/pkg/version"
zkClient "github.com/pravega/zookeeper-operator/pkg/zk"
"github.com/sirupsen/logrus"
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"os"
"runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"strings"

_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

api "github.com/pravega/zookeeper-operator/api/v1beta1"
Expand Down Expand Up @@ -69,11 +73,17 @@ func main() {
log.Error(err, "unable to get WatchNamespace, "+
"the manager will watch and manage resources in all namespaces")
}

printVersion()

if versionFlag {
os.Exit(0)
}

if zkConfig.DisableFinalizer {
logrus.Warn("----- Running with finalizer disabled. -----")
}

//When operator is started to watch resources in a specific set of namespaces, we use the MultiNamespacedCacheBuilder cache.
//In this scenario, it is also suggested to restrict the provided authorization to this namespace by replacing the default
//ClusterRole and ClusterRoleBinding to Role and RoleBinding respectively
Expand All @@ -87,12 +97,23 @@ func main() {
}
managerWatchCache = cache.MultiNamespacedCacheBuilder(ns)
}
ctx := context.TODO()

// Get a config to talk to the apiserver
cfg, err := config.GetConfig()
if err != nil {
logrus.Fatal(err)
}

operatorNs, err := GetOperatorNamespace()
if err != nil {
log.Error(err, "failed to get operator namespace")
os.Exit(1)
}

// Become the leader before proceeding
err = leader.Become(ctx, "zookeeper-operator-lock")
err = utils.BecomeLeader(context.TODO(), cfg, "zookeeper-operator-lock", operatorNs)
if err != nil {
logrus.Error(err)
log.Error(err, "")
os.Exit(1)
}

Expand Down Expand Up @@ -139,3 +160,15 @@ func getWatchNamespace() (string, error) {
}
return ns, nil
}

func GetOperatorNamespace() (string, error) {
nsBytes, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
if err != nil {
if os.IsNotExist(err) {
return "", errors.New("file does not exist")
}
return "", err
}
ns := strings.TrimSpace(string(nsBytes))
return ns, nil
}

0 comments on commit 1001aac

Please sign in to comment.