Skip to content

Commit

Permalink
add leader election namespace. (istio#373)
Browse files Browse the repository at this point in the history
* add leader election namespace.

* fix error message.
  • Loading branch information
Morven Cao authored and istio-testing committed Oct 12, 2019
1 parent e115eb7 commit cf75bd0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
20 changes: 17 additions & 3 deletions cmd/manager/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,26 @@ func getWatchNamespace() (string, error) {
return ns, nil
}

// getLeaderElectionNamespace returns the namespace in which the leader election configmap will be created
func getLeaderElectionNamespace() (string, error) {
ns, found := os.LookupEnv("LEADER_ELECTION_NAMESPACE")
if !found {
return "", fmt.Errorf("LEADER_ELECTION_NAMESPACE must be set")
}
return ns, nil
}

func run() {
namespace, err := getWatchNamespace()
watchNS, err := getWatchNamespace()
if err != nil {
log.Fatalf("Failed to get watch namespace: %v", err)
}

leaderElectionNS, err := getLeaderElectionNamespace()
if err != nil {
log.Fatalf("Failed to get leader election namespace: %v", err)
}

// Get a config to talk to the apiserver
cfg, err := config.GetConfig()
if err != nil {
Expand All @@ -94,12 +108,12 @@ func run() {

// Create a new Cmd to provide shared dependencies and start components
mgr, err := manager.New(cfg, manager.Options{
Namespace: namespace,
Namespace: watchNS,
MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
// Workaround for https://github.com/kubernetes-sigs/controller-runtime/issues/321
MapperProvider: drm.NewDynamicRESTMapper,
LeaderElection: true,
LeaderElectionNamespace: namespace,
LeaderElectionNamespace: leaderElectionNS,
LeaderElectionID: "istio-operator-lock",
})
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions deploy/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ spec:
env:
- name: WATCH_NAMESPACE
value: ""
- name: LEADER_ELECTION_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: POD_NAME
valueFrom:
fieldRef:
Expand Down

0 comments on commit cf75bd0

Please sign in to comment.