Skip to content

Commit 41dbecd

Browse files
committed
MEDIUM: health: add health check for haproxy service
1 parent 15db072 commit 41dbecd

File tree

16 files changed

+570
-11
lines changed

16 files changed

+570
-11
lines changed

configuration/configuration.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type HAProxyConfiguration struct {
4040
ReloadDelay int `short:"d" long:"reload-delay" description:"Minimum delay between two reloads (in s)" default:"5" group:"reload"`
4141
ReloadCmd string `short:"r" long:"reload-cmd" description:"Reload command" group:"reload"`
4242
RestartCmd string `short:"s" long:"restart-cmd" description:"Restart command" group:"reload"`
43+
StatusCmd string `long:"status-cmd" description:"Status command" group:"reload"`
4344
ReloadRetention int `long:"reload-retention" description:"Reload retention in days, every older reload id will be deleted" default:"1" group:"reload"`
4445
TransactionDir string `short:"t" long:"transaction-dir" description:"Path to the transaction directory" default:"/tmp/haproxy" group:"transaction"`
4546
BackupsNumber int `short:"n" long:"backups-number" description:"Number of backup configuration files you want to keep, stored in the config dir with version number suffix" default:"0" group:"transaction"`
@@ -156,9 +157,7 @@ type Configuration struct {
156157
reloadSignal chan os.Signal
157158
}
158159

159-
var (
160-
cfgInitOnce sync.Once
161-
)
160+
var cfgInitOnce sync.Once
162161

163162
// Get returns pointer to configuration
164163
func Get() *Configuration {
@@ -249,7 +248,7 @@ func (c *Configuration) Load() error {
249248

250249
func (c *Configuration) LoadRuntimeVars(swaggerJSON json.RawMessage, host string, port int) error {
251250
var m map[string]interface{}
252-
var json = jsoniter.ConfigCompatibleWithStandardLibrary
251+
json := jsoniter.ConfigCompatibleWithStandardLibrary
253252
err := json.Unmarshal(swaggerJSON, &m)
254253
if err != nil {
255254
return err

configuration/configuration_storage.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configuration/examples/example-full.hcl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ haproxy {
7474

7575
reload {
7676
reload_delay = 5
77-
reload_cmd = "null"
78-
restart_cmd = "null"
77+
reload_cmd = "systemctl reload haproxy"
78+
restart_cmd = "systemctl restart haproxy"
79+
status_cmd = "systemctl status haproxy"
7980
reload_retention = 1
8081
validate_cmd = "null"
8182
}

configuration/examples/example-full.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ haproxy:
6060
master_worker_mode: false # bool
6161
reload:
6262
reload_delay: 5 # int 2
63-
reload_cmd: null # string 2
64-
restart_cmd: null # string 2
63+
reload_cmd: "systemctl reload haproxy"
64+
restart_cmd: "systemctl restart haproxy"
65+
status_cmd: "systemctl status haproxy"
6566
reload_retention: 1 # int 2
6667
validate_cmd: null # string 2
6768
cluster:

configure_data_plane.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler {
211211
Delay: haproxyOptions.ReloadDelay,
212212
ReloadCmd: haproxyOptions.ReloadCmd,
213213
RestartCmd: haproxyOptions.RestartCmd,
214+
StatusCmd: haproxyOptions.StatusCmd,
214215
ConfigFile: haproxyOptions.ConfigFile,
215216
BackupDir: haproxyOptions.BackupsDir,
216217
Retention: haproxyOptions.ReloadRetention,
@@ -754,6 +755,9 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler {
754755
}
755756
}()
756757

758+
// Health
759+
api.HealthGetHealthHandler = &handlers.GetHealthHandlerImpl{HAProxy: ra}
760+
757761
// middlewares
758762
var adpts []adapters.Adapter
759763
adpts = append(adpts,

embedded_spec.go

Lines changed: 83 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generate/swagger/script.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ swagger generate server -f $SPEC_DIR/haproxy_spec.yaml \
6666
--tags=Discovery \
6767
--tags=ServiceDiscovery \
6868
--tags=Information \
69+
--tags=Health \
6970
--tags=Specification \
7071
--tags=SpecificationOpenapiv3 \
7172
--tags=Transactions \

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ require (
2222
github.com/go-openapi/validate v0.19.8
2323
github.com/google/renameio v1.0.1
2424
github.com/google/uuid v1.2.0
25-
github.com/haproxytech/client-native/v3 v3.1.1-0.20220706134924-687bc1e4fcfd
25+
github.com/haproxytech/client-native/v3 v3.1.1-0.20220706141425-f4b9bd8b7913
2626
github.com/haproxytech/config-parser/v4 v4.0.0-rc2.0.20220705063916-e4e79c6d3321
2727
github.com/hashicorp/consul/api v1.6.0
2828
github.com/hashicorp/hcl v1.0.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
175175
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
176176
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
177177
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
178-
github.com/haproxytech/client-native/v3 v3.1.1-0.20220706134924-687bc1e4fcfd h1:jwufMmcdi5zd5p+Ll5k7D6EBcm7gMkbYobGnJnY+jhU=
179-
github.com/haproxytech/client-native/v3 v3.1.1-0.20220706134924-687bc1e4fcfd/go.mod h1:PSYFdp1t3h2MJGdJ4cr7jVQtSn0bT5MkYITdDuDvSdA=
178+
github.com/haproxytech/client-native/v3 v3.1.1-0.20220706141425-f4b9bd8b7913 h1:qhRsHGWaGgU18Y7PRwxtW9F5jAOgK4X6ZkmGiTl+dNM=
179+
github.com/haproxytech/client-native/v3 v3.1.1-0.20220706141425-f4b9bd8b7913/go.mod h1:PSYFdp1t3h2MJGdJ4cr7jVQtSn0bT5MkYITdDuDvSdA=
180180
github.com/haproxytech/config-parser/v4 v4.0.0-rc2.0.20220705063916-e4e79c6d3321 h1:Jzl5PJuFhNU8HWjE6CRwikvZ7is0I3G7+FlSBk4u/Zs=
181181
github.com/haproxytech/config-parser/v4 v4.0.0-rc2.0.20220705063916-e4e79c6d3321/go.mod h1:7V8tKWPnQGC7tANP3uoMc5tU42fEQOe3fdQWkPdjU4o=
182182
github.com/haproxytech/go-logger v1.1.0 h1:HgGtYaI1ApkvbQdsm7f9AzQQoxTB7w37criTflh7IQE=

handlers/health.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package handlers
2+
3+
import (
4+
"github.com/go-openapi/runtime/middleware"
5+
"github.com/haproxytech/client-native/v3/models"
6+
"github.com/haproxytech/dataplaneapi/haproxy"
7+
"github.com/haproxytech/dataplaneapi/operations/health"
8+
)
9+
10+
// GetHaproxyProcessInfoHandlerImpl implementation of the GetHaproxyProcessInfoHandler interface using client-native client
11+
type GetHealthHandlerImpl struct {
12+
HAProxy haproxy.IReloadAgent
13+
}
14+
15+
func (h *GetHealthHandlerImpl) Handle(health.GetHealthParams, interface{}) middleware.Responder {
16+
data := models.Health{}
17+
status, err := h.HAProxy.Status()
18+
if err == nil {
19+
if status {
20+
data.Haproxy = models.HealthHaproxyUp
21+
} else {
22+
data.Haproxy = models.HealthHaproxyDown
23+
}
24+
} else {
25+
data.Haproxy = models.HealthHaproxyUnknown
26+
}
27+
28+
return health.NewGetHealthOK().WithPayload(&data)
29+
}

0 commit comments

Comments
 (0)