Skip to content
This repository has been archived by the owner on Aug 14, 2021. It is now read-only.

Commit

Permalink
refact(localpv): add ENV to allow skipping leader election
Browse files Browse the repository at this point in the history
commit adds the `LEADER_ELECTION_ENABLED` env to allow user
to enable/disable the leader election feature of openebs
provisioner and snapshot-provisioner.

If env is not configured, leader election will be enabled by
default.

Signed-off-by: prateekpandey14 <[email protected]>
  • Loading branch information
prateekpandey14 authored and kmova committed Aug 15, 2020
1 parent 605e07c commit 377138e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
27 changes: 27 additions & 0 deletions openebs/cmd/openebs-provisioner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package main

import (
"flag"
"os"
"strings"

"syscall"

Expand All @@ -33,6 +35,9 @@ import (

const (
provisionerName = "openebs.io/provisioner-iscsi"
// LeaderElectionKey represents ENV for disable/enable leaderElection for
// openebs-provisioner
LeaderElectionKey = "LEADER_ELECTION_ENABLED"
)

func main() {
Expand Down Expand Up @@ -82,7 +87,29 @@ func main() {
provisionerName,
openEBSProvisioner,
serverVersion.GitVersion,
controller.LeaderElection(isLeaderElectionEnabled()),
)
// Run starts all of controller's control loops
pc.Run(wait.NeverStop)
}

// isLeaderElectionEnabled returns true/false based on the ENV
// LEADER_ELECTION_ENABLED set via provisioner deployment.
// Defaults to true, means leaderElection enabled by default.
func isLeaderElectionEnabled() bool {
leaderElection := os.Getenv(LeaderElectionKey)

var leader bool
switch strings.ToLower(leaderElection) {
default:
glog.Info("Leader election enabled for openebs-provisioner")
leader = true
case "y", "yes", "true":
glog.Info("Leader election enabled for openebs-provisioner via leaderElectionKey")
leader = true
case "n", "no", "false":
glog.Info("Leader election disabled for openebs-provisioner via leaderElectionKey")
leader = false
}
return leader
}
29 changes: 28 additions & 1 deletion snapshot/cmd/snapshot-pv-provisioner/snapshot-pv-provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"errors"
"flag"
"fmt"
"os"
"strings"

"github.com/golang/glog"
"github.com/kubernetes-incubator/external-storage/lib/controller"
Expand All @@ -37,7 +39,7 @@ import (
"github.com/kubernetes-incubator/external-storage/snapshot/pkg/volume/hostpath"
"github.com/kubernetes-incubator/external-storage/snapshot/pkg/volume/openebs"

"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
Expand All @@ -49,6 +51,9 @@ import (
const (
provisionerName = "volumesnapshot.external-storage.k8s.io/snapshot-promoter"
provisionerIDAnn = "snapshotProvisionerIdentity"
// LeaderElectionKey represents ENV for disable/enable leaderElection for
// snapshot-provisioner
LeaderElectionKey = "LEADER_ELECTION_ENABLED"
)

type snapshotProvisioner struct {
Expand Down Expand Up @@ -247,6 +252,7 @@ func main() {
provisionerName,
snapshotProvisioner,
serverVersion.GitVersion,
controller.LeaderElection(isLeaderElectionEnabled()),
)
glog.Infof("starting PV provisioner %s", provisionerName)
pc.Run(wait.NeverStop)
Expand Down Expand Up @@ -282,3 +288,24 @@ func buildVolumePlugins() {
volumePlugins[openebs.GetPluginName()] = openebs.RegisterPlugin()

}

// isLeaderElectionEnabled returns true/false based on the ENV
// LEADER_ELECTION_ENABLED set via snaphot provisioner deployment.
// Defaults to true, means leaderElection enabled by default.
func isLeaderElectionEnabled() bool {
leaderElection := os.Getenv(LeaderElectionKey)

var leader bool
switch strings.ToLower(leaderElection) {
default:
glog.Info("Leader election enabled for snapshot-provisioner")
leader = true
case "y", "yes", "true":
glog.Info("Leader election enabled for snapshot-provisioner via leaderElectionKey")
leader = true
case "n", "no", "false":
glog.Info("Leader election disabled for snapshot-provisioner via leaderElectionKey")
leader = false
}
return leader
}

0 comments on commit 377138e

Please sign in to comment.