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

server: Add /healthz check endpoint #84

Merged
merged 1 commit into from
Aug 10, 2024
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
10 changes: 6 additions & 4 deletions examples/example-deployment.yaml
Original file line number Diff line number Diff line change
@@ -113,17 +113,19 @@ spec:
containerPort: 8080
protocol: TCP
livenessProbe:
tcpSocket:
httpGet:
path: "/healthz"
port: http
initialDelaySeconds: 5
periodSeconds: 10
periodSeconds: 5
successThreshold: 1
failureThreshold: 3
readinessProbe:
tcpSocket:
httpGet:
path: "/healthz"
port: http
initialDelaySeconds: 5
periodSeconds: 10
periodSeconds: 5
volumeMounts:
- mountPath: /config
name: fleetlock-config
5 changes: 5 additions & 0 deletions pkg/server/client/types.go
Original file line number Diff line number Diff line change
@@ -13,3 +13,8 @@ type FleetLockResponse struct {
Kind string `json:"kind"`
Value string `json:"value"`
}

type FleetlockHealthResponse struct {
Status string `json:"status"`
Error string `json:"error"`
}
11 changes: 11 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
@@ -211,9 +211,20 @@ func (s *Server) matchNodeToId(rw http.ResponseWriter, params client.FleetLockRe
return node, true
}

// Return a health status of the server
// URL: /healthz
func (s *Server) handleHealthCheck(rw http.ResponseWriter, _ *http.Request) {
rw.Header().Set("Content-Type", "application/json")
status := client.FleetlockHealthResponse{
Status: "ok",
}
sendResponse(rw, status)
}

// Starts the server and exits with error if that fails
func (s *Server) Run() error {
http.HandleFunc("/", s.requestHandler)
http.HandleFunc("/healthz", s.handleHealthCheck)

slog.Info("Starting server", slog.String("listen", s.cfg.Listen), slog.Bool("ssl", s.cfg.SSL.Enabled))

22 changes: 22 additions & 0 deletions pkg/server/server_test.go
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ package server

import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
@@ -285,6 +286,27 @@ func TestUncordonNode(t *testing.T) {
assert.True(s.uncordonNode(rr, params))
}

func TestHealthCheck(t *testing.T) {
s := &Server{}
rr := httptest.NewRecorder()

assert := assert.New(t)

s.handleHealthCheck(rr, nil)

assert.Equal(http.StatusOK, rr.Result().StatusCode, "Health Check should return with 200")
assert.Equal("application/json", rr.Header().Get("Content-Type"), "Content type should be json")

var res client.FleetlockHealthResponse
err := json.NewDecoder(rr.Result().Body).Decode(&res)

assert.NoError(err, "Response should be parsable")
expectedRes := client.FleetlockHealthResponse{
Status: "ok",
}
assert.Equal(expectedRes, res, "Response should match")
}

func newFleetlockRequest(group, id string) client.FleetLockRequest {
return client.FleetLockRequest{
Client: client.FleetLockRequestClient{
4 changes: 1 addition & 3 deletions pkg/server/utils.go
Original file line number Diff line number Diff line change
@@ -4,8 +4,6 @@ import (
"encoding/json"
"log/slog"
"net/http"

"github.com/heathcliff26/fleetlock/pkg/server/client"
)

func ReadUserIP(req *http.Request) string {
@@ -20,7 +18,7 @@ func ReadUserIP(req *http.Request) string {
}

// Send a response to the writer and handle impossible parse errors
func sendResponse(rw http.ResponseWriter, res client.FleetLockResponse) {
func sendResponse(rw http.ResponseWriter, res any) {
b, err := json.Marshal(res)
if err != nil {
slog.Error("Failed to create Response", "err", err)