Skip to content

Commit

Permalink
Merge pull request #9639 from hashicorp/f-gateway-task
Browse files Browse the repository at this point in the history
consul/connect: enable configuring custom gateway task
  • Loading branch information
shoenig committed Dec 17, 2020
2 parents cee6786 + ac24722 commit dde3780
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 67 deletions.
7 changes: 6 additions & 1 deletion nomad/job_endpoint_hook_connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,16 @@ func groupConnectHook(job *structs.Job, g *structs.TaskGroup) error {

// inject the gateway task only if it does not yet already exist
if !hasGatewayTaskForService(g, service.Name) {
// use the default envoy image, for now there is no support for a custom task
task := newConnectGatewayTask(service.Name, netHost)

g.Tasks = append(g.Tasks, task)

// the connect.sidecar_task stanza can also be used to configure
// a custom task to use as a gateway proxy
if service.Connect.SidecarTask != nil {
service.Connect.SidecarTask.MergeIntoTask(task)
}

task.Canonicalize(job, g)
}
}
Expand Down
81 changes: 75 additions & 6 deletions nomad/job_endpoint_hook_connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestJobEndpointConnect_groupConnectHook(t *testing.T) {
tgExp.Services[0].Name = "backend"
tgExp.Services[1].Name = "admin"

// Expect sidecar tasks to be properly canonicalized
// Expect sidecar tasks to be in canonical form.
tgExp.Tasks[0].Canonicalize(job, tgExp)
tgExp.Tasks[1].Canonicalize(job, tgExp)
tgExp.Networks[0].DynamicPorts = []structs.Port{{
Expand Down Expand Up @@ -146,6 +146,75 @@ func TestJobEndpointConnect_groupConnectHook_IngressGateway(t *testing.T) {
require.Exactly(t, expTG, job.TaskGroups[0])
}

func TestJobEndpointConnect_groupConnectHook_IngressGateway_CustomTask(t *testing.T) {
t.Parallel()

// Test that the connect gateway task is inserted if a gateway service exists
// and since this is a bridge network, will rewrite the default gateway proxy
// block with correct configuration.
job := mock.ConnectIngressGatewayJob("bridge", false)

job.Meta = map[string]string{
"gateway_name": "my-gateway",
}

job.TaskGroups[0].Services[0].Name = "${NOMAD_META_gateway_name}"
job.TaskGroups[0].Services[0].Connect.SidecarTask = &structs.SidecarTask{
Driver: "raw_exec",
User: "sidecars",
Config: map[string]interface{}{
"command": "/bin/sidecar",
"args": []string{"a", "b"},
},
Resources: &structs.Resources{
CPU: 400,
// Memory: inherit 128
},
KillSignal: "SIGHUP",
}

expTG := job.TaskGroups[0].Copy()
expTG.Tasks = []*structs.Task{
// inject merged gateway task
{
Name: "connect-ingress-my-gateway",
Kind: structs.NewTaskKind(structs.ConnectIngressPrefix, "my-gateway"),
Driver: "raw_exec",
User: "sidecars",
Config: map[string]interface{}{
"command": "/bin/sidecar",
"args": []string{"a", "b"},
},
Resources: &structs.Resources{
CPU: 400,
MemoryMB: 128,
},
LogConfig: &structs.LogConfig{
MaxFiles: 2,
MaxFileSizeMB: 2,
},
ShutdownDelay: 5 * time.Second,
KillSignal: "SIGHUP",
Constraints: structs.Constraints{
connectGatewayVersionConstraint(),
},
},
}
expTG.Services[0].Name = "my-gateway"
expTG.Tasks[0].Canonicalize(job, expTG)
expTG.Networks[0].Canonicalize()

// rewrite the service gateway proxy configuration
expTG.Services[0].Connect.Gateway.Proxy = gatewayProxyForBridge(expTG.Services[0].Connect.Gateway)

require.NoError(t, groupConnectHook(job, job.TaskGroups[0]))
require.Exactly(t, expTG, job.TaskGroups[0])

// Test that the hook is idempotent
require.NoError(t, groupConnectHook(job, job.TaskGroups[0]))
require.Exactly(t, expTG, job.TaskGroups[0])
}

// TestJobEndpoint_ConnectInterpolation asserts that when a Connect sidecar
// proxy task is being created for a group service with an interpolated name,
// the service name is interpolated *before the task is created.
Expand Down Expand Up @@ -330,7 +399,7 @@ func TestJobEndpointConnect_gatewayProxyIsDefault(t *testing.T) {
t.Run("bind-addresses set", func(t *testing.T) {
result := gatewayProxyIsDefault(&structs.ConsulGatewayProxy{
EnvoyGatewayBindAddresses: map[string]*structs.ConsulGatewayBindAddress{
"listener1": &structs.ConsulGatewayBindAddress{
"listener1": {
Address: "1.1.1.1",
Port: 9000,
},
Expand Down Expand Up @@ -362,7 +431,7 @@ func TestJobEndpointConnect_gatewayBindAddresses(t *testing.T) {
}},
})
require.Equal(t, map[string]*structs.ConsulGatewayBindAddress{
"service1": &structs.ConsulGatewayBindAddress{
"service1": {
Address: "0.0.0.0",
Port: 3000,
},
Expand All @@ -388,15 +457,15 @@ func TestJobEndpointConnect_gatewayBindAddresses(t *testing.T) {
}},
})
require.Equal(t, map[string]*structs.ConsulGatewayBindAddress{
"service1": &structs.ConsulGatewayBindAddress{
"service1": {
Address: "0.0.0.0",
Port: 3000,
},
"service2": &structs.ConsulGatewayBindAddress{
"service2": {
Address: "0.0.0.0",
Port: 3000,
},
"service3": &structs.ConsulGatewayBindAddress{
"service3": {
Address: "0.0.0.0",
Port: 3001,
},
Expand Down
98 changes: 74 additions & 24 deletions website/pages/docs/job-specification/connect.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,41 @@ job "countdash" {

## `connect` Parameters

Used to configure a connect service. Only one of `native`, `sidecar_service`,
or `gateway` may be realized per `connect` block.

- `native` - `(bool: false)` - This is used to configure the service as supporting
[Connect Native](https://www.consul.io/docs/connect/native) applications. If set,
the service definition must provide the name of the implementing task in the
[task][service_task] field.
Incompatible with `sidecar_service` and `sidecar_task`.
[Connect Native](https://www.consul.io/docs/connect/native) applications.

- `sidecar_service` - <code>([sidecar_service][]: nil)</code> - This is used to
configure the sidecar service created by Nomad for Consul Connect.

- `sidecar_service` - <code>([sidecar_service][]: nil)</code> - This is used to configure the sidecar
service injected by Nomad for Consul Connect. Incompatible with `native`.
- `sidecar_task` - <code>([sidecar_task][]:nil)</code> - This modifies the
task configuration of the Envoy proxy created as a sidecar or gateway.

- `sidecar_task` - <code>([sidecar_task][]:nil)</code> - This modifies the configuration of the Envoy
proxy task. Incompatible with `native`.
- `gateway` - <code>([gateway][]:nil)</code> - This is used to configure the
gateway service created by Nomad for Consul Connect.

## `connect` Examples

### Using Connect Native

The following example is a minimal service stanza for a
[Consul Connect Native](https://www.consul.io/docs/connect/native)
application implemented by a task named `generate`.

```hcl
service {
name = "uuid-api"
port = "${NOMAD_PORT_api}"
task = "generate"
connect {
native = true
}
}
```

### Using Sidecar Service

The following example is a minimal connect stanza with defaults and is
Expand Down Expand Up @@ -169,35 +190,64 @@ job "countdash" {
}
```

### Using Connect Native
### Using a Gateway

The following example is a minimal service stanza for a
[Consul Connect Native](https://www.consul.io/docs/connect/native)
application implemented by a task named `generate`.
The following is an example service stanza for creating and using a connect ingress
gateway. It includes a gateway service definition and an api service fronted by
the gateway. Once running, the gateway can be used to reach the api service by first
looking up the gateway Consul DNS address, e.g.

```
curl $(dig +short @127.0.0.1 -p 8600 uuid-api.ingress.dc1.consul. ANY):8080
```

```hcl
service {
name = "uuid-api"
port = "${NOMAD_PORT_api}"
task = "generate"
job "ingress-demo" {
connect {
native = true
datacenters = ["dc1"]
group "ingress-group" {
network {
mode = "bridge"
port "inbound" {
static = 8080
to = 8080
}
}
service {
name = "my-ingress-service"
port = "8080"
connect {
gateway {
ingress {
listener {
port = 8080
protocol = "tcp"
service {
name = "uuid-api"
}
}
}
}
}
}
}
}
```

### Limitations

[Nomad variable interpolation][interpolation] is _not_ yet supported ([gh-7221]).

[gateway]: /docs/job-specification/gateway
[gh-7221]: https://github.com/hashicorp/nomad/issues/7221
[job]: /docs/job-specification/job 'Nomad job Job Specification'
[group]: /docs/job-specification/group 'Nomad group Job Specification'
[task]: /docs/job-specification/task 'Nomad task Job Specification'
[interpolation]: /docs/runtime/interpolation 'Nomad interpolation'
[job]: /docs/job-specification/job 'Nomad job Job Specification'
[native]: https://www.consul.io/docs/connect/native
[service_task]: /docs/job-specification/service#task-1 'Nomad service task'
[sidecar_service]: /docs/job-specification/sidecar_service 'Nomad sidecar service Specification'
[sidecar_task]: /docs/job-specification/sidecar_task 'Nomad sidecar task config Specification'
[task]: /docs/job-specification/task 'Nomad task Job Specification'
[upstreams]: /docs/job-specification/upstreams 'Nomad sidecar service upstreams Specification'
[native]: https://www.consul.io/docs/connect/native
[service_task]: /docs/job-specification/service#task-1 'Nomad service task'
Loading

0 comments on commit dde3780

Please sign in to comment.