Skip to content

Commit

Permalink
feat: improves healthcheck for swarm nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
amir20 committed Nov 25, 2024
1 parent c36cf72 commit d0bd7da
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
5 changes: 5 additions & 0 deletions internal/support/docker/multi_host_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
14 changes: 14 additions & 0 deletions internal/support/docker/retriable_client_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions internal/support/docker/swarm_client_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
16 changes: 5 additions & 11 deletions internal/web/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit d0bd7da

Please sign in to comment.