kubeprobes/query.go
Piotr Icikowski 8dc7f27400
All checks were successful
ci/woodpecker/push/release Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
refactor(probes): rename ProbeFunction to Probe
2024-03-03 23:25:56 +01:00

55 lines
969 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 []Probe) *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
}