Skip to content

Commit

Permalink
consul/connect: enable setting datacenter in connect upstream
Browse files Browse the repository at this point in the history
Before, upstreams could only be defined using the default datacenter.
Now, the `datacenter` field can be set in a connect upstream definition,
informing consul of the desire for an instance of the upstream service
in the specified datacenter. The field is optional and continues to
default to the local datacenter.

Closes #8964
  • Loading branch information
shoenig committed Nov 30, 2020
1 parent cfb9c8c commit ce32e31
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ IMPROVEMENTS:
* consul: Support advertising CNI and multi-host network addresses to consul [[GH-8801](https://github.com/hashicorp/nomad/issues/8801)]
* consul: Support Consul namespace (Consul Enterprise) in client configuration. [[GH-8849](https://github.com/hashicorp/nomad/pull/8849)]
* consul/connect: Dynamically select envoy sidecar at runtime [[GH-8945](https://github.com/hashicorp/nomad/pull/8945)]
* consul/connect: Enable setting `datacenter` field on connect upstreams [[GH-8964](https://github.com/hashicorp/nomad/issues/8964)]
* csi: Support `nomad volume detach` with previously garbage-collected nodes. [[GH-9057](https://github.com/hashicorp/nomad/issues/9057)]
* csi: Relaxed validation requirements when checking volume capabilities with controller plugins, to accommodate existing plugin behaviors. [[GH-9049](https://github.com/hashicorp/nomad/issues/9049)]
* driver/docker: Upgrade pause container and detect architecture [[GH-8957](https://github.com/hashicorp/nomad/pull/8957)]
Expand Down
1 change: 1 addition & 0 deletions api/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ func (cp *ConsulProxy) Canonicalize() {
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"`
}

type ConsulExposeConfig struct {
Expand Down
4 changes: 3 additions & 1 deletion api/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ func TestService_Connect_proxy_settings(t *testing.T) {
{
DestinationName: "upstream",
LocalBindPort: 80,
Datacenter: "dc2",
},
},
LocalServicePort: 8000,
Expand All @@ -205,8 +206,9 @@ func TestService_Connect_proxy_settings(t *testing.T) {

service.Canonicalize(task, tg, job)
proxy := service.Connect.SidecarService.Proxy
require.Equal(t, proxy.Upstreams[0].LocalBindPort, 80)
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.LocalServicePort, 8000)
}

Expand Down
1 change: 1 addition & 0 deletions command/agent/consul/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func connectUpstreams(in []structs.ConsulUpstream) []api.Upstream {
upstreams[i] = api.Upstream{
DestinationName: upstream.DestinationName,
LocalBindPort: upstream.LocalBindPort,
Datacenter: upstream.Datacenter,
}
}
return upstreams
Expand Down
2 changes: 2 additions & 0 deletions command/agent/consul/connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,15 @@ func TestConnect_connectUpstreams(t *testing.T) {
}, {
DestinationName: "bar",
LocalBindPort: 9000,
Datacenter: "dc2",
}},
connectUpstreams([]structs.ConsulUpstream{{
DestinationName: "foo",
LocalBindPort: 8000,
}, {
DestinationName: "bar",
LocalBindPort: 9000,
Datacenter: "dc2",
}}),
)
})
Expand Down
1 change: 1 addition & 0 deletions command/agent/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -1460,6 +1460,7 @@ func apiUpstreamsToStructs(in []*api.ConsulUpstream) []structs.ConsulUpstream {
upstreams[i] = structs.ConsulUpstream{
DestinationName: upstream.DestinationName,
LocalBindPort: upstream.LocalBindPort,
Datacenter: upstream.Datacenter,
}
}
return upstreams
Expand Down
2 changes: 2 additions & 0 deletions command/agent/job_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3006,9 +3006,11 @@ func TestConversion_apiUpstreamsToStructs(t *testing.T) {
require.Equal(t, []structs.ConsulUpstream{{
DestinationName: "upstream",
LocalBindPort: 8000,
Datacenter: "dc2",
}}, apiUpstreamsToStructs([]*api.ConsulUpstream{{
DestinationName: "upstream",
LocalBindPort: 8000,
Datacenter: "dc2",
}}))
}

Expand Down
7 changes: 7 additions & 0 deletions nomad/structs/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2687,6 +2687,7 @@ func TestTaskGroupDiff(t *testing.T) {
{
DestinationName: "foo",
LocalBindPort: 8000,
Datacenter: "dc2",
},
},
Config: map[string]interface{}{
Expand Down Expand Up @@ -2941,6 +2942,12 @@ func TestTaskGroupDiff(t *testing.T) {
Type: DiffTypeAdded,
Name: "ConsulUpstreams",
Fields: []*FieldDiff{
{
Type: DiffTypeAdded,
Name: "Datacenter",
Old: "",
New: "dc2",
},
{
Type: DiffTypeAdded,
Name: "DestinationName",
Expand Down
5 changes: 5 additions & 0 deletions nomad/structs/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@ func hashConnect(h hash.Hash, connect *ConsulConnect) {
for _, upstream := range p.Upstreams {
hashString(h, upstream.DestinationName)
hashString(h, strconv.Itoa(upstream.LocalBindPort))
hashStringIfNonEmpty(h, upstream.Datacenter)
}
}
}
Expand Down Expand Up @@ -1125,6 +1126,9 @@ type ConsulUpstream struct {
// LocalBindPort is the port the proxy will receive connections for the
// upstream on.
LocalBindPort int

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

func upstreamsEquals(a, b []ConsulUpstream) bool {
Expand Down Expand Up @@ -1153,6 +1157,7 @@ func (u *ConsulUpstream) Copy() *ConsulUpstream {
return &ConsulUpstream{
DestinationName: u.DestinationName,
LocalBindPort: u.LocalBindPort,
Datacenter: u.Datacenter,
}
}

Expand Down
1 change: 1 addition & 0 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/pages/docs/job-specification/upstreams.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ job "countdash" {
upstreams {
destination_name = "count-api"
local_bind_port = 8080
datacenter = "dc1"
}
}
}
Expand Down Expand Up @@ -80,6 +81,8 @@ job "countdash" {
- `destination_name` `(string: <required>)` - Name of the upstream service.
- `local_bind_port` - `(int: <required>)` - The port the proxy will receive
connections for the upstream on.
- `datacenter` `(string: <optional>)` - The datacenter in which to issue the
discovery query. Defaults to the local datacenter.

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

0 comments on commit ce32e31

Please sign in to comment.