Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configmaps leaderelection type for k8s <1.14 #1

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions cmd/csi-snapshotter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ const (

// Default timeout of short CSI calls like GetPluginInfo
defaultCSITimeout = time.Minute

// leader election type supported in k8s 1.13
leaderElectionTypeConfigMaps = "configmaps"
leaderElectionTypeLeases = "leases"
)

// Command line flags
Expand All @@ -68,15 +72,21 @@ var (
showVersion = flag.Bool("version", false, "Show version.")
csiTimeout = flag.Duration("timeout", defaultCSITimeout, "The timeout for any RPCs to the CSI driver. Default is 1 minute.")

leaderElection = flag.Bool("leader-election", false, "Enables leader election.")
leaderElectionEnabled = flag.Bool("leader-election", false, "Enables leader election.")
leaderElectionNamespace = flag.String("leader-election-namespace", "", "The namespace where the leader election resource exists. Defaults to the pod namespace if not set.")
leaderElectionType = flag.String("leader-election-type", leaderElectionTypeLeases, "the type of leader election, options are 'configmaps' (default) or 'leases' (recommended). The 'configmaps' option is deprecated in favor of 'leases'.")
)

var (
version = "unknown"
leaderElectionLockName = "external-snapshotter-leader-election"
)

type leaderElection interface {
Run() error
WithNamespace(namespace string)
}

func main() {
klog.InitFlags(nil)
flag.Set("logtostderr", "true")
Expand Down Expand Up @@ -211,10 +221,24 @@ func main() {
close(stopCh)
}

if !*leaderElection {
if !*leaderElectionEnabled {
run(context.TODO())
} else {
le := leaderelection.NewLeaderElection(kubeClient, leaderElectionLockName, run)
var le leaderElection

switch *leaderElectionType {
case leaderElectionTypeConfigMaps:
klog.Warningf("The '%s' leader election type is deprecated and will be removed in a future release. Use '--leader-election-type=%s' instead for k8s 1.14+.", leaderElectionTypeConfigMaps, leaderElectionTypeLeases)
le = leaderelection.NewLeaderElectionWithConfigMaps(kubeClient, leaderElectionLockName, run)

case leaderElectionTypeLeases:
le = leaderelection.NewLeaderElection(kubeClient, leaderElectionLockName, run)

default:
klog.Errorf("--leader-election-type must be either '%s' or '%s'", leaderElectionTypeConfigMaps, leaderElectionTypeLeases)
os.Exit(1)
}

if *leaderElectionNamespace != "" {
le.WithNamespace(*leaderElectionNamespace)
}
Expand Down