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

consul/connect: enable setting local_bind_address in upstream #10071

Merged
merged 2 commits into from
Feb 26, 2021
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ BUG FIXES:
* cli: Fixed a bug where non-int proxy port would panic CLI [[GH-10072](https://github.com/hashicorp/nomad/issues/10072)]
* cli: Fixed a bug where `nomad operator debug` incorrectly parsed https Consul API URLs. [[GH-10082](https://github.com/hashicorp/nomad/pull/10082)]

IMPROVEMENTS:
* consul/connect: Enable setting `local_bind_address` field on connect upstreams [[GH-6248](https://github.com/hashicorp/nomad/issues/6248)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like you already mentioned, we'll want this under the 1.1.0 header now, specifically under the IMPROVEMENTS sub header

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated changelog

Also added LocalBindAddress to interpolation function, and updated interpolation tests

## 1.0.4 (February 24, 2021)

FEATURES:
Expand Down
7 changes: 4 additions & 3 deletions api/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,10 @@ func (cp *ConsulProxy) Canonicalize() {

// ConsulUpstream represents a Consul Connect upstream jobspec stanza.
type ConsulUpstream struct {
DestinationName string `mapstructure:"destination_name" hcl:"destination_name,optional"`
LocalBindPort int `mapstructure:"local_bind_port" hcl:"local_bind_port,optional"`
Datacenter string `mapstructure:"datacenter" hcl:"datacenter,optional"`
DestinationName string `mapstructure:"destination_name" hcl:"destination_name,optional"`
LocalBindPort int `mapstructure:"local_bind_port" hcl:"local_bind_port,optional"`
Datacenter string `mapstructure:"datacenter" hcl:"datacenter,optional"`
LocalBindAddress string `mapstructure:"local_bind_address" hcl:"local_bind_address,optional"`
}

type ConsulExposeConfig struct {
Expand Down
8 changes: 5 additions & 3 deletions api/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,10 @@ func TestService_Connect_proxy_settings(t *testing.T) {
Proxy: &ConsulProxy{
Upstreams: []*ConsulUpstream{
{
DestinationName: "upstream",
LocalBindPort: 80,
Datacenter: "dc2",
DestinationName: "upstream",
LocalBindPort: 80,
Datacenter: "dc2",
LocalBindAddress: "127.0.0.2",
},
},
LocalServicePort: 8000,
Expand All @@ -244,6 +245,7 @@ func TestService_Connect_proxy_settings(t *testing.T) {
require.Equal(t, proxy.Upstreams[0].DestinationName, "upstream")
require.Equal(t, proxy.Upstreams[0].LocalBindPort, 80)
require.Equal(t, proxy.Upstreams[0].Datacenter, "dc2")
require.Equal(t, proxy.Upstreams[0].LocalBindAddress, "127.0.0.2")
require.Equal(t, proxy.LocalServicePort, 8000)
}

Expand Down
1 change: 1 addition & 0 deletions client/taskenv/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func interpolateConnectSidecarService(taskEnv *TaskEnv, sidecar *structs.ConsulS
for i := 0; i < len(sidecar.Proxy.Upstreams); i++ {
sidecar.Proxy.Upstreams[i].Datacenter = taskEnv.ReplaceEnv(sidecar.Proxy.Upstreams[i].Datacenter)
sidecar.Proxy.Upstreams[i].DestinationName = taskEnv.ReplaceEnv(sidecar.Proxy.Upstreams[i].DestinationName)
sidecar.Proxy.Upstreams[i].LocalBindAddress = taskEnv.ReplaceEnv(sidecar.Proxy.Upstreams[i].LocalBindAddress)
}
sidecar.Proxy.Config = interpolateMapStringInterface(taskEnv, sidecar.Proxy.Config)
}
Expand Down
85 changes: 44 additions & 41 deletions client/taskenv/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,41 +164,42 @@ func TestInterpolate_interpolateConnect(t *testing.T) {
t.Parallel()

e := map[string]string{
"tag1": "_tag1",
"port1": "12345",
"address1": "1.2.3.4",
"destination1": "_dest1",
"datacenter1": "_datacenter1",
"path1": "_path1",
"protocol1": "_protocol1",
"port2": "_port2",
"config1": "_config1",
"driver1": "_driver1",
"user1": "_user1",
"config2": "_config2",
"env1": "_env1",
"env2": "_env2",
"mode1": "_mode1",
"device1": "_device1",
"cidr1": "10.0.0.0/64",
"ip1": "1.1.1.1",
"server1": "10.0.0.1",
"search1": "10.0.0.2",
"option1": "10.0.0.3",
"port3": "_port3",
"network1": "_network1",
"port4": "_port4",
"network2": "_network2",
"resource1": "_resource1",
"meta1": "_meta1",
"meta2": "_meta2",
"signal1": "_signal1",
"bind1": "_bind1",
"address2": "10.0.0.4",
"config3": "_config3",
"protocol2": "_protocol2",
"service1": "_service1",
"host1": "_host1",
"tag1": "_tag1",
"port1": "12345",
"address1": "1.2.3.4",
"destination1": "_dest1",
"datacenter1": "_datacenter1",
"localbindaddress1": "127.0.0.2",
"path1": "_path1",
"protocol1": "_protocol1",
"port2": "_port2",
"config1": "_config1",
"driver1": "_driver1",
"user1": "_user1",
"config2": "_config2",
"env1": "_env1",
"env2": "_env2",
"mode1": "_mode1",
"device1": "_device1",
"cidr1": "10.0.0.0/64",
"ip1": "1.1.1.1",
"server1": "10.0.0.1",
"search1": "10.0.0.2",
"option1": "10.0.0.3",
"port3": "_port3",
"network1": "_network1",
"port4": "_port4",
"network2": "_network2",
"resource1": "_resource1",
"meta1": "_meta1",
"meta2": "_meta2",
"signal1": "_signal1",
"bind1": "_bind1",
"address2": "10.0.0.4",
"config3": "_config3",
"protocol2": "_protocol2",
"service1": "_service1",
"host1": "_host1",
}
env := NewTaskEnv(e, e, nil, nil, "", "")

Expand All @@ -211,9 +212,10 @@ func TestInterpolate_interpolateConnect(t *testing.T) {
LocalServiceAddress: "${address1}",
LocalServicePort: 10000,
Upstreams: []structs.ConsulUpstream{{
DestinationName: "${destination1}",
Datacenter: "${datacenter1}",
LocalBindPort: 10001,
DestinationName: "${destination1}",
Datacenter: "${datacenter1}",
LocalBindPort: 10001,
LocalBindAddress: "${localbindaddress1}",
}},
Expose: &structs.ConsulExposeConfig{
Paths: []structs.ConsulExposePath{{
Expand Down Expand Up @@ -318,9 +320,10 @@ func TestInterpolate_interpolateConnect(t *testing.T) {
LocalServiceAddress: "1.2.3.4",
LocalServicePort: 10000,
Upstreams: []structs.ConsulUpstream{{
DestinationName: "_dest1",
Datacenter: "_datacenter1",
LocalBindPort: 10001,
DestinationName: "_dest1",
Datacenter: "_datacenter1",
LocalBindPort: 10001,
LocalBindAddress: "127.0.0.2",
}},
Expose: &structs.ConsulExposeConfig{
Paths: []structs.ConsulExposePath{{
Expand Down
7 changes: 4 additions & 3 deletions command/agent/consul/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,10 @@ func connectUpstreams(in []structs.ConsulUpstream) []api.Upstream {
upstreams := make([]api.Upstream, len(in))
for i, upstream := range in {
upstreams[i] = api.Upstream{
DestinationName: upstream.DestinationName,
LocalBindPort: upstream.LocalBindPort,
Datacenter: upstream.Datacenter,
DestinationName: upstream.DestinationName,
LocalBindPort: upstream.LocalBindPort,
Datacenter: upstream.Datacenter,
LocalBindAddress: upstream.LocalBindAddress,
}
}
return upstreams
Expand Down
14 changes: 8 additions & 6 deletions command/agent/consul/connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,17 +315,19 @@ func TestConnect_connectUpstreams(t *testing.T) {
DestinationName: "foo",
LocalBindPort: 8000,
}, {
DestinationName: "bar",
LocalBindPort: 9000,
Datacenter: "dc2",
DestinationName: "bar",
LocalBindPort: 9000,
Datacenter: "dc2",
LocalBindAddress: "127.0.0.2",
}},
connectUpstreams([]structs.ConsulUpstream{{
DestinationName: "foo",
LocalBindPort: 8000,
}, {
DestinationName: "bar",
LocalBindPort: 9000,
Datacenter: "dc2",
DestinationName: "bar",
LocalBindPort: 9000,
Datacenter: "dc2",
LocalBindAddress: "127.0.0.2",
}}),
)
})
Expand Down
7 changes: 4 additions & 3 deletions command/agent/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -1513,9 +1513,10 @@ func apiUpstreamsToStructs(in []*api.ConsulUpstream) []structs.ConsulUpstream {
upstreams := make([]structs.ConsulUpstream, len(in))
for i, upstream := range in {
upstreams[i] = structs.ConsulUpstream{
DestinationName: upstream.DestinationName,
LocalBindPort: upstream.LocalBindPort,
Datacenter: upstream.Datacenter,
DestinationName: upstream.DestinationName,
LocalBindPort: upstream.LocalBindPort,
Datacenter: upstream.Datacenter,
LocalBindAddress: upstream.LocalBindAddress,
}
}
return upstreams
Expand Down
14 changes: 8 additions & 6 deletions command/agent/job_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3009,13 +3009,15 @@ func TestConversion_apiUpstreamsToStructs(t *testing.T) {
require.Nil(t, apiUpstreamsToStructs(nil))
require.Nil(t, apiUpstreamsToStructs(make([]*api.ConsulUpstream, 0)))
require.Equal(t, []structs.ConsulUpstream{{
DestinationName: "upstream",
LocalBindPort: 8000,
Datacenter: "dc2",
DestinationName: "upstream",
LocalBindPort: 8000,
Datacenter: "dc2",
LocalBindAddress: "127.0.0.2",
}}, apiUpstreamsToStructs([]*api.ConsulUpstream{{
DestinationName: "upstream",
LocalBindPort: 8000,
Datacenter: "dc2",
DestinationName: "upstream",
LocalBindPort: 8000,
Datacenter: "dc2",
LocalBindAddress: "127.0.0.2",
}}))
}

Expand Down
13 changes: 10 additions & 3 deletions nomad/structs/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2695,9 +2695,10 @@ func TestTaskGroupDiff(t *testing.T) {
LocalServicePort: 8080,
Upstreams: []ConsulUpstream{
{
DestinationName: "foo",
LocalBindPort: 8000,
Datacenter: "dc2",
DestinationName: "foo",
LocalBindPort: 8000,
Datacenter: "dc2",
LocalBindAddress: "127.0.0.2",
},
},
Config: map[string]interface{}{
Expand Down Expand Up @@ -2986,6 +2987,12 @@ func TestTaskGroupDiff(t *testing.T) {
Old: "",
New: "foo",
},
{
Type: DiffTypeAdded,
Name: "LocalBindAddress",
Old: "",
New: "127.0.0.2",
},
{
Type: DiffTypeAdded,
Name: "LocalBindPort",
Expand Down
12 changes: 9 additions & 3 deletions nomad/structs/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ func hashConnect(h hash.Hash, connect *ConsulConnect) {
hashString(h, upstream.DestinationName)
hashString(h, strconv.Itoa(upstream.LocalBindPort))
hashStringIfNonEmpty(h, upstream.Datacenter)
hashStringIfNonEmpty(h, upstream.LocalBindAddress)
}
}
}
Expand Down Expand Up @@ -1198,6 +1199,10 @@ type ConsulUpstream struct {

// Datacenter is the datacenter in which to issue the discovery query to.
Datacenter string

// LocalBindAddress is the address the proxy will receive connections for the
// upstream on.
LocalBindAddress string
}

func upstreamsEquals(a, b []ConsulUpstream) bool {
Expand All @@ -1224,9 +1229,10 @@ func (u *ConsulUpstream) Copy() *ConsulUpstream {
}

return &ConsulUpstream{
DestinationName: u.DestinationName,
LocalBindPort: u.LocalBindPort,
Datacenter: u.Datacenter,
DestinationName: u.DestinationName,
LocalBindPort: u.LocalBindPort,
Datacenter: u.Datacenter,
LocalBindAddress: u.LocalBindAddress,
}
}

Expand Down
7 changes: 4 additions & 3 deletions vendor/github.com/hashicorp/nomad/api/services.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions website/content/docs/job-specification/upstreams.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ job "countdash" {
destination_name = "count-api"
local_bind_port = 8080
datacenter = "dc1"
local_bind_address = "127.0.0.1"
}
}
}
Expand Down Expand Up @@ -84,6 +85,8 @@ job "countdash" {
- `datacenter` `(string: "")` - The Consul datacenter in which to issue the
discovery query. Defaults to the empty string, which Consul interprets as the
local Consul datacenter.
- `local_bind_address` - `(string: "")` - The address the proxy will receive
connections for the upstream on.

The `NOMAD_UPSTREAM_ADDR_<destination_name>` environment variables may be used
to interpolate the upstream's `host:port` address.
Expand Down