2020-03-07 03:15:22 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-02-13 00:32:33 +00:00
|
|
|
"net"
|
2022-09-22 14:38:21 +00:00
|
|
|
"sort"
|
2021-02-13 00:32:33 +00:00
|
|
|
"strconv"
|
2022-04-14 16:30:21 +00:00
|
|
|
"strings"
|
2020-03-07 03:15:22 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2023-01-12 14:20:54 +00:00
|
|
|
"golang.org/x/exp/maps"
|
2022-09-21 19:53:25 +00:00
|
|
|
"golang.org/x/exp/slices"
|
2020-03-07 03:15:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// newConnect creates a new Consul AgentServiceConnect struct based on a Nomad
|
|
|
|
// Connect struct. If the nomad Connect struct is nil, nil will be returned to
|
|
|
|
// disable Connect for this service.
|
2022-09-22 14:38:21 +00:00
|
|
|
func newConnect(serviceID string, info structs.AllocInfo, serviceName string, nc *structs.ConsulConnect, networks structs.Networks, ports structs.AllocatedPorts) (*api.AgentServiceConnect, error) {
|
2020-07-28 20:12:08 +00:00
|
|
|
switch {
|
|
|
|
case nc == nil:
|
2023-01-30 14:48:43 +00:00
|
|
|
// no connect block means there is no connect service to register
|
2020-03-07 03:15:22 +00:00
|
|
|
return nil, nil
|
|
|
|
|
2020-07-28 20:12:08 +00:00
|
|
|
case nc.IsGateway():
|
|
|
|
// gateway settings are configured on the service block on the consul side
|
|
|
|
return nil, nil
|
|
|
|
|
|
|
|
case nc.IsNative():
|
|
|
|
// the service is connect native
|
2020-03-07 03:15:22 +00:00
|
|
|
return &api.AgentServiceConnect{Native: true}, nil
|
2020-07-28 20:12:08 +00:00
|
|
|
|
|
|
|
case nc.HasSidecar():
|
2020-12-15 20:38:33 +00:00
|
|
|
// must register the sidecar for this service
|
2021-02-09 12:05:28 +00:00
|
|
|
if nc.SidecarService.Port == "" {
|
|
|
|
nc.SidecarService.Port = fmt.Sprintf("%s-%s", structs.ConnectProxyPrefix, serviceName)
|
|
|
|
}
|
2022-09-22 14:38:21 +00:00
|
|
|
sidecarReg, err := connectSidecarRegistration(serviceID, info, nc.SidecarService, networks, ports)
|
2020-07-28 20:12:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &api.AgentServiceConnect{SidecarService: sidecarReg}, nil
|
|
|
|
|
|
|
|
default:
|
2020-12-15 20:38:33 +00:00
|
|
|
// a non-nil but empty connect block makes no sense
|
2020-07-28 20:12:08 +00:00
|
|
|
return nil, fmt.Errorf("Connect configuration empty for service %s", serviceName)
|
2020-03-07 03:15:22 +00:00
|
|
|
}
|
2020-07-28 20:12:08 +00:00
|
|
|
}
|
2020-03-07 03:15:22 +00:00
|
|
|
|
2020-07-28 20:12:08 +00:00
|
|
|
// newConnectGateway creates a new Consul AgentServiceConnectProxyConfig struct based on
|
|
|
|
// a Nomad Connect struct. If the Nomad Connect struct does not contain a gateway, nil
|
|
|
|
// will be returned as this service is not a gateway.
|
2022-04-14 16:30:21 +00:00
|
|
|
func newConnectGateway(connect *structs.ConsulConnect) *api.AgentServiceConnectProxyConfig {
|
2020-07-28 20:12:08 +00:00
|
|
|
if !connect.IsGateway() {
|
|
|
|
return nil
|
2020-03-07 03:15:22 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 15:27:01 +00:00
|
|
|
var envoyConfig map[string]interface{}
|
2020-07-28 20:12:08 +00:00
|
|
|
|
2023-01-30 14:48:43 +00:00
|
|
|
// Populate the envoy configuration from the gateway.proxy block, if
|
2021-01-05 15:27:01 +00:00
|
|
|
// such configuration is provided.
|
|
|
|
if proxy := connect.Gateway.Proxy; proxy != nil {
|
|
|
|
envoyConfig = make(map[string]interface{})
|
2020-07-28 20:12:08 +00:00
|
|
|
|
2021-01-05 15:27:01 +00:00
|
|
|
if len(proxy.EnvoyGatewayBindAddresses) > 0 {
|
|
|
|
envoyConfig["envoy_gateway_bind_addresses"] = proxy.EnvoyGatewayBindAddresses
|
|
|
|
}
|
2020-07-28 20:12:08 +00:00
|
|
|
|
2021-01-05 15:27:01 +00:00
|
|
|
if proxy.EnvoyGatewayNoDefaultBind {
|
|
|
|
envoyConfig["envoy_gateway_no_default_bind"] = true
|
|
|
|
}
|
2020-07-28 20:12:08 +00:00
|
|
|
|
2021-01-05 15:27:01 +00:00
|
|
|
if proxy.EnvoyGatewayBindTaggedAddresses {
|
|
|
|
envoyConfig["envoy_gateway_bind_tagged_addresses"] = true
|
|
|
|
}
|
2020-07-28 20:12:08 +00:00
|
|
|
|
2020-12-15 20:38:33 +00:00
|
|
|
if proxy.EnvoyDNSDiscoveryType != "" {
|
|
|
|
envoyConfig["envoy_dns_discovery_type"] = proxy.EnvoyDNSDiscoveryType
|
|
|
|
}
|
|
|
|
|
2021-01-05 15:27:01 +00:00
|
|
|
if proxy.ConnectTimeout != nil {
|
|
|
|
envoyConfig["connect_timeout_ms"] = proxy.ConnectTimeout.Milliseconds()
|
|
|
|
}
|
2020-07-28 20:12:08 +00:00
|
|
|
|
2021-01-05 15:27:01 +00:00
|
|
|
if len(proxy.Config) > 0 {
|
|
|
|
for k, v := range proxy.Config {
|
|
|
|
envoyConfig[k] = v
|
|
|
|
}
|
2020-07-28 20:12:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &api.AgentServiceConnectProxyConfig{Config: envoyConfig}
|
2020-03-07 03:15:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 14:38:21 +00:00
|
|
|
func connectSidecarRegistration(serviceID string, info structs.AllocInfo, css *structs.ConsulSidecarService, networks structs.Networks, ports structs.AllocatedPorts) (*api.AgentServiceRegistration, error) {
|
2020-03-07 03:15:22 +00:00
|
|
|
if css == nil {
|
2023-01-30 14:48:43 +00:00
|
|
|
// no sidecar block means there is no sidecar service to register
|
2020-03-07 03:15:22 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-02-09 12:05:28 +00:00
|
|
|
cMapping, err := connectPort(css.Port, networks, ports)
|
2020-03-07 03:15:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-09-22 14:38:21 +00:00
|
|
|
proxy, err := connectSidecarProxy(info, css.Proxy, cMapping.To, networks)
|
2020-03-07 03:15:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-08 19:54:21 +00:00
|
|
|
|
2021-05-07 16:10:26 +00:00
|
|
|
// if the service has a TCP check that's failing, we need an alias to
|
|
|
|
// ensure service discovery excludes this sidecar from queries
|
|
|
|
// (ex. in the case of Connect upstreams)
|
|
|
|
checks := api.AgentServiceChecks{{
|
2022-04-14 16:30:21 +00:00
|
|
|
Name: "Connect Sidecar Aliasing " + serviceID,
|
|
|
|
AliasService: serviceID,
|
2021-05-07 16:10:26 +00:00
|
|
|
}}
|
|
|
|
if !css.DisableDefaultTCPCheck {
|
|
|
|
checks = append(checks, &api.AgentServiceCheck{
|
|
|
|
Name: "Connect Sidecar Listening",
|
|
|
|
TCP: net.JoinHostPort(cMapping.HostIP, strconv.Itoa(cMapping.Value)),
|
|
|
|
Interval: "10s",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-07 03:15:22 +00:00
|
|
|
return &api.AgentServiceRegistration{
|
2022-09-21 19:53:25 +00:00
|
|
|
Tags: slices.Clone(css.Tags),
|
2021-02-09 12:05:28 +00:00
|
|
|
Port: cMapping.Value,
|
|
|
|
Address: cMapping.HostIP,
|
2020-03-07 03:15:22 +00:00
|
|
|
Proxy: proxy,
|
2021-05-07 16:10:26 +00:00
|
|
|
Checks: checks,
|
2020-03-07 03:15:22 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-09-22 14:38:21 +00:00
|
|
|
func connectSidecarProxy(info structs.AllocInfo, proxy *structs.ConsulProxy, cPort int, networks structs.Networks) (*api.AgentServiceConnectProxyConfig, error) {
|
2020-03-07 03:15:22 +00:00
|
|
|
if proxy == nil {
|
2020-04-01 16:22:31 +00:00
|
|
|
proxy = new(structs.ConsulProxy)
|
2020-03-07 03:15:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expose, err := connectProxyExpose(proxy.Expose, networks)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &api.AgentServiceConnectProxyConfig{
|
|
|
|
LocalServiceAddress: proxy.LocalServiceAddress,
|
|
|
|
LocalServicePort: proxy.LocalServicePort,
|
2022-09-22 14:38:21 +00:00
|
|
|
Config: connectProxyConfig(proxy.Config, cPort, info),
|
2020-03-07 03:15:22 +00:00
|
|
|
Upstreams: connectUpstreams(proxy.Upstreams),
|
|
|
|
Expose: expose,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func connectProxyExpose(expose *structs.ConsulExposeConfig, networks structs.Networks) (api.ExposeConfig, error) {
|
|
|
|
if expose == nil {
|
|
|
|
return api.ExposeConfig{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
paths, err := connectProxyExposePaths(expose.Paths, networks)
|
|
|
|
if err != nil {
|
|
|
|
return api.ExposeConfig{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return api.ExposeConfig{
|
|
|
|
Checks: false,
|
|
|
|
Paths: paths,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func connectProxyExposePaths(in []structs.ConsulExposePath, networks structs.Networks) ([]api.ExposePath, error) {
|
|
|
|
if len(in) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
paths := make([]api.ExposePath, len(in))
|
|
|
|
for i, path := range in {
|
|
|
|
if _, exposedPort, err := connectExposePathPort(path.ListenerPort, networks); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
paths[i] = api.ExposePath{
|
|
|
|
ListenerPort: exposedPort,
|
|
|
|
Path: path.Path,
|
|
|
|
LocalPathPort: path.LocalPathPort,
|
|
|
|
Protocol: path.Protocol,
|
|
|
|
ParsedFromCheck: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return paths, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func connectUpstreams(in []structs.ConsulUpstream) []api.Upstream {
|
|
|
|
if len(in) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
upstreams := make([]api.Upstream, len(in))
|
|
|
|
for i, upstream := range in {
|
|
|
|
upstreams[i] = api.Upstream{
|
2022-05-25 20:05:15 +00:00
|
|
|
DestinationName: upstream.DestinationName,
|
|
|
|
DestinationNamespace: upstream.DestinationNamespace,
|
|
|
|
LocalBindPort: upstream.LocalBindPort,
|
|
|
|
Datacenter: upstream.Datacenter,
|
|
|
|
LocalBindAddress: upstream.LocalBindAddress,
|
|
|
|
MeshGateway: connectMeshGateway(upstream.MeshGateway),
|
2023-01-12 14:20:54 +00:00
|
|
|
Config: maps.Clone(upstream.Config),
|
2020-03-07 03:15:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return upstreams
|
|
|
|
}
|
|
|
|
|
2021-04-12 19:10:10 +00:00
|
|
|
// connectMeshGateway creates an api.MeshGatewayConfig from the nomad upstream
|
|
|
|
// block. A non-existent config or unsupported gateway mode will default to the
|
|
|
|
// Consul default mode.
|
2022-08-13 14:31:17 +00:00
|
|
|
func connectMeshGateway(in structs.ConsulMeshGateway) api.MeshGatewayConfig {
|
2021-04-12 19:10:10 +00:00
|
|
|
gw := api.MeshGatewayConfig{
|
|
|
|
Mode: api.MeshGatewayModeDefault,
|
|
|
|
}
|
|
|
|
|
|
|
|
switch in.Mode {
|
|
|
|
case "local":
|
|
|
|
gw.Mode = api.MeshGatewayModeLocal
|
|
|
|
case "remote":
|
|
|
|
gw.Mode = api.MeshGatewayModeRemote
|
|
|
|
case "none":
|
|
|
|
gw.Mode = api.MeshGatewayModeNone
|
|
|
|
}
|
|
|
|
|
|
|
|
return gw
|
|
|
|
}
|
|
|
|
|
2022-09-22 14:38:21 +00:00
|
|
|
func connectProxyConfig(cfg map[string]interface{}, port int, info structs.AllocInfo) map[string]interface{} {
|
2020-03-07 03:15:22 +00:00
|
|
|
if cfg == nil {
|
|
|
|
cfg = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
cfg["bind_address"] = "0.0.0.0"
|
|
|
|
cfg["bind_port"] = port
|
2022-09-22 14:38:21 +00:00
|
|
|
|
|
|
|
tags := map[string]string{
|
|
|
|
"nomad.group=": info.Group,
|
|
|
|
"nomad.job=": info.JobID,
|
|
|
|
"nomad.namespace=": info.Namespace,
|
|
|
|
"nomad.alloc_id=": info.AllocID,
|
|
|
|
}
|
|
|
|
injectNomadInfo(cfg, tags)
|
2022-04-14 16:30:21 +00:00
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
2022-09-22 14:38:21 +00:00
|
|
|
// injectNomadInfo merges nomad information into cfg=>envoy_stats_tags
|
2022-04-14 16:30:21 +00:00
|
|
|
//
|
|
|
|
// cfg must not be nil
|
2022-09-22 14:38:21 +00:00
|
|
|
func injectNomadInfo(cfg map[string]interface{}, defaultTags map[string]string) {
|
|
|
|
const configKey = "envoy_stats_tags"
|
2022-04-11 17:27:14 +00:00
|
|
|
|
2022-09-22 14:38:21 +00:00
|
|
|
existingTagsI := cfg[configKey]
|
|
|
|
switch existingTags := existingTagsI.(type) {
|
2022-04-14 16:30:21 +00:00
|
|
|
case []string:
|
2022-09-22 14:38:21 +00:00
|
|
|
if len(existingTags) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
OUTER:
|
|
|
|
for key, value := range defaultTags {
|
|
|
|
for _, tag := range existingTags {
|
|
|
|
if strings.HasPrefix(tag, key) {
|
|
|
|
continue OUTER
|
|
|
|
}
|
2022-04-14 16:30:21 +00:00
|
|
|
}
|
2022-09-22 14:38:21 +00:00
|
|
|
existingTags = append(existingTags, key+value)
|
|
|
|
}
|
|
|
|
cfg[configKey] = existingTags
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// common case.
|
|
|
|
var tags []string
|
|
|
|
for key, value := range defaultTags {
|
|
|
|
if value == "" {
|
|
|
|
continue
|
2022-04-14 16:30:21 +00:00
|
|
|
}
|
2022-09-22 14:38:21 +00:00
|
|
|
tag := key + value
|
|
|
|
tags = append(tags, tag)
|
2022-04-14 16:30:21 +00:00
|
|
|
}
|
2022-09-22 14:38:21 +00:00
|
|
|
sort.Strings(tags) // mostly for test stability
|
|
|
|
cfg[configKey] = tags
|
2020-03-07 03:15:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func connectNetworkInvariants(networks structs.Networks) error {
|
|
|
|
if n := len(networks); n != 1 {
|
|
|
|
return fmt.Errorf("Connect only supported with exactly 1 network (found %d)", n)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// connectPort returns the network and port for the Connect proxy sidecar
|
|
|
|
// defined for this service. An error is returned if the network and port
|
|
|
|
// cannot be determined.
|
2021-02-09 12:05:28 +00:00
|
|
|
func connectPort(portLabel string, networks structs.Networks, ports structs.AllocatedPorts) (structs.AllocatedPortMapping, error) {
|
2020-03-07 03:15:22 +00:00
|
|
|
if err := connectNetworkInvariants(networks); err != nil {
|
2021-02-09 12:05:28 +00:00
|
|
|
return structs.AllocatedPortMapping{}, err
|
2020-03-07 03:15:22 +00:00
|
|
|
}
|
2021-02-09 12:05:28 +00:00
|
|
|
mapping, ok := ports.Get(portLabel)
|
2020-03-07 03:15:22 +00:00
|
|
|
if !ok {
|
2021-02-09 12:05:28 +00:00
|
|
|
mapping = networks.Port(portLabel)
|
|
|
|
if mapping.Value > 0 {
|
|
|
|
return mapping, nil
|
|
|
|
}
|
2021-02-13 00:32:33 +00:00
|
|
|
return structs.AllocatedPortMapping{}, fmt.Errorf("No port of label %q defined", portLabel)
|
2020-03-07 03:15:22 +00:00
|
|
|
}
|
2021-02-09 12:05:28 +00:00
|
|
|
return mapping, nil
|
2020-03-07 03:15:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// connectExposePathPort returns the port for the exposed path for the exposed
|
|
|
|
// proxy path.
|
|
|
|
func connectExposePathPort(portLabel string, networks structs.Networks) (string, int, error) {
|
|
|
|
if err := connectNetworkInvariants(networks); err != nil {
|
|
|
|
return "", 0, err
|
|
|
|
}
|
|
|
|
|
2021-02-09 12:05:28 +00:00
|
|
|
mapping := networks.Port(portLabel)
|
|
|
|
if mapping.Value == 0 {
|
2020-03-07 03:15:22 +00:00
|
|
|
return "", 0, fmt.Errorf("No port of label %q defined", portLabel)
|
|
|
|
}
|
|
|
|
|
2021-02-09 12:05:28 +00:00
|
|
|
return mapping.HostIP, mapping.Value, nil
|
2020-03-07 03:15:22 +00:00
|
|
|
}
|