Skip to content

Commit 1294f19

Browse files
Swopxvzf
authored andcommitted
feat: k8s probes endpoints (#6)
1 parent e9a9d21 commit 1294f19

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

internal/server/handlers/k8s.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package handlers
2+
3+
import (
4+
"github.com/ankorstore/gh-action-mq-lease-service/internal/lease"
5+
"github.com/ankorstore/gh-action-mq-lease-service/internal/storage"
6+
"github.com/gofiber/fiber/v2"
7+
)
8+
9+
func Liveness() func(c *fiber.Ctx) error {
10+
return func(c *fiber.Ctx) error {
11+
return c.SendStatus(fiber.StatusOK)
12+
}
13+
}
14+
15+
func Readiness(storage storage.Storage[*lease.ProviderState]) func(c *fiber.Ctx) error {
16+
return func(c *fiber.Ctx) error {
17+
if passed := storage.HealthCheck(c.UserContext(), func() *lease.ProviderState {
18+
return lease.NewProviderState(lease.NewProviderStateOpts{
19+
ID: "test-healthcheck",
20+
})
21+
}); !passed {
22+
return c.SendStatus(fiber.StatusInternalServerError)
23+
}
24+
return c.SendStatus(fiber.StatusOK)
25+
}
26+
}

internal/server/routes.go

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package server
33
import (
44
"github.com/ankorstore/gh-action-mq-lease-service/internal/lease"
55
"github.com/ankorstore/gh-action-mq-lease-service/internal/server/handlers"
6+
"github.com/ankorstore/gh-action-mq-lease-service/internal/storage"
67
"github.com/gofiber/fiber/v2"
78
)
89

@@ -14,3 +15,8 @@ func RegisterRoutes(app *fiber.App, orchestrator lease.ProviderOrchestrator) {
1415
providerRoutes.Post("/release", handlers.Release(orchestrator)).Name("release")
1516
providerRoutes.Get("/", handlers.ProviderDetails(orchestrator)).Name("show")
1617
}
18+
19+
func RegisterK8sProbesRoutes(app *fiber.App, storage storage.Storage[*lease.ProviderState]) {
20+
app.Get("/k8s/liveness", handlers.Liveness()).Name("k8s.liveness")
21+
app.Get("/k8s/readiness", handlers.Readiness(storage)).Name("k8s.readiness")
22+
}

internal/server/server.go

+2
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ func (s *serverImpl) Init() error {
141141
},
142142
}))
143143

144+
// register k8s probes handlers
145+
RegisterK8sProbesRoutes(s.app, s.storage)
144146
// register API routes on the fiber app
145147
RegisterRoutes(s.app, s.orchestrator)
146148

internal/storage/storage.go

+24
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ type Storage[T object] interface {
6262
// Save store the provided object in the storage
6363
// the provided object should at least be able to return a non-null and unique Identifier (via the GetIdentifier() method)
6464
Save(ctx context.Context, obj T) error
65+
// HealthCheck verifies if the storage is connected and usable
66+
HealthCheck(ctx context.Context, hydrationSample func() T) bool
6567
}
6668

6769
type storageImpl[T object] struct {
@@ -149,6 +151,24 @@ func (s *storageImpl[T]) Save(ctx context.Context, obj T) error {
149151
return txn.Commit()
150152
}
151153

154+
// HealthCheck verifies if the storage is connected and usable
155+
func (s *storageImpl[T]) HealthCheck(ctx context.Context, hydrationSample func() T) bool {
156+
if s.db == nil {
157+
log.Ctx(ctx).Error().Msg("Storage healthcheck failed: db is nil")
158+
return false
159+
}
160+
if s.db.IsClosed() {
161+
log.Ctx(ctx).Error().Msg("Storage healthcheck failed: db is closed")
162+
return false
163+
}
164+
if err := s.Hydrate(ctx, hydrationSample()); err != nil {
165+
log.Ctx(ctx).Error().Err(err).Msg("Storage healthcheck failed: could not hydrate sample")
166+
return false
167+
}
168+
169+
return true
170+
}
171+
152172
// NullStorage is a dummy object honoring the Storage interface, and can be used in unit tests
153173
// as a drop-in replacement in the dependencies if the test don't actually care about storage actions.
154174
type NullStorage[T object] struct{}
@@ -168,3 +188,7 @@ func (s NullStorage[T]) Hydrate(context.Context, T) error {
168188
func (s NullStorage[T]) Save(context.Context, T) error {
169189
return nil
170190
}
191+
192+
func (s NullStorage[T]) HealthCheck(context.Context, func() T) bool {
193+
return true
194+
}

0 commit comments

Comments
 (0)