2019-06-27 18:38:21 +00:00
|
|
|
package discoverychain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-08-02 03:03:34 +00:00
|
|
|
"github.com/mitchellh/hashstructure"
|
2019-07-02 16:01:17 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2020-12-11 21:10:00 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2019-06-27 18:38:21 +00:00
|
|
|
)
|
|
|
|
|
2019-07-01 20:23:36 +00:00
|
|
|
type CompileRequest struct {
|
2019-08-19 18:03:03 +00:00
|
|
|
ServiceName string
|
|
|
|
EvaluateInNamespace string
|
2021-08-20 16:57:45 +00:00
|
|
|
EvaluateInPartition string
|
2019-08-19 18:03:03 +00:00
|
|
|
EvaluateInDatacenter string
|
|
|
|
EvaluateInTrustDomain string
|
2019-08-02 03:03:34 +00:00
|
|
|
|
|
|
|
// OverrideMeshGateway allows for the setting to be overridden for any
|
|
|
|
// resolver in the compiled chain.
|
|
|
|
OverrideMeshGateway structs.MeshGatewayConfig
|
|
|
|
|
|
|
|
// OverrideProtocol allows for the final protocol for the chain to be
|
|
|
|
// altered.
|
|
|
|
//
|
|
|
|
// - If the chain ordinarily would be TCP and an L7 protocol is passed here
|
|
|
|
// the chain will not include Routers or Splitters.
|
|
|
|
//
|
|
|
|
// - If the chain ordinarily would be L7 and TCP is passed here the chain
|
|
|
|
// will not include Routers or Splitters.
|
|
|
|
OverrideProtocol string
|
|
|
|
|
|
|
|
// OverrideConnectTimeout allows for the ConnectTimeout setting to be
|
|
|
|
// overridden for any resolver in the compiled chain.
|
|
|
|
OverrideConnectTimeout time.Duration
|
|
|
|
|
2019-08-02 03:44:05 +00:00
|
|
|
Entries *structs.DiscoveryChainConfigEntries
|
2019-07-01 20:23:36 +00:00
|
|
|
}
|
2019-06-27 18:38:21 +00:00
|
|
|
|
|
|
|
// Compile assembles a discovery chain in the form of a graph of nodes using
|
|
|
|
// raw config entries and local context.
|
|
|
|
//
|
|
|
|
// "Node" referenced in this file refers to a node in a graph and not to the
|
|
|
|
// Consul construct called a "Node".
|
|
|
|
//
|
|
|
|
// Omitting router and splitter entries for services not using an L7 protocol
|
|
|
|
// (like HTTP) happens during initial fetching, but for sanity purposes a quick
|
|
|
|
// reinforcement of that happens here, too.
|
2019-07-01 20:23:36 +00:00
|
|
|
//
|
|
|
|
// May return a *structs.ConfigEntryGraphError, but that is only expected when
|
|
|
|
// being used to validate modifications to the config entry graph. It should
|
|
|
|
// not be expected when compiling existing entries at runtime that are already
|
|
|
|
// valid.
|
|
|
|
func Compile(req CompileRequest) (*structs.CompiledDiscoveryChain, error) {
|
|
|
|
var (
|
2019-08-19 18:03:03 +00:00
|
|
|
serviceName = req.ServiceName
|
|
|
|
evaluateInNamespace = req.EvaluateInNamespace
|
2021-09-07 20:29:32 +00:00
|
|
|
evaluateInPartition = req.EvaluateInPartition
|
2019-08-19 18:03:03 +00:00
|
|
|
evaluateInDatacenter = req.EvaluateInDatacenter
|
|
|
|
evaluateInTrustDomain = req.EvaluateInTrustDomain
|
|
|
|
entries = req.Entries
|
2019-07-01 20:23:36 +00:00
|
|
|
)
|
2019-06-27 18:38:21 +00:00
|
|
|
if serviceName == "" {
|
|
|
|
return nil, fmt.Errorf("serviceName is required")
|
|
|
|
}
|
2019-08-05 18:30:35 +00:00
|
|
|
if evaluateInNamespace == "" {
|
|
|
|
return nil, fmt.Errorf("evaluateInNamespace is required")
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
2021-09-07 20:29:32 +00:00
|
|
|
if evaluateInPartition == "" {
|
|
|
|
return nil, fmt.Errorf("evaluateInPartition is required")
|
|
|
|
}
|
2019-08-05 18:30:35 +00:00
|
|
|
if evaluateInDatacenter == "" {
|
|
|
|
return nil, fmt.Errorf("evaluateInDatacenter is required")
|
|
|
|
}
|
2019-08-19 18:03:03 +00:00
|
|
|
if evaluateInTrustDomain == "" {
|
|
|
|
return nil, fmt.Errorf("evaluateInTrustDomain is required")
|
|
|
|
}
|
2019-06-27 18:38:21 +00:00
|
|
|
if entries == nil {
|
|
|
|
return nil, fmt.Errorf("entries is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &compiler{
|
2019-08-02 03:03:34 +00:00
|
|
|
serviceName: serviceName,
|
2021-09-07 20:29:32 +00:00
|
|
|
evaluateInPartition: evaluateInPartition,
|
2019-08-05 18:30:35 +00:00
|
|
|
evaluateInNamespace: evaluateInNamespace,
|
|
|
|
evaluateInDatacenter: evaluateInDatacenter,
|
2019-08-19 18:03:03 +00:00
|
|
|
evaluateInTrustDomain: evaluateInTrustDomain,
|
2019-08-02 03:03:34 +00:00
|
|
|
overrideMeshGateway: req.OverrideMeshGateway,
|
|
|
|
overrideProtocol: req.OverrideProtocol,
|
|
|
|
overrideConnectTimeout: req.OverrideConnectTimeout,
|
|
|
|
entries: entries,
|
2019-06-27 18:38:21 +00:00
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
resolvers: make(map[structs.ServiceID]*structs.ServiceResolverConfigEntry),
|
2019-08-02 03:44:05 +00:00
|
|
|
splitterNodes: make(map[string]*structs.DiscoveryGraphNode),
|
2019-08-02 20:34:54 +00:00
|
|
|
resolveNodes: make(map[string]*structs.DiscoveryGraphNode),
|
2019-06-27 18:38:21 +00:00
|
|
|
|
2019-08-02 20:34:54 +00:00
|
|
|
nodes: make(map[string]*structs.DiscoveryGraphNode),
|
|
|
|
loadedTargets: make(map[string]*structs.DiscoveryTarget),
|
|
|
|
retainedTargets: make(map[string]struct{}),
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 03:03:34 +00:00
|
|
|
if req.OverrideProtocol != "" {
|
|
|
|
c.disableAdvancedRoutingFeatures = !enableAdvancedRoutingForProtocol(req.OverrideProtocol)
|
|
|
|
}
|
|
|
|
|
2019-06-27 18:38:21 +00:00
|
|
|
// Clone this resolver map to avoid mutating the input map during compilation.
|
|
|
|
if len(entries.Resolvers) > 0 {
|
|
|
|
for k, v := range entries.Resolvers {
|
|
|
|
c.resolvers[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.compile()
|
|
|
|
}
|
|
|
|
|
|
|
|
// compiler is a single-use struct for handling intermediate state necessary
|
|
|
|
// for assembling a discovery chain from raw config entries.
|
|
|
|
type compiler struct {
|
2019-08-02 03:03:34 +00:00
|
|
|
serviceName string
|
2019-08-05 18:30:35 +00:00
|
|
|
evaluateInNamespace string
|
2021-09-07 20:29:32 +00:00
|
|
|
evaluateInPartition string
|
2019-08-05 18:30:35 +00:00
|
|
|
evaluateInDatacenter string
|
2019-08-19 18:03:03 +00:00
|
|
|
evaluateInTrustDomain string
|
2019-08-02 03:03:34 +00:00
|
|
|
overrideMeshGateway structs.MeshGatewayConfig
|
|
|
|
overrideProtocol string
|
|
|
|
overrideConnectTimeout time.Duration
|
2019-06-27 18:38:21 +00:00
|
|
|
|
|
|
|
// config entries that are being compiled (will be mutated during compilation)
|
|
|
|
//
|
|
|
|
// This is an INPUT field.
|
|
|
|
entries *structs.DiscoveryChainConfigEntries
|
|
|
|
|
2019-08-02 03:44:05 +00:00
|
|
|
// resolvers is initially seeded by copying the provided entries.Resolvers
|
|
|
|
// map and default resolvers are added as they are needed.
|
2020-01-24 15:04:58 +00:00
|
|
|
resolvers map[structs.ServiceID]*structs.ServiceResolverConfigEntry
|
2019-08-02 03:44:05 +00:00
|
|
|
|
2019-06-27 18:38:21 +00:00
|
|
|
// cached nodes
|
2019-08-02 03:44:05 +00:00
|
|
|
splitterNodes map[string]*structs.DiscoveryGraphNode
|
2019-08-02 20:34:54 +00:00
|
|
|
resolveNodes map[string]*structs.DiscoveryGraphNode
|
2019-06-27 18:38:21 +00:00
|
|
|
|
2019-07-01 20:23:36 +00:00
|
|
|
// usesAdvancedRoutingFeatures is set to true if config entries for routing
|
|
|
|
// or splitting appear in the compiled chain
|
|
|
|
usesAdvancedRoutingFeatures bool
|
|
|
|
|
2019-08-02 03:03:34 +00:00
|
|
|
// disableAdvancedRoutingFeatures is set to true if overrideProtocol is set to tcp
|
|
|
|
disableAdvancedRoutingFeatures bool
|
|
|
|
|
|
|
|
// customizedBy indicates which override values customized how the
|
|
|
|
// compilation behaved.
|
|
|
|
//
|
|
|
|
// This is an OUTPUT field.
|
|
|
|
customizedBy customizationMarkers
|
|
|
|
|
2019-06-27 18:38:21 +00:00
|
|
|
// protocol is the common protocol used for all referenced services. These
|
|
|
|
// cannot be mixed.
|
|
|
|
//
|
|
|
|
// This is an OUTPUT field.
|
|
|
|
protocol string
|
|
|
|
|
2019-08-02 03:44:05 +00:00
|
|
|
// startNode is computed inside of assembleChain()
|
2019-06-27 18:38:21 +00:00
|
|
|
//
|
2019-08-02 03:44:05 +00:00
|
|
|
// This is an OUTPUT field.
|
|
|
|
startNode string
|
|
|
|
|
|
|
|
// nodes is computed inside of compile()
|
2019-06-27 18:38:21 +00:00
|
|
|
//
|
|
|
|
// This is an OUTPUT field.
|
2019-08-02 03:44:05 +00:00
|
|
|
nodes map[string]*structs.DiscoveryGraphNode
|
2019-06-27 18:38:21 +00:00
|
|
|
|
|
|
|
// This is an OUTPUT field.
|
2019-08-02 20:34:54 +00:00
|
|
|
loadedTargets map[string]*structs.DiscoveryTarget
|
|
|
|
retainedTargets map[string]struct{}
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 03:03:34 +00:00
|
|
|
type customizationMarkers struct {
|
|
|
|
MeshGateway bool
|
|
|
|
Protocol bool
|
|
|
|
ConnectTimeout bool
|
|
|
|
}
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
// serviceIDString deviates from the standard formatting you would get with
|
|
|
|
// the String() method on the type itself. It is this way to be more
|
|
|
|
// consistent with other string ids within the discovery chain.
|
|
|
|
func serviceIDString(sid structs.ServiceID) string {
|
2021-11-13 01:57:05 +00:00
|
|
|
return fmt.Sprintf("%s.%s.%s", sid.ID, sid.NamespaceOrDefault(), sid.PartitionOrDefault())
|
2020-01-24 15:04:58 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 03:03:34 +00:00
|
|
|
func (m *customizationMarkers) IsZero() bool {
|
|
|
|
return !m.MeshGateway && !m.Protocol && !m.ConnectTimeout
|
|
|
|
}
|
|
|
|
|
2019-08-02 03:44:05 +00:00
|
|
|
// recordNode stores the node internally in the compiled chain.
|
|
|
|
func (c *compiler) recordNode(node *structs.DiscoveryGraphNode) {
|
|
|
|
// Some types have their own type-specific lookups, so record those, too.
|
|
|
|
switch node.Type {
|
|
|
|
case structs.DiscoveryGraphNodeTypeRouter:
|
|
|
|
// no special storage
|
|
|
|
case structs.DiscoveryGraphNodeTypeSplitter:
|
2019-08-02 20:34:54 +00:00
|
|
|
c.splitterNodes[node.Name] = node
|
2019-08-02 03:44:05 +00:00
|
|
|
case structs.DiscoveryGraphNodeTypeResolver:
|
|
|
|
c.resolveNodes[node.Resolver.Target] = node
|
|
|
|
default:
|
|
|
|
panic("unknown node type '" + node.Type + "'")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.nodes[node.MapKey()] = node
|
|
|
|
}
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
func (c *compiler) recordServiceProtocol(sid structs.ServiceID) error {
|
|
|
|
if serviceDefault := c.entries.GetService(sid); serviceDefault != nil {
|
|
|
|
return c.recordProtocol(sid, serviceDefault.Protocol)
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
2021-11-13 01:57:05 +00:00
|
|
|
if proxyDefault := c.entries.GetProxyDefaults(sid.PartitionOrDefault()); proxyDefault != nil {
|
2019-07-02 16:01:17 +00:00
|
|
|
var cfg proxyConfig
|
|
|
|
// Ignore errors and fallback on defaults if it does happen.
|
2021-11-13 01:57:05 +00:00
|
|
|
_ = mapstructure.WeakDecode(proxyDefault.Config, &cfg)
|
2019-07-02 16:01:17 +00:00
|
|
|
if cfg.Protocol != "" {
|
2020-01-24 15:04:58 +00:00
|
|
|
return c.recordProtocol(sid, cfg.Protocol)
|
2019-07-02 16:01:17 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-24 15:04:58 +00:00
|
|
|
return c.recordProtocol(sid, "")
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 16:01:17 +00:00
|
|
|
// proxyConfig is a snippet from agent/xds/config.go:ProxyConfig
|
|
|
|
type proxyConfig struct {
|
|
|
|
Protocol string `mapstructure:"protocol"`
|
|
|
|
}
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
func (c *compiler) recordProtocol(fromService structs.ServiceID, protocol string) error {
|
2019-06-27 18:38:21 +00:00
|
|
|
if protocol == "" {
|
|
|
|
protocol = "tcp"
|
|
|
|
} else {
|
|
|
|
protocol = strings.ToLower(protocol)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.protocol == "" {
|
|
|
|
c.protocol = protocol
|
|
|
|
} else if c.protocol != protocol {
|
2019-07-01 20:23:36 +00:00
|
|
|
return &structs.ConfigEntryGraphError{
|
|
|
|
Message: fmt.Sprintf(
|
|
|
|
"discovery chain %q uses inconsistent protocols; service %q has %q which is not %q",
|
2020-01-24 15:04:58 +00:00
|
|
|
c.serviceName, fromService.String(), protocol, c.protocol,
|
2019-07-01 20:23:36 +00:00
|
|
|
),
|
|
|
|
}
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *compiler) compile() (*structs.CompiledDiscoveryChain, error) {
|
|
|
|
if err := c.assembleChain(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-02 03:44:05 +00:00
|
|
|
// We don't need these intermediates anymore.
|
|
|
|
c.splitterNodes = nil
|
|
|
|
c.resolveNodes = nil
|
|
|
|
|
|
|
|
if c.startNode == "" {
|
|
|
|
panic("impossible to return no results")
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 14:18:45 +00:00
|
|
|
if err := c.detectCircularReferences(); err != nil {
|
2019-06-27 18:38:21 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-26 13:50:49 +00:00
|
|
|
if err := c.flattenAdjacentSplitterNodes(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-27 18:38:21 +00:00
|
|
|
|
2019-08-02 03:44:05 +00:00
|
|
|
if err := c.removeUnusedNodes(); err != nil {
|
|
|
|
return nil, err
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 17:19:31 +00:00
|
|
|
for targetID := range c.loadedTargets {
|
2019-08-02 20:34:54 +00:00
|
|
|
if _, ok := c.retainedTargets[targetID]; !ok {
|
|
|
|
delete(c.loadedTargets, targetID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-01 20:23:36 +00:00
|
|
|
if !enableAdvancedRoutingForProtocol(c.protocol) && c.usesAdvancedRoutingFeatures {
|
|
|
|
return nil, &structs.ConfigEntryGraphError{
|
|
|
|
Message: fmt.Sprintf(
|
|
|
|
"discovery chain %q uses a protocol %q that does not permit advanced routing or splitting behavior",
|
|
|
|
c.serviceName, c.protocol,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 03:03:34 +00:00
|
|
|
if c.overrideProtocol != "" {
|
|
|
|
if c.overrideProtocol != c.protocol {
|
|
|
|
c.protocol = c.overrideProtocol
|
|
|
|
c.customizedBy.Protocol = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var customizationHash string
|
|
|
|
if !c.customizedBy.IsZero() {
|
|
|
|
var customization struct {
|
|
|
|
OverrideMeshGateway structs.MeshGatewayConfig
|
|
|
|
OverrideProtocol string
|
|
|
|
OverrideConnectTimeout time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.customizedBy.MeshGateway {
|
|
|
|
customization.OverrideMeshGateway = c.overrideMeshGateway
|
|
|
|
}
|
|
|
|
if c.customizedBy.Protocol {
|
|
|
|
customization.OverrideProtocol = c.overrideProtocol
|
|
|
|
}
|
|
|
|
if c.customizedBy.ConnectTimeout {
|
|
|
|
customization.OverrideConnectTimeout = c.overrideConnectTimeout
|
|
|
|
}
|
|
|
|
v, err := hashstructure.Hash(customization, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot create customization hash key: %v", err)
|
|
|
|
}
|
|
|
|
customizationHash = fmt.Sprintf("%x", v)[0:8]
|
|
|
|
}
|
|
|
|
|
2019-06-27 18:38:21 +00:00
|
|
|
return &structs.CompiledDiscoveryChain{
|
2019-08-02 03:44:05 +00:00
|
|
|
ServiceName: c.serviceName,
|
2019-08-05 18:30:35 +00:00
|
|
|
Namespace: c.evaluateInNamespace,
|
2021-09-07 20:29:32 +00:00
|
|
|
Partition: c.evaluateInPartition,
|
2019-08-05 18:30:35 +00:00
|
|
|
Datacenter: c.evaluateInDatacenter,
|
2019-08-02 03:44:05 +00:00
|
|
|
CustomizationHash: customizationHash,
|
|
|
|
Protocol: c.protocol,
|
|
|
|
StartNode: c.startNode,
|
|
|
|
Nodes: c.nodes,
|
2019-08-02 20:34:54 +00:00
|
|
|
Targets: c.loadedTargets,
|
2019-06-27 18:38:21 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-08-02 14:18:45 +00:00
|
|
|
func (c *compiler) detectCircularReferences() error {
|
|
|
|
var (
|
|
|
|
todo stringStack
|
|
|
|
visited = make(map[string]struct{})
|
|
|
|
visitChain stringStack
|
|
|
|
)
|
|
|
|
|
|
|
|
todo.Push(c.startNode)
|
|
|
|
for {
|
|
|
|
current, ok := todo.Pop()
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if current == "_popvisit" {
|
|
|
|
if v, ok := visitChain.Pop(); ok {
|
|
|
|
delete(visited, v)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
visitChain.Push(current)
|
|
|
|
|
|
|
|
if _, ok := visited[current]; ok {
|
|
|
|
return &structs.ConfigEntryGraphError{
|
|
|
|
Message: fmt.Sprintf(
|
|
|
|
"detected circular reference: [%s]",
|
|
|
|
strings.Join(visitChain.Items(), " -> "),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
visited[current] = struct{}{}
|
|
|
|
|
|
|
|
todo.Push("_popvisit")
|
|
|
|
|
|
|
|
node := c.nodes[current]
|
|
|
|
|
|
|
|
switch node.Type {
|
|
|
|
case structs.DiscoveryGraphNodeTypeRouter:
|
|
|
|
for _, route := range node.Routes {
|
|
|
|
todo.Push(route.NextNode)
|
|
|
|
}
|
|
|
|
case structs.DiscoveryGraphNodeTypeSplitter:
|
|
|
|
for _, split := range node.Splits {
|
|
|
|
todo.Push(split.NextNode)
|
|
|
|
}
|
|
|
|
case structs.DiscoveryGraphNodeTypeResolver:
|
|
|
|
// Circular redirects are detected elsewhere and failover isn't
|
|
|
|
// recursive so there's nothing more to do here.
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unexpected graph node type: %s", node.Type)
|
|
|
|
}
|
|
|
|
}
|
2019-06-27 18:38:21 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-26 13:50:49 +00:00
|
|
|
func (c *compiler) flattenAdjacentSplitterNodes() error {
|
2019-06-27 18:38:21 +00:00
|
|
|
for {
|
|
|
|
anyChanged := false
|
2019-08-02 03:44:05 +00:00
|
|
|
for _, node := range c.nodes {
|
|
|
|
if node.Type != structs.DiscoveryGraphNodeTypeSplitter {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fixedSplits := make([]*structs.DiscoverySplit, 0, len(node.Splits))
|
2019-06-27 18:38:21 +00:00
|
|
|
changed := false
|
2019-08-02 03:44:05 +00:00
|
|
|
for _, split := range node.Splits {
|
|
|
|
nextNode := c.nodes[split.NextNode]
|
|
|
|
if nextNode.Type != structs.DiscoveryGraphNodeTypeSplitter {
|
2019-06-27 18:38:21 +00:00
|
|
|
fixedSplits = append(fixedSplits, split)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
changed = true
|
|
|
|
|
2019-08-02 03:44:05 +00:00
|
|
|
for _, innerSplit := range nextNode.Splits {
|
2019-06-27 18:38:21 +00:00
|
|
|
effectiveWeight := split.Weight * innerSplit.Weight / 100
|
|
|
|
|
2021-08-26 13:50:49 +00:00
|
|
|
// Copy the definition from the inner node but merge in the parent
|
|
|
|
// to preserve any config it needs to pass through.
|
|
|
|
newDef, err := innerSplit.Definition.MergeParent(split.Definition)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-27 18:38:21 +00:00
|
|
|
newDiscoverySplit := &structs.DiscoverySplit{
|
2021-08-26 13:50:49 +00:00
|
|
|
Definition: newDef,
|
2021-07-13 18:49:14 +00:00
|
|
|
Weight: structs.NormalizeServiceSplitWeight(effectiveWeight),
|
|
|
|
NextNode: innerSplit.NextNode,
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fixedSplits = append(fixedSplits, newDiscoverySplit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if changed {
|
2019-08-02 03:44:05 +00:00
|
|
|
node.Splits = fixedSplits
|
2019-06-27 18:38:21 +00:00
|
|
|
anyChanged = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !anyChanged {
|
2021-08-26 13:50:49 +00:00
|
|
|
return nil
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 03:44:05 +00:00
|
|
|
// removeUnusedNodes walks the chain from the start and prunes any nodes that
|
|
|
|
// are no longer referenced. This can happen as a result of operations like
|
|
|
|
// flattenAdjacentSplitterNodes().
|
|
|
|
func (c *compiler) removeUnusedNodes() error {
|
|
|
|
var (
|
|
|
|
visited = make(map[string]struct{})
|
|
|
|
todo = make(map[string]struct{})
|
|
|
|
)
|
|
|
|
|
|
|
|
todo[c.startNode] = struct{}{}
|
|
|
|
|
|
|
|
getNext := func() string {
|
|
|
|
if len(todo) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
2020-06-16 17:19:31 +00:00
|
|
|
for k := range todo {
|
2019-08-02 03:44:05 +00:00
|
|
|
delete(todo, k)
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
next := getNext()
|
|
|
|
if next == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if _, ok := visited[next]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
visited[next] = struct{}{}
|
|
|
|
|
|
|
|
node := c.nodes[next]
|
|
|
|
if node == nil {
|
|
|
|
return fmt.Errorf("compilation references non-retained node %q", next)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch node.Type {
|
|
|
|
case structs.DiscoveryGraphNodeTypeRouter:
|
|
|
|
for _, route := range node.Routes {
|
|
|
|
todo[route.NextNode] = struct{}{}
|
|
|
|
}
|
|
|
|
case structs.DiscoveryGraphNodeTypeSplitter:
|
|
|
|
for _, split := range node.Splits {
|
|
|
|
todo[split.NextNode] = struct{}{}
|
|
|
|
}
|
|
|
|
case structs.DiscoveryGraphNodeTypeResolver:
|
|
|
|
// nothing special
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown node type %q", node.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(visited) == len(c.nodes) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-16 17:19:31 +00:00
|
|
|
for name := range c.nodes {
|
2019-08-02 03:44:05 +00:00
|
|
|
if _, ok := visited[name]; !ok {
|
|
|
|
delete(c.nodes, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-27 18:38:21 +00:00
|
|
|
// assembleChain will do the initial assembly of a chain of DiscoveryGraphNode
|
2019-08-02 03:44:05 +00:00
|
|
|
// entries from the provided config entries.
|
2019-06-27 18:38:21 +00:00
|
|
|
func (c *compiler) assembleChain() error {
|
2019-08-02 03:44:05 +00:00
|
|
|
if c.startNode != "" || len(c.nodes) > 0 {
|
2019-06-27 18:38:21 +00:00
|
|
|
return fmt.Errorf("assembleChain should only be called once")
|
|
|
|
}
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
sid := structs.NewServiceID(c.serviceName, c.GetEnterpriseMeta())
|
|
|
|
|
2019-06-27 18:38:21 +00:00
|
|
|
// Check for short circuit path.
|
|
|
|
if len(c.resolvers) == 0 && c.entries.IsChainEmpty() {
|
|
|
|
// Materialize defaults and cache.
|
2020-01-24 15:04:58 +00:00
|
|
|
c.resolvers[sid] = newDefaultServiceResolver(sid)
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The only router we consult is the one for the service name at the top of
|
|
|
|
// the chain.
|
2020-01-24 15:04:58 +00:00
|
|
|
router := c.entries.GetRouter(sid)
|
2019-08-02 03:03:34 +00:00
|
|
|
if router != nil && c.disableAdvancedRoutingFeatures {
|
|
|
|
router = nil
|
|
|
|
c.customizedBy.Protocol = true
|
|
|
|
}
|
|
|
|
|
2019-07-01 20:23:36 +00:00
|
|
|
if router == nil {
|
2019-06-27 18:38:21 +00:00
|
|
|
// If no router is configured, move on down the line to the next hop of
|
|
|
|
// the chain.
|
2021-09-07 20:29:32 +00:00
|
|
|
node, err := c.getSplitterOrResolverNode(c.newTarget(c.serviceName, "", "", "", ""))
|
2019-06-27 18:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-02 03:44:05 +00:00
|
|
|
c.startNode = node.MapKey()
|
2019-06-27 18:38:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
routerID := structs.NewServiceID(router.Name, router.GetEnterpriseMeta())
|
|
|
|
|
2019-06-27 18:38:21 +00:00
|
|
|
routeNode := &structs.DiscoveryGraphNode{
|
|
|
|
Type: structs.DiscoveryGraphNodeTypeRouter,
|
2020-01-24 15:04:58 +00:00
|
|
|
Name: serviceIDString(routerID),
|
2019-06-27 18:38:21 +00:00
|
|
|
Routes: make([]*structs.DiscoveryRoute, 0, len(router.Routes)+1),
|
|
|
|
}
|
2019-07-01 20:23:36 +00:00
|
|
|
c.usesAdvancedRoutingFeatures = true
|
2020-01-24 15:04:58 +00:00
|
|
|
if err := c.recordServiceProtocol(routerID); err != nil {
|
2019-06-27 18:38:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-16 17:19:31 +00:00
|
|
|
for i := range router.Routes {
|
2019-06-27 18:38:21 +00:00
|
|
|
// We don't use range variables here because we'll take the address of
|
|
|
|
// this route and store that in a DiscoveryGraphNode and the range
|
|
|
|
// variables share memory addresses between iterations which is exactly
|
|
|
|
// wrong for us here.
|
|
|
|
route := router.Routes[i]
|
|
|
|
|
|
|
|
compiledRoute := &structs.DiscoveryRoute{Definition: &route}
|
|
|
|
routeNode.Routes = append(routeNode.Routes, compiledRoute)
|
|
|
|
|
|
|
|
dest := route.Destination
|
2020-05-05 15:49:50 +00:00
|
|
|
if dest == nil {
|
|
|
|
dest = &structs.ServiceRouteDestination{
|
|
|
|
Service: c.serviceName,
|
|
|
|
Namespace: router.NamespaceOrDefault(),
|
2021-11-13 01:57:05 +00:00
|
|
|
Partition: router.PartitionOrDefault(),
|
2020-05-05 15:49:50 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-27 18:38:21 +00:00
|
|
|
svc := defaultIfEmpty(dest.Service, c.serviceName)
|
2020-02-05 15:06:11 +00:00
|
|
|
destNamespace := defaultIfEmpty(dest.Namespace, router.NamespaceOrDefault())
|
2021-11-13 01:57:05 +00:00
|
|
|
destPartition := defaultIfEmpty(dest.Partition, router.PartitionOrDefault())
|
2019-06-27 18:38:21 +00:00
|
|
|
|
|
|
|
// Check to see if the destination is eligible for splitting.
|
|
|
|
var (
|
|
|
|
node *structs.DiscoveryGraphNode
|
|
|
|
err error
|
|
|
|
)
|
2020-01-24 15:04:58 +00:00
|
|
|
if dest.ServiceSubset == "" {
|
2019-08-02 03:44:05 +00:00
|
|
|
node, err = c.getSplitterOrResolverNode(
|
2021-09-07 20:29:32 +00:00
|
|
|
c.newTarget(svc, "", destNamespace, destPartition, ""),
|
2019-06-27 18:38:21 +00:00
|
|
|
)
|
|
|
|
} else {
|
2019-08-02 03:44:05 +00:00
|
|
|
node, err = c.getResolverNode(
|
2021-09-07 20:29:32 +00:00
|
|
|
c.newTarget(svc, dest.ServiceSubset, destNamespace, destPartition, ""),
|
2019-06-27 18:38:21 +00:00
|
|
|
false,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-02 03:44:05 +00:00
|
|
|
compiledRoute.NextNode = node.MapKey()
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a router, we'll add a catch-all route at the end to send
|
|
|
|
// unmatched traffic to the next hop in the chain.
|
2021-09-07 20:29:32 +00:00
|
|
|
defaultDestinationNode, err := c.getSplitterOrResolverNode(c.newTarget(router.Name, "", router.NamespaceOrDefault(), router.PartitionOrDefault(), ""))
|
2019-06-27 18:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultRoute := &structs.DiscoveryRoute{
|
2021-11-13 01:57:05 +00:00
|
|
|
Definition: newDefaultServiceRoute(router.Name, router.NamespaceOrDefault(), router.PartitionOrDefault()),
|
2019-08-02 03:44:05 +00:00
|
|
|
NextNode: defaultDestinationNode.MapKey(),
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
|
|
|
routeNode.Routes = append(routeNode.Routes, defaultRoute)
|
|
|
|
|
2019-08-02 03:44:05 +00:00
|
|
|
c.startNode = routeNode.MapKey()
|
|
|
|
c.recordNode(routeNode)
|
|
|
|
|
2019-06-27 18:38:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-13 01:57:05 +00:00
|
|
|
func newDefaultServiceRoute(serviceName, namespace, partition string) *structs.ServiceRoute {
|
2019-06-27 18:38:21 +00:00
|
|
|
return &structs.ServiceRoute{
|
|
|
|
Match: &structs.ServiceRouteMatch{
|
|
|
|
HTTP: &structs.ServiceRouteHTTPMatch{
|
|
|
|
PathPrefix: "/",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Destination: &structs.ServiceRouteDestination{
|
2020-01-24 15:04:58 +00:00
|
|
|
Service: serviceName,
|
|
|
|
Namespace: namespace,
|
2021-11-13 01:57:05 +00:00
|
|
|
Partition: partition,
|
2019-06-27 18:38:21 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 20:29:32 +00:00
|
|
|
func (c *compiler) newTarget(service, serviceSubset, namespace, partition, datacenter string) *structs.DiscoveryTarget {
|
2019-06-27 18:38:21 +00:00
|
|
|
if service == "" {
|
|
|
|
panic("newTarget called with empty service which makes no sense")
|
|
|
|
}
|
2019-08-02 20:34:54 +00:00
|
|
|
|
|
|
|
t := structs.NewDiscoveryTarget(
|
|
|
|
service,
|
|
|
|
serviceSubset,
|
2019-08-05 18:30:35 +00:00
|
|
|
defaultIfEmpty(namespace, c.evaluateInNamespace),
|
2021-09-07 20:29:32 +00:00
|
|
|
defaultIfEmpty(partition, c.evaluateInPartition),
|
2019-08-05 18:30:35 +00:00
|
|
|
defaultIfEmpty(datacenter, c.evaluateInDatacenter),
|
2019-08-02 20:34:54 +00:00
|
|
|
)
|
|
|
|
|
2019-08-19 18:03:03 +00:00
|
|
|
// Set default connect SNI. This will be overridden later if the service
|
|
|
|
// has an explicit SNI value configured in service-defaults.
|
|
|
|
t.SNI = connect.TargetSNI(t, c.evaluateInTrustDomain)
|
|
|
|
|
|
|
|
// Use the same representation for the name. This will NOT be overridden
|
|
|
|
// later.
|
|
|
|
t.Name = t.SNI
|
|
|
|
|
2019-08-02 20:34:54 +00:00
|
|
|
prev, ok := c.loadedTargets[t.ID]
|
|
|
|
if ok {
|
|
|
|
return prev
|
|
|
|
}
|
|
|
|
c.loadedTargets[t.ID] = t
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2021-09-07 20:29:32 +00:00
|
|
|
func (c *compiler) rewriteTarget(t *structs.DiscoveryTarget, service, serviceSubset, partition, namespace, datacenter string) *structs.DiscoveryTarget {
|
2019-08-02 20:34:54 +00:00
|
|
|
var (
|
|
|
|
service2 = t.Service
|
|
|
|
serviceSubset2 = t.ServiceSubset
|
2021-09-07 20:29:32 +00:00
|
|
|
partition2 = t.Partition
|
2019-08-02 20:34:54 +00:00
|
|
|
namespace2 = t.Namespace
|
|
|
|
datacenter2 = t.Datacenter
|
|
|
|
)
|
|
|
|
|
|
|
|
if service != "" && service != service2 {
|
|
|
|
service2 = service
|
|
|
|
// Reset the chosen subset if we reference a service other than our own.
|
|
|
|
serviceSubset2 = ""
|
|
|
|
}
|
|
|
|
if serviceSubset != "" {
|
|
|
|
serviceSubset2 = serviceSubset
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
2021-09-07 20:29:32 +00:00
|
|
|
if partition != "" {
|
|
|
|
partition2 = partition
|
|
|
|
}
|
2019-08-02 20:34:54 +00:00
|
|
|
if namespace != "" {
|
|
|
|
namespace2 = namespace
|
|
|
|
}
|
|
|
|
if datacenter != "" {
|
|
|
|
datacenter2 = datacenter
|
|
|
|
}
|
|
|
|
|
2021-09-07 20:29:32 +00:00
|
|
|
return c.newTarget(service2, serviceSubset2, namespace2, partition2, datacenter2)
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 20:34:54 +00:00
|
|
|
func (c *compiler) getSplitterOrResolverNode(target *structs.DiscoveryTarget) (*structs.DiscoveryGraphNode, error) {
|
2020-01-24 15:04:58 +00:00
|
|
|
nextNode, err := c.getSplitterNode(target.ServiceID())
|
2019-06-27 18:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if nextNode != nil {
|
|
|
|
return nextNode, nil
|
|
|
|
}
|
2019-08-02 03:44:05 +00:00
|
|
|
return c.getResolverNode(target, false)
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
func (c *compiler) getSplitterNode(sid structs.ServiceID) (*structs.DiscoveryGraphNode, error) {
|
|
|
|
name := serviceIDString(sid)
|
2019-06-27 18:38:21 +00:00
|
|
|
// Do we already have the node?
|
|
|
|
if prev, ok := c.splitterNodes[name]; ok {
|
|
|
|
return prev, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the config entry.
|
2020-01-24 15:04:58 +00:00
|
|
|
splitter := c.entries.GetSplitter(sid)
|
2019-08-02 03:03:34 +00:00
|
|
|
if splitter != nil && c.disableAdvancedRoutingFeatures {
|
|
|
|
splitter = nil
|
|
|
|
c.customizedBy.Protocol = true
|
|
|
|
}
|
2019-07-01 20:23:36 +00:00
|
|
|
if splitter == nil {
|
2019-06-27 18:38:21 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build node.
|
|
|
|
splitNode := &structs.DiscoveryGraphNode{
|
|
|
|
Type: structs.DiscoveryGraphNodeTypeSplitter,
|
|
|
|
Name: name,
|
|
|
|
Splits: make([]*structs.DiscoverySplit, 0, len(splitter.Splits)),
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we record this exists before recursing down it will short-circuit
|
2021-07-02 16:18:46 +00:00
|
|
|
// reasonably if there is some sort of graph loop below.
|
2019-08-02 03:44:05 +00:00
|
|
|
c.recordNode(splitNode)
|
2019-06-27 18:38:21 +00:00
|
|
|
|
2020-08-28 19:11:04 +00:00
|
|
|
var hasLB bool
|
2021-07-13 18:49:14 +00:00
|
|
|
for i := range splitter.Splits {
|
|
|
|
// We don't use range variables here because we'll take the address of
|
|
|
|
// this split and store that in a DiscoveryGraphNode and the range
|
|
|
|
// variables share memory addresses between iterations which is exactly
|
|
|
|
// wrong for us here.
|
|
|
|
split := splitter.Splits[i]
|
|
|
|
|
2019-06-27 18:38:21 +00:00
|
|
|
compiledSplit := &structs.DiscoverySplit{
|
2021-07-13 18:49:14 +00:00
|
|
|
Definition: &split,
|
|
|
|
Weight: split.Weight,
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
|
|
|
splitNode.Splits = append(splitNode.Splits, compiledSplit)
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
svc := defaultIfEmpty(split.Service, sid.ID)
|
|
|
|
splitID := structs.ServiceID{
|
|
|
|
ID: svc,
|
|
|
|
EnterpriseMeta: *split.GetEnterpriseMeta(&sid.EnterpriseMeta),
|
|
|
|
}
|
|
|
|
|
2019-06-27 18:38:21 +00:00
|
|
|
// Check to see if the split is eligible for additional splitting.
|
2020-12-11 21:10:00 +00:00
|
|
|
if !splitID.Matches(sid) && split.ServiceSubset == "" {
|
2020-01-24 15:04:58 +00:00
|
|
|
nextNode, err := c.getSplitterNode(splitID)
|
2019-06-27 18:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if nextNode != nil {
|
2019-08-02 03:44:05 +00:00
|
|
|
compiledSplit.NextNode = nextNode.MapKey()
|
2019-06-27 18:38:21 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// fall through to group-resolver
|
|
|
|
}
|
|
|
|
|
2019-08-02 03:44:05 +00:00
|
|
|
node, err := c.getResolverNode(
|
2021-09-07 20:29:32 +00:00
|
|
|
c.newTarget(splitID.ID, split.ServiceSubset, splitID.NamespaceOrDefault(), splitID.PartitionOrDefault(), ""),
|
2019-06-27 18:38:21 +00:00
|
|
|
false,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-02 03:44:05 +00:00
|
|
|
compiledSplit.NextNode = node.MapKey()
|
2020-08-28 19:11:04 +00:00
|
|
|
|
|
|
|
// There exists the possibility that a splitter may split between two distinct service names
|
|
|
|
// with distinct hash-based load balancer configs specified in their service resolvers.
|
|
|
|
// We cannot apply multiple hash policies to a splitter node's route action.
|
|
|
|
// Therefore, we attach the first hash-based load balancer config we encounter.
|
2020-09-02 15:10:50 +00:00
|
|
|
if !hasLB {
|
2020-09-11 15:21:43 +00:00
|
|
|
if lb := node.LoadBalancer; lb != nil && lb.IsHashBased() {
|
2020-09-02 15:10:50 +00:00
|
|
|
splitNode.LoadBalancer = node.LoadBalancer
|
|
|
|
hasLB = true
|
|
|
|
}
|
2020-08-28 19:11:04 +00:00
|
|
|
}
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-01 20:23:36 +00:00
|
|
|
c.usesAdvancedRoutingFeatures = true
|
2019-06-27 18:38:21 +00:00
|
|
|
return splitNode, nil
|
|
|
|
}
|
|
|
|
|
2019-08-02 03:44:05 +00:00
|
|
|
// getResolverNode handles most of the code to handle redirection/rewriting
|
|
|
|
// capabilities from a resolver config entry. It recurses into itself to
|
|
|
|
// _generate_ targets used for failover out of convenience.
|
2019-08-02 20:34:54 +00:00
|
|
|
func (c *compiler) getResolverNode(target *structs.DiscoveryTarget, recursedForFailover bool) (*structs.DiscoveryGraphNode, error) {
|
2019-08-02 14:18:45 +00:00
|
|
|
var (
|
|
|
|
// State to help detect redirect cycles and print helpful error
|
|
|
|
// messages.
|
2019-08-02 20:34:54 +00:00
|
|
|
redirectHistory = make(map[string]struct{})
|
|
|
|
redirectOrder []string
|
2019-08-02 14:18:45 +00:00
|
|
|
)
|
|
|
|
|
2019-06-27 18:38:21 +00:00
|
|
|
RESOLVE_AGAIN:
|
|
|
|
// Do we already have the node?
|
2019-08-02 20:34:54 +00:00
|
|
|
if prev, ok := c.resolveNodes[target.ID]; ok {
|
2019-06-27 18:38:21 +00:00
|
|
|
return prev, nil
|
|
|
|
}
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
targetID := target.ServiceID()
|
|
|
|
|
|
|
|
if err := c.recordServiceProtocol(targetID); err != nil {
|
2019-06-27 18:38:21 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the config entry.
|
2020-01-24 15:04:58 +00:00
|
|
|
resolver, ok := c.resolvers[targetID]
|
2019-06-27 18:38:21 +00:00
|
|
|
if !ok {
|
|
|
|
// Materialize defaults and cache.
|
2020-01-24 15:04:58 +00:00
|
|
|
resolver = newDefaultServiceResolver(targetID)
|
|
|
|
c.resolvers[targetID] = resolver
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 20:34:54 +00:00
|
|
|
if _, ok := redirectHistory[target.ID]; ok {
|
|
|
|
redirectOrder = append(redirectOrder, target.ID)
|
2019-08-02 14:18:45 +00:00
|
|
|
|
|
|
|
return nil, &structs.ConfigEntryGraphError{
|
|
|
|
Message: fmt.Sprintf(
|
|
|
|
"detected circular resolver redirect: [%s]",
|
2019-08-02 20:34:54 +00:00
|
|
|
strings.Join(redirectOrder, " -> "),
|
2019-08-02 14:18:45 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
2019-08-02 20:34:54 +00:00
|
|
|
redirectHistory[target.ID] = struct{}{}
|
|
|
|
redirectOrder = append(redirectOrder, target.ID)
|
2019-08-02 14:18:45 +00:00
|
|
|
|
2019-06-27 18:38:21 +00:00
|
|
|
// Handle redirects right up front.
|
2019-07-01 20:23:36 +00:00
|
|
|
//
|
|
|
|
// TODO(rb): What about a redirected subset reference? (web/v2, but web redirects to alt/"")
|
2021-09-07 20:29:32 +00:00
|
|
|
|
2019-06-27 18:38:21 +00:00
|
|
|
if resolver.Redirect != nil {
|
|
|
|
redirect := resolver.Redirect
|
|
|
|
|
2019-08-02 20:34:54 +00:00
|
|
|
redirectedTarget := c.rewriteTarget(
|
|
|
|
target,
|
2019-06-27 18:38:21 +00:00
|
|
|
redirect.Service,
|
|
|
|
redirect.ServiceSubset,
|
2021-11-13 01:57:05 +00:00
|
|
|
redirect.Partition,
|
2019-06-27 18:38:21 +00:00
|
|
|
redirect.Namespace,
|
|
|
|
redirect.Datacenter,
|
|
|
|
)
|
2019-08-02 20:34:54 +00:00
|
|
|
if redirectedTarget.ID != target.ID {
|
2019-06-27 18:38:21 +00:00
|
|
|
target = redirectedTarget
|
|
|
|
goto RESOLVE_AGAIN
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle default subset.
|
|
|
|
if target.ServiceSubset == "" && resolver.DefaultSubset != "" {
|
2019-08-02 20:34:54 +00:00
|
|
|
target = c.rewriteTarget(
|
|
|
|
target,
|
|
|
|
"",
|
|
|
|
resolver.DefaultSubset,
|
|
|
|
"",
|
|
|
|
"",
|
2021-09-07 20:29:32 +00:00
|
|
|
"",
|
2019-08-02 20:34:54 +00:00
|
|
|
)
|
2019-06-27 18:38:21 +00:00
|
|
|
goto RESOLVE_AGAIN
|
|
|
|
}
|
|
|
|
|
2019-07-01 20:23:36 +00:00
|
|
|
if target.ServiceSubset != "" && !resolver.SubsetExists(target.ServiceSubset) {
|
|
|
|
return nil, &structs.ConfigEntryGraphError{
|
|
|
|
Message: fmt.Sprintf(
|
|
|
|
"service %q does not have a subset named %q",
|
|
|
|
target.Service,
|
|
|
|
target.ServiceSubset,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 18:38:21 +00:00
|
|
|
connectTimeout := resolver.ConnectTimeout
|
|
|
|
if connectTimeout < 1 {
|
|
|
|
connectTimeout = 5 * time.Second
|
|
|
|
}
|
|
|
|
|
2019-08-02 03:03:34 +00:00
|
|
|
if c.overrideConnectTimeout > 0 {
|
|
|
|
if connectTimeout != c.overrideConnectTimeout {
|
|
|
|
connectTimeout = c.overrideConnectTimeout
|
|
|
|
c.customizedBy.ConnectTimeout = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 18:38:21 +00:00
|
|
|
// Build node.
|
2019-08-02 03:44:05 +00:00
|
|
|
node := &structs.DiscoveryGraphNode{
|
|
|
|
Type: structs.DiscoveryGraphNodeTypeResolver,
|
2019-08-02 20:34:54 +00:00
|
|
|
Name: target.ID,
|
2019-08-02 03:44:05 +00:00
|
|
|
Resolver: &structs.DiscoveryResolver{
|
2019-06-27 18:38:21 +00:00
|
|
|
Default: resolver.IsDefault(),
|
2019-08-02 20:34:54 +00:00
|
|
|
Target: target.ID,
|
2019-06-27 18:38:21 +00:00
|
|
|
ConnectTimeout: connectTimeout,
|
|
|
|
},
|
2020-08-28 19:11:04 +00:00
|
|
|
LoadBalancer: resolver.LoadBalancer,
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
2019-08-02 03:44:05 +00:00
|
|
|
|
2019-08-02 20:34:54 +00:00
|
|
|
target.Subset = resolver.Subsets[target.ServiceSubset]
|
2019-06-27 18:38:21 +00:00
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
if serviceDefault := c.entries.GetService(targetID); serviceDefault != nil && serviceDefault.ExternalSNI != "" {
|
2019-08-19 18:03:03 +00:00
|
|
|
// Override the default SNI value.
|
2019-08-19 17:19:44 +00:00
|
|
|
target.SNI = serviceDefault.ExternalSNI
|
|
|
|
target.External = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// If using external SNI the service is fundamentally external.
|
|
|
|
if target.External {
|
|
|
|
if resolver.Redirect != nil {
|
|
|
|
return nil, &structs.ConfigEntryGraphError{
|
|
|
|
Message: fmt.Sprintf(
|
|
|
|
"service %q has an external SNI set; cannot define redirects for external services",
|
|
|
|
target.Service,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(resolver.Subsets) > 0 {
|
|
|
|
return nil, &structs.ConfigEntryGraphError{
|
|
|
|
Message: fmt.Sprintf(
|
|
|
|
"service %q has an external SNI set; cannot define subsets for external services",
|
|
|
|
target.Service,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(resolver.Failover) > 0 {
|
|
|
|
return nil, &structs.ConfigEntryGraphError{
|
|
|
|
Message: fmt.Sprintf(
|
|
|
|
"service %q has an external SNI set; cannot define failover for external services",
|
|
|
|
target.Service,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-22 21:13:48 +00:00
|
|
|
// TODO(partitions): Document this change in behavior. Discovery chain targets will now return a mesh gateway
|
|
|
|
// mode as long as they are not external. Regardless of the datacenter/partition where
|
|
|
|
// the chain will be used.
|
|
|
|
if target.External {
|
2019-08-19 17:19:44 +00:00
|
|
|
// Bypass mesh gateways if it is an external service.
|
|
|
|
target.MeshGateway.Mode = structs.MeshGatewayModeDefault
|
2019-08-05 18:30:35 +00:00
|
|
|
} else {
|
|
|
|
// Default mesh gateway settings
|
2020-01-24 15:04:58 +00:00
|
|
|
if serviceDefault := c.entries.GetService(targetID); serviceDefault != nil {
|
2019-08-05 18:30:35 +00:00
|
|
|
target.MeshGateway = serviceDefault.MeshGateway
|
|
|
|
}
|
2021-11-13 01:57:05 +00:00
|
|
|
proxyDefault := c.entries.GetProxyDefaults(targetID.PartitionOrDefault())
|
|
|
|
if proxyDefault != nil && target.MeshGateway.Mode == structs.MeshGatewayModeDefault {
|
|
|
|
target.MeshGateway.Mode = proxyDefault.MeshGateway.Mode
|
2019-08-05 18:30:35 +00:00
|
|
|
}
|
2019-07-02 03:10:51 +00:00
|
|
|
|
2019-08-05 18:30:35 +00:00
|
|
|
if c.overrideMeshGateway.Mode != structs.MeshGatewayModeDefault {
|
|
|
|
if target.MeshGateway.Mode != c.overrideMeshGateway.Mode {
|
|
|
|
target.MeshGateway.Mode = c.overrideMeshGateway.Mode
|
|
|
|
c.customizedBy.MeshGateway = true
|
|
|
|
}
|
2019-08-02 03:03:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 20:34:54 +00:00
|
|
|
// Retain this target in the final results.
|
|
|
|
c.retainedTargets[target.ID] = struct{}{}
|
2019-06-27 18:38:21 +00:00
|
|
|
|
|
|
|
if recursedForFailover {
|
|
|
|
// If we recursed here from ourselves in a failover context, just emit
|
|
|
|
// this node without caching it or even processing failover again.
|
|
|
|
// This is a little weird but it keeps the redirect/default-subset
|
|
|
|
// logic in one place.
|
2019-08-02 03:44:05 +00:00
|
|
|
return node, nil
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we record this exists before recursing down it will short-circuit
|
2021-07-02 16:18:46 +00:00
|
|
|
// reasonably if there is some sort of graph loop below.
|
2019-08-02 03:44:05 +00:00
|
|
|
c.recordNode(node)
|
2019-06-27 18:38:21 +00:00
|
|
|
|
|
|
|
if len(resolver.Failover) > 0 {
|
|
|
|
f := resolver.Failover
|
|
|
|
|
|
|
|
// Determine which failover section applies.
|
|
|
|
failover, ok := f[target.ServiceSubset]
|
|
|
|
if !ok {
|
|
|
|
failover, ok = f["*"]
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
// Determine which failover definitions apply.
|
2019-08-02 20:34:54 +00:00
|
|
|
var failoverTargets []*structs.DiscoveryTarget
|
2019-06-27 18:38:21 +00:00
|
|
|
if len(failover.Datacenters) > 0 {
|
|
|
|
for _, dc := range failover.Datacenters {
|
|
|
|
// Rewrite the target as per the failover policy.
|
2019-08-02 20:34:54 +00:00
|
|
|
failoverTarget := c.rewriteTarget(
|
|
|
|
target,
|
2019-06-27 18:38:21 +00:00
|
|
|
failover.Service,
|
|
|
|
failover.ServiceSubset,
|
2021-11-13 01:57:05 +00:00
|
|
|
failover.Partition,
|
2019-06-27 18:38:21 +00:00
|
|
|
failover.Namespace,
|
|
|
|
dc,
|
|
|
|
)
|
2019-08-02 20:34:54 +00:00
|
|
|
if failoverTarget.ID != target.ID { // don't failover to yourself
|
2019-06-27 18:38:21 +00:00
|
|
|
failoverTargets = append(failoverTargets, failoverTarget)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Rewrite the target as per the failover policy.
|
2019-08-02 20:34:54 +00:00
|
|
|
failoverTarget := c.rewriteTarget(
|
|
|
|
target,
|
2019-06-27 18:38:21 +00:00
|
|
|
failover.Service,
|
|
|
|
failover.ServiceSubset,
|
2021-11-13 01:57:05 +00:00
|
|
|
failover.Partition,
|
2019-06-27 18:38:21 +00:00
|
|
|
failover.Namespace,
|
|
|
|
"",
|
|
|
|
)
|
2019-08-02 20:34:54 +00:00
|
|
|
if failoverTarget.ID != target.ID { // don't failover to yourself
|
2019-06-27 18:38:21 +00:00
|
|
|
failoverTargets = append(failoverTargets, failoverTarget)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we filtered everything out then no point in having a failover.
|
|
|
|
if len(failoverTargets) > 0 {
|
2019-08-02 20:34:54 +00:00
|
|
|
df := &structs.DiscoveryFailover{}
|
2019-08-02 03:44:05 +00:00
|
|
|
node.Resolver.Failover = df
|
2019-06-27 18:38:21 +00:00
|
|
|
|
2019-08-02 20:34:54 +00:00
|
|
|
// Take care of doing any redirects or configuration loading
|
|
|
|
// related to targets by cheating a bit and recursing into
|
|
|
|
// ourselves.
|
2019-06-27 18:38:21 +00:00
|
|
|
for _, target := range failoverTargets {
|
2019-08-02 03:44:05 +00:00
|
|
|
failoverResolveNode, err := c.getResolverNode(target, true)
|
2019-06-27 18:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-02 03:44:05 +00:00
|
|
|
failoverTarget := failoverResolveNode.Resolver.Target
|
2019-06-27 18:38:21 +00:00
|
|
|
df.Targets = append(df.Targets, failoverTarget)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 03:44:05 +00:00
|
|
|
return node, nil
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
func newDefaultServiceResolver(sid structs.ServiceID) *structs.ServiceResolverConfigEntry {
|
2019-06-27 18:38:21 +00:00
|
|
|
return &structs.ServiceResolverConfigEntry{
|
2020-01-24 15:04:58 +00:00
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
Name: sid.ID,
|
|
|
|
EnterpriseMeta: sid.EnterpriseMeta,
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultIfEmpty(val, defaultVal string) string {
|
|
|
|
if val != "" {
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
return defaultVal
|
|
|
|
}
|
2019-07-01 20:23:36 +00:00
|
|
|
|
|
|
|
func enableAdvancedRoutingForProtocol(protocol string) bool {
|
2020-08-27 17:20:58 +00:00
|
|
|
return structs.IsProtocolHTTPLike(protocol)
|
2019-07-01 20:23:36 +00:00
|
|
|
}
|