Skip to content

Commit dff64ef

Browse files
committed
Fix service.check_restart stanza propagation
There was a bug in jobspec parsing, a bug in CheckRestart merging, and a bug in CheckRestart canonicalization. All are now tested.
1 parent 39e08eb commit dff64ef

File tree

6 files changed

+87
-6
lines changed

6 files changed

+87
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
BUG FIXES:
44
* core: Fix search endpoint forwarding for multi-region clusters [[GH-3680](https://github.com/hashicorp/nomad/issues/3680)]
55
* config: Revert minimum CPU limit back to 20 from 100.
6+
* discovery: Fix handling of `service.check_restart` [[GH-3718](https://github.com/hashicorp/nomad/issues/3718)]
67

78
## 0.7.1 (December 19, 2017)
89

api/tasks.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (c *CheckRestart) Merge(o *CheckRestart) *CheckRestart {
136136
nc.Grace = o.Grace
137137
}
138138

139-
if nc.IgnoreWarnings {
139+
if !nc.IgnoreWarnings {
140140
nc.IgnoreWarnings = o.IgnoreWarnings
141141
}
142142

@@ -189,9 +189,9 @@ func (s *Service) Canonicalize(t *Task, tg *TaskGroup, job *Job) {
189189

190190
// Canonicallize CheckRestart on Checks and merge Service.CheckRestart
191191
// into each check.
192-
for _, c := range s.Checks {
192+
for i, c := range s.Checks {
193+
s.Checks[i].CheckRestart = c.CheckRestart.Merge(s.CheckRestart)
193194
c.CheckRestart.Canonicalize()
194-
c.CheckRestart = c.CheckRestart.Merge(s.CheckRestart)
195195
}
196196
}
197197

command/agent/job_endpoint_test.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,10 @@ func TestJobs_ApiJobToStructsJob(t *testing.T) {
12121212
Name: "serviceA",
12131213
Tags: []string{"1", "2"},
12141214
PortLabel: "foo",
1215+
CheckRestart: &api.CheckRestart{
1216+
Limit: 4,
1217+
Grace: helper.TimeToPtr(11 * time.Second),
1218+
},
12151219
Checks: []api.ServiceCheck{
12161220
{
12171221
Id: "hello",
@@ -1228,10 +1232,17 @@ func TestJobs_ApiJobToStructsJob(t *testing.T) {
12281232
InitialStatus: "ok",
12291233
CheckRestart: &api.CheckRestart{
12301234
Limit: 3,
1231-
Grace: helper.TimeToPtr(10 * time.Second),
12321235
IgnoreWarnings: true,
12331236
},
12341237
},
1238+
{
1239+
Id: "check2id",
1240+
Name: "check2",
1241+
Type: "tcp",
1242+
PortLabel: "foo",
1243+
Interval: 4 * time.Second,
1244+
Timeout: 2 * time.Second,
1245+
},
12351246
},
12361247
},
12371248
},
@@ -1425,10 +1436,21 @@ func TestJobs_ApiJobToStructsJob(t *testing.T) {
14251436
InitialStatus: "ok",
14261437
CheckRestart: &structs.CheckRestart{
14271438
Limit: 3,
1428-
Grace: 10 * time.Second,
1439+
Grace: 11 * time.Second,
14291440
IgnoreWarnings: true,
14301441
},
14311442
},
1443+
{
1444+
Name: "check2",
1445+
Type: "tcp",
1446+
PortLabel: "foo",
1447+
Interval: 4 * time.Second,
1448+
Timeout: 2 * time.Second,
1449+
CheckRestart: &structs.CheckRestart{
1450+
Limit: 4,
1451+
Grace: 11 * time.Second,
1452+
},
1453+
},
14321454
},
14331455
},
14341456
},

jobspec/parse.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,7 @@ func parseServices(jobName string, taskGroupName string, task *api.Task, service
912912
"port",
913913
"check",
914914
"address_mode",
915+
"check_restart",
915916
}
916917
if err := helper.CheckHCLKeys(o.Val, valid); err != nil {
917918
return multierror.Prefix(err, fmt.Sprintf("service (%d) ->", idx))
@@ -1042,7 +1043,7 @@ func parseChecks(service *api.Service, checkObjs *ast.ObjectList) error {
10421043
if ot, ok := co.Val.(*ast.ObjectType); ok {
10431044
checkRestartList = ot.List
10441045
} else {
1045-
return fmt.Errorf("check_restart '%s': should be an object", check.Name)
1046+
return fmt.Errorf("check '%s': should be an object", check.Name)
10461047
}
10471048

10481049
if cro := checkRestartList.Filter("check_restart"); len(cro.Items) > 0 {

jobspec/parse_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,42 @@ func TestParse(t *testing.T) {
631631
},
632632
false,
633633
},
634+
{
635+
"service-check-restart.hcl",
636+
&api.Job{
637+
ID: helper.StringToPtr("service_check_restart"),
638+
Name: helper.StringToPtr("service_check_restart"),
639+
Type: helper.StringToPtr("service"),
640+
TaskGroups: []*api.TaskGroup{
641+
{
642+
Name: helper.StringToPtr("group"),
643+
Tasks: []*api.Task{
644+
{
645+
Name: "task",
646+
Services: []*api.Service{
647+
{
648+
Name: "http-service",
649+
CheckRestart: &api.CheckRestart{
650+
Limit: 3,
651+
Grace: helper.TimeToPtr(10 * time.Second),
652+
IgnoreWarnings: true,
653+
},
654+
Checks: []api.ServiceCheck{
655+
{
656+
Name: "random-check",
657+
Type: "tcp",
658+
PortLabel: "9001",
659+
},
660+
},
661+
},
662+
},
663+
},
664+
},
665+
},
666+
},
667+
},
668+
false,
669+
},
634670
}
635671

636672
for _, tc := range cases {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
job "service_check_restart" {
2+
type = "service"
3+
group "group" {
4+
task "task" {
5+
service {
6+
name = "http-service"
7+
check_restart {
8+
limit = 3
9+
grace = "10s"
10+
ignore_warnings = true
11+
}
12+
check {
13+
name = "random-check"
14+
type = "tcp"
15+
port = "9001"
16+
}
17+
}
18+
}
19+
}
20+
}
21+

0 commit comments

Comments
 (0)