2018-10-03 18:18:55 +00:00
|
|
|
package xds
|
|
|
|
|
|
|
|
import (
|
2019-02-19 13:45:33 +00:00
|
|
|
"encoding/json"
|
2018-10-03 18:18:55 +00:00
|
|
|
"errors"
|
2019-02-19 13:45:33 +00:00
|
|
|
"fmt"
|
2018-10-03 18:18:55 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
envoy "github.com/envoyproxy/go-control-plane/envoy/api/v2"
|
|
|
|
envoyauth "github.com/envoyproxy/go-control-plane/envoy/api/v2/auth"
|
2019-03-22 19:37:14 +00:00
|
|
|
envoycluster "github.com/envoyproxy/go-control-plane/envoy/api/v2/cluster"
|
2018-10-03 18:18:55 +00:00
|
|
|
envoycore "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
|
2019-04-29 16:27:57 +00:00
|
|
|
envoyendpoint "github.com/envoyproxy/go-control-plane/envoy/api/v2/endpoint"
|
2019-07-02 03:10:51 +00:00
|
|
|
envoytype "github.com/envoyproxy/go-control-plane/envoy/type"
|
2019-02-19 13:45:33 +00:00
|
|
|
"github.com/gogo/protobuf/jsonpb"
|
2018-10-03 18:18:55 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2019-02-19 13:45:33 +00:00
|
|
|
"github.com/gogo/protobuf/types"
|
2018-10-03 18:18:55 +00:00
|
|
|
|
2019-08-19 18:03:03 +00:00
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
2018-10-03 18:18:55 +00:00
|
|
|
"github.com/hashicorp/consul/agent/proxycfg"
|
2019-02-19 13:45:33 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2018-10-03 18:18:55 +00:00
|
|
|
)
|
|
|
|
|
2019-06-24 19:05:36 +00:00
|
|
|
// clustersFromSnapshot returns the xDS API representation of the "clusters" in the snapshot.
|
2020-03-27 21:57:16 +00:00
|
|
|
func (s *Server) clustersFromSnapshot(cfgSnap *proxycfg.ConfigSnapshot, _ string) ([]proto.Message, error) {
|
2019-06-24 19:05:36 +00:00
|
|
|
if cfgSnap == nil {
|
|
|
|
return nil, errors.New("nil config given")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch cfgSnap.Kind {
|
|
|
|
case structs.ServiceKindConnectProxy:
|
2020-03-27 21:57:16 +00:00
|
|
|
return s.clustersFromSnapshotConnectProxy(cfgSnap)
|
2020-04-13 16:33:01 +00:00
|
|
|
case structs.ServiceKindTerminatingGateway:
|
2020-04-27 22:25:37 +00:00
|
|
|
return s.makeGatewayServiceClusters(cfgSnap)
|
2019-06-18 00:52:01 +00:00
|
|
|
case structs.ServiceKindMeshGateway:
|
2020-03-27 21:57:16 +00:00
|
|
|
return s.clustersFromSnapshotMeshGateway(cfgSnap)
|
2020-04-16 21:00:48 +00:00
|
|
|
case structs.ServiceKindIngressGateway:
|
|
|
|
return s.clustersFromSnapshotIngressGateway(cfgSnap)
|
2019-06-24 19:05:36 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Invalid service kind: %v", cfgSnap.Kind)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-03 18:18:55 +00:00
|
|
|
// clustersFromSnapshot returns the xDS API representation of the "clusters"
|
|
|
|
// (upstreams) in the snapshot.
|
2020-03-27 21:57:16 +00:00
|
|
|
func (s *Server) clustersFromSnapshotConnectProxy(cfgSnap *proxycfg.ConfigSnapshot) ([]proto.Message, error) {
|
2019-07-02 03:10:51 +00:00
|
|
|
// TODO(rb): this sizing is a low bound.
|
2019-07-02 19:53:06 +00:00
|
|
|
clusters := make([]proto.Message, 0, len(cfgSnap.Proxy.Upstreams)+1)
|
2018-10-03 18:18:55 +00:00
|
|
|
|
2019-07-02 03:10:51 +00:00
|
|
|
// Include the "app" cluster for the public listener
|
2019-09-26 02:55:52 +00:00
|
|
|
appCluster, err := s.makeAppCluster(cfgSnap, LocalAppClusterName, "", cfgSnap.Proxy.LocalServicePort)
|
2019-02-19 13:45:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-03 18:18:55 +00:00
|
|
|
|
2019-07-02 03:10:51 +00:00
|
|
|
clusters = append(clusters, appCluster)
|
|
|
|
|
|
|
|
for _, u := range cfgSnap.Proxy.Upstreams {
|
|
|
|
id := u.Identifier()
|
|
|
|
|
2019-08-22 20:11:56 +00:00
|
|
|
if u.DestinationType == structs.UpstreamDestTypePreparedQuery {
|
|
|
|
upstreamCluster, err := s.makeUpstreamClusterForPreparedQuery(u, cfgSnap)
|
2019-07-02 03:10:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
clusters = append(clusters, upstreamCluster)
|
|
|
|
|
|
|
|
} else {
|
2019-08-22 20:11:56 +00:00
|
|
|
chain := cfgSnap.ConnectProxy.DiscoveryChain[id]
|
2020-04-16 21:00:48 +00:00
|
|
|
chainEndpoints, ok := cfgSnap.ConnectProxy.WatchedUpstreamEndpoints[id]
|
|
|
|
if !ok {
|
|
|
|
// this should not happen
|
|
|
|
return nil, fmt.Errorf("no endpoint map for upstream %q", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
upstreamClusters, err := s.makeUpstreamClustersForDiscoveryChain(u, chain, chainEndpoints, cfgSnap)
|
2019-07-02 03:10:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cluster := range upstreamClusters {
|
|
|
|
clusters = append(clusters, cluster)
|
|
|
|
}
|
2019-02-19 13:45:33 +00:00
|
|
|
}
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
cfgSnap.Proxy.Expose.Finalize()
|
2019-09-26 02:55:52 +00:00
|
|
|
paths := cfgSnap.Proxy.Expose.Paths
|
|
|
|
|
|
|
|
// Add service health checks to the list of paths to create clusters for if needed
|
|
|
|
if cfgSnap.Proxy.Expose.Checks {
|
2020-01-24 15:04:58 +00:00
|
|
|
psid := structs.NewServiceID(cfgSnap.Proxy.DestinationServiceID, &cfgSnap.ProxyID.EnterpriseMeta)
|
2019-12-10 02:26:41 +00:00
|
|
|
for _, check := range s.CheckFetcher.ServiceHTTPBasedChecks(psid) {
|
2019-09-26 02:55:52 +00:00
|
|
|
p, err := parseCheckPath(check)
|
|
|
|
if err != nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
s.Logger.Warn("failed to create cluster for", "check", check.CheckID, "error", err)
|
2019-09-26 02:55:52 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
paths = append(paths, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new cluster if we need to expose a port that is different from the service port
|
|
|
|
for _, path := range paths {
|
|
|
|
if path.LocalPathPort == cfgSnap.Proxy.LocalServicePort {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c, err := s.makeAppCluster(cfgSnap, makeExposeClusterName(path.LocalPathPort), path.Protocol, path.LocalPathPort)
|
|
|
|
if err != nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
s.Logger.Warn("failed to make local cluster", "path", path.Path, "error", err)
|
2019-09-26 02:55:52 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
clusters = append(clusters, c)
|
|
|
|
}
|
2018-10-03 18:18:55 +00:00
|
|
|
return clusters, nil
|
|
|
|
}
|
|
|
|
|
2019-09-26 02:55:52 +00:00
|
|
|
func makeExposeClusterName(destinationPort int) string {
|
|
|
|
return fmt.Sprintf("exposed_cluster_%d", destinationPort)
|
|
|
|
}
|
|
|
|
|
2019-06-18 00:52:01 +00:00
|
|
|
// clustersFromSnapshotMeshGateway returns the xDS API representation of the "clusters"
|
|
|
|
// for a mesh gateway. This will include 1 cluster per remote datacenter as well as
|
|
|
|
// 1 cluster for each service subset.
|
2020-03-27 21:57:16 +00:00
|
|
|
func (s *Server) clustersFromSnapshotMeshGateway(cfgSnap *proxycfg.ConfigSnapshot) ([]proto.Message, error) {
|
2020-03-09 20:59:02 +00:00
|
|
|
datacenters := cfgSnap.MeshGateway.Datacenters()
|
|
|
|
|
2019-07-02 13:43:35 +00:00
|
|
|
// 1 cluster per remote dc + 1 cluster per local service (this is a lower bound - all subset specific clusters will be appended)
|
2020-03-09 20:59:02 +00:00
|
|
|
clusters := make([]proto.Message, 0, len(datacenters)+len(cfgSnap.MeshGateway.ServiceGroups))
|
2019-06-18 00:52:01 +00:00
|
|
|
|
|
|
|
// generate the remote dc clusters
|
2020-03-09 20:59:02 +00:00
|
|
|
for _, dc := range datacenters {
|
|
|
|
if dc == cfgSnap.Datacenter {
|
|
|
|
continue // skip local
|
|
|
|
}
|
2019-08-19 18:03:03 +00:00
|
|
|
clusterName := connect.DatacenterSNI(dc, cfgSnap.Roots.TrustDomain)
|
2019-06-18 00:52:01 +00:00
|
|
|
|
2020-04-27 22:25:37 +00:00
|
|
|
cluster, err := s.makeGatewayCluster(cfgSnap, clusterName)
|
2019-06-18 00:52:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-02 13:43:35 +00:00
|
|
|
clusters = append(clusters, cluster)
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
if cfgSnap.ServiceMeta[structs.MetaWANFederationKey] == "1" && cfgSnap.ServerSNIFn != nil {
|
|
|
|
// Add all of the remote wildcard datacenter mappings for servers.
|
|
|
|
for _, dc := range datacenters {
|
|
|
|
clusterName := cfgSnap.ServerSNIFn(dc, "")
|
|
|
|
|
2020-04-27 22:25:37 +00:00
|
|
|
cluster, err := s.makeGatewayCluster(cfgSnap, clusterName)
|
2020-03-09 20:59:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
clusters = append(clusters, cluster)
|
|
|
|
}
|
|
|
|
|
|
|
|
// And for the current datacenter, send all flavors appropriately.
|
|
|
|
for _, srv := range cfgSnap.MeshGateway.ConsulServers {
|
|
|
|
clusterName := cfgSnap.ServerSNIFn(cfgSnap.Datacenter, srv.Node.Node)
|
|
|
|
|
2020-04-27 22:25:37 +00:00
|
|
|
cluster, err := s.makeGatewayCluster(cfgSnap, clusterName)
|
2020-03-09 20:59:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
clusters = append(clusters, cluster)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 14:59:23 +00:00
|
|
|
// generate the per-service/subset clusters
|
2020-04-27 22:25:37 +00:00
|
|
|
c, err := s.makeGatewayServiceClusters(cfgSnap)
|
2020-04-14 14:59:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
clusters = append(clusters, c...)
|
|
|
|
|
|
|
|
return clusters, nil
|
|
|
|
}
|
|
|
|
|
2020-04-27 22:25:37 +00:00
|
|
|
func (s *Server) makeGatewayServiceClusters(cfgSnap *proxycfg.ConfigSnapshot) ([]proto.Message, error) {
|
|
|
|
var services map[structs.ServiceID]structs.CheckServiceNodes
|
|
|
|
var resolvers map[structs.ServiceID]*structs.ServiceResolverConfigEntry
|
|
|
|
|
|
|
|
switch cfgSnap.Kind {
|
|
|
|
case structs.ServiceKindTerminatingGateway:
|
|
|
|
services = cfgSnap.TerminatingGateway.ServiceGroups
|
|
|
|
resolvers = cfgSnap.TerminatingGateway.ServiceResolvers
|
|
|
|
case structs.ServiceKindMeshGateway:
|
|
|
|
services = cfgSnap.MeshGateway.ServiceGroups
|
|
|
|
resolvers = cfgSnap.MeshGateway.ServiceResolvers
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported gateway kind %q", cfgSnap.Kind)
|
|
|
|
}
|
2020-04-14 14:59:23 +00:00
|
|
|
|
|
|
|
clusters := make([]proto.Message, 0, len(services))
|
|
|
|
|
|
|
|
for svc, _ := range services {
|
2020-01-24 15:04:58 +00:00
|
|
|
clusterName := connect.ServiceSNI(svc.ID, "", svc.NamespaceOrDefault(), cfgSnap.Datacenter, cfgSnap.Roots.TrustDomain)
|
2020-04-14 14:59:23 +00:00
|
|
|
resolver, hasResolver := resolvers[svc]
|
2019-06-18 00:52:01 +00:00
|
|
|
|
2020-03-17 19:50:14 +00:00
|
|
|
// Create the cluster for default/unnamed services
|
|
|
|
var cluster *envoy.Cluster
|
|
|
|
var err error
|
2020-04-27 22:25:37 +00:00
|
|
|
|
|
|
|
if !hasResolver {
|
|
|
|
// Use a zero value resolver with no timeout and no subsets
|
|
|
|
resolver = &structs.ServiceResolverConfigEntry{}
|
2020-03-17 19:50:14 +00:00
|
|
|
}
|
2020-04-27 22:25:37 +00:00
|
|
|
cluster, err = s.makeGatewayClusterWithConnectTimeout(cfgSnap, clusterName, resolver.ConnectTimeout)
|
2019-06-18 00:52:01 +00:00
|
|
|
if err != nil {
|
2020-04-14 14:59:23 +00:00
|
|
|
return nil, fmt.Errorf("failed to make %s cluster: %v", cfgSnap.Kind, err)
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
2020-04-27 22:25:37 +00:00
|
|
|
|
|
|
|
if cfgSnap.Kind == structs.ServiceKindTerminatingGateway {
|
|
|
|
injectTerminatingGatewayTLSContext(cfgSnap, cluster, svc)
|
|
|
|
}
|
2019-07-02 13:43:35 +00:00
|
|
|
clusters = append(clusters, cluster)
|
|
|
|
|
2020-04-27 22:25:37 +00:00
|
|
|
// If there is a service-resolver for this service then also setup a cluster for each subset
|
|
|
|
for subsetName := range resolver.Subsets {
|
|
|
|
clusterName := connect.ServiceSNI(svc.ID, subsetName, svc.NamespaceOrDefault(), cfgSnap.Datacenter, cfgSnap.Roots.TrustDomain)
|
2019-07-02 13:43:35 +00:00
|
|
|
|
2020-04-27 22:25:37 +00:00
|
|
|
cluster, err := s.makeGatewayClusterWithConnectTimeout(cfgSnap, clusterName, resolver.ConnectTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to make %s cluster: %v", cfgSnap.Kind, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfgSnap.Kind == structs.ServiceKindTerminatingGateway {
|
|
|
|
injectTerminatingGatewayTLSContext(cfgSnap, cluster, svc)
|
2019-07-02 13:43:35 +00:00
|
|
|
}
|
2020-04-27 22:25:37 +00:00
|
|
|
clusters = append(clusters, cluster)
|
2019-07-02 13:43:35 +00:00
|
|
|
}
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return clusters, nil
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:00:48 +00:00
|
|
|
func (s *Server) clustersFromSnapshotIngressGateway(cfgSnap *proxycfg.ConfigSnapshot) ([]proto.Message, error) {
|
|
|
|
var clusters []proto.Message
|
|
|
|
for _, u := range cfgSnap.IngressGateway.Upstreams {
|
|
|
|
id := u.Identifier()
|
|
|
|
chain, ok := cfgSnap.IngressGateway.DiscoveryChain[id]
|
|
|
|
if !ok {
|
|
|
|
// this should not happen
|
|
|
|
return nil, fmt.Errorf("no discovery chain for upstream %q", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
chainEndpoints, ok := cfgSnap.IngressGateway.WatchedUpstreamEndpoints[id]
|
|
|
|
if !ok {
|
|
|
|
// this should not happen
|
|
|
|
return nil, fmt.Errorf("no endpoint map for upstream %q", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
upstreamClusters, err := s.makeUpstreamClustersForDiscoveryChain(u, chain, chainEndpoints, cfgSnap)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range upstreamClusters {
|
|
|
|
clusters = append(clusters, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return clusters, nil
|
|
|
|
}
|
|
|
|
|
2019-09-26 02:55:52 +00:00
|
|
|
func (s *Server) makeAppCluster(cfgSnap *proxycfg.ConfigSnapshot, name, pathProtocol string, port int) (*envoy.Cluster, error) {
|
2019-02-19 13:45:33 +00:00
|
|
|
var c *envoy.Cluster
|
|
|
|
var err error
|
|
|
|
|
2019-04-29 16:27:57 +00:00
|
|
|
cfg, err := ParseProxyConfig(cfgSnap.Proxy.Config)
|
|
|
|
if err != nil {
|
|
|
|
// Don't hard fail on a config typo, just warn. The parse func returns
|
|
|
|
// default config if there is an error so it's safe to continue.
|
2020-01-28 23:50:41 +00:00
|
|
|
s.Logger.Warn("failed to parse Connect.Proxy.Config", "error", err)
|
2019-04-29 16:27:57 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 17:13:28 +00:00
|
|
|
// If we have overridden local cluster config try to parse it into an Envoy cluster
|
2019-04-29 16:27:57 +00:00
|
|
|
if cfg.LocalClusterJSON != "" {
|
|
|
|
return makeClusterFromUserConfig(cfg.LocalClusterJSON)
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
2019-02-19 13:45:33 +00:00
|
|
|
|
2019-07-19 11:53:42 +00:00
|
|
|
addr := cfgSnap.Proxy.LocalServiceAddress
|
|
|
|
if addr == "" {
|
|
|
|
addr = "127.0.0.1"
|
|
|
|
}
|
|
|
|
c = &envoy.Cluster{
|
2019-09-26 02:55:52 +00:00
|
|
|
Name: name,
|
2019-07-19 11:53:42 +00:00
|
|
|
ConnectTimeout: time.Duration(cfg.LocalConnectTimeoutMs) * time.Millisecond,
|
|
|
|
ClusterDiscoveryType: &envoy.Cluster_Type{Type: envoy.Cluster_STATIC},
|
|
|
|
LoadAssignment: &envoy.ClusterLoadAssignment{
|
2019-09-26 02:55:52 +00:00
|
|
|
ClusterName: name,
|
2019-07-19 11:53:42 +00:00
|
|
|
Endpoints: []envoyendpoint.LocalityLbEndpoints{
|
|
|
|
{
|
|
|
|
LbEndpoints: []envoyendpoint.LbEndpoint{
|
2019-09-26 02:55:52 +00:00
|
|
|
makeEndpoint(name,
|
2019-07-19 11:53:42 +00:00
|
|
|
addr,
|
2019-09-26 02:55:52 +00:00
|
|
|
port),
|
2019-04-29 16:27:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-19 11:53:42 +00:00
|
|
|
},
|
|
|
|
}
|
2020-04-02 07:35:04 +00:00
|
|
|
protocol := pathProtocol
|
|
|
|
if protocol == "" {
|
|
|
|
protocol = cfg.Protocol
|
|
|
|
}
|
|
|
|
if protocol == "http2" || protocol == "grpc" {
|
2019-07-19 11:53:42 +00:00
|
|
|
c.Http2ProtocolOptions = &envoycore.Http2ProtocolOptions{}
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
2019-02-19 13:45:33 +00:00
|
|
|
|
|
|
|
return c, err
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 20:11:56 +00:00
|
|
|
func (s *Server) makeUpstreamClusterForPreparedQuery(upstream structs.Upstream, cfgSnap *proxycfg.ConfigSnapshot) (*envoy.Cluster, error) {
|
2019-02-19 13:45:33 +00:00
|
|
|
var c *envoy.Cluster
|
|
|
|
var err error
|
|
|
|
|
2019-08-19 18:03:03 +00:00
|
|
|
dc := upstream.Datacenter
|
|
|
|
if dc == "" {
|
|
|
|
dc = cfgSnap.Datacenter
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
2019-08-19 18:03:03 +00:00
|
|
|
sni := connect.UpstreamSNI(&upstream, "", dc, cfgSnap.Roots.TrustDomain)
|
2019-06-18 00:52:01 +00:00
|
|
|
|
2019-04-29 16:27:57 +00:00
|
|
|
cfg, err := ParseUpstreamConfig(upstream.Config)
|
|
|
|
if err != nil {
|
|
|
|
// Don't hard fail on a config typo, just warn. The parse func returns
|
|
|
|
// default config if there is an error so it's safe to continue.
|
2020-01-28 23:50:41 +00:00
|
|
|
s.Logger.Warn("failed to parse", "upstream", upstream.Identifier(), "error", err)
|
2019-04-29 16:27:57 +00:00
|
|
|
}
|
|
|
|
if cfg.ClusterJSON != "" {
|
|
|
|
c, err = makeClusterFromUserConfig(cfg.ClusterJSON)
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
2019-02-19 13:45:33 +00:00
|
|
|
}
|
2019-04-29 16:27:57 +00:00
|
|
|
// In the happy path don't return yet as we need to inject TLS config still.
|
2019-02-19 13:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
c = &envoy.Cluster{
|
2019-07-08 11:48:48 +00:00
|
|
|
Name: sni,
|
2019-06-07 12:10:43 +00:00
|
|
|
ConnectTimeout: time.Duration(cfg.ConnectTimeoutMs) * time.Millisecond,
|
|
|
|
ClusterDiscoveryType: &envoy.Cluster_Type{Type: envoy.Cluster_EDS},
|
2019-02-19 13:45:33 +00:00
|
|
|
EdsClusterConfig: &envoy.Cluster_EdsClusterConfig{
|
|
|
|
EdsConfig: &envoycore.ConfigSource{
|
|
|
|
ConfigSourceSpecifier: &envoycore.ConfigSource_Ads{
|
|
|
|
Ads: &envoycore.AggregatedConfigSource{},
|
|
|
|
},
|
2018-10-03 18:18:55 +00:00
|
|
|
},
|
|
|
|
},
|
2019-12-03 20:13:33 +00:00
|
|
|
CircuitBreakers: &envoycluster.CircuitBreakers{
|
|
|
|
Thresholds: makeThresholdsIfNeeded(cfg.Limits),
|
|
|
|
},
|
2019-03-22 19:37:14 +00:00
|
|
|
// Having an empty config enables outlier detection with default config.
|
|
|
|
OutlierDetection: &envoycluster.OutlierDetection{},
|
2019-02-19 13:45:33 +00:00
|
|
|
}
|
2019-04-29 16:27:57 +00:00
|
|
|
if cfg.Protocol == "http2" || cfg.Protocol == "grpc" {
|
|
|
|
c.Http2ProtocolOptions = &envoycore.Http2ProtocolOptions{}
|
|
|
|
}
|
2019-02-19 13:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enable TLS upstream with the configured client certificate.
|
|
|
|
c.TlsContext = &envoyauth.UpstreamTlsContext{
|
2020-04-27 22:25:37 +00:00
|
|
|
CommonTlsContext: makeCommonTLSContextFromLeaf(cfgSnap, cfgSnap.Leaf()),
|
2019-06-18 00:52:01 +00:00
|
|
|
Sni: sni,
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
2019-02-19 13:45:33 +00:00
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2019-07-02 03:10:51 +00:00
|
|
|
func (s *Server) makeUpstreamClustersForDiscoveryChain(
|
2019-07-08 11:48:48 +00:00
|
|
|
upstream structs.Upstream,
|
2019-07-02 03:10:51 +00:00
|
|
|
chain *structs.CompiledDiscoveryChain,
|
2020-04-16 21:00:48 +00:00
|
|
|
chainEndpoints map[string]structs.CheckServiceNodes,
|
2019-07-02 03:10:51 +00:00
|
|
|
cfgSnap *proxycfg.ConfigSnapshot,
|
|
|
|
) ([]*envoy.Cluster, error) {
|
2019-08-22 20:11:56 +00:00
|
|
|
if chain == nil {
|
2020-02-03 14:26:47 +00:00
|
|
|
return nil, fmt.Errorf("cannot create upstream cluster without discovery chain for %s", upstream.Identifier())
|
2019-08-22 20:11:56 +00:00
|
|
|
}
|
|
|
|
|
2019-07-08 11:48:48 +00:00
|
|
|
cfg, err := ParseUpstreamConfigNoDefaults(upstream.Config)
|
|
|
|
if err != nil {
|
|
|
|
// Don't hard fail on a config typo, just warn. The parse func returns
|
|
|
|
// default config if there is an error so it's safe to continue.
|
2020-01-28 23:50:41 +00:00
|
|
|
s.Logger.Warn("failed to parse", "upstream", upstream.Identifier(),
|
|
|
|
"error", err)
|
2019-07-08 11:48:48 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 20:11:56 +00:00
|
|
|
var escapeHatchCluster *envoy.Cluster
|
|
|
|
if cfg.ClusterJSON != "" {
|
|
|
|
if chain.IsDefault() {
|
|
|
|
// If you haven't done anything to setup the discovery chain, then
|
|
|
|
// you can use the envoy_cluster_json escape hatch.
|
|
|
|
escapeHatchCluster, err = makeClusterFromUserConfig(cfg.ClusterJSON)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-28 23:50:41 +00:00
|
|
|
s.Logger.Warn("ignoring escape hatch setting, because a discovery chain is configued for",
|
|
|
|
"discovery chain", chain.ServiceName, "upstream", upstream.Identifier(),
|
|
|
|
"envoy_cluster_json", chain.ServiceName)
|
2019-08-22 20:11:56 +00:00
|
|
|
}
|
2019-07-02 03:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var out []*envoy.Cluster
|
2019-08-02 03:44:05 +00:00
|
|
|
for _, node := range chain.Nodes {
|
|
|
|
if node.Type != structs.DiscoveryGraphNodeTypeResolver {
|
|
|
|
continue
|
|
|
|
}
|
2019-08-05 18:30:35 +00:00
|
|
|
failover := node.Resolver.Failover
|
2019-08-02 20:34:54 +00:00
|
|
|
targetID := node.Resolver.Target
|
|
|
|
|
|
|
|
target := chain.Targets[targetID]
|
2019-07-02 03:10:51 +00:00
|
|
|
|
2019-08-05 18:30:35 +00:00
|
|
|
// Determine if we have to generate the entire cluster differently.
|
|
|
|
failoverThroughMeshGateway := chain.WillFailoverThroughMeshGateway(node)
|
|
|
|
|
2019-08-19 18:03:03 +00:00
|
|
|
sni := target.SNI
|
|
|
|
clusterName := CustomizeClusterName(target.Name, chain)
|
2019-08-02 03:03:34 +00:00
|
|
|
|
2019-08-05 18:30:35 +00:00
|
|
|
if failoverThroughMeshGateway {
|
|
|
|
actualTargetID := firstHealthyTarget(
|
|
|
|
chain.Targets,
|
2020-04-16 21:00:48 +00:00
|
|
|
chainEndpoints,
|
2019-08-05 18:30:35 +00:00
|
|
|
targetID,
|
|
|
|
failover.Targets,
|
|
|
|
)
|
|
|
|
|
|
|
|
if actualTargetID != targetID {
|
|
|
|
actualTarget := chain.Targets[actualTargetID]
|
2019-08-19 18:03:03 +00:00
|
|
|
sni = actualTarget.SNI
|
2019-08-05 18:30:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
s.Logger.Debug("generating cluster for", "cluster", clusterName)
|
2019-07-02 03:10:51 +00:00
|
|
|
c := &envoy.Cluster{
|
2019-08-02 03:03:34 +00:00
|
|
|
Name: clusterName,
|
|
|
|
AltStatName: clusterName,
|
2019-08-02 03:44:05 +00:00
|
|
|
ConnectTimeout: node.Resolver.ConnectTimeout,
|
2019-07-02 03:10:51 +00:00
|
|
|
ClusterDiscoveryType: &envoy.Cluster_Type{Type: envoy.Cluster_EDS},
|
|
|
|
CommonLbConfig: &envoy.Cluster_CommonLbConfig{
|
|
|
|
HealthyPanicThreshold: &envoytype.Percent{
|
|
|
|
Value: 0, // disable panic threshold
|
|
|
|
},
|
|
|
|
},
|
|
|
|
EdsClusterConfig: &envoy.Cluster_EdsClusterConfig{
|
|
|
|
EdsConfig: &envoycore.ConfigSource{
|
|
|
|
ConfigSourceSpecifier: &envoycore.ConfigSource_Ads{
|
|
|
|
Ads: &envoycore.AggregatedConfigSource{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-12-03 20:13:33 +00:00
|
|
|
CircuitBreakers: &envoycluster.CircuitBreakers{
|
|
|
|
Thresholds: makeThresholdsIfNeeded(cfg.Limits),
|
|
|
|
},
|
2019-07-02 03:10:51 +00:00
|
|
|
// Having an empty config enables outlier detection with default config.
|
|
|
|
OutlierDetection: &envoycluster.OutlierDetection{},
|
|
|
|
}
|
2019-07-08 11:48:48 +00:00
|
|
|
|
|
|
|
proto := cfg.Protocol
|
|
|
|
if proto == "" {
|
|
|
|
proto = chain.Protocol
|
|
|
|
}
|
|
|
|
|
|
|
|
if proto == "" {
|
|
|
|
proto = "tcp"
|
|
|
|
}
|
|
|
|
|
|
|
|
if proto == "http2" || proto == "grpc" {
|
2019-07-02 03:10:51 +00:00
|
|
|
c.Http2ProtocolOptions = &envoycore.Http2ProtocolOptions{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enable TLS upstream with the configured client certificate.
|
|
|
|
c.TlsContext = &envoyauth.UpstreamTlsContext{
|
2020-04-27 22:25:37 +00:00
|
|
|
CommonTlsContext: makeCommonTLSContextFromLeaf(cfgSnap, cfgSnap.Leaf()),
|
2019-08-19 18:03:03 +00:00
|
|
|
Sni: sni,
|
2019-07-02 03:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out = append(out, c)
|
|
|
|
}
|
|
|
|
|
2019-08-22 20:11:56 +00:00
|
|
|
if escapeHatchCluster != nil {
|
|
|
|
if len(out) != 1 {
|
|
|
|
return nil, fmt.Errorf("cannot inject escape hatch cluster when discovery chain had no nodes")
|
|
|
|
}
|
|
|
|
defaultCluster := out[0]
|
|
|
|
|
|
|
|
// Overlay what the user provided.
|
|
|
|
escapeHatchCluster.TlsContext = defaultCluster.TlsContext
|
|
|
|
|
|
|
|
out = []*envoy.Cluster{escapeHatchCluster}
|
|
|
|
}
|
|
|
|
|
2019-07-02 03:10:51 +00:00
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2019-02-19 13:45:33 +00:00
|
|
|
// makeClusterFromUserConfig returns the listener config decoded from an
|
|
|
|
// arbitrary proto3 json format string or an error if it's invalid.
|
|
|
|
//
|
|
|
|
// For now we only support embedding in JSON strings because of the hcl parsing
|
2019-06-28 16:35:35 +00:00
|
|
|
// pain (see config.go comment above call to PatchSliceOfMaps). Until we
|
2019-02-19 13:45:33 +00:00
|
|
|
// refactor config parser a _lot_ user's opaque config that contains arrays will
|
|
|
|
// be mangled. We could actually fix that up in mapstructure which knows the
|
|
|
|
// type of the target so could resolve the slices to singletons unambiguously
|
|
|
|
// and it would work for us here... but we still have the problem that the
|
|
|
|
// config would render incorrectly in general in our HTTP API responses so we
|
|
|
|
// really need to fix it "properly".
|
|
|
|
//
|
|
|
|
// When we do that we can support just nesting the config directly into the
|
|
|
|
// JSON/hcl naturally but this is a stop-gap that gets us an escape hatch
|
|
|
|
// immediately. It's also probably not a bad thing to support long-term since
|
|
|
|
// any config generated by other systems will likely be in canonical protobuf
|
|
|
|
// from rather than our slight variant in JSON/hcl.
|
|
|
|
func makeClusterFromUserConfig(configJSON string) (*envoy.Cluster, error) {
|
|
|
|
var jsonFields map[string]*json.RawMessage
|
|
|
|
if err := json.Unmarshal([]byte(configJSON), &jsonFields); err != nil {
|
|
|
|
fmt.Println("Custom error", err, configJSON)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var c envoy.Cluster
|
|
|
|
|
|
|
|
if _, ok := jsonFields["@type"]; ok {
|
|
|
|
// Type field is present so decode it as a types.Any
|
|
|
|
var any types.Any
|
|
|
|
err := jsonpb.UnmarshalString(configJSON, &any)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// And then unmarshal the listener again...
|
|
|
|
err = proto.Unmarshal(any.Value, &c)
|
|
|
|
if err != nil {
|
2019-04-29 16:27:57 +00:00
|
|
|
return nil, err
|
2019-02-19 13:45:33 +00:00
|
|
|
}
|
|
|
|
return &c, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// No @type so try decoding as a straight listener.
|
|
|
|
err := jsonpb.UnmarshalString(configJSON, &c)
|
|
|
|
return &c, err
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
2019-06-18 00:52:01 +00:00
|
|
|
|
2020-04-27 22:25:37 +00:00
|
|
|
func (s *Server) makeGatewayCluster(cfgSnap *proxycfg.ConfigSnapshot, clusterName string) (*envoy.Cluster, error) {
|
|
|
|
return s.makeGatewayClusterWithConnectTimeout(cfgSnap, clusterName, 0)
|
2020-03-17 19:50:14 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 14:59:23 +00:00
|
|
|
// makeGatewayClusterWithConnectTimeout initializes a gateway cluster
|
2020-03-17 19:50:14 +00:00
|
|
|
// with the specified connect timeout. If the timeout is 0, the connect timeout
|
2020-04-14 14:59:23 +00:00
|
|
|
// defaults to use the configured gateway timeout.
|
2020-04-27 22:25:37 +00:00
|
|
|
func (s *Server) makeGatewayClusterWithConnectTimeout(cfgSnap *proxycfg.ConfigSnapshot,
|
|
|
|
clusterName string, connectTimeout time.Duration) (*envoy.Cluster, error) {
|
|
|
|
|
2020-03-26 16:20:56 +00:00
|
|
|
cfg, err := ParseGatewayConfig(cfgSnap.Proxy.Config)
|
2019-06-18 00:52:01 +00:00
|
|
|
if err != nil {
|
|
|
|
// Don't hard fail on a config typo, just warn. The parse func returns
|
|
|
|
// default config if there is an error so it's safe to continue.
|
2020-04-13 16:33:01 +00:00
|
|
|
s.Logger.Warn("failed to parse gateway config", "error", err)
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
|
|
|
|
2020-03-17 19:50:14 +00:00
|
|
|
if connectTimeout <= 0 {
|
|
|
|
connectTimeout = time.Duration(cfg.ConnectTimeoutMs) * time.Millisecond
|
|
|
|
}
|
|
|
|
|
2020-04-27 22:25:37 +00:00
|
|
|
cluster := envoy.Cluster{
|
2019-06-18 00:52:01 +00:00
|
|
|
Name: clusterName,
|
2020-03-17 19:50:14 +00:00
|
|
|
ConnectTimeout: connectTimeout,
|
2019-06-18 00:52:01 +00:00
|
|
|
ClusterDiscoveryType: &envoy.Cluster_Type{Type: envoy.Cluster_EDS},
|
|
|
|
EdsClusterConfig: &envoy.Cluster_EdsClusterConfig{
|
|
|
|
EdsConfig: &envoycore.ConfigSource{
|
|
|
|
ConfigSourceSpecifier: &envoycore.ConfigSource_Ads{
|
|
|
|
Ads: &envoycore.AggregatedConfigSource{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Having an empty config enables outlier detection with default config.
|
|
|
|
OutlierDetection: &envoycluster.OutlierDetection{},
|
2020-04-27 22:25:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &cluster, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// injectTerminatingGatewayTLSContext adds an UpstreamTlsContext to a cluster for TLS origination
|
|
|
|
func injectTerminatingGatewayTLSContext(cfgSnap *proxycfg.ConfigSnapshot, cluster *envoy.Cluster, service structs.ServiceID) {
|
|
|
|
if mapping, ok := cfgSnap.TerminatingGateway.GatewayServices[service]; ok && mapping.CAFile != "" {
|
|
|
|
cluster.TlsContext = &envoyauth.UpstreamTlsContext{
|
|
|
|
CommonTlsContext: makeCommonTLSContextFromFiles(mapping.CAFile, mapping.CertFile, mapping.KeyFile),
|
|
|
|
|
|
|
|
// TODO (gateways) (freddy) If mapping.SNI is empty, does Envoy behave any differently if TlsContext.Sni is excluded?
|
|
|
|
Sni: mapping.SNI,
|
|
|
|
}
|
|
|
|
}
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
2019-12-03 20:13:33 +00:00
|
|
|
|
|
|
|
func makeThresholdsIfNeeded(limits UpstreamLimits) []*envoycluster.CircuitBreakers_Thresholds {
|
|
|
|
var empty UpstreamLimits
|
|
|
|
// Make sure to not create any thresholds when passed the zero-value in order
|
|
|
|
// to rely on Envoy defaults
|
|
|
|
if limits == empty {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
threshold := &envoycluster.CircuitBreakers_Thresholds{}
|
|
|
|
// Likewise, make sure to not set any threshold values on the zero-value in
|
|
|
|
// order to rely on Envoy defaults
|
|
|
|
if limits.MaxConnections != nil {
|
|
|
|
threshold.MaxConnections = makeUint32Value(*limits.MaxConnections)
|
|
|
|
}
|
|
|
|
if limits.MaxPendingRequests != nil {
|
|
|
|
threshold.MaxPendingRequests = makeUint32Value(*limits.MaxPendingRequests)
|
|
|
|
}
|
|
|
|
if limits.MaxConcurrentRequests != nil {
|
|
|
|
threshold.MaxRequests = makeUint32Value(*limits.MaxConcurrentRequests)
|
|
|
|
}
|
|
|
|
|
|
|
|
return []*envoycluster.CircuitBreakers_Thresholds{threshold}
|
|
|
|
}
|