Merge pull request #9739 from hashicorp/b-alloc-netmode-ports

Use port's to value when building service address under 'alloc' addr_mode
This commit is contained in:
Nick Ethier
2021-01-07 09:16:27 -05:00
committed by GitHub
4 changed files with 29 additions and 2 deletions

View File

@@ -1547,7 +1547,7 @@ func getAddress(addrMode, portLabel string, networks structs.Networks, driverNet
return driverNet.IP, port, nil
case "alloc":
case structs.AddressModeAlloc:
if netStatus == nil {
return "", 0, fmt.Errorf(`cannot use address_mode="alloc": no allocation network status reported`)
}
@@ -1559,6 +1559,10 @@ func getAddress(addrMode, portLabel string, networks structs.Networks, driverNet
// If port is a label and is found then return it
if port, ok := ports.Get(portLabel); ok {
// Use port.To value unless not set
if port.To > 0 {
return netStatus.Address, port.To, nil
}
return netStatus.Address, port.Value, nil
}

View File

@@ -1632,6 +1632,24 @@ func TestGetAddress(t *testing.T) {
Address: "172.26.0.1",
},
ExpectedIP: "172.26.0.1",
ExpectedPort: 6379,
},
{
Name: "Alloc no to value",
Mode: structs.AddressModeAlloc,
PortLabel: "db",
Ports: []structs.AllocatedPortMapping{
{
Label: "db",
Value: 12345,
HostIP: HostIP,
},
},
Status: &structs.AllocNetworkStatus{
InterfaceName: "eth0",
Address: "172.26.0.1",
},
ExpectedIP: "172.26.0.1",
ExpectedPort: 12345,
},
{