kubeprobes/README.md

140 lines
5.3 KiB
Markdown
Raw Permalink Normal View History

2023-07-21 23:13:32 +02:00
# kubeprobes
2024-05-28 01:52:49 +02:00
[![Go Report Card](https://goreportcard.com/badge/pkg.icikowski.pl/kubeprobes)](https://goreportcard.com/report/pkg.icikowski.pl/kubeprobes)
2023-07-21 23:13:32 +02:00
Simple and effective package for implementing [Kubernetes liveness and readiness probes](https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/)' handler.
## Installation
```bash
go get -u pkg.icikowski.pl/kubeprobes
```
## Usage
2024-03-01 23:43:53 +01:00
The package provides `kubeprobes.New` function which returns a probes handler of type `kubeprobes.Kubeprobes`, which is compliant with `http.Handler` interface.
2023-07-21 23:13:32 +02:00
The handler serves two endpoints, which are used to implement liveness and readiness probes by returning either `200` (healthy) or `503` (unhealthy) status and JSON response with probes results:
2023-07-21 23:13:32 +02:00
- `/live` - endpoint for liveness probe;
- `/ready` - endpoint for readiness probe.
Default paths can be overriden with options described below. Accessing any other endpoint will return `404` status. By default, response body only contains a list of failed probes, but this behavior can be changed with provided option or by adding `?v` query parameter.
2023-07-21 23:13:32 +02:00
2024-03-01 23:43:53 +01:00
The `kubeprobes.New` function accepts following options as arguments:
2023-07-21 23:13:32 +02:00
- `kubeprobes.WithLivenessProbes(...)` - adds particular [probes](#probes) to the list of liveness probes;
2024-03-01 23:43:53 +01:00
- `kubeprobes.WithLivenessPath("/some/liveness/path")` - sets liveness probe path to given path (default is `/live`);
- `kubeprobes.WithReadinessProbes(...)` - adds particular [probes](#probes) to the list of readiness probes;
- `kubeprobes.WithReadinessPath("/some/readiness/path")` - sets readiness probe path to given path (default is `/ready`);
- `kubeprobes.WithVerboseOutput()` - enables verbose output by default (returns both failed and passed probes).
2023-07-21 23:13:32 +02:00
## Probes
In order to determine the state of particular element of application, probes need to be implemented either by creating [probes from functions](#standard-probes) or by using simple and thread-safe [manual probes](#manual-probes).
2023-07-21 23:13:32 +02:00
### Standard probes
2023-07-21 23:13:32 +02:00
Probes (instances of `Probe` interface) are wrappers for functions that performs user defined logic with given interval of updates in order to determine whether the probe should be marked as healthy or not. Those functions should take no arguments and return error (if no error is returned, the probe is considered to be healthy; if error is returned, the probe is considered to be unhealthy). If given interval is less or equal zero, then function is only checked on probe creation and remains in determined state forever.
2023-07-21 23:13:32 +02:00
```go
someProbe := kubeprobes.NewProbe("live", func() error {
2023-07-21 23:13:32 +02:00
// Some logic here
if time.Now().Weekday() == time.Wednesday {
// Fail only on wednesday!
return errors.New("It's wednesday, my dudes!")
2023-07-21 23:13:32 +02:00
}
return nil
}, 1 * time.Hour)
2023-07-21 23:13:32 +02:00
someOtherProbe := kubeprobes.NewProbe("ready", func() error {
2023-07-21 23:13:32 +02:00
// Always healthy
return nil
}, 0) // This probe is checked once
2023-07-21 23:13:32 +02:00
// Use functions in probes handler
2024-03-01 23:43:53 +01:00
kp, _ := kubeprobes.New(
2023-07-21 23:13:32 +02:00
kubeprobes.WithLivenessProbes(someOtherProbe),
kubeprobes.WithReadinessProbes(someProbe),
)
```
### Manual probes
Manual probes (instances of `ManualProbe` interface) are objects that can be marked either as healthy or unhealthy and implement `Probe` for easy integration. Those objects utilize `sync.RMutex` mechanism to ensure thread-safety.
2023-07-21 23:13:32 +02:00
Those probes can be changed by user with provided methods:
- `Pass()` marks probe as healthy;
- `Fail()` marks probe as unhealthy with generic cause;
- `FailWithCause(someError)` marks probe as unhealthy with given error as cause.
2023-07-21 23:13:32 +02:00
```go
// Unhealthy by default
someProbe := kubeprobes.NewManualProbe("live")
someOtherProbe := kubeprobes.NewManualProbe("ready")
2023-07-21 23:13:32 +02:00
// Use it in probes handler
2024-03-01 23:43:53 +01:00
kp, _ := kubeprobes.New(
kubeprobes.WithLivenessProbes(someProbe),
kubeprobes.WithReadinessProbes(someOtherProbe),
2023-07-21 23:13:32 +02:00
)
// Can be later marked according to needs
someProbe.Pass()
someOtherProbe.FailWithCause(errors.New("I'm not doing anything!"))
2023-07-21 23:13:32 +02:00
```
2024-03-01 23:43:53 +01:00
## Direct handler access
It is possible to fetch `http.Handler`s for liveness & readiness probes from `kubeprobes.Kubeprobes` instance as follows:
```go
kp, _ := kubeprobes.New(
// ...
)
livenessHandler := kp.LivenessHandler()
readinessHandler := kp.ReadinessHandler()
2024-03-01 23:43:53 +01:00
```
Those handler can be used for manually mounting them on other servers/routers/muxes (eg. `go-chi/chi`, `gorilla/mux`, `http`'s `ServeMux` etc.).
2024-03-01 23:43:53 +01:00
2023-07-21 23:13:32 +02:00
## Example usage
```go
2024-03-01 23:43:53 +01:00
// Create probe functions
appProbe := func() error {
// Some logic for checking app status
return nil
}
// Create manual probes
2024-05-28 01:43:09 +02:00
live := kubeprobes.NewManualProbe("liveness")
ready := kubeprobes.NewManualProbe("readiness")
2023-07-21 23:13:32 +02:00
// Prepare handler
2024-03-01 23:43:53 +01:00
kp, err := kubeprobes.New(
kubeprobes.WithLivenessProbes(live),
kubeprobes.WithReadinessProbes(ready, appProbe),
2024-03-01 23:43:53 +01:00
kubeprobes.WithLivenessPath("/livez"),
kubeprobes.WithReadinessPath("/readyz"),
kubeprobes.WithVerboseOutput(),
2023-07-21 23:13:32 +02:00
)
2024-03-01 23:43:53 +01:00
if err != nil {
// Kubeprobes object is validated for invalid or conflicting paths! ;)
panic(err)
}
2023-07-21 23:13:32 +02:00
// Start the probes server
probes := &http.Server{
Addr: ":8080",
Handler: kp,
}
go probes.ListenAndServe()
// Mark probes as healthy
live.Pass()
ready.Pass()
2023-07-21 23:13:32 +02:00
```