Skip to content

Commit

Permalink
Merge pull request #7 from andrewcole/feat/environment-variables
Browse files Browse the repository at this point in the history
Make configurable via environment variables
  • Loading branch information
kkosmrli authored May 16, 2021
2 parents e626c69 + 7e65115 commit 14ad36b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 36 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ARG ARCH=amd64
FROM golang:1.16-alpine3.13 AS builder
WORKDIR /src
ADD . /src
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} go get github.com/alexflint/go-arg
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} go build -ldflags='-s -w -extldflags "-static"' -o elector cmd/leader-elector/main.go

FROM gcr.io/distroless/static:nonroot-${ARCH}
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ A simple leader election sidecar container for Kubernetes based on the recent [c

## Configuration Flags

* `election` - name of the election and the corresponding lock resource.
* `namespace` - the Kubernetes namespace to run the election in.
* `locktype` - the leaselock resource type to use for this deployment. Supported lock types are:
The following arguments can be passed on the command line, or by setting the environment variable named in brackets.

* `election` - name of the election and the corresponding lock resource (ELECTION_NAME).
* `namespace` - the Kubernetes namespace to run the election in (ELECTION_NAMESPACE).
* `locktype` - the leaselock resource type to use for this deployment (ELECTION_TYPE). Supported lock types are:
* `configmaps` - default
* `leases`
* `endpoints`
* `port` - the port on which the election sidecar can be queried.
* `port` - the port on which the election sidecar can be queried (ELECTION_PORT).

## Example
A working example deployment can be found under `example/deployment.yaml`
Expand Down
51 changes: 19 additions & 32 deletions cmd/leader-elector/main.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@ package main
import (
"context"
"encoding/json"
"flag"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"github.com/alexflint/go-arg"
"github.com/kkosmrli/leader-elector/pkg/election"
"k8s.io/klog"
)

var (
electionName string
electionNamespace string
lockType string
renewDeadline time.Duration
retryPeriod time.Duration
leaseDuration time.Duration
port string
leader Leader
args struct {
LockName string `arg:"--election,env:ELECTION_NAME" default:"default" help:"Name of this election"`
Namespace string `arg:"env:ELECTION_NAMESPACE" default:"default" help:"Namespace of this election"`
LockType string `arg:"env:ELECTION_TYPE" default:"configmaps" help:"Resource lock type, must be one of the following: configmaps, endpoints, leases"`
RenewDeadline time.Duration `arg:"--renew-deadline,env:ELECTION_RENEW_DEADLINE" default:"10s" help:"Duration that the acting leader will retry refreshing leadership before giving up"`
RetryPeriod time.Duration `arg:"--retry-period,env:ELECTION_RETRY_PERIOD" default:"2s" help:"Duration between each action retry"`
LeaseDuration time.Duration `arg:"--lease-duration,env:ELECTION_LEASE_DURATION" default:"15s" help:"Duration that non-leader candidates will wait after observing a leadership renewal until attempting to acquire leadership of a led but unrenewed leader slot"`
Port string `arg:"env:ELECTION_PORT" default:"4040" help:"Port on which to query the leader"`
}
leader Leader
)

// Leader contains the name of the current leader of this election
Expand All @@ -40,23 +42,8 @@ func leaderHandler(res http.ResponseWriter, req *http.Request) {
res.Write(data)
}

func parseFlags() {
flag.StringVar(&electionName, "election", "default", "Name of the resource used for this election")
flag.StringVar(&electionNamespace, "namespace", "default", "Namespace of the resource used for this election")
flag.StringVar(&lockType, "locktype", "configmaps",
"Resource lock type, must be one of the following: configmaps, endpoints, leases")
flag.DurationVar(&renewDeadline, "renew-deadline", 10*time.Second,
"Duration that the acting leader will retry refreshing leadership before giving up")
flag.DurationVar(&leaseDuration, "lease-duration", 15*time.Second,
`Duration that non-leader candidates will wait after observing a leadership
renewal until attempting to acquire leadership of a led but unrenewed leader slot`)
flag.DurationVar(&retryPeriod, "retry-period", 2*time.Second, "Duration between each action retry")
flag.StringVar(&port, "port", "4040", "Port on which to query the leader")
flag.Parse()
}

func main() {
parseFlags()
arg.MustParse(&args)

// configuring context
ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -73,7 +60,7 @@ func main() {

// configuring HTTP server
http.HandleFunc("/", leaderHandler)
server := &http.Server{Addr: ":" + port, Handler: nil}
server := &http.Server{Addr: ":" + args.Port, Handler: nil}
go func() {
if err := server.ListenAndServe(); err != nil {
klog.Fatal(err)
Expand All @@ -87,12 +74,12 @@ func main() {
}

electionConfig := election.Config{
LockName: electionName,
LockNamespace: electionNamespace,
LockType: lockType,
RenewDeadline: renewDeadline,
RetryPeriod: retryPeriod,
LeaseDuration: leaseDuration,
LockName: args.LockName,
LockNamespace: args.Namespace,
LockType: args.LockType,
RenewDeadline: args.RenewDeadline,
RetryPeriod: args.RetryPeriod,
LeaseDuration: args.LeaseDuration,
Callback: callback,
}
election.Run(ctx, electionConfig)
Expand Down

0 comments on commit 14ad36b

Please sign in to comment.