Piotr Icikowski
9dd25ff024
All checks were successful
ci/woodpecker/pr/test Pipeline was successful
- `ProbeFunction` is now an interface for easier use - `ProbeFunction`s can be auto-updated with specified update interval - `StatefulProbes` changed into `ManualProbes` and implement `ProbeFunction` interface - `ManualProbes` allows for marking probe as unhealthy with custom cause - handlers now return JSON response with failed probes - handler's response can be set to verbose via `Kubeprobes` option or via `?v` request param BREAKING CHANGE: type definitions were replaced with more robust implementation.
72 lines
1.4 KiB
Go
72 lines
1.4 KiB
Go
package kubeprobes
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
// ManualProbe represents the simple probe that can be either
|
|
// marked as "up" (healthy) or "down" (unhealthy).
|
|
type ManualProbe interface {
|
|
ProbeFunction
|
|
|
|
// Pass marks the probe as healthy.
|
|
Pass()
|
|
// Fail marks the probe as unhealthy.
|
|
Fail()
|
|
// FailWitCause marks the probe as unhealthy with given cause.
|
|
FailWithCause(err error)
|
|
}
|
|
|
|
type manualProbe struct {
|
|
probeName string
|
|
err error
|
|
mux sync.RWMutex
|
|
}
|
|
|
|
// NewManualProbe returns a new instance of a manual probe
|
|
// which can be either marked as healthy or unhealthy.
|
|
// The probe is initially marked as unhealthy.
|
|
func NewManualProbe(name string) (ManualProbe, error) {
|
|
if name == "" {
|
|
return nil, errProbeNameEmpty
|
|
}
|
|
|
|
return &manualProbe{
|
|
probeName: name,
|
|
mux: sync.RWMutex{},
|
|
}, nil
|
|
}
|
|
|
|
// name implements ManualProbe.
|
|
func (mp *manualProbe) name() string {
|
|
return mp.probeName
|
|
}
|
|
|
|
// status implements ManualProbe.
|
|
func (mp *manualProbe) status() error {
|
|
mp.mux.RLock()
|
|
defer mp.mux.RUnlock()
|
|
return mp.err
|
|
}
|
|
|
|
// Pass implements ManualProbe.
|
|
func (mp *manualProbe) Pass() {
|
|
mp.mux.Lock()
|
|
defer mp.mux.Unlock()
|
|
mp.err = nil
|
|
}
|
|
|
|
// Fail implements ManualProbe.
|
|
func (mp *manualProbe) Fail() {
|
|
mp.mux.Lock()
|
|
defer mp.mux.Unlock()
|
|
mp.err = errProbeFailed
|
|
}
|
|
|
|
// FailWithCause implements ManualProbe.
|
|
func (mp *manualProbe) FailWithCause(err error) {
|
|
mp.mux.Lock()
|
|
defer mp.mux.Unlock()
|
|
mp.err = err
|
|
}
|