Interpolating service tags

This commit is contained in:
Diptanu Choudhury 2016-03-26 12:49:49 -07:00
parent 5512d69438
commit 5439d4c23c
4 changed files with 29 additions and 2 deletions

View file

@ -388,6 +388,9 @@ func (e *UniversalExecutor) SyncServices(ctx *ConsulContext) error {
cs.SetDelegatedChecks(e.createCheckMap(), e.createCheck)
e.consulService = cs
}
if e.ctx != nil {
e.interpolateServices(e.ctx.Task)
}
err := e.consulService.SyncTask(e.ctx.Task)
go e.consulService.PeriodicSync()
return err
@ -541,3 +544,10 @@ func (e *UniversalExecutor) createCheck(check *structs.ServiceCheck, checkID str
}
return nil, fmt.Errorf("couldn't create check for %v", check.Name)
}
func (e *UniversalExecutor) interpolateServices(task *structs.Task) {
e.ctx.TaskEnv.Build()
for _, service := range task.Services {
service.Tags = e.ctx.TaskEnv.ParseAndReplace(service.Tags)
}
}

View file

@ -5,6 +5,7 @@ import (
"log"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
@ -267,3 +268,18 @@ func TestExecutor_MakeExecutable(t *testing.T) {
t.Fatalf("expected permissions %v; got %v", err)
}
}
func TestExecutorInterpolateServices(t *testing.T) {
task := mock.Job().TaskGroups[0].Tasks[0]
// Make a fake exececutor
ctx := testExecutorContext(t)
defer ctx.AllocDir.Destroy()
executor := NewExecutor(log.New(os.Stdout, "", log.LstdFlags))
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)
}
}

View file

@ -9,10 +9,10 @@ var (
// ReplaceEnv takes an arg and replaces all occurences of environment variables.
// If the variable is found in the passed map it is replaced, otherwise the
// original string is returned.
func ReplaceEnv(arg string, environents ...map[string]string) string {
func ReplaceEnv(arg string, environments ...map[string]string) string {
return envRe.ReplaceAllStringFunc(arg, func(arg string) string {
stripped := arg[2 : len(arg)-1]
for _, env := range environents {
for _, env := range environments {
if value, ok := env[stripped]; ok {
return value
}

View file

@ -96,6 +96,7 @@ func Job() *structs.Job {
{
Name: "${TASK}-frontend",
PortLabel: "http",
Tags: []string{"pci:${meta.pci-dss}", "datacenter:${node.datacenter}"},
},
{
Name: "${TASK}-admin",