Skip to content

Commit

Permalink
Merge pull request #5641 from hashicorp/b-task-networks-ports
Browse files Browse the repository at this point in the history
client: handle 0.8 server network resources
  • Loading branch information
Mahmood Ali authored May 2, 2019
2 parents a988ae8 + 4a16a31 commit 01c267b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
15 changes: 9 additions & 6 deletions client/taskenv/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,15 +544,9 @@ func (b *Builder) setTask(task *structs.Task) *Builder {
if task.Resources == nil {
b.memLimit = 0
b.cpuLimit = 0
b.networks = []*structs.NetworkResource{}
} else {
b.memLimit = int64(task.Resources.MemoryMB)
b.cpuLimit = int64(task.Resources.CPU)
// Copy networks to prevent sharing
b.networks = make([]*structs.NetworkResource, len(task.Resources.Networks))
for i, n := range task.Resources.Networks {
b.networks[i] = n.Copy()
}
}
return b
}
Expand Down Expand Up @@ -622,6 +616,15 @@ func (b *Builder) setAlloc(alloc *structs.Allocation) *Builder {
}
}
} else if alloc.TaskResources != nil {
if tr, ok := alloc.TaskResources[b.taskName]; ok {
// Copy networks to prevent sharing
b.networks = make([]*structs.NetworkResource, len(tr.Networks))
for i, n := range tr.Networks {
b.networks[i] = n.Copy()
}

}

for taskName, resources := range alloc.TaskResources {
// Add ports from other tasks
if taskName == b.taskName {
Expand Down
16 changes: 8 additions & 8 deletions client/taskenv/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,11 @@ func TestEnvironment_AsList_Old(t *testing.T) {
Device: "eth0",
IP: "192.168.0.100",
ReservedPorts: []structs.Port{
{Label: "admin", Value: 5000},
{Label: "ssh", Value: 22},
{Label: "other", Value: 1234},
},
MBits: 50,
DynamicPorts: []structs.Port{{Label: "http"}},
DynamicPorts: []structs.Port{{Label: "http", Value: 2000}},
},
},
}
Expand All @@ -244,10 +243,10 @@ func TestEnvironment_AsList_Old(t *testing.T) {
Networks: []*structs.NetworkResource{
{
Device: "eth0",
IP: "192.168.0.100",
ReservedPorts: []structs.Port{{Label: "admin", Value: 5000}},
IP: "127.0.0.1",
ReservedPorts: []structs.Port{{Label: "https", Value: 8080}},
MBits: 50,
DynamicPorts: []structs.Port{{Label: "http", Value: 2000}},
DynamicPorts: []structs.Port{{Label: "http", Value: 80}},
},
},
},
Expand All @@ -270,10 +269,11 @@ func TestEnvironment_AsList_Old(t *testing.T) {
"taskEnvKey": "taskEnvVal",
}
task.Resources.Networks = []*structs.NetworkResource{
// Nomad 0.8 didn't fully populate the fields in task Resource Networks
{
IP: "127.0.0.1",
ReservedPorts: []structs.Port{{Label: "http", Value: 80}},
DynamicPorts: []structs.Port{{Label: "https", Value: 8080}},
IP: "",
ReservedPorts: []structs.Port{{Label: "https"}},
DynamicPorts: []structs.Port{{Label: "http"}},
},
}
env := NewBuilder(n, a, task, "global").SetDriverNetwork(
Expand Down

0 comments on commit 01c267b

Please sign in to comment.