Refactor method to return affinity struct, and add extra test at task level

This commit is contained in:
Preetha Appan 2018-07-18 14:16:02 -05:00
parent f06c7ab2ad
commit f3c4eead91
No known key found for this signature in database
GPG Key ID: 9F7C19990A50EAFC
3 changed files with 25 additions and 15 deletions

View File

@ -619,9 +619,7 @@ func ApiJobToStructJob(job *api.Job) *structs.Job {
if l := len(job.Affinities); l != 0 {
j.Affinities = make([]*structs.Affinity, l)
for i, a := range job.Affinities {
aff := &structs.Affinity{}
ApiAffinityToStructs(a, aff)
j.Affinities[i] = aff
j.Affinities[i] = ApiAffinityToStructs(a)
}
}
@ -687,9 +685,7 @@ func ApiTgToStructsTG(taskGroup *api.TaskGroup, tg *structs.TaskGroup) {
if l := len(taskGroup.Affinities); l != 0 {
tg.Affinities = make([]*structs.Affinity, l)
for k, affinity := range taskGroup.Affinities {
a := &structs.Affinity{}
ApiAffinityToStructs(affinity, a)
tg.Affinities[k] = a
tg.Affinities[k] = ApiAffinityToStructs(affinity)
}
}
@ -775,9 +771,7 @@ func ApiTaskToStructsTask(apiTask *api.Task, structsTask *structs.Task) {
if l := len(apiTask.Affinities); l != 0 {
structsTask.Affinities = make([]*structs.Affinity, l)
for i, a := range apiTask.Affinities {
aff := &structs.Affinity{}
ApiAffinityToStructs(a, aff)
structsTask.Affinities[i] = aff
structsTask.Affinities[i] = ApiAffinityToStructs(a)
}
}
@ -920,9 +914,11 @@ func ApiConstraintToStructs(c1 *api.Constraint, c2 *structs.Constraint) {
c2.Operand = c1.Operand
}
func ApiAffinityToStructs(a1 *api.Affinity, a2 *structs.Affinity) {
a2.LTarget = a1.LTarget
a2.Operand = a1.Operand
a2.RTarget = a1.RTarget
a2.Weight = a1.Weight
func ApiAffinityToStructs(a1 *api.Affinity) *structs.Affinity {
return &structs.Affinity{
LTarget: a1.LTarget,
Operand: a1.Operand,
RTarget: a1.RTarget,
Weight: a1.Weight,
}
}

View File

@ -148,6 +148,14 @@ func TestParse(t *testing.T) {
},
},
},
Affinities: []*api.Affinity{
{
LTarget: "${meta.foo}",
RTarget: "bar",
Operand: "=",
Weight: 25,
},
},
Services: []*api.Service{
{
Tags: []string{"foo", "bar"},

View File

@ -90,12 +90,18 @@ job "binstore-storagelocker" {
weight = 100
}
task "binstore" {
driver = "docker"
user = "bob"
leader = true
affinity {
attribute = "${meta.foo}"
value = "bar"
operator = "="
weight = 25
}
config {
image = "hashicorp/binstore"