Skip to content

Commit

Permalink
Merge pull request #119 from nais/sse-api
Browse files Browse the repository at this point in the history
Implement SSE API for more advanced clients
  • Loading branch information
mortenlj authored Aug 22, 2024
2 parents 5fbecac + 094d7c0 commit 415bd00
Show file tree
Hide file tree
Showing 13 changed files with 396 additions and 140 deletions.
8 changes: 1 addition & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build the manager binary
FROM --platform=$BUILDPLATFORM golang:1.23 as builder
FROM --platform=$BUILDPLATFORM golang:1.23 AS builder

WORKDIR /workspace

Expand All @@ -17,15 +17,9 @@ RUN go build std
# Copy rest of project
COPY . /workspace

# Download envtest tools early to avoid re-downloading on every code change
RUN make .envtest

# Run tests
RUN make test

# Run integration tests
RUN make integration_test

# Build
RUN CGO_ENABLED=0 go build -a -installsuffix cgo -o elector cmd/elector/main.go

Expand Down
9 changes: 3 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
K8S_VERSION := 1.28.3
K8S_VERSION := 1.29
SHELL := /bin/bash

elector:
go build -o bin/elector cmd/elector/*.go

test:
go test ./... -count=1 -coverprofile cover.out -short

.ONESHELL:
integration_test: .envtest
test: .envtest
source .envtest
go test ./pkg/election/... -tags=integration -v -count=1
go run github.com/onsi/ginkgo/v2/ginkgo run pkg/...

.envtest:
go run sigs.k8s.io/controller-runtime/tools/setup-envtest use -p env $(K8S_VERSION) > .envtest
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,30 @@ Users should make sure to have alerts to detect when a leader is stuck.
API
---

When running, elector can be queried on the election port to get the name of the current leader.
Elector has two API endpoints on the election port for getting information about the currently elected leader.
The endpoints return the same information, but one is a simple JSON object and the other is a [Server Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) stream.

The object returned looks like this:

```json
{
"name": "pod-name",
"last_update": "timestamp of last update"
}
```

### Original API: `/`

Simple GET with immediate return of the described object.


### SSE API: `/sse`

The SSE API is a stream of server sent events that will send a message whenever there is an update.
Each event will be a JSON object as described above.


### Ports

Default election port is 6060 (override with `--http`).
Metrics are available on port 9090 (override with `--metrics-address`).
Expand All @@ -26,8 +49,8 @@ Probes are available on port 8080 (override with `--probe-address`).
Development
-----------

The integration tests uses envtest to simulate a kubernetes cluster.
Using the `integration_test` target in make will configure envtest for you before running the tests.
Some of the tests uses envtest to simulate a kubernetes cluster.
Using the `test` target in make will configure envtest for you before running the tests.

If you would rather control the setup of envtest yourself, use the setup-envtest command to install and configure envtest.

Expand Down
7 changes: 4 additions & 3 deletions cmd/elector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"context"
"fmt"
"github.com/go-logr/logr"
"github.com/nais/elector/pkg/election"
"github.com/nais/elector/pkg/election/candidate"
"github.com/nais/elector/pkg/election/official"
"github.com/nais/elector/pkg/logging"
"k8s.io/apimachinery/pkg/types"
"os"
Expand Down Expand Up @@ -154,13 +155,13 @@ func main() {
terminator := context.Background()
electionResults := make(chan string)

err = election.AddCandidateToManager(mgr, logger, electionResults, electionName)
err = candidate.AddCandidateToManager(mgr, logger, electionResults, electionName)
if err != nil {
logger.Error(err)
os.Exit(ExitCandidateAdded)
}

err = election.AddOfficialToManager(mgr, logger, electionResults, viper.GetString(ElectionAddress))
err = official.AddOfficialToManager(mgr, logger, electionResults, viper.GetString(ElectionAddress))
if err != nil {
logger.Error(fmt.Errorf("failed to add election official to controller-runtime manager: %w", err))
os.Exit(ExitOfficialAdded)
Expand Down
16 changes: 11 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ go 1.22.0
toolchain go1.22.2

require (
github.com/benjamintf1/unmarshalledmatchers v1.0.0
github.com/go-logr/logr v1.4.2
github.com/nais/liberator v0.0.0-20231114130128-a3a9edbe0da1
github.com/onsi/ginkgo/v2 v2.20.0
github.com/onsi/gomega v1.34.1
github.com/prometheus/client_golang v1.20.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/pflag v1.0.5
Expand All @@ -32,12 +35,14 @@ require (
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
Expand Down Expand Up @@ -65,13 +70,14 @@ require (
github.com/x448/float16 v0.8.4 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/sys v0.23.0 // indirect
golang.org/x/term v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.24.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
Expand Down
Loading

0 comments on commit 415bd00

Please sign in to comment.