structs: namespace port validation by host_network

This commit is contained in:
Nick Ethier 2021-02-02 14:56:52 -05:00
parent dc29b679b4
commit 43a4d72fda
2 changed files with 107 additions and 1 deletions

View File

@ -6167,7 +6167,8 @@ func (tg *TaskGroup) Validate(j *Job) error {
func (tg *TaskGroup) validateNetworks() error {
var mErr multierror.Error
portLabels := make(map[string]string)
staticPorts := make(map[int]string)
// host_network -> static port tracking
staticPortsIndex := make(map[string]map[int]string)
for _, net := range tg.Networks {
for _, port := range append(net.ReservedPorts, net.DynamicPorts...) {
@ -6178,6 +6179,14 @@ func (tg *TaskGroup) validateNetworks() error {
}
if port.Value != 0 {
hostNetwork := port.HostNetwork
if hostNetwork == "" {
hostNetwork = "default"
}
staticPorts, ok := staticPortsIndex[hostNetwork]
if !ok {
staticPorts = make(map[int]string)
}
// static port
if other, ok := staticPorts[port.Value]; ok {
err := fmt.Errorf("Static port %d already reserved by %s", port.Value, other)
@ -6187,6 +6196,7 @@ func (tg *TaskGroup) validateNetworks() error {
mErr.Errors = append(mErr.Errors, err)
} else {
staticPorts[port.Value] = fmt.Sprintf("taskgroup network:%s", port.Label)
staticPortsIndex[hostNetwork] = staticPorts
}
}
@ -6212,6 +6222,14 @@ func (tg *TaskGroup) validateNetworks() error {
}
if port.Value != 0 {
hostNetwork := port.HostNetwork
if hostNetwork == "" {
hostNetwork = "default"
}
staticPorts, ok := staticPortsIndex[hostNetwork]
if !ok {
staticPorts = make(map[int]string)
}
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)
@ -6220,6 +6238,7 @@ func (tg *TaskGroup) validateNetworks() error {
mErr.Errors = append(mErr.Errors, err)
} else {
staticPorts[port.Value] = fmt.Sprintf("%s:%s", task.Name, port.Label)
staticPortsIndex[hostNetwork] = staticPorts
}
}
}

View File

@ -1298,6 +1298,93 @@ func TestTaskGroupNetwork_Validate(t *testing.T) {
},
ErrContains: "greater than",
},
{
TG: &TaskGroup{
Name: "group-same-static-port-different-host_network",
Networks: Networks{
&NetworkResource{
ReservedPorts: []Port{
{
Label: "net1_http",
Value: 80,
HostNetwork: "net1",
},
{
Label: "net2_http",
Value: 80,
HostNetwork: "net2",
},
},
},
},
},
},
{
TG: &TaskGroup{
Name: "mixing-group-task-ports",
Networks: Networks{
&NetworkResource{
ReservedPorts: []Port{
{
Label: "group_http",
Value: 80,
},
},
},
},
Tasks: []*Task{
&Task{
Name: "task1",
Resources: &Resources{
Networks: Networks{
&NetworkResource{
ReservedPorts: []Port{
{
Label: "task_http",
Value: 80,
},
},
},
},
},
},
},
},
ErrContains: "already reserved by",
},
{
TG: &TaskGroup{
Name: "mixing-group-task-ports-with-host_network",
Networks: Networks{
&NetworkResource{
ReservedPorts: []Port{
{
Label: "group_http",
Value: 80,
HostNetwork: "net1",
},
},
},
},
Tasks: []*Task{
&Task{
Name: "task1",
Resources: &Resources{
Networks: Networks{
&NetworkResource{
ReservedPorts: []Port{
{
Label: "task_http",
Value: 80,
},
},
},
},
},
},
},
},
},
}
for i := range cases {