Skip to content
This repository is currently being migrated. It's locked while the migration is in progress.

Commit

Permalink
change leader election to only use leases (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mojachieee authored Jun 9, 2022
1 parent 8e7f4c6 commit 72ddad9
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"net/http/pprof"
"time"

"os"
"strings"
Expand All @@ -13,6 +14,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/tools/leaderelection/resourcelock"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

Expand All @@ -33,6 +35,7 @@ var (
// prefix and version of the controller image. See Dockerfile.
defaultBackupAgentImage = "REPLACE_ME"
defaultRestoreAgentImage = "REPLACE_ME"
leaderRetryDuration = 5 * time.Second
)

const (
Expand All @@ -58,6 +61,7 @@ func main() {
restoreAgentImage string
backupAgentImage string
defragThreshold uint
leaderRenewSeconds uint
)

flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
Expand All @@ -73,8 +77,12 @@ func main() {
flag.BoolVar(&printVersion, "version", false,
"Print version to stdout and exit")
flag.UintVar(&defragThreshold, "defrag-threshold", 80, "The percentage of used space at which the operator will defrag an etcd member")
flag.UintVar(&leaderRenewSeconds, "leader-renew-seconds", 10, "Leader renewal frequency - for leader election")
flag.Parse()

renewDeadline := time.Duration(leaderRenewSeconds) * time.Second
leaseDuration := time.Duration(int(1.2*float64(leaderRenewSeconds))) * time.Second

if printVersion {
fmt.Println(version.Version)
return
Expand Down Expand Up @@ -107,6 +115,10 @@ func main() {
LeaderElectionID: leaderElectionID,
LeaderElectionNamespace: leaderElectionCMNamespace,
LeaderElectionReleaseOnCancel: true,
LeaderElectionResourceLock: resourcelock.LeasesResourceLock,
RenewDeadline: &renewDeadline,
LeaseDuration: &leaseDuration,
RetryPeriod: &leaderRetryDuration,
Port: 9443,
})
if err != nil {
Expand Down

0 comments on commit 72ddad9

Please sign in to comment.