kubeprobes/query.go
Piotr Icikowski 9dd25ff024
All checks were successful
ci/woodpecker/pr/test Pipeline was successful
feat(probes)!: rewrite probes logic with named probes
- `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.
2024-03-03 00:00:57 +01:00

55 lines
977 B
Go

package kubeprobes
import "sync"
type statusQuery struct {
ok bool
passed []statusEntry
failed []statusEntry
mux sync.Mutex
wg sync.WaitGroup
}
type statusEntry struct {
Probe string `json:"probe"`
Status error `json:"status,omitempty"`
}
func (sq *statusQuery) wait() {
sq.wg.Wait()
}
func newStatusQuery(probes []ProbeFunction) *statusQuery {
sq := &statusQuery{
ok: true,
passed: make([]statusEntry, 0, len(probes)),
failed: make([]statusEntry, 0, len(probes)),
mux: sync.Mutex{},
wg: sync.WaitGroup{},
}
sq.wg.Add(len(probes))
for _, probe := range probes {
probe := probe
go func() {
defer sq.wg.Done()
sq.mux.Lock()
if err := probe.status(); err != nil {
sq.ok = false
sq.failed = append(sq.failed, statusEntry{
Probe: probe.name(),
Status: err,
})
} else {
sq.passed = append(sq.passed, statusEntry{
Probe: probe.name(),
})
}
sq.mux.Unlock()
}()
}
return sq
}