2018-10-03 18:18:55 +00:00
package xds
import (
"errors"
2019-07-02 03:10:51 +00:00
"fmt"
2020-07-07 15:43:04 +00:00
"net"
2019-07-24 01:56:39 +00:00
"strings"
2020-09-12 00:34:03 +00:00
"time"
2018-10-03 18:18:55 +00:00
2019-07-02 03:10:51 +00:00
envoy "github.com/envoyproxy/go-control-plane/envoy/api/v2"
envoyroute "github.com/envoyproxy/go-control-plane/envoy/api/v2/route"
2020-07-09 22:04:51 +00:00
envoymatcher "github.com/envoyproxy/go-control-plane/envoy/type/matcher"
2021-02-22 21:00:15 +00:00
2020-06-23 20:19:56 +00:00
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
2021-02-22 21:00:15 +00:00
2020-08-28 20:27:40 +00:00
"github.com/hashicorp/consul/agent/connect"
2018-10-03 18:18:55 +00:00
"github.com/hashicorp/consul/agent/proxycfg"
2019-07-02 03:10:51 +00:00
"github.com/hashicorp/consul/agent/structs"
2020-09-11 16:49:26 +00:00
"github.com/hashicorp/consul/logging"
2018-10-03 18:18:55 +00:00
)
2019-07-02 03:10:51 +00:00
// routesFromSnapshot returns the xDS API representation of the "routes" in the
// snapshot.
2020-09-03 22:19:58 +00:00
func ( s * Server ) routesFromSnapshot ( cInfo connectionInfo , cfgSnap * proxycfg . ConfigSnapshot ) ( [ ] proto . Message , error ) {
2018-10-03 18:18:55 +00:00
if cfgSnap == nil {
return nil , errors . New ( "nil config given" )
}
2019-07-02 03:10:51 +00:00
switch cfgSnap . Kind {
case structs . ServiceKindConnectProxy :
2020-08-28 20:27:40 +00:00
return routesForConnectProxy ( cInfo , cfgSnap . Proxy . Upstreams , cfgSnap . ConnectProxy . DiscoveryChain )
2020-04-03 19:41:10 +00:00
case structs . ServiceKindIngressGateway :
2020-08-28 20:27:40 +00:00
return routesForIngressGateway ( cInfo , cfgSnap . IngressGateway . Upstreams , cfgSnap . IngressGateway . DiscoveryChain )
case structs . ServiceKindTerminatingGateway :
2020-09-03 22:19:58 +00:00
return s . routesFromSnapshotTerminatingGateway ( cInfo , cfgSnap )
2019-07-02 03:10:51 +00:00
default :
return nil , fmt . Errorf ( "Invalid service kind: %v" , cfgSnap . Kind )
}
}
2020-09-11 16:49:26 +00:00
// routesFromSnapshotConnectProxy returns the xDS API representation of the
// "routes" in the snapshot.
func routesForConnectProxy (
cInfo connectionInfo ,
upstreams structs . Upstreams ,
chains map [ string ] * structs . CompiledDiscoveryChain ,
) ( [ ] proto . Message , error ) {
var resources [ ] proto . Message
for _ , u := range upstreams {
upstreamID := u . Identifier ( )
var chain * structs . CompiledDiscoveryChain
if u . DestinationType != structs . UpstreamDestTypePreparedQuery {
chain = chains [ upstreamID ]
}
if chain == nil || chain . IsDefault ( ) {
// TODO(rb): make this do the old school stuff too
} else {
virtualHost , err := makeUpstreamRouteForDiscoveryChain ( cInfo , upstreamID , chain , [ ] string { "*" } )
if err != nil {
return nil , err
}
route := & envoy . RouteConfiguration {
Name : upstreamID ,
VirtualHosts : [ ] * envoyroute . VirtualHost { virtualHost } ,
// ValidateClusters defaults to true when defined statically and false
// when done via RDS. Re-set the sane value of true to prevent
// null-routing traffic.
ValidateClusters : makeBoolValue ( true ) ,
}
resources = append ( resources , route )
}
}
// TODO(rb): make sure we don't generate an empty result
return resources , nil
}
2020-08-28 20:27:40 +00:00
// routesFromSnapshotTerminatingGateway returns the xDS API representation of the "routes" in the snapshot.
// For any HTTP service we will return a default route.
2020-09-03 22:19:58 +00:00
func ( s * Server ) routesFromSnapshotTerminatingGateway ( _ connectionInfo , cfgSnap * proxycfg . ConfigSnapshot ) ( [ ] proto . Message , error ) {
2019-07-02 03:10:51 +00:00
if cfgSnap == nil {
return nil , errors . New ( "nil config given" )
}
2020-09-03 22:19:58 +00:00
logger := s . Logger . Named ( logging . TerminatingGateway )
2019-07-02 03:10:51 +00:00
var resources [ ] proto . Message
2020-09-02 15:10:50 +00:00
for _ , svc := range cfgSnap . TerminatingGateway . ValidServices ( ) {
2020-08-28 20:27:40 +00:00
clusterName := connect . ServiceSNI ( svc . Name , "" , svc . NamespaceOrDefault ( ) , cfgSnap . Datacenter , cfgSnap . Roots . TrustDomain )
resolver , hasResolver := cfgSnap . TerminatingGateway . ServiceResolvers [ svc ]
svcConfig := cfgSnap . TerminatingGateway . ServiceConfigs [ svc ]
cfg , err := ParseProxyConfig ( svcConfig . ProxyConfig )
2020-09-03 16:21:20 +00:00
if err != nil {
return nil , fmt . Errorf ( "failed to parse upstream config: %v" , err )
}
if ! structs . IsProtocolHTTPLike ( cfg . Protocol ) {
2020-08-28 20:27:40 +00:00
// Routes can only be defined for HTTP services
continue
}
if ! hasResolver {
// Use a zero value resolver with no timeout and no subsets
resolver = & structs . ServiceResolverConfigEntry { }
}
2020-09-02 15:10:50 +00:00
2020-09-11 15:21:43 +00:00
var lb * structs . LoadBalancer
2020-09-02 15:10:50 +00:00
if resolver . LoadBalancer != nil {
2020-09-11 15:21:43 +00:00
lb = resolver . LoadBalancer
2020-09-02 15:10:50 +00:00
}
route , err := makeNamedDefaultRouteWithLB ( clusterName , lb )
2020-08-28 20:27:40 +00:00
if err != nil {
2020-09-03 22:19:58 +00:00
logger . Error ( "failed to make route" , "cluster" , clusterName , "error" , err )
2020-08-28 20:27:40 +00:00
continue
}
resources = append ( resources , route )
// If there is a service-resolver for this service then also setup routes for each subset
for name := range resolver . Subsets {
clusterName = connect . ServiceSNI ( svc . Name , name , svc . NamespaceOrDefault ( ) , cfgSnap . Datacenter , cfgSnap . Roots . TrustDomain )
2020-09-02 15:10:50 +00:00
route , err := makeNamedDefaultRouteWithLB ( clusterName , lb )
2020-08-28 20:27:40 +00:00
if err != nil {
2020-09-03 22:19:58 +00:00
logger . Error ( "failed to make route" , "cluster" , clusterName , "error" , err )
2020-08-28 20:27:40 +00:00
continue
}
resources = append ( resources , route )
}
}
return resources , nil
}
2020-09-11 15:21:43 +00:00
func makeNamedDefaultRouteWithLB ( clusterName string , lb * structs . LoadBalancer ) ( * envoy . RouteConfiguration , error ) {
2020-08-28 20:27:40 +00:00
action := makeRouteActionFromName ( clusterName )
2020-09-02 15:10:50 +00:00
2020-09-02 21:13:50 +00:00
if err := injectLBToRouteAction ( lb , action . Route ) ; err != nil {
2020-08-28 20:27:40 +00:00
return nil , fmt . Errorf ( "failed to apply load balancer configuration to route action: %v" , err )
}
return & envoy . RouteConfiguration {
Name : clusterName ,
VirtualHosts : [ ] * envoyroute . VirtualHost {
{
Name : clusterName ,
Domains : [ ] string { "*" } ,
Routes : [ ] * envoyroute . Route {
{
Match : makeDefaultRouteMatch ( ) ,
Action : action ,
} ,
} ,
} ,
} ,
// ValidateClusters defaults to true when defined statically and false
// when done via RDS. Re-set the sane value of true to prevent
// null-routing traffic.
ValidateClusters : makeBoolValue ( true ) ,
} , nil
}
// routesForIngressGateway returns the xDS API representation of the
2020-04-16 23:24:11 +00:00
// "routes" in the snapshot.
2020-08-28 20:27:40 +00:00
func routesForIngressGateway (
cInfo connectionInfo ,
upstreams map [ proxycfg . IngressListenerKey ] structs . Upstreams ,
chains map [ string ] * structs . CompiledDiscoveryChain ,
) ( [ ] proto . Message , error ) {
2020-04-16 23:24:11 +00:00
var result [ ] proto . Message
2020-08-28 20:27:40 +00:00
for listenerKey , upstreams := range upstreams {
2020-04-16 23:24:11 +00:00
// Do not create any route configuration for TCP listeners
if listenerKey . Protocol == "tcp" {
continue
}
upstreamRoute := & envoy . RouteConfiguration {
Name : listenerKey . RouteName ( ) ,
// ValidateClusters defaults to true when defined statically and false
// when done via RDS. Re-set the sane value of true to prevent
// null-routing traffic.
ValidateClusters : makeBoolValue ( true ) ,
}
for _ , u := range upstreams {
upstreamID := u . Identifier ( )
2020-08-28 20:27:40 +00:00
chain := chains [ upstreamID ]
2020-04-23 15:06:19 +00:00
if chain == nil {
continue
}
2020-07-07 15:43:04 +00:00
domains := generateUpstreamIngressDomains ( listenerKey , u )
2020-07-09 22:04:51 +00:00
virtualHost , err := makeUpstreamRouteForDiscoveryChain ( cInfo , upstreamID , chain , domains )
2020-04-23 15:06:19 +00:00
if err != nil {
return nil , err
2020-04-16 23:24:11 +00:00
}
2020-04-23 15:06:19 +00:00
upstreamRoute . VirtualHosts = append ( upstreamRoute . VirtualHosts , virtualHost )
2020-04-16 23:24:11 +00:00
}
result = append ( result , upstreamRoute )
}
return result , nil
}
2020-07-07 15:43:04 +00:00
func generateUpstreamIngressDomains ( listenerKey proxycfg . IngressListenerKey , u structs . Upstream ) [ ] string {
var domains [ ] string
domainsSet := make ( map [ string ] bool )
namespace := u . GetEnterpriseMeta ( ) . NamespaceOrDefault ( )
switch {
case len ( u . IngressHosts ) > 0 :
// If a user has specified hosts, do not add the default
// "<service-name>.ingress.*" prefixes
domains = u . IngressHosts
case namespace != structs . IntentionDefaultNamespace :
domains = [ ] string { fmt . Sprintf ( "%s.ingress.%s.*" , u . DestinationName , namespace ) }
default :
domains = [ ] string { fmt . Sprintf ( "%s.ingress.*" , u . DestinationName ) }
}
for _ , h := range domains {
domainsSet [ h ] = true
}
// Host headers may contain port numbers in them, so we need to make sure
// we match on the host with and without the port number. Well-known
// ports like HTTP/HTTPS are stripped from Host headers, but other ports
// will be in the header.
for _ , h := range domains {
_ , _ , err := net . SplitHostPort ( h )
// Error message from Go's net/ipsock.go
// We check to see if a port is not missing, and ignore the
// error from SplitHostPort otherwise, since we have previously
// validated the Host values and should trust the user's input.
if err == nil || ! strings . Contains ( err . Error ( ) , "missing port in address" ) {
continue
}
domainWithPort := fmt . Sprintf ( "%s:%d" , h , listenerKey . Port )
// Do not add a duplicate domain if a hostname with port is already in the
// set
if ! domainsSet [ domainWithPort ] {
domains = append ( domains , domainWithPort )
}
}
return domains
}
2019-07-02 03:10:51 +00:00
func makeUpstreamRouteForDiscoveryChain (
2020-07-09 22:04:51 +00:00
cInfo connectionInfo ,
2020-04-16 23:24:11 +00:00
routeName string ,
2019-07-02 03:10:51 +00:00
chain * structs . CompiledDiscoveryChain ,
2020-04-23 15:06:19 +00:00
serviceDomains [ ] string ,
2020-06-23 20:19:56 +00:00
) ( * envoyroute . VirtualHost , error ) {
var routes [ ] * envoyroute . Route
2019-07-02 03:10:51 +00:00
2019-08-02 03:44:05 +00:00
startNode := chain . Nodes [ chain . StartNode ]
if startNode == nil {
2020-08-12 16:19:20 +00:00
return nil , fmt . Errorf ( "missing first node in compiled discovery chain for: %s" , chain . ServiceName )
2019-08-02 03:44:05 +00:00
}
switch startNode . Type {
2019-07-02 03:10:51 +00:00
case structs . DiscoveryGraphNodeTypeRouter :
2020-06-23 20:19:56 +00:00
routes = make ( [ ] * envoyroute . Route , 0 , len ( startNode . Routes ) )
2019-07-02 03:10:51 +00:00
2019-08-02 03:44:05 +00:00
for _ , discoveryRoute := range startNode . Routes {
2020-07-09 22:04:51 +00:00
routeMatch := makeRouteMatchForDiscoveryRoute ( cInfo , discoveryRoute )
2019-07-02 03:10:51 +00:00
var (
routeAction * envoyroute . Route_Route
err error
)
2019-08-02 03:44:05 +00:00
nextNode := chain . Nodes [ discoveryRoute . NextNode ]
2020-09-11 16:49:26 +00:00
var lb * structs . LoadBalancer
2020-09-02 15:10:50 +00:00
if nextNode . LoadBalancer != nil {
2020-09-11 15:21:43 +00:00
lb = nextNode . LoadBalancer
2020-09-02 15:10:50 +00:00
}
2019-08-02 03:44:05 +00:00
switch nextNode . Type {
case structs . DiscoveryGraphNodeTypeSplitter :
2020-04-03 19:41:10 +00:00
routeAction , err = makeRouteActionForSplitter ( nextNode . Splits , chain )
2019-07-02 03:10:51 +00:00
if err != nil {
2020-06-23 20:19:56 +00:00
return nil , err
2019-07-02 03:10:51 +00:00
}
2020-09-02 21:13:50 +00:00
if err := injectLBToRouteAction ( lb , routeAction . Route ) ; err != nil {
2020-08-28 20:27:40 +00:00
return nil , fmt . Errorf ( "failed to apply load balancer configuration to route action: %v" , err )
}
2019-08-02 03:44:05 +00:00
case structs . DiscoveryGraphNodeTypeResolver :
2020-08-28 20:27:40 +00:00
routeAction = makeRouteActionForChainCluster ( nextNode . Resolver . Target , chain )
2020-09-02 21:13:50 +00:00
if err := injectLBToRouteAction ( lb , routeAction . Route ) ; err != nil {
2020-08-28 20:27:40 +00:00
return nil , fmt . Errorf ( "failed to apply load balancer configuration to route action: %v" , err )
}
2019-07-02 03:10:51 +00:00
2019-08-02 03:44:05 +00:00
default :
2020-06-23 20:19:56 +00:00
return nil , fmt . Errorf ( "unexpected graph node after route %q" , nextNode . Type )
2019-07-02 03:10:51 +00:00
}
2019-07-12 19:16:21 +00:00
// TODO(rb): Better help handle the envoy case where you need (prefix=/foo/,rewrite=/) and (exact=/foo,rewrite=/) to do a full rewrite
destination := discoveryRoute . Definition . Destination
if destination != nil {
if destination . PrefixRewrite != "" {
routeAction . Route . PrefixRewrite = destination . PrefixRewrite
}
if destination . RequestTimeout > 0 {
2020-06-23 20:19:56 +00:00
routeAction . Route . Timeout = ptypes . DurationProto ( destination . RequestTimeout )
2019-07-12 19:16:21 +00:00
}
if destination . HasRetryFeatures ( ) {
retryPolicy := & envoyroute . RetryPolicy { }
if destination . NumRetries > 0 {
retryPolicy . NumRetries = makeUint32Value ( int ( destination . NumRetries ) )
}
// The RetryOn magic values come from: https://www.envoyproxy.io/docs/envoy/v1.10.0/configuration/http_filters/router_filter#config-http-filters-router-x-envoy-retry-on
if destination . RetryOnConnectFailure {
retryPolicy . RetryOn = "connect-failure"
}
if len ( destination . RetryOnStatusCodes ) > 0 {
if retryPolicy . RetryOn != "" {
2020-02-18 15:25:47 +00:00
retryPolicy . RetryOn = retryPolicy . RetryOn + ",retriable-status-codes"
2019-07-12 19:16:21 +00:00
} else {
retryPolicy . RetryOn = "retriable-status-codes"
}
retryPolicy . RetriableStatusCodes = destination . RetryOnStatusCodes
}
routeAction . Route . RetryPolicy = retryPolicy
}
}
2020-06-23 20:19:56 +00:00
routes = append ( routes , & envoyroute . Route {
2019-07-02 03:10:51 +00:00
Match : routeMatch ,
Action : routeAction ,
} )
}
case structs . DiscoveryGraphNodeTypeSplitter :
2020-04-03 19:41:10 +00:00
routeAction , err := makeRouteActionForSplitter ( startNode . Splits , chain )
2019-07-02 03:10:51 +00:00
if err != nil {
2020-06-23 20:19:56 +00:00
return nil , err
2019-07-02 03:10:51 +00:00
}
2020-09-11 16:49:26 +00:00
var lb * structs . LoadBalancer
2020-09-02 15:10:50 +00:00
if startNode . LoadBalancer != nil {
2020-09-11 15:21:43 +00:00
lb = startNode . LoadBalancer
2020-09-02 15:10:50 +00:00
}
2020-09-02 21:13:50 +00:00
if err := injectLBToRouteAction ( lb , routeAction . Route ) ; err != nil {
2020-08-28 20:27:40 +00:00
return nil , fmt . Errorf ( "failed to apply load balancer configuration to route action: %v" , err )
}
2020-06-23 20:19:56 +00:00
defaultRoute := & envoyroute . Route {
2019-07-02 03:10:51 +00:00
Match : makeDefaultRouteMatch ( ) ,
Action : routeAction ,
}
2020-06-23 20:19:56 +00:00
routes = [ ] * envoyroute . Route { defaultRoute }
2019-07-02 03:10:51 +00:00
2019-08-02 03:44:05 +00:00
case structs . DiscoveryGraphNodeTypeResolver :
2020-08-28 20:27:40 +00:00
routeAction := makeRouteActionForChainCluster ( startNode . Resolver . Target , chain )
2020-09-11 16:49:26 +00:00
var lb * structs . LoadBalancer
2020-09-02 15:10:50 +00:00
if startNode . LoadBalancer != nil {
2020-09-11 15:21:43 +00:00
lb = startNode . LoadBalancer
2020-09-02 15:10:50 +00:00
}
2020-09-02 21:13:50 +00:00
if err := injectLBToRouteAction ( lb , routeAction . Route ) ; err != nil {
2020-08-28 20:27:40 +00:00
return nil , fmt . Errorf ( "failed to apply load balancer configuration to route action: %v" , err )
}
2019-07-02 03:10:51 +00:00
2020-06-23 20:19:56 +00:00
defaultRoute := & envoyroute . Route {
2019-07-02 03:10:51 +00:00
Match : makeDefaultRouteMatch ( ) ,
Action : routeAction ,
}
2020-06-23 20:19:56 +00:00
routes = [ ] * envoyroute . Route { defaultRoute }
2019-07-02 03:10:51 +00:00
default :
2020-08-12 16:19:20 +00:00
return nil , fmt . Errorf ( "unknown first node in discovery chain of type: %s" , startNode . Type )
2019-07-02 03:10:51 +00:00
}
2020-06-23 20:19:56 +00:00
host := & envoyroute . VirtualHost {
2020-04-16 23:24:11 +00:00
Name : routeName ,
2020-04-23 15:06:19 +00:00
Domains : serviceDomains ,
2020-04-16 23:24:11 +00:00
Routes : routes ,
}
return host , nil
2019-07-02 03:10:51 +00:00
}
2020-07-31 20:52:49 +00:00
func makeRouteMatchForDiscoveryRoute ( _ connectionInfo , discoveryRoute * structs . DiscoveryRoute ) * envoyroute . RouteMatch {
2019-07-02 03:10:51 +00:00
match := discoveryRoute . Definition . Match
if match == nil || match . IsEmpty ( ) {
return makeDefaultRouteMatch ( )
}
2020-06-23 20:19:56 +00:00
em := & envoyroute . RouteMatch { }
2019-07-02 03:10:51 +00:00
switch {
case match . HTTP . PathExact != "" :
2019-07-12 19:16:21 +00:00
em . PathSpecifier = & envoyroute . RouteMatch_Path {
Path : match . HTTP . PathExact ,
}
2019-07-02 03:10:51 +00:00
case match . HTTP . PathPrefix != "" :
2019-07-12 19:16:21 +00:00
em . PathSpecifier = & envoyroute . RouteMatch_Prefix {
Prefix : match . HTTP . PathPrefix ,
}
2019-07-02 03:10:51 +00:00
case match . HTTP . PathRegex != "" :
2020-07-31 20:52:49 +00:00
em . PathSpecifier = & envoyroute . RouteMatch_SafeRegex {
SafeRegex : makeEnvoyRegexMatch ( match . HTTP . PathRegex ) ,
2019-07-12 19:16:21 +00:00
}
2019-07-02 03:10:51 +00:00
default :
2019-07-12 19:16:21 +00:00
em . PathSpecifier = & envoyroute . RouteMatch_Prefix {
Prefix : "/" ,
}
2019-07-02 03:10:51 +00:00
}
if len ( match . HTTP . Header ) > 0 {
em . Headers = make ( [ ] * envoyroute . HeaderMatcher , 0 , len ( match . HTTP . Header ) )
for _ , hdr := range match . HTTP . Header {
eh := & envoyroute . HeaderMatcher {
Name : hdr . Name ,
}
switch {
case hdr . Exact != "" :
eh . HeaderMatchSpecifier = & envoyroute . HeaderMatcher_ExactMatch {
ExactMatch : hdr . Exact ,
}
case hdr . Regex != "" :
2020-07-31 20:52:49 +00:00
eh . HeaderMatchSpecifier = & envoyroute . HeaderMatcher_SafeRegexMatch {
SafeRegexMatch : makeEnvoyRegexMatch ( hdr . Regex ) ,
2019-07-02 03:10:51 +00:00
}
case hdr . Prefix != "" :
eh . HeaderMatchSpecifier = & envoyroute . HeaderMatcher_PrefixMatch {
PrefixMatch : hdr . Prefix ,
}
case hdr . Suffix != "" :
eh . HeaderMatchSpecifier = & envoyroute . HeaderMatcher_SuffixMatch {
SuffixMatch : hdr . Suffix ,
}
case hdr . Present :
eh . HeaderMatchSpecifier = & envoyroute . HeaderMatcher_PresentMatch {
PresentMatch : true ,
}
default :
continue // skip this impossible situation
}
if hdr . Invert {
eh . InvertMatch = true
}
em . Headers = append ( em . Headers , eh )
}
}
2019-07-24 01:56:39 +00:00
if len ( match . HTTP . Methods ) > 0 {
methodHeaderRegex := strings . Join ( match . HTTP . Methods , "|" )
eh := & envoyroute . HeaderMatcher {
Name : ":method" ,
2020-07-31 20:52:49 +00:00
HeaderMatchSpecifier : & envoyroute . HeaderMatcher_SafeRegexMatch {
2020-07-09 22:04:51 +00:00
SafeRegexMatch : makeEnvoyRegexMatch ( methodHeaderRegex ) ,
2020-07-31 20:52:49 +00:00
} ,
2019-07-24 01:56:39 +00:00
}
2020-07-09 22:04:51 +00:00
2019-07-24 01:56:39 +00:00
em . Headers = append ( em . Headers , eh )
}
2019-07-02 03:10:51 +00:00
if len ( match . HTTP . QueryParam ) > 0 {
em . QueryParameters = make ( [ ] * envoyroute . QueryParameterMatcher , 0 , len ( match . HTTP . QueryParam ) )
for _ , qm := range match . HTTP . QueryParam {
eq := & envoyroute . QueryParameterMatcher {
2019-07-24 01:55:26 +00:00
Name : qm . Name ,
}
switch {
case qm . Exact != "" :
2020-07-31 20:52:49 +00:00
eq . QueryParameterMatchSpecifier = & envoyroute . QueryParameterMatcher_StringMatch {
StringMatch : & envoymatcher . StringMatcher {
MatchPattern : & envoymatcher . StringMatcher_Exact {
Exact : qm . Exact ,
2020-07-09 22:04:51 +00:00
} ,
2020-07-31 20:52:49 +00:00
} ,
2020-07-09 22:04:51 +00:00
}
2019-07-24 01:55:26 +00:00
case qm . Regex != "" :
2020-07-31 20:52:49 +00:00
eq . QueryParameterMatchSpecifier = & envoyroute . QueryParameterMatcher_StringMatch {
StringMatch : & envoymatcher . StringMatcher {
MatchPattern : & envoymatcher . StringMatcher_SafeRegex {
SafeRegex : makeEnvoyRegexMatch ( qm . Regex ) ,
2020-07-09 22:04:51 +00:00
} ,
2020-07-31 20:52:49 +00:00
} ,
2020-07-09 22:04:51 +00:00
}
2019-07-24 01:55:26 +00:00
case qm . Present :
2020-07-31 20:52:49 +00:00
eq . QueryParameterMatchSpecifier = & envoyroute . QueryParameterMatcher_PresentMatch {
PresentMatch : true ,
2020-07-09 22:04:51 +00:00
}
2019-07-24 01:55:26 +00:00
default :
continue // skip this impossible situation
2019-07-02 03:10:51 +00:00
}
em . QueryParameters = append ( em . QueryParameters , eq )
}
}
return em
}
2020-06-23 20:19:56 +00:00
func makeDefaultRouteMatch ( ) * envoyroute . RouteMatch {
return & envoyroute . RouteMatch {
2019-07-02 03:10:51 +00:00
PathSpecifier : & envoyroute . RouteMatch_Prefix {
Prefix : "/" ,
} ,
// TODO(banks) Envoy supports matching only valid GRPC
// requests which might be nice to add here for gRPC services
// but it's not supported in our current envoy SDK version
// although docs say it was supported by 1.8.0. Going to defer
// that until we've updated the deps.
}
}
2020-08-28 20:27:40 +00:00
func makeRouteActionForChainCluster ( targetID string , chain * structs . CompiledDiscoveryChain ) * envoyroute . Route_Route {
2019-08-02 20:34:54 +00:00
target := chain . Targets [ targetID ]
2020-08-28 20:27:40 +00:00
return makeRouteActionFromName ( CustomizeClusterName ( target . Name , chain ) )
}
2019-08-02 20:34:54 +00:00
2020-08-28 20:27:40 +00:00
func makeRouteActionFromName ( clusterName string ) * envoyroute . Route_Route {
2019-07-02 03:10:51 +00:00
return & envoyroute . Route_Route {
Route : & envoyroute . RouteAction {
ClusterSpecifier : & envoyroute . RouteAction_Cluster {
Cluster : clusterName ,
} ,
} ,
}
}
2020-04-03 19:41:10 +00:00
func makeRouteActionForSplitter ( splits [ ] * structs . DiscoverySplit , chain * structs . CompiledDiscoveryChain ) ( * envoyroute . Route_Route , error ) {
2019-07-02 03:10:51 +00:00
clusters := make ( [ ] * envoyroute . WeightedCluster_ClusterWeight , 0 , len ( splits ) )
for _ , split := range splits {
2019-08-02 03:44:05 +00:00
nextNode := chain . Nodes [ split . NextNode ]
if nextNode . Type != structs . DiscoveryGraphNodeTypeResolver {
return nil , fmt . Errorf ( "unexpected splitter destination node type: %s" , nextNode . Type )
2019-07-02 03:10:51 +00:00
}
2019-08-02 20:34:54 +00:00
targetID := nextNode . Resolver . Target
target := chain . Targets [ targetID ]
2019-08-02 03:03:34 +00:00
2019-08-19 18:03:03 +00:00
clusterName := CustomizeClusterName ( target . Name , chain )
2019-07-02 03:10:51 +00:00
2019-07-12 19:16:21 +00:00
// The smallest representable weight is 1/10000 or .01% but envoy
// deals with integers so scale everything up by 100x.
2019-07-02 03:10:51 +00:00
cw := & envoyroute . WeightedCluster_ClusterWeight {
2019-07-12 19:16:21 +00:00
Weight : makeUint32Value ( int ( split . Weight * 100 ) ) ,
2019-07-02 03:10:51 +00:00
Name : clusterName ,
}
clusters = append ( clusters , cw )
}
return & envoyroute . Route_Route {
Route : & envoyroute . RouteAction {
ClusterSpecifier : & envoyroute . RouteAction_WeightedClusters {
WeightedClusters : & envoyroute . WeightedCluster {
Clusters : clusters ,
2019-07-12 19:16:21 +00:00
TotalWeight : makeUint32Value ( 10000 ) , // scaled up 100%
2019-07-02 03:10:51 +00:00
} ,
} ,
} ,
} , nil
2018-10-03 18:18:55 +00:00
}
2020-09-02 21:13:50 +00:00
2020-09-11 15:21:43 +00:00
func injectLBToRouteAction ( lb * structs . LoadBalancer , action * envoyroute . RouteAction ) error {
if lb == nil || ! lb . IsHashBased ( ) {
2020-09-02 21:13:50 +00:00
return nil
}
2020-09-11 15:21:43 +00:00
result := make ( [ ] * envoyroute . RouteAction_HashPolicy , 0 , len ( lb . HashPolicies ) )
for _ , policy := range lb . HashPolicies {
2020-09-02 21:13:50 +00:00
if policy . SourceIP {
result = append ( result , & envoyroute . RouteAction_HashPolicy {
PolicySpecifier : & envoyroute . RouteAction_HashPolicy_ConnectionProperties_ {
ConnectionProperties : & envoyroute . RouteAction_HashPolicy_ConnectionProperties {
SourceIp : true ,
} ,
} ,
Terminal : policy . Terminal ,
} )
continue
}
switch policy . Field {
case structs . HashPolicyHeader :
result = append ( result , & envoyroute . RouteAction_HashPolicy {
PolicySpecifier : & envoyroute . RouteAction_HashPolicy_Header_ {
Header : & envoyroute . RouteAction_HashPolicy_Header {
HeaderName : policy . FieldValue ,
} ,
} ,
Terminal : policy . Terminal ,
} )
case structs . HashPolicyCookie :
cookie := envoyroute . RouteAction_HashPolicy_Cookie {
Name : policy . FieldValue ,
}
if policy . CookieConfig != nil {
cookie . Path = policy . CookieConfig . Path
2020-09-12 00:34:03 +00:00
if policy . CookieConfig . TTL != 0 * time . Second {
cookie . Ttl = ptypes . DurationProto ( policy . CookieConfig . TTL )
}
// Envoy will generate a session cookie if the ttl is present and zero.
if policy . CookieConfig . Session {
cookie . Ttl = ptypes . DurationProto ( 0 * time . Second )
}
2020-09-02 21:13:50 +00:00
}
result = append ( result , & envoyroute . RouteAction_HashPolicy {
PolicySpecifier : & envoyroute . RouteAction_HashPolicy_Cookie_ {
Cookie : & cookie ,
} ,
Terminal : policy . Terminal ,
} )
case structs . HashPolicyQueryParam :
result = append ( result , & envoyroute . RouteAction_HashPolicy {
PolicySpecifier : & envoyroute . RouteAction_HashPolicy_QueryParameter_ {
QueryParameter : & envoyroute . RouteAction_HashPolicy_QueryParameter {
Name : policy . FieldValue ,
} ,
} ,
Terminal : policy . Terminal ,
} )
default :
return fmt . Errorf ( "unsupported load balancer hash policy field: %v" , policy . Field )
}
}
action . HashPolicy = result
return nil
}