mirror of
https://github.com/amir20/dozzle.git
synced 2025-12-21 13:23:07 +01:00
feat: improves healthcheck for swarm nodes (#3422)
This commit is contained in:
@@ -1,9 +1,15 @@
|
||||
services:
|
||||
dozzle-service:
|
||||
image: amir20/dozzle:latest
|
||||
image: amir20/dozzle:local-test
|
||||
environment:
|
||||
- DOZZLE_LEVEL=debug
|
||||
- DOZZLE_MODE=swarm
|
||||
healthcheck:
|
||||
test: ["CMD", "/dozzle", "healthcheck"]
|
||||
interval: 3s
|
||||
timeout: 30s
|
||||
retries: 5
|
||||
start_period: 30s
|
||||
volumes:
|
||||
- /var/run/docker.sock:/var/run/docker.sock
|
||||
ports:
|
||||
|
||||
@@ -26,6 +26,7 @@ type ClientManager interface {
|
||||
RetryAndList() ([]ClientService, []error)
|
||||
Subscribe(ctx context.Context, channel chan<- docker.Host)
|
||||
Hosts(ctx context.Context) []docker.Host
|
||||
LocalClients() []docker.Client
|
||||
}
|
||||
|
||||
type MultiHostService struct {
|
||||
@@ -156,3 +157,7 @@ func (m *MultiHostService) LocalHost() (docker.Host, error) {
|
||||
func (m *MultiHostService) SubscribeAvailableHosts(ctx context.Context, hosts chan<- docker.Host) {
|
||||
m.manager.Subscribe(ctx, hosts)
|
||||
}
|
||||
|
||||
func (m *MultiHostService) LocalClients() []docker.Client {
|
||||
return m.manager.LocalClients()
|
||||
}
|
||||
|
||||
@@ -179,3 +179,17 @@ func (m *RetriableClientManager) Hosts(ctx context.Context) []docker.Host {
|
||||
|
||||
return hosts
|
||||
}
|
||||
|
||||
func (m *RetriableClientManager) LocalClients() []docker.Client {
|
||||
services := m.List()
|
||||
|
||||
clients := make([]docker.Client, 0)
|
||||
|
||||
for _, service := range services {
|
||||
if clientService, ok := service.(*dockerClientService); ok {
|
||||
clients = append(clients, clientService.client)
|
||||
}
|
||||
}
|
||||
|
||||
return clients
|
||||
}
|
||||
|
||||
@@ -229,3 +229,7 @@ func (m *SwarmClientManager) Hosts(ctx context.Context) []docker.Host {
|
||||
func (m *SwarmClientManager) String() string {
|
||||
return fmt.Sprintf("SwarmClientManager{clients: %d}", len(m.clients))
|
||||
}
|
||||
|
||||
func (m *SwarmClientManager) LocalClients() []docker.Client {
|
||||
return []docker.Client{m.localClient}
|
||||
}
|
||||
|
||||
@@ -9,17 +9,11 @@ import (
|
||||
func (h *handler) healthcheck(w http.ResponseWriter, r *http.Request) {
|
||||
log.Debug().Msg("Executing healthcheck")
|
||||
|
||||
for _, host := range h.multiHostService.Hosts() {
|
||||
if host.Type == "agent" {
|
||||
log.Debug().Str("host", host.ID).Msg("Skipping agent host for healthcheck")
|
||||
continue
|
||||
}
|
||||
|
||||
_, err := h.multiHostService.ListContainersForHost(host.ID)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("host", host.ID).Msg("Error listing containers")
|
||||
http.Error(w, "Error listing containers", http.StatusInternalServerError)
|
||||
return
|
||||
clients := h.multiHostService.LocalClients()
|
||||
for _, client := range clients {
|
||||
if _, err := client.Ping(r.Context()); err != nil {
|
||||
log.Error().Err(err).Str("host", client.Host().Name).Msg("error pinging host")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user