Major enhancements #6

Merged
Piotr Icikowski merged 3 commits from enhancements into devel 2024-03-01 23:46:04 +01:00
Showing only changes of commit d6beb81938 - Show all commits

View File

@ -10,19 +10,23 @@ go get -u pkg.icikowski.pl/kubeprobes
## Usage ## Usage
The package provides `kubeprobes.New` function which returns a probes handler compliant with `http.Handler` interface. The package provides `kubeprobes.New` function which returns a probes handler of type `kubeprobes.Kubeprobes`, which is compliant with `http.Handler` interface.
The handler serves two endpoints, which are used to implement liveness and readiness probes by returning either `200` (healthy) or `503` (unhealthy) status: The handler serves two endpoints, which are used to implement liveness and readiness probes by returning either `200` (healthy) or `503` (unhealthy) status:
- `/live` - endpoint for liveness probe; - `/live` - endpoint for liveness probe;
- `/ready` - endpoint for readiness probe. - `/ready` - endpoint for readiness probe.
Accessing any other endpoint will return `404` status. In order to provide maximum performance, no body is ever returned. Default paths can be overriden with options described below. Accessing any other endpoint will return `404` status. In order to provide maximum performance, no body is ever returned.
The `kubeprobes.New` function accepts following options-applying functions as arguments: The `kubeprobes.New` function accepts following options as arguments:
- `kubeprobes.WithLivenessProbes(/* ... */)` - adds particular [probes](#probes) to the list of liveness probes; - `kubeprobes.WithLivenessProbes(...)` - adds particular [probe functions](#probe-functions) to the list of liveness probes;
- `kubeprobes.WithReadinessProbes(/* ... */)` - adds particular [probes](#probes) to the list of readiness probes. - `kubeprobes.WithLivenessStatefulProbes(...)` - adds particular [`StatefulProbe`s](#stateful-probes) to the list of liveness probes;
- `kubeprobes.WithLivenessPath("/some/liveness/path")` - sets liveness probe path to given path (default is `/live`);
- `kubeprobes.WithReadinessProbes(...)` - adds particular [probe functions](#probe-functions) to the list of readiness probes;
- `kubeprobes.WithReadinessStatefulProbes(...)` - adds particular [`StatefulProbe`s](#stateful-probes) to the list of readiness probes;
- `kubeprobes.WithReadinessPath("/some/readiness/path")` - sets readiness probe path to given path (default is `/ready`).
## Probes ## Probes
@ -47,7 +51,7 @@ someOtherProbe := func() error {
} }
// Use functions in probes handler // Use functions in probes handler
kp := kubeprobes.New( kp, _ := kubeprobes.New(
kubeprobes.WithLivenessProbes(someOtherProbe), kubeprobes.WithLivenessProbes(someOtherProbe),
kubeprobes.WithReadinessProbes(someProbe), kubeprobes.WithReadinessProbes(someProbe),
) )
@ -63,24 +67,52 @@ someProbe := kubeprobes.NewStatefulProbe()
someOtherProbe := kubeprobes.NewStatefulProbe() someOtherProbe := kubeprobes.NewStatefulProbe()
// Use it in probes handler // Use it in probes handler
kp := kubeprobes.New( kp, _ := kubeprobes.New(
kubeprobes.WithLivenessProbes(someProbe.GetProbeFunction()), kubeprobes.WithLivenessStatefulProbes(someProbe),
kubeprobes.WithReadinessProbes(someOtherProbe.GetProbeFunction()), kubeprobes.WithReadinessStatefulProbes(someOtherProbe),
) )
``` ```
## 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.GetLivenessHandler()
readinessHandler := kp.GetReadinessHandler()
```
Those handler can be used for manually mounting them on other servers/routers/muxes (eg. `go-chi/chi`, `gorilla/mux`, `http`'s `ServeMux` etc.).
## Example usage ## Example usage
```go ```go
// Create probe functions
appProbe := func() error {
// Some logic for checking app status
return nil
}
// Create stateful probes // Create stateful probes
live := kubeprobes.NewStatefulProbe() live := kubeprobes.NewStatefulProbe()
ready := kubeprobes.NewStatefulProbe() ready := kubeprobes.NewStatefulProbe()
// Prepare handler // Prepare handler
kp := kubeprobes.New( kp, err := kubeprobes.New(
kubeprobes.WithLivenessProbes(live.GetProbeFunction()), kubeprobes.WithLivenessStatefulProbes(live),
kubeprobes.WithReadinessProbes(ready.GetProbeFunction()), kubeprobes.WithReadinessStatefulProbes(ready),
kubeprobes.WithReadinessProbes(appProbe),
kubeprobes.WithLivenessPath("/livez"),
kubeprobes.WithReadinessPath("/readyz"),
) )
if err != nil {
// Kubeprobes object is validated for invalid or conflicting paths! ;)
panic(err)
}
// Start the probes server // Start the probes server
probes := &http.Server{ probes := &http.Server{