8000 feat(hatchery): /mon/status (#3156) · ovh/cds@067465e · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 067465e

Browse files
authored
feat(hatchery): /mon/status (#3156)
1 parent bdb9429 commit 067465e

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed

engine/hatchery/serve.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ func (c *Common) initRouter(ctx context.Context, h hatchery.Interface) {
103103
r.Middlewares = append(r.Middlewares, c.AuthMiddleware)
104104

105105
r.Handle("/mon/version", r.GET(api.VersionHandler, api.Auth(false)))
106+
r.Handle("/mon/status", r.GET(getStatusHandler(h), api.Auth(false)))
106107
r.Handle("/mon/workers", r.GET(getWorkersPoolHandler(h), api.Auth(false)))
107108
}
108109

@@ -120,3 +121,19 @@ func getWorkersPoolHandler(h hatchery.Interface) api.HandlerFunc {
120121
}
121122
}
122123
}
124+
125+
func getStatusHandler(h hatchery.Interface) api.HandlerFunc {
126+
return func() api.Handler {
127+
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
128+
if h == nil {
129+
return nil
130+
}
131+
srv, ok := h.(service.Service)
132+
if !ok {
133+
return fmt.Errorf("unable to get status from %s", h.Hatchery().Name)
134+
}
135+
status := srv.Status()
136+
return api.WriteJSON(w, status, status.HTTPStatusCode())
137+
}
138+
}
139+
}

engine/hatchery/swarm/swarm.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func (h *HatcherySwarm) Init() error {
5959
MaxContainers: h.Configuration().Provision.MaxWorker,
6060
name: "default",
6161
}
62+
log.Info("hatchery> swarm> connected to default docker engine")
6263

6364
} else {
6465
for hostName, cfg := range h.Config.DockerEngines {

engine/hatchery/swarm/swarm_conf.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,26 @@ func (h *HatcherySwarm) ApplyConfiguration(cfg interface{}) error {
5151
// Status returns sdk.MonitoringStatus, implements interface service.Service
5252
func (h *HatcherySwarm) Status() sdk.MonitoringStatus {
5353
m := h.CommonMonitoring()
54-
5554
if h.IsInitialized() {
5655
m.Lines = append(m.Lines, sdk.MonitoringStatusLine{Component: "Workers", Value: fmt.Sprintf("%d/%d", len(h.WorkersStarted()), h.Config.Provision.MaxWorker), Status: sdk.MonitoringStatusOK})
5756
for dockerName, dockerClient := range h.dockerClients {
57+
//Check images
5858
status := sdk.MonitoringStatusOK
5959
images, err := dockerClient.ImageList(context.Background(), types.ImageListOptions{All: true})
6060
if err != nil {
6161
log.Warning("hatchery> swarm> %s> Status> Unable to list images on %s: %s", h.Name, dockerName, err)
6262
status = sdk.MonitoringStatusAlert
6363
}
6464
m.Lines = append(m.Lines, sdk.MonitoringStatusLine{Component: "Images-" + dockerName, Value: fmt.Sprintf("%d", len(images)), Status: status})
65+
//Check containers
66+
status = sdk.MonitoringStatusOK
67+
cs, err := h.getContainers(dockerClient, types.ContainerListOptions{All: true})
68+
if err != nil {
69+
log.Warning("hatchery> swarm> %s> Status> Unable to list containers on %s: %s", h.Name, dockerName, err)
70+
status = sdk.MonitoringStatusAlert
71+
}
72+
m.Lines = append(m.Lines, sdk.MonitoringStatusLine{Component: "Containers-" + dockerName, Value: fmt.Sprintf("%d", len(cs)), Status: status})
73+
6574
}
6675
}
6776

sdk/hatchery/hatchery.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ func Create(h Interface) error {
8585
// purges expired items every minute
8686
spawnIDs := cache.New(10*time.Second, 60*time.Second)
8787

88+
// hatchery is now fully Initialized
89+
h.SetInitialized()
90+
8891
sdk.GoRoutine("heartbeat", func() {
8992
hearbeat(h, h.Configuration().API.Token, h.Configuration().API.MaxHeartbeatFailures)
9093
})
@@ -96,9 +99,6 @@ func Create(h Interface) error {
9699
}
97100
})
98101

99-
// hatchery is now fully Initialized
100-
h.SetInitialized()
101-
102102
// run the starters pool
103103
workersStartChan, workerStartResultChan := startWorkerStarters(h)
104104

sdk/status.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sdk
33
import (
44
"encoding/json"
55
"fmt"
6+
"net/http"
67
"time"
78
)
89

@@ -27,6 +28,16 @@ type MonitoringStatusLine struct {
2728
Type string `json:"type" cli:"type"`
2829
}
2930

31+
// HTTPStatusCode return the http status code
32+
func (m MonitoringStatus) HTTPStatusCode() int {
33+
for _, l := range m.Lines {
34+
if l.Status != MonitoringStatusOK {
35+
return http.StatusServiceUnavailable
36+
}
37+
}
38+
return http.StatusOK
39+
}
40+
3041
func (m MonitoringStatusLine) String() string {
3142
return fmt.Sprintf("%s - %s: %s", m.Status, m.Component, m.Value)
3243
}

0 commit comments

Comments
 (0)
0