2019-08-15 15:22:37 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2022-04-02 00:24:02 +00:00
|
|
|
"errors"
|
2019-08-15 15:22:37 +00:00
|
|
|
"fmt"
|
2021-04-27 16:23:57 +00:00
|
|
|
"strings"
|
2019-08-15 15:22:37 +00:00
|
|
|
"time"
|
|
|
|
|
2020-11-16 16:41:19 +00:00
|
|
|
"github.com/hashicorp/nomad/client/taskenv"
|
2020-07-28 20:12:08 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2020-12-11 15:57:13 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/envoy"
|
2019-08-15 15:22:37 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2021-01-19 16:11:02 +00:00
|
|
|
const (
|
|
|
|
// defaultConnectTimeout is the default amount of time a connect gateway will
|
|
|
|
// wait for a response from an upstream service (same as consul)
|
|
|
|
defaultConnectTimeout = 5 * time.Second
|
|
|
|
)
|
|
|
|
|
2020-12-15 20:38:33 +00:00
|
|
|
// connectSidecarResources returns the set of resources used by default for
|
|
|
|
// the Consul Connect sidecar task
|
|
|
|
func connectSidecarResources() *structs.Resources {
|
|
|
|
return &structs.Resources{
|
|
|
|
CPU: 250,
|
|
|
|
MemoryMB: 128,
|
2019-08-20 05:22:46 +00:00
|
|
|
}
|
2020-12-15 20:38:33 +00:00
|
|
|
}
|
2019-08-20 05:22:46 +00:00
|
|
|
|
2020-12-15 20:38:33 +00:00
|
|
|
// connectSidecarDriverConfig is the driver configuration used by the injected
|
|
|
|
// connect proxy sidecar task.
|
|
|
|
func connectSidecarDriverConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"image": envoy.SidecarConfigVar,
|
|
|
|
"args": []interface{}{
|
|
|
|
"-c", structs.EnvoyBootstrapPath,
|
|
|
|
"-l", "${meta.connect.log_level}",
|
|
|
|
"--concurrency", "${meta.connect.proxy_concurrency}",
|
|
|
|
"--disable-hot-restart",
|
|
|
|
},
|
2019-08-15 15:22:37 +00:00
|
|
|
}
|
2020-12-15 20:38:33 +00:00
|
|
|
}
|
2019-08-15 15:22:37 +00:00
|
|
|
|
2020-12-15 20:38:33 +00:00
|
|
|
// connectGatewayDriverConfig is the Docker driver configuration used by the
|
|
|
|
// injected connect proxy sidecar task.
|
|
|
|
//
|
|
|
|
// A gateway may run in a group with bridge or host networking, and if host
|
|
|
|
// networking is being used the network_mode driver configuration is set here.
|
|
|
|
func connectGatewayDriverConfig(hostNetwork bool) map[string]interface{} {
|
|
|
|
m := map[string]interface{}{
|
|
|
|
"image": envoy.GatewayConfigVar,
|
|
|
|
"args": []interface{}{
|
|
|
|
"-c", structs.EnvoyBootstrapPath,
|
|
|
|
"-l", "${meta.connect.log_level}",
|
|
|
|
"--concurrency", "${meta.connect.proxy_concurrency}",
|
|
|
|
"--disable-hot-restart",
|
|
|
|
},
|
|
|
|
}
|
2020-07-28 20:12:08 +00:00
|
|
|
|
2020-12-15 20:38:33 +00:00
|
|
|
if hostNetwork {
|
|
|
|
m["network_mode"] = "host"
|
2020-07-28 20:12:08 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 20:38:33 +00:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// connectSidecarVersionConstraint is used when building the sidecar task to ensure
|
|
|
|
// the proper Consul version is used that supports the necessary Connect
|
|
|
|
// features. This includes bootstrapping envoy with a unix socket for Consul's
|
|
|
|
// gRPC xDS API.
|
|
|
|
func connectSidecarVersionConstraint() *structs.Constraint {
|
|
|
|
return &structs.Constraint{
|
|
|
|
LTarget: "${attr.consul.version}",
|
|
|
|
RTarget: ">= 1.6.0-beta1",
|
|
|
|
Operand: structs.ConstraintSemver,
|
2019-08-15 15:22:37 +00:00
|
|
|
}
|
2020-12-15 20:38:33 +00:00
|
|
|
}
|
2020-07-28 20:12:08 +00:00
|
|
|
|
2020-12-15 20:38:33 +00:00
|
|
|
// connectGatewayVersionConstraint is used when building a connect gateway
|
|
|
|
// task to ensure proper Consul version is used that supports Connect Gateway
|
|
|
|
// features. This includes making use of Consul Configuration Entries of type
|
|
|
|
// {ingress,terminating,mesh}-gateway.
|
|
|
|
func connectGatewayVersionConstraint() *structs.Constraint {
|
|
|
|
return &structs.Constraint{
|
|
|
|
LTarget: "${attr.consul.version}",
|
|
|
|
RTarget: ">= 1.8.0",
|
|
|
|
Operand: structs.ConstraintSemver,
|
2020-07-28 20:12:08 +00:00
|
|
|
}
|
2020-12-15 20:38:33 +00:00
|
|
|
}
|
2019-08-15 15:22:37 +00:00
|
|
|
|
2021-06-03 18:47:39 +00:00
|
|
|
func connectListenerConstraint() *structs.Constraint {
|
|
|
|
return &structs.Constraint{
|
|
|
|
LTarget: "${attr.consul.grpc}",
|
|
|
|
RTarget: "0",
|
|
|
|
Operand: ">",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 20:46:46 +00:00
|
|
|
// jobConnectHook implements a job Mutating and Validating admission controller
|
2019-08-15 15:22:37 +00:00
|
|
|
type jobConnectHook struct{}
|
|
|
|
|
|
|
|
func (jobConnectHook) Name() string {
|
|
|
|
return "connect"
|
|
|
|
}
|
|
|
|
|
2020-12-15 20:38:33 +00:00
|
|
|
func (jobConnectHook) Mutate(job *structs.Job) (*structs.Job, []error, error) {
|
2019-08-15 15:22:37 +00:00
|
|
|
for _, g := range job.TaskGroups {
|
2019-10-28 14:46:51 +00:00
|
|
|
// TG isn't validated yet, but validation
|
|
|
|
// may depend on mutation results.
|
|
|
|
// Do basic validation here and skip mutation,
|
|
|
|
// so Validate can return a meaningful error
|
|
|
|
// messages
|
|
|
|
if len(g.Networks) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-12 23:46:14 +00:00
|
|
|
if err := groupConnectHook(job, g); err != nil {
|
2019-08-15 15:22:37 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return job, nil, nil
|
|
|
|
}
|
|
|
|
|
2020-12-15 20:38:33 +00:00
|
|
|
func (jobConnectHook) Validate(job *structs.Job) ([]error, error) {
|
|
|
|
var warnings []error
|
|
|
|
|
2019-08-15 15:22:37 +00:00
|
|
|
for _, g := range job.TaskGroups {
|
2021-06-18 21:50:53 +00:00
|
|
|
if err := groupConnectValidate(g); err != nil {
|
2019-08-15 15:22:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return warnings, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getSidecarTaskForService looks for the sidecar task for a given service within
|
|
|
|
// a task group. If no sidecar task is found nil is returned
|
|
|
|
func getSidecarTaskForService(tg *structs.TaskGroup, svc string) *structs.Task {
|
|
|
|
for _, t := range tg.Tasks {
|
|
|
|
if isSidecarForService(t, svc) {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-12 19:10:10 +00:00
|
|
|
func isSidecarForService(t *structs.Task, service string) bool {
|
|
|
|
return t.Kind == structs.NewTaskKind(structs.ConnectProxyPrefix, service)
|
2020-07-28 20:12:08 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 19:10:10 +00:00
|
|
|
func hasGatewayTaskForService(tg *structs.TaskGroup, service string) bool {
|
2020-07-28 20:12:08 +00:00
|
|
|
for _, t := range tg.Tasks {
|
|
|
|
switch {
|
2021-04-12 19:10:10 +00:00
|
|
|
case isIngressGatewayForService(t, service):
|
2020-12-15 20:38:33 +00:00
|
|
|
return true
|
2021-04-12 19:10:10 +00:00
|
|
|
case isTerminatingGatewayForService(t, service):
|
|
|
|
return true
|
|
|
|
case isMeshGatewayForService(t, service):
|
2020-07-28 20:12:08 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func isIngressGatewayForService(t *structs.Task, svc string) bool {
|
|
|
|
return t.Kind == structs.NewTaskKind(structs.ConnectIngressPrefix, svc)
|
2019-08-15 15:22:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 20:38:33 +00:00
|
|
|
func isTerminatingGatewayForService(t *structs.Task, svc string) bool {
|
|
|
|
return t.Kind == structs.NewTaskKind(structs.ConnectTerminatingPrefix, svc)
|
|
|
|
}
|
|
|
|
|
2021-04-12 19:10:10 +00:00
|
|
|
func isMeshGatewayForService(t *structs.Task, svc string) bool {
|
|
|
|
return t.Kind == structs.NewTaskKind(structs.ConnectMeshPrefix, svc)
|
|
|
|
}
|
|
|
|
|
2020-07-08 15:19:36 +00:00
|
|
|
// getNamedTaskForNativeService retrieves the Task with the name specified in the
|
|
|
|
// group service definition. If the task name is empty and there is only one task
|
|
|
|
// in the group, infer the name from the only option.
|
|
|
|
func getNamedTaskForNativeService(tg *structs.TaskGroup, serviceName, taskName string) (*structs.Task, error) {
|
|
|
|
if taskName == "" {
|
|
|
|
if len(tg.Tasks) == 1 {
|
|
|
|
return tg.Tasks[0], nil
|
|
|
|
}
|
2022-04-02 00:24:02 +00:00
|
|
|
return nil, fmt.Errorf("task for Consul Connect Native service %s->%s is ambiguous and must be set", tg.Name, serviceName)
|
2020-07-08 15:19:36 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 20:15:55 +00:00
|
|
|
for _, t := range tg.Tasks {
|
|
|
|
if t.Name == taskName {
|
2020-07-08 15:19:36 +00:00
|
|
|
return t, nil
|
2020-05-13 20:15:55 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-02 00:24:02 +00:00
|
|
|
return nil, fmt.Errorf("task %s named by Consul Connect Native service %s->%s does not exist", taskName, tg.Name, serviceName)
|
2020-05-13 20:15:55 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 20:38:33 +00:00
|
|
|
func injectPort(group *structs.TaskGroup, label string) {
|
|
|
|
// check that port hasn't already been defined before adding it to tg
|
|
|
|
for _, p := range group.Networks[0].DynamicPorts {
|
|
|
|
if p.Label == label {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// inject a port of label that maps inside the bridge namespace
|
|
|
|
group.Networks[0].DynamicPorts = append(group.Networks[0].DynamicPorts, structs.Port{
|
|
|
|
Label: label,
|
|
|
|
// -1 is a sentinel value to instruct the
|
|
|
|
// scheduler to map the host's dynamic port to
|
|
|
|
// the same port in the netns.
|
|
|
|
To: -1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-12 23:46:14 +00:00
|
|
|
func groupConnectHook(job *structs.Job, g *structs.TaskGroup) error {
|
2020-11-16 16:41:19 +00:00
|
|
|
// Create an environment interpolator with what we have at submission time.
|
|
|
|
// This should only be used to interpolate connect service names which are
|
|
|
|
// used in sidecar or gateway task names. Note that the service name might
|
|
|
|
// also be interpolated with job specifics during service canonicalization.
|
|
|
|
env := taskenv.NewEmptyBuilder().UpdateTask(&structs.Allocation{
|
|
|
|
Job: job,
|
|
|
|
TaskGroup: g.Name,
|
|
|
|
}, nil).Build()
|
|
|
|
|
2019-08-15 15:22:37 +00:00
|
|
|
for _, service := range g.Services {
|
2020-07-28 20:12:08 +00:00
|
|
|
switch {
|
|
|
|
// mutate depending on what the connect block is being used for
|
|
|
|
|
|
|
|
case service.Connect.HasSidecar():
|
2020-11-16 16:41:19 +00:00
|
|
|
// interpolate the connect service name, which is used to create
|
|
|
|
// a name of an injected sidecar task
|
|
|
|
service.Name = env.ReplaceEnv(service.Name)
|
|
|
|
|
2019-08-15 15:22:37 +00:00
|
|
|
// Check to see if the sidecar task already exists
|
|
|
|
task := getSidecarTaskForService(g, service.Name)
|
|
|
|
|
|
|
|
// If the task doesn't already exist, create a new one and add it to the job
|
|
|
|
if task == nil {
|
2020-12-15 20:38:33 +00:00
|
|
|
task = newConnectSidecarTask(service.Name)
|
2019-08-15 15:22:37 +00:00
|
|
|
|
|
|
|
// If there happens to be a task defined with the same name
|
|
|
|
// append an UUID fragment to the task name
|
|
|
|
for _, t := range g.Tasks {
|
|
|
|
if t.Name == task.Name {
|
|
|
|
task.Name = task.Name + "-" + uuid.Generate()[:6]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g.Tasks = append(g.Tasks, task)
|
|
|
|
}
|
|
|
|
|
2019-08-20 05:22:46 +00:00
|
|
|
if service.Connect.SidecarTask != nil {
|
|
|
|
service.Connect.SidecarTask.MergeIntoTask(task)
|
|
|
|
}
|
2019-08-15 15:22:37 +00:00
|
|
|
|
2019-12-12 23:46:14 +00:00
|
|
|
// Canonicalize task since this mutator runs after job canonicalization
|
|
|
|
task.Canonicalize(job, g)
|
|
|
|
|
2020-03-07 03:15:22 +00:00
|
|
|
// create a port for the sidecar task's proxy port
|
2021-02-09 12:05:28 +00:00
|
|
|
portLabel := service.Connect.SidecarService.Port
|
2021-02-09 13:31:48 +00:00
|
|
|
if portLabel == "" {
|
2021-04-12 19:10:10 +00:00
|
|
|
portLabel = envoy.PortLabel(structs.ConnectProxyPrefix, service.Name, "")
|
2021-02-09 12:05:28 +00:00
|
|
|
}
|
|
|
|
injectPort(g, portLabel)
|
2020-07-28 20:12:08 +00:00
|
|
|
|
|
|
|
case service.Connect.IsNative():
|
2020-07-08 15:19:36 +00:00
|
|
|
// find the task backing this connect native service and set the kind
|
2020-06-22 17:55:59 +00:00
|
|
|
nativeTaskName := service.TaskName
|
2020-07-08 15:19:36 +00:00
|
|
|
if t, err := getNamedTaskForNativeService(g, service.Name, nativeTaskName); err != nil {
|
|
|
|
return err
|
2020-05-13 20:15:55 +00:00
|
|
|
} else {
|
2020-07-08 15:19:36 +00:00
|
|
|
t.Kind = structs.NewTaskKind(structs.ConnectNativePrefix, service.Name)
|
|
|
|
service.TaskName = t.Name // in case the task was inferred
|
2020-05-13 20:15:55 +00:00
|
|
|
}
|
2020-07-28 20:12:08 +00:00
|
|
|
|
|
|
|
case service.Connect.IsGateway():
|
2020-11-16 16:41:19 +00:00
|
|
|
// interpolate the connect service name, which is used to create
|
|
|
|
// a name of an injected gateway task
|
|
|
|
service.Name = env.ReplaceEnv(service.Name)
|
|
|
|
|
2021-10-04 19:52:07 +00:00
|
|
|
// Generate a proxy configuration, if one is not provided, that is
|
|
|
|
// most appropriate for the network mode being used.
|
|
|
|
netMode := g.Networks[0].Mode
|
|
|
|
service.Connect.Gateway.Proxy = gatewayProxy(service.Connect.Gateway, netMode)
|
2020-07-28 20:12:08 +00:00
|
|
|
|
2020-12-15 20:38:33 +00:00
|
|
|
// Inject a port whether bridge or host network (if not already set).
|
|
|
|
// This port is accessed by the magic of Connect plumbing so it seems
|
|
|
|
// reasonable to keep the magic alive here.
|
|
|
|
if service.Connect.IsTerminating() && service.PortLabel == "" {
|
|
|
|
// Inject a dynamic port for the terminating gateway.
|
2021-04-12 19:10:10 +00:00
|
|
|
portLabel := envoy.PortLabel(structs.ConnectTerminatingPrefix, service.Name, "")
|
2020-12-15 20:38:33 +00:00
|
|
|
service.PortLabel = portLabel
|
|
|
|
injectPort(g, portLabel)
|
|
|
|
}
|
|
|
|
|
2021-04-12 19:10:10 +00:00
|
|
|
// A mesh Gateway will need 2 ports (lan and wan).
|
|
|
|
if service.Connect.IsMesh() {
|
|
|
|
|
|
|
|
// service port is used for mesh gateway wan address - it should
|
|
|
|
// come from a configured host_network to make sense
|
|
|
|
if service.PortLabel == "" {
|
|
|
|
return errors.New("service.port must be set for mesh gateway service")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inject a dynamic port for mesh gateway LAN address.
|
|
|
|
lanPortLabel := envoy.PortLabel(structs.ConnectMeshPrefix, service.Name, "lan")
|
|
|
|
injectPort(g, lanPortLabel)
|
|
|
|
}
|
|
|
|
|
2020-07-28 20:12:08 +00:00
|
|
|
// inject the gateway task only if it does not yet already exist
|
|
|
|
if !hasGatewayTaskForService(g, service.Name) {
|
2020-12-15 20:38:33 +00:00
|
|
|
prefix := service.Connect.Gateway.Prefix()
|
2021-10-04 19:52:07 +00:00
|
|
|
|
|
|
|
// detect whether the group is in host networking mode, which will
|
|
|
|
// require tweaking the default gateway task config
|
|
|
|
netHost := netMode == "host"
|
2020-12-15 20:38:33 +00:00
|
|
|
task := newConnectGatewayTask(prefix, service.Name, netHost)
|
2020-07-28 20:12:08 +00:00
|
|
|
g.Tasks = append(g.Tasks, task)
|
2020-12-11 15:57:13 +00:00
|
|
|
|
2020-12-14 22:41:14 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2020-07-28 20:12:08 +00:00
|
|
|
task.Canonicalize(job, g)
|
|
|
|
}
|
2019-08-15 15:22:37 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-10 14:59:51 +00:00
|
|
|
|
|
|
|
// re-canonicalize group network since this hook runs after canonicalizaton
|
|
|
|
g.Networks[0].Canonicalize()
|
2019-08-15 15:22:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-28 20:12:08 +00:00
|
|
|
// gatewayProxyIsDefault returns false if any of these gateway proxy configuration
|
|
|
|
// have been modified from their default values, indicating the operator wants
|
|
|
|
// custom behavior. Otherwise, we assume the operator wants Nomad to do the Right
|
|
|
|
// Thing, setting the configuration automatically.
|
|
|
|
//
|
|
|
|
// - envoy_gateway_no_default_bind
|
|
|
|
// - envoy_gateway_bind_tagged_addresses
|
|
|
|
// - envoy_gateway_bind_addresses
|
|
|
|
func gatewayProxyIsDefault(proxy *structs.ConsulGatewayProxy) bool {
|
|
|
|
if proxy == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if !proxy.EnvoyGatewayNoDefaultBind &&
|
|
|
|
!proxy.EnvoyGatewayBindTaggedAddresses &&
|
|
|
|
len(proxy.EnvoyGatewayBindAddresses) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-10-04 19:52:07 +00:00
|
|
|
// gatewayProxy scans an existing gateway proxy configuration and tweaks it
|
|
|
|
// given an associated configuration entry so that it works as intended with
|
|
|
|
// the network mode specified.
|
|
|
|
func gatewayProxy(gateway *structs.ConsulGateway, mode string) *structs.ConsulGatewayProxy {
|
2020-07-28 20:12:08 +00:00
|
|
|
if gateway == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// operator has supplied custom proxy configuration, just use that without
|
|
|
|
// modification
|
|
|
|
if !gatewayProxyIsDefault(gateway.Proxy) {
|
|
|
|
return gateway.Proxy
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy over unrelated fields if proxy block exists
|
|
|
|
proxy := new(structs.ConsulGatewayProxy)
|
|
|
|
if gateway.Proxy != nil {
|
|
|
|
proxy.ConnectTimeout = gateway.Proxy.ConnectTimeout
|
2020-12-15 20:38:33 +00:00
|
|
|
proxy.EnvoyDNSDiscoveryType = gateway.Proxy.EnvoyDNSDiscoveryType
|
2020-07-28 20:12:08 +00:00
|
|
|
proxy.Config = gateway.Proxy.Config
|
|
|
|
}
|
|
|
|
|
2021-01-19 16:11:02 +00:00
|
|
|
// set default connect timeout if not set
|
|
|
|
if proxy.ConnectTimeout == nil {
|
|
|
|
proxy.ConnectTimeout = helper.TimeToPtr(defaultConnectTimeout)
|
|
|
|
}
|
|
|
|
|
2021-10-04 19:52:07 +00:00
|
|
|
if mode == "bridge" {
|
|
|
|
// magically configure bind address(es) for bridge networking, per gateway type
|
|
|
|
// non-default configuration is gated above
|
|
|
|
switch {
|
|
|
|
case gateway.Ingress != nil:
|
|
|
|
proxy.EnvoyGatewayNoDefaultBind = true
|
|
|
|
proxy.EnvoyGatewayBindTaggedAddresses = false
|
|
|
|
proxy.EnvoyGatewayBindAddresses = gatewayBindAddressesIngressForBridge(gateway.Ingress)
|
|
|
|
case gateway.Terminating != nil:
|
|
|
|
proxy.EnvoyGatewayNoDefaultBind = true
|
|
|
|
proxy.EnvoyGatewayBindTaggedAddresses = false
|
|
|
|
proxy.EnvoyGatewayBindAddresses = map[string]*structs.ConsulGatewayBindAddress{
|
|
|
|
"default": {
|
|
|
|
Address: "0.0.0.0",
|
|
|
|
Port: -1, // filled in later with dynamic port
|
|
|
|
}}
|
|
|
|
case gateway.Mesh != nil:
|
|
|
|
proxy.EnvoyGatewayNoDefaultBind = true
|
|
|
|
proxy.EnvoyGatewayBindTaggedAddresses = false
|
|
|
|
proxy.EnvoyGatewayBindAddresses = map[string]*structs.ConsulGatewayBindAddress{
|
|
|
|
"wan": {
|
|
|
|
Address: "0.0.0.0",
|
|
|
|
Port: -1, // filled in later with configured port
|
|
|
|
},
|
|
|
|
"lan": {
|
|
|
|
Address: "0.0.0.0",
|
|
|
|
Port: -1, // filled in later with generated port
|
|
|
|
},
|
|
|
|
}
|
2021-04-12 19:10:10 +00:00
|
|
|
}
|
2020-12-15 20:38:33 +00:00
|
|
|
}
|
2020-07-28 20:12:08 +00:00
|
|
|
|
|
|
|
return proxy
|
|
|
|
}
|
|
|
|
|
2021-10-04 19:52:07 +00:00
|
|
|
func gatewayBindAddressesIngressForBridge(ingress *structs.ConsulIngressConfigEntry) map[string]*structs.ConsulGatewayBindAddress {
|
2020-07-28 20:12:08 +00:00
|
|
|
if ingress == nil || len(ingress.Listeners) == 0 {
|
2021-01-22 14:18:17 +00:00
|
|
|
return make(map[string]*structs.ConsulGatewayBindAddress)
|
2020-07-28 20:12:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addresses := make(map[string]*structs.ConsulGatewayBindAddress)
|
|
|
|
for _, listener := range ingress.Listeners {
|
|
|
|
port := listener.Port
|
|
|
|
for _, service := range listener.Services {
|
|
|
|
addresses[service.Name] = &structs.ConsulGatewayBindAddress{
|
|
|
|
Address: "0.0.0.0",
|
|
|
|
Port: port,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return addresses
|
|
|
|
}
|
|
|
|
|
2020-12-15 20:38:33 +00:00
|
|
|
func newConnectGatewayTask(prefix, service string, netHost bool) *structs.Task {
|
2020-07-28 20:12:08 +00:00
|
|
|
return &structs.Task{
|
|
|
|
// Name is used in container name so must start with '[A-Za-z0-9]'
|
2020-12-15 20:38:33 +00:00
|
|
|
Name: fmt.Sprintf("%s-%s", prefix, service),
|
|
|
|
Kind: structs.NewTaskKind(prefix, service),
|
2020-07-28 20:12:08 +00:00
|
|
|
Driver: "docker",
|
|
|
|
Config: connectGatewayDriverConfig(netHost),
|
|
|
|
ShutdownDelay: 5 * time.Second,
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 2,
|
|
|
|
MaxFileSizeMB: 2,
|
|
|
|
},
|
|
|
|
Resources: connectSidecarResources(),
|
|
|
|
Constraints: structs.Constraints{
|
|
|
|
connectGatewayVersionConstraint(),
|
2021-06-03 18:47:39 +00:00
|
|
|
connectListenerConstraint(),
|
2020-07-28 20:12:08 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-15 20:38:33 +00:00
|
|
|
func newConnectSidecarTask(service string) *structs.Task {
|
2020-07-28 20:12:08 +00:00
|
|
|
return &structs.Task{
|
2019-08-15 15:22:37 +00:00
|
|
|
// Name is used in container name so must start with '[A-Za-z0-9]'
|
2020-12-15 20:38:33 +00:00
|
|
|
Name: fmt.Sprintf("%s-%s", structs.ConnectProxyPrefix, service),
|
|
|
|
Kind: structs.NewTaskKind(structs.ConnectProxyPrefix, service),
|
2019-08-20 05:22:46 +00:00
|
|
|
Driver: "docker",
|
2020-07-28 20:12:08 +00:00
|
|
|
Config: connectSidecarDriverConfig(),
|
2019-08-15 15:22:37 +00:00
|
|
|
ShutdownDelay: 5 * time.Second,
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 2,
|
|
|
|
MaxFileSizeMB: 2,
|
|
|
|
},
|
2019-08-20 05:22:46 +00:00
|
|
|
Resources: connectSidecarResources(),
|
2020-04-02 01:18:41 +00:00
|
|
|
Lifecycle: &structs.TaskLifecycleConfig{
|
2020-04-02 01:23:47 +00:00
|
|
|
Hook: structs.TaskLifecycleHookPrestart,
|
2020-04-02 01:18:41 +00:00
|
|
|
Sidecar: true,
|
|
|
|
},
|
2019-08-15 15:22:37 +00:00
|
|
|
Constraints: structs.Constraints{
|
2020-12-15 20:38:33 +00:00
|
|
|
connectSidecarVersionConstraint(),
|
2021-06-03 18:47:39 +00:00
|
|
|
connectListenerConstraint(),
|
2019-08-15 15:22:37 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-18 21:50:53 +00:00
|
|
|
func groupConnectValidate(g *structs.TaskGroup) error {
|
2019-08-15 15:22:37 +00:00
|
|
|
for _, s := range g.Services {
|
2020-07-28 20:12:08 +00:00
|
|
|
switch {
|
|
|
|
case s.Connect.HasSidecar():
|
2021-04-27 16:23:57 +00:00
|
|
|
if err := groupConnectSidecarValidate(g, s); err != nil {
|
2021-06-18 21:50:53 +00:00
|
|
|
return err
|
2019-08-15 15:22:37 +00:00
|
|
|
}
|
2020-07-28 20:12:08 +00:00
|
|
|
case s.Connect.IsNative():
|
2020-06-22 17:55:59 +00:00
|
|
|
if err := groupConnectNativeValidate(g, s); err != nil {
|
2021-06-18 21:50:53 +00:00
|
|
|
return err
|
2019-08-15 15:22:37 +00:00
|
|
|
}
|
2020-07-28 20:12:08 +00:00
|
|
|
case s.Connect.IsGateway():
|
|
|
|
if err := groupConnectGatewayValidate(g); err != nil {
|
2021-06-18 21:50:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := groupConnectUpstreamsValidate(g.Name, g.Services); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func groupConnectUpstreamsValidate(group string, services []*structs.Service) error {
|
|
|
|
listeners := make(map[string]string) // address -> service
|
|
|
|
|
|
|
|
for _, service := range services {
|
|
|
|
if service.Connect.HasSidecar() && service.Connect.SidecarService.Proxy != nil {
|
|
|
|
for _, up := range service.Connect.SidecarService.Proxy.Upstreams {
|
|
|
|
listener := fmt.Sprintf("%s:%d", up.LocalBindAddress, up.LocalBindPort)
|
|
|
|
if s, exists := listeners[listener]; exists {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Consul Connect services %q and %q in group %q using same address for upstreams (%s)",
|
|
|
|
service.Name, s, group, listener,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
listeners[listener] = service.Name
|
2020-07-28 20:12:08 +00:00
|
|
|
}
|
2019-08-15 15:22:37 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-18 21:50:53 +00:00
|
|
|
return nil
|
2019-08-15 15:22:37 +00:00
|
|
|
}
|
2020-06-22 17:55:59 +00:00
|
|
|
|
2021-04-27 16:23:57 +00:00
|
|
|
func groupConnectSidecarValidate(g *structs.TaskGroup, s *structs.Service) error {
|
2020-06-22 17:55:59 +00:00
|
|
|
if n := len(g.Networks); n != 1 {
|
|
|
|
return fmt.Errorf("Consul Connect sidecars require exactly 1 network, found %d in group %q", n, g.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if g.Networks[0].Mode != "bridge" {
|
|
|
|
return fmt.Errorf("Consul Connect sidecar requires bridge network, found %q in group %q", g.Networks[0].Mode, g.Name)
|
|
|
|
}
|
2021-04-27 16:23:57 +00:00
|
|
|
|
|
|
|
// We must enforce lowercase characters on group and service names for connect
|
|
|
|
// sidecar proxies, because Consul assumes this invariant without validating it.
|
|
|
|
// https://github.com/hashicorp/consul/blob/v1.9.5/command/connect/proxy/proxy.go#L235
|
|
|
|
|
|
|
|
if s.Name != strings.ToLower(s.Name) {
|
|
|
|
return fmt.Errorf("Consul Connect service name %q in group %q must not contain uppercase characters", s.Name, g.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if g.Name != strings.ToLower(g.Name) {
|
|
|
|
return fmt.Errorf("Consul Connect group %q with service %q must not contain uppercase characters", g.Name, s.Name)
|
|
|
|
}
|
|
|
|
|
2020-06-22 17:55:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func groupConnectNativeValidate(g *structs.TaskGroup, s *structs.Service) error {
|
|
|
|
// note that network mode is not enforced for connect native services
|
|
|
|
|
2020-07-08 15:19:36 +00:00
|
|
|
if _, err := getNamedTaskForNativeService(g, s.Name, s.TaskName); err != nil {
|
|
|
|
return err
|
2020-06-22 17:55:59 +00:00
|
|
|
}
|
2020-07-08 15:19:36 +00:00
|
|
|
return nil
|
2020-06-22 17:55:59 +00:00
|
|
|
}
|
2020-07-28 20:12:08 +00:00
|
|
|
|
|
|
|
func groupConnectGatewayValidate(g *structs.TaskGroup) error {
|
|
|
|
// the group needs to be either bridge or host mode so we know how to configure
|
|
|
|
// the docker driver config
|
|
|
|
|
|
|
|
if n := len(g.Networks); n != 1 {
|
|
|
|
return fmt.Errorf("Consul Connect gateways require exactly 1 network, found %d in group %q", n, g.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
modes := []string{"bridge", "host"}
|
|
|
|
if !helper.SliceStringContains(modes, g.Networks[0].Mode) {
|
|
|
|
return fmt.Errorf(`Consul Connect Gateway service requires Task Group with network mode of type "bridge" or "host"`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|