Skip to content

Commit

Permalink
Only interpret vars wrapped in braces
Browse files Browse the repository at this point in the history
  • Loading branch information
dadgar committed Feb 5, 2016
1 parent d5e6a9c commit 8b55fd6
Show file tree
Hide file tree
Showing 20 changed files with 65 additions and 69 deletions.
2 changes: 1 addition & 1 deletion command/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ job "example" {
# Restrict our job to only linux. We can specify multiple
# constraints as needed.
constraint {
attribute = "$attr.kernel.name"
attribute = "${attr.kernel.name}"
value = "linux"
}
Expand Down
8 changes: 2 additions & 6 deletions helper/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@ package args
import "regexp"

var (
envRe = regexp.MustCompile(`\$({[a-zA-Z0-9_\.]+}|[a-zA-Z0-9_\.]+)`)
envRe = regexp.MustCompile(`\${[a-zA-Z0-9_\.]+}`)
)

// 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 {
return envRe.ReplaceAllStringFunc(arg, func(arg string) string {
stripped := arg[1:]
if stripped[0] == '{' {
stripped = stripped[1 : len(stripped)-1]
}

stripped := arg[2 : len(arg)-1]
for _, env := range environents {
if value, ok := env[stripped]; ok {
return value
Expand Down
10 changes: 5 additions & 5 deletions helper/args/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ var (
)

func TestArgs_ReplaceEnv_Invalid(t *testing.T) {
input := "$FOO"
exp := "$FOO"
input := "${FOO}"
exp := input
act := ReplaceEnv(input, envVars)

if !reflect.DeepEqual(act, exp) {
Expand All @@ -34,7 +34,7 @@ func TestArgs_ReplaceEnv_Invalid(t *testing.T) {
}

func TestArgs_ReplaceEnv_Valid(t *testing.T) {
input := fmt.Sprintf(`"$%v"!`, ipKey)
input := fmt.Sprintf(`"${%v}"!`, ipKey)
exp := fmt.Sprintf("\"%s\"!", ipVal)
act := ReplaceEnv(input, envVars)

Expand All @@ -44,7 +44,7 @@ func TestArgs_ReplaceEnv_Valid(t *testing.T) {
}

func TestArgs_ReplaceEnv_Period(t *testing.T) {
input := fmt.Sprintf(`"$%v"!`, periodKey)
input := fmt.Sprintf(`"${%v}"!`, periodKey)
exp := fmt.Sprintf("\"%s\"!", periodVal)
act := ReplaceEnv(input, envVars)

Expand All @@ -54,7 +54,7 @@ func TestArgs_ReplaceEnv_Period(t *testing.T) {
}

func TestArgs_ReplaceEnv_Chained(t *testing.T) {
input := fmt.Sprintf("$%s$%s", ipKey, portKey)
input := fmt.Sprintf("${%s}${%s}", ipKey, portKey)
exp := fmt.Sprintf("%s%s", ipVal, portVal)
act := ReplaceEnv(input, envVars)

Expand Down
4 changes: 2 additions & 2 deletions nomad/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func Job() *structs.Job {
Datacenters: []string{"dc1"},
Constraints: []*structs.Constraint{
&structs.Constraint{
LTarget: "$attr.kernel.name",
LTarget: "${attr.kernel.name}",
RTarget: "linux",
Operand: "=",
},
Expand Down Expand Up @@ -145,7 +145,7 @@ func SystemJob() *structs.Job {
Datacenters: []string{"dc1"},
Constraints: []*structs.Constraint{
&structs.Constraint{
LTarget: "$attr.kernel.name",
LTarget: "${attr.kernel.name}",
RTarget: "linux",
Operand: "=",
},
Expand Down
6 changes: 3 additions & 3 deletions nomad/structs/node_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ func EscapedConstraints(constraints []*Constraint) []*Constraint {
// computed node class optimization.
func constraintTargetEscapes(target string) bool {
switch {
case strings.HasPrefix(target, "$node.unique."):
case strings.HasPrefix(target, "${node.unique."):
return true
case strings.HasPrefix(target, "$attr.unique."):
case strings.HasPrefix(target, "${attr.unique."):
return true
case strings.HasPrefix(target, "$meta.unique."):
case strings.HasPrefix(target, "${meta.unique."):
return true
default:
return false
Expand Down
12 changes: 6 additions & 6 deletions nomad/structs/node_class_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,34 +172,34 @@ func TestNode_ComputedClass_Meta(t *testing.T) {
func TestNode_EscapedConstraints(t *testing.T) {
// Non-escaped constraints
ne1 := &Constraint{
LTarget: "$attr.kernel.name",
LTarget: "${attr.kernel.name}",
RTarget: "linux",
Operand: "=",
}
ne2 := &Constraint{
LTarget: "$meta.key_foo",
LTarget: "${meta.key_foo}",
RTarget: "linux",
Operand: "<",
}
ne3 := &Constraint{
LTarget: "$node.dc",
LTarget: "${node.dc}",
RTarget: "test",
Operand: "!=",
}

// Escaped constraints
e1 := &Constraint{
LTarget: "$attr.unique.kernel.name",
LTarget: "${attr.unique.kernel.name}",
RTarget: "linux",
Operand: "=",
}
e2 := &Constraint{
LTarget: "$meta.unique.key_foo",
LTarget: "${meta.unique.key_foo}",
RTarget: "linux",
Operand: "<",
}
e3 := &Constraint{
LTarget: "$unique.node.id",
LTarget: "${unique.node.id}",
RTarget: "test",
Operand: "!=",
}
Expand Down
8 changes: 4 additions & 4 deletions scheduler/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,22 +166,22 @@ func TestEvalEligibility_TaskGroupStatus(t *testing.T) {
func TestEvalEligibility_SetJob(t *testing.T) {
e := NewEvalEligibility()
ne1 := &structs.Constraint{
LTarget: "$attr.kernel.name",
LTarget: "${attr.kernel.name}",
RTarget: "linux",
Operand: "=",
}
e1 := &structs.Constraint{
LTarget: "$attr.unique.kernel.name",
LTarget: "${attr.unique.kernel.name}",
RTarget: "linux",
Operand: "=",
}
e2 := &structs.Constraint{
LTarget: "$meta.unique.key_foo",
LTarget: "${meta.unique.key_foo}",
RTarget: "linux",
Operand: "<",
}
e3 := &structs.Constraint{
LTarget: "$meta.unique.key_foo",
LTarget: "${meta.unique.key_foo}",
RTarget: "Windows",
Operand: "<",
}
Expand Down
16 changes: 8 additions & 8 deletions scheduler/feasible.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,25 +296,25 @@ func resolveConstraintTarget(target string, node *structs.Node) (interface{}, bo

// Handle the interpolations
switch {
case "$node.unique.id" == target:
case "${node.unique.id}" == target:
return node.ID, true

case "$node.datacenter" == target:
case "${node.datacenter}" == target:
return node.Datacenter, true

case "$node.unique.name" == target:
case "${node.unique.name}" == target:
return node.Name, true

case "$node.class" == target:
case "${node.class}" == target:
return node.NodeClass, true

case strings.HasPrefix(target, "$attr."):
attr := strings.TrimPrefix(target, "$attr.")
case strings.HasPrefix(target, "${attr."):
attr := strings.TrimSuffix(strings.TrimPrefix(target, "${attr."), "}")
val, ok := node.Attributes[attr]
return val, ok

case strings.HasPrefix(target, "$meta."):
meta := strings.TrimPrefix(target, "$meta.")
case strings.HasPrefix(target, "${meta."):
meta := strings.TrimSuffix(strings.TrimPrefix(target, "${meta."), "}")
val, ok := node.Meta[meta]
return val, ok

Expand Down
24 changes: 12 additions & 12 deletions scheduler/feasible_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,17 @@ func TestConstraintChecker(t *testing.T) {
constraints := []*structs.Constraint{
&structs.Constraint{
Operand: "=",
LTarget: "$node.datacenter",
LTarget: "${node.datacenter}",
RTarget: "dc1",
},
&structs.Constraint{
Operand: "is",
LTarget: "$attr.kernel.name",
LTarget: "${attr.kernel.name}",
RTarget: "linux",
},
&structs.Constraint{
Operand: "is",
LTarget: "$node.class",
LTarget: "${node.class}",
RTarget: "large",
},
}
Expand Down Expand Up @@ -189,53 +189,53 @@ func TestResolveConstraintTarget(t *testing.T) {
node := mock.Node()
cases := []tcase{
{
target: "$node.unique.id",
target: "${node.unique.id}",
node: node,
val: node.ID,
result: true,
},
{
target: "$node.datacenter",
target: "${node.datacenter}",
node: node,
val: node.Datacenter,
result: true,
},
{
target: "$node.unique.name",
target: "${node.unique.name}",
node: node,
val: node.Name,
result: true,
},
{
target: "$node.class",
target: "${node.class}",
node: node,
val: node.NodeClass,
result: true,
},
{
target: "$node.foo",
target: "${node.foo}",
node: node,
result: false,
},
{
target: "$attr.kernel.name",
target: "${attr.kernel.name}",
node: node,
val: node.Attributes["kernel.name"],
result: true,
},
{
target: "$attr.rand",
target: "${attr.rand}",
node: node,
result: false,
},
{
target: "$meta.pci-dss",
target: "${meta.pci-dss}",
node: node,
val: node.Meta["pci-dss"],
result: true,
},
{
target: "$meta.rand",
target: "${meta.rand}",
node: node,
result: false,
},
Expand Down
2 changes: 1 addition & 1 deletion scheduler/generic_sched_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func TestServiceSched_JobRegister_FeasibleAndInfeasibleTG(t *testing.T) {
job.TaskGroups[0].Count = 2
job.TaskGroups[0].Constraints = append(job.Constraints,
&structs.Constraint{
LTarget: "$node.class",
LTarget: "${node.class}",
RTarget: "class_0",
Operand: "=",
},
Expand Down
6 changes: 3 additions & 3 deletions scheduler/stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func benchmarkServiceStack_MetaKeyConstraint(b *testing.B, key string, numNodes,
// Create a job whose constraint meets two node classes.
job := mock.Job()
job.Constraints[0] = &structs.Constraint{
LTarget: "$meta." + key,
LTarget: fmt.Sprintf("${meta.%v}", key),
RTarget: "1",
Operand: "<",
}
Expand Down Expand Up @@ -224,7 +224,7 @@ func TestServiceStack_Select_ConstraintFilter(t *testing.T) {
if met.ClassFiltered["linux-medium-pci"] != 1 {
t.Fatalf("bad: %#v", met)
}
if met.ConstraintFiltered["$attr.kernel.name = freebsd"] != 1 {
if met.ConstraintFiltered["${attr.kernel.name} = freebsd"] != 1 {
t.Fatalf("bad: %#v", met)
}
}
Expand Down Expand Up @@ -440,7 +440,7 @@ func TestSystemStack_Select_ConstraintFilter(t *testing.T) {
if met.ClassFiltered["linux-medium-pci"] != 1 {
t.Fatalf("bad: %#v", met)
}
if met.ConstraintFiltered["$attr.kernel.name = freebsd"] != 1 {
if met.ConstraintFiltered["${attr.kernel.name} = freebsd"] != 1 {
t.Fatalf("bad: %#v", met)
}
}
Expand Down
2 changes: 1 addition & 1 deletion website/source/docs/drivers/docker.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ The following options are available for use in the job specification.
before launching the task. For example:

```
args = ["$nomad.ip", "$MY_ENV", "$meta.foo"]
args = ["${nomad.ip}", "${MY_ENV}", "${meta.foo}"]
```

* `labels` - (Optional) A key/value map of labels to set to the containers on
Expand Down
2 changes: 1 addition & 1 deletion website/source/docs/drivers/exec.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ The `exec` driver supports the following configuration in the job spec:
before launching the task. For example:

```
args = ["$nomad.ip", "$MY_ENV", "$meta.foo"]
args = ["${nomad.ip}", "${MY_ENV}", "${meta.foo}"]
```

## Client Requirements
Expand Down
2 changes: 1 addition & 1 deletion website/source/docs/drivers/java.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ The `java` driver supports the following configuration in the job spec:
before launching the task. For example:

```
args = ["$nomad.ip", "$MY_ENV", "$meta.foo"]
args = ["${nomad.ip}", "${MY_ENV}", "${meta.foo}"]
```

* `jvm_options` - (Optional) A list of JVM options to be passed while invoking
Expand Down
2 changes: 1 addition & 1 deletion website/source/docs/drivers/raw_exec.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ The `raw_exec` driver supports the following configuration in the job spec:
before launching the task. For example:

```
args = ["$nomad.ip", "$MY_ENV", "$meta.foo"]
args = ["${nomad.ip}", "${MY_ENV}", "${meta.foo}"]
```

## Client Requirements
Expand Down
2 changes: 1 addition & 1 deletion website/source/docs/drivers/rkt.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ The `rkt` driver supports the following configuration in the job spec:
before launching the task. For example:

```
args = ["$nomad.ip", "$MY_ENV", $meta.foo"]
args = ["${nomad.ip}", "${MY_ENV}", ${meta.foo}"]
```

* `trust_prefix` - (Optional) The trust prefix to be passed to rkt. Must be
Expand Down
2 changes: 1 addition & 1 deletion website/source/docs/http/alloc.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ be specified using the `?region=` query parameter.
{
"Operand": "=",
"RTarget": "linux",
"LTarget": "$attr.kernel.name"
"LTarget": "${attr.kernel.name}"
}
]
},
Expand Down
Loading

0 comments on commit 8b55fd6

Please sign in to comment.