Skip to content

Commit

Permalink
Merge pull request #2807 from hashicorp/f-validate-ports
Browse files Browse the repository at this point in the history
Prevent port conflicts
  • Loading branch information
dadgar committed Jul 7, 2017
2 parents 25b393b + c26e813 commit c0d35bf
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ BUG FIXES:
* driver/exec: Properly set file/dir ownership in chroots [GH-2552]
* driver/docker: Fix panic in Docker driver on Windows [GH-2614]
* driver/rkt: Fix env var interpolation [GH-2777]
* jobspec/validation: Prevent static port conflicts [GH-2807]
* server: Reject non-TLS clients when TLS enabled [GH-2525]
* server: Fix a panic in plan evaluation with partial failures and all_at_once
set [GH-2544]
Expand Down
19 changes: 18 additions & 1 deletion nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2531,8 +2531,10 @@ func (tg *TaskGroup) Validate(j *Job) error {
}
}

// Check for duplicate tasks and that there is only leader task if any
// Check for duplicate tasks, that there is only leader task if any,
// and no duplicated static ports
tasks := make(map[string]int)
staticPorts := make(map[int]string)
leaderTasks := 0
for idx, task := range tg.Tasks {
if task.Name == "" {
Expand All @@ -2546,6 +2548,21 @@ func (tg *TaskGroup) Validate(j *Job) error {
if task.Leader {
leaderTasks++
}

if task.Resources == nil {
continue
}

for _, net := range task.Resources.Networks {
for _, port := range net.ReservedPorts {
if other, ok := staticPorts[port.Value]; ok {
err := fmt.Errorf("Static port %d already reserved by %s", port.Value, other)
mErr.Errors = append(mErr.Errors, err)
} else {
staticPorts[port.Value] = fmt.Sprintf("%s:%s", task.Name, port.Label)
}
}
}
}

if leaderTasks > 1 {
Expand Down
53 changes: 53 additions & 0 deletions nomad/structs/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,59 @@ func TestTaskGroup_Validate(t *testing.T) {
t.Fatalf("err: %s", err)
}

tg = &TaskGroup{
Tasks: []*Task{
&Task{
Name: "task-a",
Resources: &Resources{
Networks: []*NetworkResource{
&NetworkResource{
ReservedPorts: []Port{{Label: "foo", Value: 123}},
},
},
},
},
&Task{
Name: "task-b",
Resources: &Resources{
Networks: []*NetworkResource{
&NetworkResource{
ReservedPorts: []Port{{Label: "foo", Value: 123}},
},
},
},
},
},
}
err = tg.Validate(&Job{})
expected := `Static port 123 already reserved by task-a:foo`
if !strings.Contains(err.Error(), expected) {
t.Errorf("expected %s but found: %v", expected, err)
}

tg = &TaskGroup{
Tasks: []*Task{
&Task{
Name: "task-a",
Resources: &Resources{
Networks: []*NetworkResource{
&NetworkResource{
ReservedPorts: []Port{
{Label: "foo", Value: 123},
{Label: "bar", Value: 123},
},
},
},
},
},
},
}
err = tg.Validate(&Job{})
expected = `Static port 123 already reserved by task-a:foo`
if !strings.Contains(err.Error(), expected) {
t.Errorf("expected %s but found: %v", expected, err)
}

tg = &TaskGroup{
Name: "web",
Count: 1,
Expand Down

0 comments on commit c0d35bf

Please sign in to comment.