Skip to content

Commit

Permalink
Add a 'body' field to the check stanza
Browse files Browse the repository at this point in the history
Consul allows specifying the HTTP body to send in a health check. Nomad
uses Consul for health checking so this just plumbs the value through to
where the Consul API is called.

There is no validation that `body` is not used with an incompatible
check method like GET.
  • Loading branch information
nick96 committed Mar 18, 2021
1 parent e200caa commit 7a6695e
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type ServiceCheck struct {
TaskName string `mapstructure:"task" hcl:"task,optional"`
SuccessBeforePassing int `mapstructure:"success_before_passing" hcl:"success_before_passing,optional"`
FailuresBeforeCritical int `mapstructure:"failures_before_critical" hcl:"failures_before_critical,optional"`
Body string `hcl:"body,optional"`
OnUpdate string `mapstructure:"on_update" hcl:"on_update,optional"`
}

Expand Down
1 change: 1 addition & 0 deletions command/agent/consul/service_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,7 @@ func createCheckReg(serviceID, checkID string, check *structs.ServiceCheck, host
chkReg.HTTP = checkURL.String()
chkReg.Method = check.Method
chkReg.Header = check.Header
chkReg.Body = check.Body

case structs.ServiceCheckTCP:
chkReg.TCP = net.JoinHostPort(host, strconv.Itoa(port))
Expand Down
1 change: 1 addition & 0 deletions jobspec/parse_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,7 @@ func parseChecks(service *api.Service, checkObjs *ast.ObjectList) error {
"tls_skip_verify",
"header",
"method",
"body",
"check_restart",
"address_mode",
"grpc_service",
Expand Down
41 changes: 41 additions & 0 deletions jobspec/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,47 @@ func TestParse(t *testing.T) {
},
false,
},
{
"tg-service-check-body.hcl",
&api.Job{
ID: stringToPtr("group_service_check_script"),
Name: stringToPtr("group_service_check_script"),
TaskGroups: []*api.TaskGroup{
{
Name: stringToPtr("group"),
Count: intToPtr(1),
Networks: []*api.NetworkResource{
{
Mode: "bridge",
ReservedPorts: []api.Port{
{
Label: "http",
Value: 80,
To: 8080,
},
},
},
},
Services: []*api.Service{
{
Name: "foo-service",
PortLabel: "http",
Checks: []api.ServiceCheck{
{
Name: "check-name",
Type: "http",
Method: "POST",
Body: "{\"check\":\"mem\"}",
},
},
},
},
Tasks: []*api.Task{{Name: "foo"}},
},
},
},
false,
},
{
"tg-service-connect-native.hcl",
&api.Job{
Expand Down
28 changes: 28 additions & 0 deletions jobspec/test-fixtures/tg-service-check-body.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
job "group_service_check_script" {
group "group" {
count = 1

network {
mode = "bridge"

port "http" {
static = 80
to = 8080
}
}

service {
name = "foo-service"
port = "http"

check {
name = "check-name"
type = "http"
method = "POST"
body = "{\"check\":\"mem\"}"
}
}

task "foo" {}
}
}
12 changes: 12 additions & 0 deletions nomad/structs/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2805,6 +2805,12 @@ func TestTaskGroupDiff(t *testing.T) {
Old: "",
New: "",
},
{
Type: DiffTypeNone,
Name: "Body",
Old: "",
New: "",
},
{
Type: DiffTypeEdited,
Name: "Command",
Expand Down Expand Up @@ -5996,6 +6002,12 @@ func TestTaskDiff(t *testing.T) {
Old: "",
New: "",
},
{
Type: DiffTypeNone,
Name: "Body",
Old: "",
New: "",
},
{
Type: DiffTypeNone,
Name: "Command",
Expand Down
5 changes: 5 additions & 0 deletions nomad/structs/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type ServiceCheck struct {
TaskName string // What task to execute this check in
SuccessBeforePassing int // Number of consecutive successes required before considered healthy
FailuresBeforeCritical int // Number of consecutive failures required before considered unhealthy
Body string // Body to use in HTTP check
OnUpdate string
}

Expand Down Expand Up @@ -168,6 +169,10 @@ func (sc *ServiceCheck) Equals(o *ServiceCheck) bool {
return false
}

if sc.Body != o.Body {
return false
}

if sc.OnUpdate != o.OnUpdate {
return false
}
Expand Down
1 change: 1 addition & 0 deletions vendor/github.com/hashicorp/nomad/api/services.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7a6695e

Please sign in to comment.