From 43938ed8a8369068f3ceec25227708ab518f023c Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Tue, 19 May 2026 14:08:53 +0100 Subject: [PATCH] feat(oidcgate): healthz and readyz endpoints --- cmd/oidcgate/health.go | 24 +++++++++++++++++++++++ cmd/oidcgate/health_test.go | 38 +++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 cmd/oidcgate/health.go create mode 100644 cmd/oidcgate/health_test.go diff --git a/cmd/oidcgate/health.go b/cmd/oidcgate/health.go new file mode 100644 index 0000000..83b5739 --- /dev/null +++ b/cmd/oidcgate/health.go @@ -0,0 +1,24 @@ +package main + +import "net/http" + +// readyReporter is satisfied by *traefikoidc.TraefikOidc via its Ready() method. +type readyReporter interface { + Ready() bool +} + +func newHealthzHandler() http.Handler { + return http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) { + rw.WriteHeader(http.StatusOK) + }) +} + +func newReadyzHandler(r readyReporter) http.Handler { + return http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) { + if r.Ready() { + rw.WriteHeader(http.StatusOK) + return + } + rw.WriteHeader(http.StatusServiceUnavailable) + }) +} diff --git a/cmd/oidcgate/health_test.go b/cmd/oidcgate/health_test.go new file mode 100644 index 0000000..09e1267 --- /dev/null +++ b/cmd/oidcgate/health_test.go @@ -0,0 +1,38 @@ +package main + +import ( + "net/http" + "net/http/httptest" + "testing" +) + +type readyStub struct{ ready bool } + +func (r *readyStub) Ready() bool { return r.ready } + +func TestHealthz_Always200(t *testing.T) { + h := newHealthzHandler() + rec := httptest.NewRecorder() + h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/healthz", nil)) + if rec.Code != http.StatusOK { + t.Fatalf("healthz: want 200, got %d", rec.Code) + } +} + +func TestReadyz_503BeforeDiscovery(t *testing.T) { + h := newReadyzHandler(&readyStub{ready: false}) + rec := httptest.NewRecorder() + h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/readyz", nil)) + if rec.Code != http.StatusServiceUnavailable { + t.Fatalf("readyz pre-discovery: want 503, got %d", rec.Code) + } +} + +func TestReadyz_200AfterDiscovery(t *testing.T) { + h := newReadyzHandler(&readyStub{ready: true}) + rec := httptest.NewRecorder() + h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/readyz", nil)) + if rec.Code != http.StatusOK { + t.Fatalf("readyz post-discovery: want 200, got %d", rec.Code) + } +}