Skip to content
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

Prevent port conflicts #2807

Merged
merged 3 commits into from
Jul 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,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 @@ -2527,8 +2527,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 @@ -2542,6 +2544,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 @@ -806,6 +806,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