mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 02:15:43 +03:00
Added support for interpolation in check cmd and args
This commit is contained in:
@@ -545,11 +545,17 @@ func (e *UniversalExecutor) createCheck(check *structs.ServiceCheck, checkID str
|
||||
return nil, fmt.Errorf("couldn't create check for %v", check.Name)
|
||||
}
|
||||
|
||||
// interpolateServices interpolates tags in a service with values from the
|
||||
// interpolateServices interpolates tags in a service and checks with values from the
|
||||
// task's environment.
|
||||
func (e *UniversalExecutor) interpolateServices(task *structs.Task) {
|
||||
e.ctx.TaskEnv.Build()
|
||||
for _, service := range task.Services {
|
||||
for _, check := range service.Checks {
|
||||
if check.Type == structs.ServiceCheckScript {
|
||||
check.Cmd = e.ctx.TaskEnv.ReplaceEnv(check.Cmd)
|
||||
check.Args = e.ctx.TaskEnv.ParseAndReplace(check.Args)
|
||||
}
|
||||
}
|
||||
service.Tags = e.ctx.TaskEnv.ParseAndReplace(service.Tags)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -278,8 +278,18 @@ func TestExecutorInterpolateServices(t *testing.T) {
|
||||
|
||||
executor.(*UniversalExecutor).ctx = ctx
|
||||
executor.(*UniversalExecutor).interpolateServices(task)
|
||||
expected := []string{"pci:true", "datacenter:dc1"}
|
||||
if !reflect.DeepEqual(task.Services[0].Tags, expected) {
|
||||
t.Fatalf("expected: %v, actual: %v", expected, task.Services[0].Tags)
|
||||
expectedTags := []string{"pci:true", "datacenter:dc1"}
|
||||
if !reflect.DeepEqual(task.Services[0].Tags, expectedTags) {
|
||||
t.Fatalf("expected: %v, actual: %v", expectedTags, task.Services[0].Tags)
|
||||
}
|
||||
|
||||
expectedCheckCmd := "/usr/local/check-table-mysql"
|
||||
expectedCheckArgs := []string{"5.6"}
|
||||
if !reflect.DeepEqual(task.Services[0].Checks[0].Cmd, expectedCheckCmd) {
|
||||
t.Fatalf("expected: %v, actual: %v", expectedCheckCmd, task.Services[0].Checks[0].Cmd)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(task.Services[0].Checks[0].Args, expectedCheckArgs) {
|
||||
t.Fatalf("expected: %v, actual: %v", expectedCheckArgs, task.Services[0].Checks[0].Args)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,9 @@ func Node() *structs.Node {
|
||||
"consul": "foobar.dc1",
|
||||
},
|
||||
Meta: map[string]string{
|
||||
"pci-dss": "true",
|
||||
"pci-dss": "true",
|
||||
"database": "mysql",
|
||||
"version": "5.6",
|
||||
},
|
||||
NodeClass: "linux-medium-pci",
|
||||
Status: structs.NodeStatusReady,
|
||||
@@ -97,6 +99,16 @@ func Job() *structs.Job {
|
||||
Name: "${TASK}-frontend",
|
||||
PortLabel: "http",
|
||||
Tags: []string{"pci:${meta.pci-dss}", "datacenter:${node.datacenter}"},
|
||||
Checks: []*structs.ServiceCheck{
|
||||
{
|
||||
Name: "check-table",
|
||||
Type: structs.ServiceCheckScript,
|
||||
Cmd: "/usr/local/check-table-${meta.database}",
|
||||
Args: []string{"${meta.version}"},
|
||||
Interval: 30 * time.Second,
|
||||
Timeout: 5 * time.Second,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "${TASK}-admin",
|
||||
|
||||
Reference in New Issue
Block a user