-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Affinity struct, API and parsing #4512
Changes from 3 commits
1570aaa
d53a9b9
63c4b9b
dcf1726
cf3152a
40d0972
7bbad35
63d9691
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,42 @@ func TestTaskGroup_Constrain(t *testing.T) { | |
} | ||
} | ||
|
||
func TestTaskGroup_AddAffinity(t *testing.T) { | ||
t.Parallel() | ||
grp := NewTaskGroup("grp1", 1) | ||
|
||
// Add an affinity to the group | ||
out := grp.AddAffinity(NewAffinity("kernel.version", "=", "4.6", 100)) | ||
if n := len(grp.Affinities); n != 1 { | ||
t.Fatalf("expected 1 affinity, got: %d", n) | ||
} | ||
|
||
// Check that the group was returned | ||
if out != grp { | ||
t.Fatalf("expected: %#v, got: %#v", grp, out) | ||
} | ||
|
||
// Add a second affinity | ||
grp.AddAffinity(NewAffinity("${node.affinity}", "=", "dc2", 50)) | ||
expect := []*Affinity{ | ||
{ | ||
LTarget: "kernel.version", | ||
RTarget: "4.6", | ||
Operand: "=", | ||
Weight: 100, | ||
}, | ||
{ | ||
LTarget: "${node.affinity}", | ||
RTarget: "dc2", | ||
Operand: "=", | ||
Weight: 50, | ||
}, | ||
} | ||
if !reflect.DeepEqual(grp.Affinities, expect) { | ||
t.Fatalf("expect: %#v, got: %#v", expect, grp.Constraints) | ||
} | ||
} | ||
|
||
func TestTaskGroup_SetMeta(t *testing.T) { | ||
t.Parallel() | ||
grp := NewTaskGroup("grp1", 1) | ||
|
@@ -232,6 +268,42 @@ func TestTask_Constrain(t *testing.T) { | |
} | ||
} | ||
|
||
func TestTask_AddAffinity(t *testing.T) { | ||
t.Parallel() | ||
task := NewTask("task1", "exec") | ||
|
||
// Add an affinity to the task | ||
out := task.AddAffinity(NewAffinity("kernel.version", "=", "4.6", 100)) | ||
if n := len(task.Affinities); n != 1 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. require.Len is handy for this https://godoc.org/github.com/stretchr/testify/require#Len |
||
t.Fatalf("expected 1 affinity, got: %d", n) | ||
} | ||
|
||
// Check that the task was returned | ||
if out != task { | ||
t.Fatalf("expected: %#v, got: %#v", task, out) | ||
} | ||
|
||
// Add a second affinity | ||
task.AddAffinity(NewAffinity("${node.datacenter}", "=", "dc2", 50)) | ||
expect := []*Affinity{ | ||
{ | ||
LTarget: "kernel.version", | ||
RTarget: "4.6", | ||
Operand: "=", | ||
Weight: 100, | ||
}, | ||
{ | ||
LTarget: "${node.datacenter}", | ||
RTarget: "dc2", | ||
Operand: "=", | ||
Weight: 50, | ||
}, | ||
} | ||
if !reflect.DeepEqual(task.Affinities, expect) { | ||
t.Fatalf("expect: %#v, got: %#v", expect, task.Affinities) | ||
} | ||
} | ||
|
||
func TestTask_Artifact(t *testing.T) { | ||
t.Parallel() | ||
a := TaskArtifact{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -616,6 +616,15 @@ 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 | ||
} | ||
} | ||
|
||
// COMPAT: Remove in 0.7.0. Update has been pushed into the task groups | ||
if job.Update != nil { | ||
j.Update = structs.UpdateStrategy{} | ||
|
@@ -675,6 +684,15 @@ 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.RestartPolicy = &structs.RestartPolicy{ | ||
Attempts: *taskGroup.RestartPolicy.Attempts, | ||
Interval: *taskGroup.RestartPolicy.Interval, | ||
|
@@ -754,6 +772,15 @@ 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 | ||
} | ||
} | ||
|
||
if l := len(apiTask.Services); l != 0 { | ||
structsTask.Services = make([]*structs.Service, l) | ||
for i, service := range apiTask.Services { | ||
|
@@ -892,3 +919,10 @@ func ApiConstraintToStructs(c1 *api.Constraint, c2 *structs.Constraint) { | |
c2.RTarget = c1.RTarget | ||
c2.Operand = c1.Operand | ||
} | ||
|
||
func ApiAffinityToStructs(a1 *api.Affinity, a2 *structs.Affinity) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not have this return the structs affinity? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I followed the pattern in the rest of this file, like we do for constraint above, but happy to fix this one and the upcoming equivalent method for spread. |
||
a2.LTarget = a1.LTarget | ||
a2.Operand = a1.Operand | ||
a2.RTarget = a1.RTarget | ||
a2.Weight = a1.Weight | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to change; just fyi: you can skip repeating the type if it's the same between arguments: