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

Allow static ports duplication on network host with multiple matches #23702

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions .changelog/23693.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
networking: Fixed a bug where if a client have two IP on the same network host, only one can provide the same static port
sbenoistmics marked this conversation as resolved.
Show resolved Hide resolved
```
3 changes: 2 additions & 1 deletion nomad/structs/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,8 @@ func (idx *NetworkIndex) AssignPorts(ask *NetworkResource) (AllocatedPorts, erro

// Check if in use
if used != nil && used.Check(uint(port.Value)) {
return nil, fmt.Errorf("reserved port collision %s=%d", port.Label, port.Value)
addrErr = fmt.Errorf("reserved port collision %s=%d", port.Label, port.Value)
continue
}

allocPort = &AllocatedPortMapping{
Expand Down
82 changes: 82 additions & 0 deletions nomad/structs/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,88 @@ func TestNetworkIndex_AssignPortss_SmallRange(t *testing.T) {

}

// TestNetworkIndex_AssignPorts_TwoIp exercises assigning ports on group
// networks with two ip matching host network
func TestNetworkIndex_AssignPorts_TwoIp(t *testing.T) {
ci.Parallel(t)

n := &Node{
NodeResources: &NodeResources{
NodeNetworks: []*NodeNetworkResource{
{
Mode: "host",
Device: "eth0",
Speed: 1000,
Addresses: []NodeNetworkAddress{
{
Alias: "two_ip_test",
Address: "192.168.0.100",
Family: NodeNetworkAF_IPv4,
},
},
},
{
Mode: "host",
Device: "eth1",
Speed: 1000,
Addresses: []NodeNetworkAddress{
{
Alias: "two_ip_test",
Address: "192.168.0.101",
Family: NodeNetworkAF_IPv4,
},
},
},
},
},
}

testCases := []struct {
name string
allocated []AllocatedPortMapping
ask []Port
expectErr string
}{
{
name: "1 reserved port asked, 2 ip, 1 already used",
allocated: []AllocatedPortMapping{{"static", 7000, 7000, "192.168.0.100"}},
ask: []Port{{"static", 7000, 7000, "two_ip_test"}},
expectErr: "",
},
{
name: "1 reserved port asked, 2 ip, 2 already used",
allocated: []AllocatedPortMapping{
{"static", 7000, 7000, "192.168.0.100"},
{"static", 7000, 7000, "192.168.0.101"},
},
ask: []Port{{"static", 7000, 7000, "two_ip_test"}},
expectErr: "reserved port collision static=7000",
},
}

for _, tc := range testCases {

idx := NewNetworkIndex()
idx.SetNode(n)
idx.AddReservedPorts(tc.allocated)

ask := &NetworkResource{ReservedPorts: tc.ask}
offer, err := idx.AssignPorts(ask)
if tc.expectErr != "" {
must.EqError(t, err, tc.expectErr)
} else {
must.NoError(t, err)
must.NotNil(t, offer, must.Sprint("did not get an offer"))

for _, port := range tc.ask {
_, ok := offer.Get(port.Label)
must.True(t, ok)
}
}
}

}

func TestNetworkIndex_AssignTaskNetwork(t *testing.T) {
ci.Parallel(t)
idx := NewNetworkIndex()
Expand Down