2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2019-04-29 16:27:57 +00:00
|
|
|
package xds
|
|
|
|
|
|
|
|
import (
|
2021-03-09 05:10:27 +00:00
|
|
|
"strings"
|
2023-12-11 16:31:59 +00:00
|
|
|
"time"
|
2021-02-22 21:00:15 +00:00
|
|
|
|
2021-04-29 18:54:05 +00:00
|
|
|
envoy_cluster_v3 "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
|
2022-05-25 01:44:54 +00:00
|
|
|
"google.golang.org/protobuf/types/known/durationpb"
|
2021-04-29 18:54:05 +00:00
|
|
|
|
2021-02-22 21:00:15 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2023-01-11 14:39:10 +00:00
|
|
|
"google.golang.org/protobuf/types/known/wrapperspb"
|
2021-02-22 21:00:15 +00:00
|
|
|
|
2019-06-18 00:52:01 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2020-05-27 18:42:01 +00:00
|
|
|
"github.com/hashicorp/consul/lib/decode"
|
2019-04-29 16:27:57 +00:00
|
|
|
)
|
|
|
|
|
2023-12-11 16:31:59 +00:00
|
|
|
func parseConfig[T any](m map[string]any, cfg *T) error {
|
|
|
|
d, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
|
|
|
DecodeHook: mapstructure.ComposeDecodeHookFunc(
|
|
|
|
decode.HookWeakDecodeFromSlice,
|
|
|
|
decode.HookTranslateKeys,
|
|
|
|
),
|
|
|
|
Result: cfg,
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return d.Decode(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// XDSCommonConfig contains the configuration from the opaque map that is common to both gateways and sidecar proxies.
|
|
|
|
type XDSCommonConfig struct {
|
|
|
|
// XDSFetchTimeoutMs specifies the amount of milliseconds to wait for dynamically configured Envoy data (EDS, RDS).
|
|
|
|
// Uses the Envoy default value if not specified or negative. A value of zero disables the timeout.
|
|
|
|
XDSFetchTimeoutMs *int `mapstructure:"xds_fetch_timeout_ms"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseXDSCommonConfig returns the XDSCommonConfig parsed from an opaque map. If an
|
|
|
|
// error occurs during parsing, it is returned along with the default config. This
|
|
|
|
// allows the caller to choose whether and how to report the error
|
|
|
|
func ParseXDSCommonConfig(m map[string]interface{}) (XDSCommonConfig, error) {
|
|
|
|
var cfg XDSCommonConfig
|
|
|
|
err := parseConfig(m, &cfg)
|
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *XDSCommonConfig) GetXDSFetchTimeout() *durationpb.Duration {
|
|
|
|
if c == nil || c.XDSFetchTimeoutMs == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if *c.XDSFetchTimeoutMs >= 0 {
|
|
|
|
return durationpb.New(time.Duration(*c.XDSFetchTimeoutMs) * time.Millisecond)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-29 16:27:57 +00:00
|
|
|
// ProxyConfig describes the keys we understand from Connect.Proxy.Config. Note
|
|
|
|
// that this only includes config keys that affects runtime config delivered by
|
|
|
|
// xDS. For Envoy config keys that affect bootstrap generation see
|
|
|
|
// command/connect/envoy/bootstrap_config.go.
|
|
|
|
type ProxyConfig struct {
|
|
|
|
// PublicListenerJSON is a complete override ("escape hatch") for the
|
|
|
|
// upstream's public listener. The Connect server TLS certificate and
|
|
|
|
// validation context will be injected overriding any TLS settings present. An
|
|
|
|
// AuthZ filter will also be prepended to each filterChain provided to enforce
|
|
|
|
// Connect's access control.
|
2019-08-21 20:10:12 +00:00
|
|
|
//
|
|
|
|
// Note: This escape hatch is compatible with the discovery chain.
|
2019-04-29 16:27:57 +00:00
|
|
|
PublicListenerJSON string `mapstructure:"envoy_public_listener_json"`
|
|
|
|
|
2022-08-30 06:36:06 +00:00
|
|
|
// ListenerTracingJSON is a complete override ("escape hatch") for the
|
2022-08-02 06:52:48 +00:00
|
|
|
// listeners tracing configuration.
|
|
|
|
//
|
|
|
|
// Note: This escape hatch is compatible with the discovery chain.
|
2022-08-30 06:36:06 +00:00
|
|
|
ListenerTracingJSON string `mapstructure:"envoy_listener_tracing_json"`
|
2022-08-02 06:52:48 +00:00
|
|
|
|
2019-04-29 16:27:57 +00:00
|
|
|
// LocalClusterJSON is a complete override ("escape hatch") for the
|
|
|
|
// local application cluster.
|
2019-08-21 20:10:12 +00:00
|
|
|
//
|
|
|
|
// Note: This escape hatch is compatible with the discovery chain.
|
2019-04-29 16:27:57 +00:00
|
|
|
LocalClusterJSON string `mapstructure:"envoy_local_cluster_json"`
|
|
|
|
|
|
|
|
// LocalConnectTimeoutMs is the number of milliseconds to timeout making a new
|
|
|
|
// connection to the local app instance. Defaults to 5000 (5 seconds) if not
|
|
|
|
// set.
|
|
|
|
LocalConnectTimeoutMs int `mapstructure:"local_connect_timeout_ms"`
|
|
|
|
|
2021-01-25 19:50:00 +00:00
|
|
|
// LocalRequestTimeoutMs is the number of milliseconds to timeout HTTP requests
|
|
|
|
// to the local app instance. If not set, no value is set, Envoy defaults are
|
|
|
|
// respected (15s)
|
|
|
|
LocalRequestTimeoutMs *int `mapstructure:"local_request_timeout_ms"`
|
|
|
|
|
2022-11-29 22:43:15 +00:00
|
|
|
// LocalIdleTimeoutMs is the number of milliseconds to timeout HTTP streams
|
|
|
|
// to the local app instance. If not set, no value is set, Envoy defaults are
|
|
|
|
// respected (300s)
|
|
|
|
LocalIdleTimeoutMs *int `mapstructure:"local_idle_timeout_ms"`
|
|
|
|
|
2019-04-29 16:27:57 +00:00
|
|
|
// Protocol describes the service's protocol. Valid values are "tcp",
|
2019-07-05 15:06:47 +00:00
|
|
|
// "http" and "grpc". Anything else is treated as tcp. This enables
|
|
|
|
// protocol aware features like per-request metrics and connection
|
|
|
|
// pooling, tracing, routing etc.
|
2019-04-29 16:27:57 +00:00
|
|
|
Protocol string `mapstructure:"protocol"`
|
2019-07-05 15:06:47 +00:00
|
|
|
|
|
|
|
// BindAddress overrides the address the proxy's listener binds to. This
|
|
|
|
// enables proxies in network namespaces to bind to a different address
|
|
|
|
// than the host address.
|
|
|
|
BindAddress string `mapstructure:"bind_address"`
|
|
|
|
|
|
|
|
// BindPort overrides the port the proxy's listener binds to. This
|
|
|
|
// enable proxies in network namespaces to bind to a different port
|
|
|
|
// than the host port being advertised.
|
|
|
|
BindPort int `mapstructure:"bind_port"`
|
2022-05-19 17:06:13 +00:00
|
|
|
|
|
|
|
// MaxInboundConnections is the maximum number of inbound connections to
|
|
|
|
// the proxy. If not set, the default is 0 (no limit).
|
|
|
|
MaxInboundConnections int `mapstructure:"max_inbound_connections"`
|
2022-09-26 16:29:06 +00:00
|
|
|
|
|
|
|
// BalanceInboundConnections indicates how the proxy should attempt to distribute
|
|
|
|
// connections across worker threads. Only used by envoy proxies.
|
|
|
|
BalanceInboundConnections string `json:",omitempty" alias:"balance_inbound_connections"`
|
2019-04-29 16:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseProxyConfig returns the ProxyConfig parsed from the an opaque map. If an
|
|
|
|
// error occurs during parsing it is returned along with the default config this
|
|
|
|
// allows caller to choose whether and how to report the error.
|
|
|
|
func ParseProxyConfig(m map[string]interface{}) (ProxyConfig, error) {
|
|
|
|
var cfg ProxyConfig
|
2023-12-11 16:31:59 +00:00
|
|
|
if err := parseConfig(m, &cfg); err != nil {
|
2020-05-27 17:02:22 +00:00
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
|
2019-04-29 16:27:57 +00:00
|
|
|
if cfg.Protocol == "" {
|
|
|
|
cfg.Protocol = "tcp"
|
|
|
|
} else {
|
|
|
|
cfg.Protocol = strings.ToLower(cfg.Protocol)
|
|
|
|
}
|
|
|
|
if cfg.LocalConnectTimeoutMs < 1 {
|
|
|
|
cfg.LocalConnectTimeoutMs = 5000
|
|
|
|
}
|
2021-01-25 19:50:00 +00:00
|
|
|
|
2023-12-11 16:31:59 +00:00
|
|
|
return cfg, nil
|
2019-04-29 16:27:57 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 16:20:56 +00:00
|
|
|
type GatewayConfig struct {
|
2019-06-18 00:52:01 +00:00
|
|
|
// BindTaggedAddresses when set will cause all of the services tagged
|
|
|
|
// addresses to have listeners bound to them in addition to the main service
|
|
|
|
// address listener. This is only suitable when the tagged addresses are IP
|
|
|
|
// addresses of network interfaces Envoy can see. i.e. When using DNS names
|
|
|
|
// for those addresses or where an external entity maps that IP to the Envoy
|
|
|
|
// (like AWS EC2 mapping a public IP to the private interface) then this
|
|
|
|
// cannot be used. See the BindAddresses config instead
|
2020-05-27 18:28:28 +00:00
|
|
|
BindTaggedAddresses bool `mapstructure:"envoy_gateway_bind_tagged_addresses" alias:"envoy_mesh_gateway_bind_tagged_addresses"`
|
2019-06-18 00:52:01 +00:00
|
|
|
|
|
|
|
// BindAddresses additional bind addresses to configure listeners for
|
2020-05-27 18:28:28 +00:00
|
|
|
BindAddresses map[string]structs.ServiceAddress `mapstructure:"envoy_gateway_bind_addresses" alias:"envoy_mesh_gateway_bind_addresses"`
|
2019-06-18 00:52:01 +00:00
|
|
|
|
|
|
|
// NoDefaultBind indicates that we should not bind to the default address of the
|
|
|
|
// gateway service
|
2020-05-27 18:28:28 +00:00
|
|
|
NoDefaultBind bool `mapstructure:"envoy_gateway_no_default_bind" alias:"envoy_mesh_gateway_no_default_bind"`
|
2019-06-18 00:52:01 +00:00
|
|
|
|
2020-06-03 21:28:45 +00:00
|
|
|
// DNSDiscoveryType indicates the DNS service discovery type.
|
|
|
|
// See: https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/upstream/service_discovery#arch-overview-service-discovery-types
|
|
|
|
DNSDiscoveryType string `mapstructure:"envoy_dns_discovery_type"`
|
|
|
|
|
2019-06-18 00:52:01 +00:00
|
|
|
// ConnectTimeoutMs is the number of milliseconds to timeout making a new
|
|
|
|
// connection to this upstream. Defaults to 5000 (5 seconds) if not set.
|
|
|
|
ConnectTimeoutMs int `mapstructure:"connect_timeout_ms"`
|
2022-09-29 07:14:15 +00:00
|
|
|
|
2022-10-11 19:22:55 +00:00
|
|
|
// TCP keepalive settings for remote gateway upstreams (mesh gateways and terminating gateway upstreams).
|
2022-09-29 07:14:15 +00:00
|
|
|
// See: https://www.envoyproxy.io/docs/envoy/latest/api-v3/config/core/v3/address.proto#envoy-v3-api-msg-config-core-v3-tcpkeepalive
|
2022-10-11 19:22:55 +00:00
|
|
|
TcpKeepaliveEnable bool `mapstructure:"envoy_gateway_remote_tcp_enable_keepalive"`
|
|
|
|
TcpKeepaliveTime int `mapstructure:"envoy_gateway_remote_tcp_keepalive_time"`
|
|
|
|
TcpKeepaliveInterval int `mapstructure:"envoy_gateway_remote_tcp_keepalive_interval"`
|
|
|
|
TcpKeepaliveProbes int `mapstructure:"envoy_gateway_remote_tcp_keepalive_probes"`
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 16:20:56 +00:00
|
|
|
// ParseGatewayConfig returns the GatewayConfig parsed from an opaque map. If an
|
2019-06-18 00:52:01 +00:00
|
|
|
// error occurs during parsing, it is returned along with the default config. This
|
|
|
|
// allows the caller to choose whether and how to report the error
|
2020-03-26 16:20:56 +00:00
|
|
|
func ParseGatewayConfig(m map[string]interface{}) (GatewayConfig, error) {
|
|
|
|
var cfg GatewayConfig
|
2023-12-11 16:31:59 +00:00
|
|
|
if err := parseConfig(m, &cfg); err != nil {
|
2020-05-27 18:42:01 +00:00
|
|
|
return cfg, err
|
|
|
|
}
|
2019-06-18 00:52:01 +00:00
|
|
|
if cfg.ConnectTimeoutMs < 1 {
|
|
|
|
cfg.ConnectTimeoutMs = 5000
|
|
|
|
}
|
2020-06-03 21:28:45 +00:00
|
|
|
cfg.DNSDiscoveryType = strings.ToLower(cfg.DNSDiscoveryType)
|
2023-12-11 16:31:59 +00:00
|
|
|
return cfg, nil
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 22:59:48 +00:00
|
|
|
// Return an envoy.OutlierDetection populated by the values from structs.PassiveHealthCheck.
|
2020-04-27 18:53:21 +00:00
|
|
|
// If all values are zero a default empty OutlierDetection will be returned to
|
|
|
|
// enable outlier detection with default values.
|
2022-12-13 16:51:37 +00:00
|
|
|
// - If override is not nil, it will overwrite the values from p, e.g., ingress gateway defaults
|
|
|
|
// - allowZero is added to handle the legacy case where connect-proxy and mesh gateway can set 0
|
|
|
|
// for EnforcingConsecutive5xx. Due to the definition of proto of PassiveHealthCheck, ingress
|
|
|
|
// gateway's EnforcingConsecutive5xx must be > 0.
|
|
|
|
func ToOutlierDetection(p *structs.PassiveHealthCheck, override *structs.PassiveHealthCheck, allowZero bool) *envoy_cluster_v3.OutlierDetection {
|
2021-02-26 22:23:15 +00:00
|
|
|
od := &envoy_cluster_v3.OutlierDetection{}
|
2022-12-13 16:51:37 +00:00
|
|
|
if p != nil {
|
|
|
|
|
|
|
|
if p.Interval != 0 {
|
|
|
|
od.Interval = durationpb.New(p.Interval)
|
|
|
|
}
|
|
|
|
if p.MaxFailures != 0 {
|
2023-01-11 14:39:10 +00:00
|
|
|
od.Consecutive_5Xx = &wrapperspb.UInt32Value{Value: p.MaxFailures}
|
2022-12-13 16:51:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if p.EnforcingConsecutive5xx != nil {
|
2023-04-26 22:59:48 +00:00
|
|
|
// NOTE: EnforcingConsecutive5xx must be greater than 0 for ingress-gateway
|
2022-12-13 16:51:37 +00:00
|
|
|
if *p.EnforcingConsecutive5xx != 0 {
|
2023-01-11 14:39:10 +00:00
|
|
|
od.EnforcingConsecutive_5Xx = &wrapperspb.UInt32Value{Value: *p.EnforcingConsecutive5xx}
|
2022-12-13 16:51:37 +00:00
|
|
|
} else if allowZero {
|
2023-01-11 14:39:10 +00:00
|
|
|
od.EnforcingConsecutive_5Xx = &wrapperspb.UInt32Value{Value: *p.EnforcingConsecutive5xx}
|
2022-12-13 16:51:37 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-26 22:59:48 +00:00
|
|
|
|
|
|
|
if p.MaxEjectionPercent != nil {
|
|
|
|
od.MaxEjectionPercent = &wrapperspb.UInt32Value{Value: *p.MaxEjectionPercent}
|
|
|
|
}
|
|
|
|
if p.BaseEjectionTime != nil {
|
|
|
|
od.BaseEjectionTime = durationpb.New(*p.BaseEjectionTime)
|
|
|
|
}
|
2022-12-13 16:51:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if override == nil {
|
2021-03-11 18:04:40 +00:00
|
|
|
return od
|
|
|
|
}
|
|
|
|
|
2022-12-13 16:51:37 +00:00
|
|
|
// override the default outlier detection value
|
|
|
|
if override.Interval != 0 {
|
|
|
|
od.Interval = durationpb.New(override.Interval)
|
2020-04-27 18:53:21 +00:00
|
|
|
}
|
2022-12-13 16:51:37 +00:00
|
|
|
if override.MaxFailures != 0 {
|
2023-01-11 14:39:10 +00:00
|
|
|
od.Consecutive_5Xx = &wrapperspb.UInt32Value{Value: override.MaxFailures}
|
2020-04-27 18:53:21 +00:00
|
|
|
}
|
2022-09-01 16:59:11 +00:00
|
|
|
|
2022-12-13 16:51:37 +00:00
|
|
|
if override.EnforcingConsecutive5xx != nil {
|
|
|
|
// NOTE: EnforcingConsecutive5xx must be great than 0 for ingress-gateway
|
|
|
|
if *override.EnforcingConsecutive5xx != 0 {
|
2023-01-11 14:39:10 +00:00
|
|
|
od.EnforcingConsecutive_5Xx = &wrapperspb.UInt32Value{Value: *override.EnforcingConsecutive5xx}
|
2022-12-13 16:51:37 +00:00
|
|
|
} else if allowZero {
|
2023-01-11 14:39:10 +00:00
|
|
|
od.EnforcingConsecutive_5Xx = &wrapperspb.UInt32Value{Value: *override.EnforcingConsecutive5xx}
|
2022-12-13 16:51:37 +00:00
|
|
|
}
|
2022-09-01 16:59:11 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 22:59:48 +00:00
|
|
|
if override.MaxEjectionPercent != nil {
|
|
|
|
od.MaxEjectionPercent = &wrapperspb.UInt32Value{Value: *override.MaxEjectionPercent}
|
|
|
|
}
|
|
|
|
if override.BaseEjectionTime != nil {
|
|
|
|
od.BaseEjectionTime = durationpb.New(*override.BaseEjectionTime)
|
|
|
|
}
|
|
|
|
|
2020-04-27 18:53:21 +00:00
|
|
|
return od
|
2019-04-29 16:27:57 +00:00
|
|
|
}
|