2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2019-06-27 18:38:21 +00:00
|
|
|
package structs
|
|
|
|
|
|
|
|
import (
|
2019-08-19 20:31:05 +00:00
|
|
|
"encoding/json"
|
2019-06-27 18:38:21 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
2019-12-06 16:14:56 +00:00
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2019-12-06 16:14:56 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2019-06-27 18:38:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// CompiledDiscoveryChain is the result from taking a set of related config
|
|
|
|
// entries for a single service's discovery chain and restructuring them into a
|
|
|
|
// form that is more usable for actual service discovery.
|
|
|
|
type CompiledDiscoveryChain struct {
|
|
|
|
ServiceName string
|
|
|
|
Namespace string // the namespace that the chain was compiled within
|
2021-09-07 20:29:32 +00:00
|
|
|
Partition string // the partition that the chain was compiled within
|
2019-06-27 18:38:21 +00:00
|
|
|
Datacenter string // the datacenter that the chain was compiled within
|
|
|
|
|
2019-08-02 03:03:34 +00:00
|
|
|
// CustomizationHash is a unique hash of any data that affects the
|
|
|
|
// compilation of the discovery chain other than config entries or the
|
|
|
|
// name/namespace/datacenter evaluation criteria.
|
|
|
|
//
|
|
|
|
// If set, this value should be used to prefix/suffix any generated load
|
|
|
|
// balancer data plane objects to avoid sharing customized and
|
|
|
|
// non-customized versions.
|
2019-08-02 20:34:54 +00:00
|
|
|
CustomizationHash string `json:",omitempty"`
|
2019-08-02 03:03:34 +00:00
|
|
|
|
2022-03-30 15:04:18 +00:00
|
|
|
// Default indicates if this discovery chain is based on no
|
|
|
|
// service-resolver, service-splitter, or service-router config entries.
|
|
|
|
Default bool `json:",omitempty"`
|
|
|
|
|
2019-08-02 03:03:34 +00:00
|
|
|
// Protocol is the overall protocol shared by everything in the chain.
|
2019-08-02 20:34:54 +00:00
|
|
|
Protocol string `json:",omitempty"`
|
2019-06-27 18:38:21 +00:00
|
|
|
|
2022-03-30 15:04:18 +00:00
|
|
|
// ServiceMeta is the metadata from the underlying service-defaults config
|
|
|
|
// entry for the service named ServiceName.
|
|
|
|
ServiceMeta map[string]string `json:",omitempty"`
|
|
|
|
|
2022-12-19 20:19:37 +00:00
|
|
|
// EnvoyExtensions has a list of configurations for an extension that patches Envoy resources.
|
|
|
|
EnvoyExtensions []EnvoyExtension `json:",omitempty"`
|
|
|
|
|
2019-08-02 03:44:05 +00:00
|
|
|
// StartNode is the first key into the Nodes map that should be followed
|
|
|
|
// when walking the discovery chain.
|
|
|
|
StartNode string `json:",omitempty"`
|
2019-06-27 18:38:21 +00:00
|
|
|
|
2019-08-02 03:44:05 +00:00
|
|
|
// Nodes contains all nodes available for traversal in the chain keyed by a
|
|
|
|
// unique name. You can walk this by starting with StartNode.
|
2019-06-27 18:38:21 +00:00
|
|
|
//
|
2019-08-02 03:44:05 +00:00
|
|
|
// NOTE: The names should be treated as opaque values and are only
|
|
|
|
// guaranteed to be consistent within a single compilation.
|
|
|
|
Nodes map[string]*DiscoveryGraphNode `json:",omitempty"`
|
2019-06-27 18:38:21 +00:00
|
|
|
|
2019-08-02 20:34:54 +00:00
|
|
|
// Targets is a list of all targets used in this chain.
|
|
|
|
Targets map[string]*DiscoveryTarget `json:",omitempty"`
|
2023-05-12 02:28:16 +00:00
|
|
|
|
|
|
|
// VirtualIPs is a list of virtual IPs associated with the service.
|
|
|
|
AutoVirtualIPs []string
|
|
|
|
ManualVirtualIPs []string
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
|
|
|
|
2021-09-07 20:29:32 +00:00
|
|
|
// ID returns an ID that encodes the service, namespace, partition, and datacenter.
|
2021-05-04 14:45:19 +00:00
|
|
|
// This ID allows us to compare a discovery chain target to the chain upstream itself.
|
|
|
|
func (c *CompiledDiscoveryChain) ID() string {
|
2023-03-22 18:56:18 +00:00
|
|
|
return ChainID(DiscoveryTargetOpts{
|
2022-08-23 13:13:43 +00:00
|
|
|
Service: c.ServiceName,
|
|
|
|
Namespace: c.Namespace,
|
|
|
|
Partition: c.Partition,
|
|
|
|
Datacenter: c.Datacenter,
|
|
|
|
})
|
2021-05-04 14:45:19 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 20:34:17 +00:00
|
|
|
func (c *CompiledDiscoveryChain) CompoundServiceName() ServiceName {
|
2022-03-13 03:55:53 +00:00
|
|
|
entMeta := acl.NewEnterpriseMetaWithPartition(c.Partition, c.Namespace)
|
2021-06-09 20:34:17 +00:00
|
|
|
return NewServiceName(c.ServiceName, &entMeta)
|
|
|
|
}
|
|
|
|
|
2019-06-27 18:38:21 +00:00
|
|
|
const (
|
2019-08-02 03:44:05 +00:00
|
|
|
DiscoveryGraphNodeTypeRouter = "router"
|
|
|
|
DiscoveryGraphNodeTypeSplitter = "splitter"
|
|
|
|
DiscoveryGraphNodeTypeResolver = "resolver"
|
2019-06-27 18:38:21 +00:00
|
|
|
)
|
|
|
|
|
2019-08-02 03:44:05 +00:00
|
|
|
// DiscoveryGraphNode is a single node in the compiled discovery chain.
|
2019-06-27 18:38:21 +00:00
|
|
|
type DiscoveryGraphNode struct {
|
|
|
|
Type string
|
2019-08-02 03:44:05 +00:00
|
|
|
Name string // this is NOT necessarily a service
|
2019-06-27 18:38:21 +00:00
|
|
|
|
|
|
|
// fields for Type==router
|
|
|
|
Routes []*DiscoveryRoute `json:",omitempty"`
|
|
|
|
|
|
|
|
// fields for Type==splitter
|
|
|
|
Splits []*DiscoverySplit `json:",omitempty"`
|
|
|
|
|
2019-08-02 03:44:05 +00:00
|
|
|
// fields for Type==resolver
|
|
|
|
Resolver *DiscoveryResolver `json:",omitempty"`
|
2020-08-28 19:11:04 +00:00
|
|
|
|
|
|
|
// shared by Type==resolver || Type==splitter
|
2020-09-02 15:10:50 +00:00
|
|
|
LoadBalancer *LoadBalancer `json:",omitempty"`
|
2019-08-02 03:44:05 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 14:18:45 +00:00
|
|
|
func (s *DiscoveryGraphNode) IsRouter() bool {
|
|
|
|
return s.Type == DiscoveryGraphNodeTypeRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DiscoveryGraphNode) IsSplitter() bool {
|
|
|
|
return s.Type == DiscoveryGraphNodeTypeSplitter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DiscoveryGraphNode) IsResolver() bool {
|
|
|
|
return s.Type == DiscoveryGraphNodeTypeResolver
|
|
|
|
}
|
|
|
|
|
2019-08-02 03:44:05 +00:00
|
|
|
func (s *DiscoveryGraphNode) MapKey() string {
|
|
|
|
return fmt.Sprintf("%s:%s", s.Type, s.Name)
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 03:44:05 +00:00
|
|
|
// compiled form of ServiceResolverConfigEntry
|
|
|
|
type DiscoveryResolver struct {
|
2023-06-13 15:03:30 +00:00
|
|
|
Default bool `json:",omitempty"`
|
|
|
|
ConnectTimeout time.Duration `json:",omitempty"`
|
|
|
|
RequestTimeout time.Duration `json:",omitempty"`
|
|
|
|
Target string `json:",omitempty"`
|
|
|
|
Failover *DiscoveryFailover `json:",omitempty"`
|
2019-08-02 03:44:05 +00:00
|
|
|
}
|
|
|
|
|
2019-08-19 20:31:05 +00:00
|
|
|
func (r *DiscoveryResolver) MarshalJSON() ([]byte, error) {
|
|
|
|
type Alias DiscoveryResolver
|
|
|
|
exported := &struct {
|
|
|
|
ConnectTimeout string `json:",omitempty"`
|
|
|
|
*Alias
|
|
|
|
}{
|
|
|
|
ConnectTimeout: r.ConnectTimeout.String(),
|
|
|
|
Alias: (*Alias)(r),
|
|
|
|
}
|
|
|
|
if r.ConnectTimeout == 0 {
|
|
|
|
exported.ConnectTimeout = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(exported)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DiscoveryResolver) UnmarshalJSON(data []byte) error {
|
|
|
|
type Alias DiscoveryResolver
|
|
|
|
aux := &struct {
|
|
|
|
ConnectTimeout string
|
|
|
|
*Alias
|
|
|
|
}{
|
|
|
|
Alias: (*Alias)(r),
|
|
|
|
}
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := lib.UnmarshalJSON(data, &aux); err != nil {
|
2019-08-19 20:31:05 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
if aux.ConnectTimeout != "" {
|
|
|
|
if r.ConnectTimeout, err = time.ParseDuration(aux.ConnectTimeout); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-27 18:38:21 +00:00
|
|
|
// compiled form of ServiceRoute
|
|
|
|
type DiscoveryRoute struct {
|
2019-08-02 03:44:05 +00:00
|
|
|
Definition *ServiceRoute `json:",omitempty"`
|
|
|
|
NextNode string `json:",omitempty"`
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// compiled form of ServiceSplit
|
|
|
|
type DiscoverySplit struct {
|
2021-07-13 18:49:14 +00:00
|
|
|
Definition *ServiceSplit `json:",omitempty"`
|
|
|
|
// Weight is not necessarily a duplicate of Definition.Weight since when
|
|
|
|
// multiple splits are compiled down to a single set of splits the effective
|
|
|
|
// weight of a split leg might not be the same as in the original definition.
|
|
|
|
// Proxies should use this compiled weight. The Definition is provided above
|
|
|
|
// for any other significant configuration that the proxy might need to apply
|
|
|
|
// to that leg of the split.
|
2019-08-02 03:44:05 +00:00
|
|
|
Weight float32 `json:",omitempty"`
|
|
|
|
NextNode string `json:",omitempty"`
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// compiled form of ServiceResolverFailover
|
|
|
|
type DiscoveryFailover struct {
|
2023-03-03 16:12:38 +00:00
|
|
|
Targets []string `json:",omitempty"`
|
|
|
|
Policy *ServiceResolverFailoverPolicy `json:",omitempty"`
|
2023-03-30 14:08:38 +00:00
|
|
|
Regions []string `json:",omitempty"`
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 20:42:54 +00:00
|
|
|
// compiled form of ServiceResolverPrioritizeByLocality
|
|
|
|
type DiscoveryPrioritizeByLocality struct {
|
|
|
|
Mode string `json:",omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pbl *ServiceResolverPrioritizeByLocality) ToDiscovery() *DiscoveryPrioritizeByLocality {
|
|
|
|
if pbl == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &DiscoveryPrioritizeByLocality{
|
|
|
|
Mode: pbl.Mode,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 18:38:21 +00:00
|
|
|
// DiscoveryTarget represents all of the inputs necessary to use a resolver
|
|
|
|
// config entry to execute a catalog query to generate a list of service
|
|
|
|
// instances during discovery.
|
|
|
|
type DiscoveryTarget struct {
|
2019-08-19 18:03:03 +00:00
|
|
|
// ID is a unique identifier for referring to this target in a compiled
|
|
|
|
// chain. It should be treated as a per-compile opaque string.
|
2019-08-02 20:34:54 +00:00
|
|
|
ID string `json:",omitempty"`
|
|
|
|
|
2023-03-10 17:59:47 +00:00
|
|
|
Service string `json:",omitempty"`
|
|
|
|
ServiceSubset string `json:",omitempty"`
|
|
|
|
Namespace string `json:",omitempty"`
|
|
|
|
Partition string `json:",omitempty"`
|
|
|
|
Datacenter string `json:",omitempty"`
|
|
|
|
Peer string `json:",omitempty"`
|
|
|
|
Locality *Locality `json:",omitempty"`
|
2019-06-27 18:38:21 +00:00
|
|
|
|
2022-12-13 15:16:31 +00:00
|
|
|
MeshGateway MeshGatewayConfig `json:",omitempty"`
|
|
|
|
Subset ServiceResolverSubset `json:",omitempty"`
|
|
|
|
TransparentProxy TransparentProxyConfig `json:",omitempty"`
|
2019-08-19 17:19:44 +00:00
|
|
|
|
2022-04-07 21:58:21 +00:00
|
|
|
ConnectTimeout time.Duration `json:",omitempty"`
|
|
|
|
|
2019-08-19 18:03:03 +00:00
|
|
|
// External is true if this target is outside of this consul cluster.
|
|
|
|
External bool `json:",omitempty"`
|
|
|
|
|
|
|
|
// SNI is the sni field to use when connecting to this set of endpoints
|
|
|
|
// over TLS.
|
|
|
|
SNI string `json:",omitempty"`
|
|
|
|
|
|
|
|
// Name is the unique name for this target for use when generating load
|
|
|
|
// balancer objects. This has a structure similar to SNI, but will not be
|
|
|
|
// affected by SNI customizations.
|
|
|
|
Name string `json:",omitempty"`
|
2023-06-13 15:03:30 +00:00
|
|
|
|
|
|
|
PrioritizeByLocality *DiscoveryPrioritizeByLocality `json:",omitempty"`
|
2019-08-02 03:44:05 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 21:58:21 +00:00
|
|
|
func (t *DiscoveryTarget) MarshalJSON() ([]byte, error) {
|
|
|
|
type Alias DiscoveryTarget
|
|
|
|
exported := struct {
|
|
|
|
ConnectTimeout string `json:",omitempty"`
|
|
|
|
*Alias
|
|
|
|
}{
|
|
|
|
ConnectTimeout: t.ConnectTimeout.String(),
|
|
|
|
Alias: (*Alias)(t),
|
|
|
|
}
|
|
|
|
if t.ConnectTimeout == 0 {
|
|
|
|
exported.ConnectTimeout = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(exported)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *DiscoveryTarget) UnmarshalJSON(data []byte) error {
|
|
|
|
type Alias DiscoveryTarget
|
|
|
|
aux := &struct {
|
|
|
|
ConnectTimeout string
|
|
|
|
*Alias
|
|
|
|
}{
|
|
|
|
Alias: (*Alias)(t),
|
|
|
|
}
|
|
|
|
if err := lib.UnmarshalJSON(data, &aux); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
if aux.ConnectTimeout != "" {
|
|
|
|
if t.ConnectTimeout, err = time.ParseDuration(aux.ConnectTimeout); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-23 13:13:43 +00:00
|
|
|
type DiscoveryTargetOpts struct {
|
2023-06-13 15:03:30 +00:00
|
|
|
Service string
|
|
|
|
ServiceSubset string
|
|
|
|
Namespace string
|
|
|
|
Partition string
|
|
|
|
Datacenter string
|
|
|
|
Peer string
|
|
|
|
PrioritizeByLocality *DiscoveryPrioritizeByLocality
|
2022-08-23 13:13:43 +00:00
|
|
|
}
|
|
|
|
|
2023-03-31 18:35:56 +00:00
|
|
|
func MergeDiscoveryTargetOpts(opts ...DiscoveryTargetOpts) DiscoveryTargetOpts {
|
|
|
|
var final DiscoveryTargetOpts
|
|
|
|
for _, o := range opts {
|
|
|
|
if o.Service != "" {
|
|
|
|
final.Service = o.Service
|
|
|
|
}
|
2023-03-20 13:12:37 +00:00
|
|
|
|
2023-03-31 18:35:56 +00:00
|
|
|
if o.ServiceSubset != "" {
|
|
|
|
final.ServiceSubset = o.ServiceSubset
|
|
|
|
}
|
2023-03-20 13:12:37 +00:00
|
|
|
|
2023-03-31 18:35:56 +00:00
|
|
|
// default should override the existing value
|
|
|
|
if o.Namespace != "" {
|
|
|
|
final.Namespace = o.Namespace
|
|
|
|
}
|
2023-03-20 13:12:37 +00:00
|
|
|
|
2023-03-31 18:35:56 +00:00
|
|
|
// default should override the existing value
|
|
|
|
if o.Partition != "" {
|
|
|
|
final.Partition = o.Partition
|
|
|
|
}
|
2023-03-20 13:12:37 +00:00
|
|
|
|
2023-03-31 18:35:56 +00:00
|
|
|
if o.Datacenter != "" {
|
|
|
|
final.Datacenter = o.Datacenter
|
|
|
|
}
|
2023-03-20 13:12:37 +00:00
|
|
|
|
2023-03-31 18:35:56 +00:00
|
|
|
if o.Peer != "" {
|
|
|
|
final.Peer = o.Peer
|
|
|
|
}
|
2023-03-20 13:12:37 +00:00
|
|
|
}
|
|
|
|
|
2023-03-31 18:35:56 +00:00
|
|
|
return final
|
2023-03-20 13:12:37 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 13:13:43 +00:00
|
|
|
func NewDiscoveryTarget(opts DiscoveryTargetOpts) *DiscoveryTarget {
|
2019-08-02 20:34:54 +00:00
|
|
|
t := &DiscoveryTarget{
|
2022-08-23 13:13:43 +00:00
|
|
|
Service: opts.Service,
|
|
|
|
ServiceSubset: opts.ServiceSubset,
|
|
|
|
Namespace: opts.Namespace,
|
|
|
|
Partition: opts.Partition,
|
|
|
|
Datacenter: opts.Datacenter,
|
|
|
|
Peer: opts.Peer,
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
2019-08-02 20:34:54 +00:00
|
|
|
t.setID()
|
|
|
|
return t
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 13:13:43 +00:00
|
|
|
func (t *DiscoveryTarget) ToDiscoveryTargetOpts() DiscoveryTargetOpts {
|
|
|
|
return DiscoveryTargetOpts{
|
|
|
|
Service: t.Service,
|
|
|
|
ServiceSubset: t.ServiceSubset,
|
|
|
|
Namespace: t.Namespace,
|
|
|
|
Partition: t.Partition,
|
|
|
|
Datacenter: t.Datacenter,
|
|
|
|
Peer: t.Peer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 18:56:18 +00:00
|
|
|
func ChainID(opts DiscoveryTargetOpts) string {
|
2019-08-02 20:34:54 +00:00
|
|
|
// NOTE: this format is similar to the SNI syntax for simplicity
|
2022-08-23 13:13:43 +00:00
|
|
|
if opts.Peer != "" {
|
2023-03-24 20:40:19 +00:00
|
|
|
return fmt.Sprintf("%s.%s.%s.external.%s", opts.Service, opts.Namespace, opts.Partition, opts.Peer)
|
2022-08-23 13:13:43 +00:00
|
|
|
}
|
|
|
|
if opts.ServiceSubset == "" {
|
|
|
|
return fmt.Sprintf("%s.%s.%s.%s", opts.Service, opts.Namespace, opts.Partition, opts.Datacenter)
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
2022-08-23 13:13:43 +00:00
|
|
|
return fmt.Sprintf("%s.%s.%s.%s.%s", opts.ServiceSubset, opts.Service, opts.Namespace, opts.Partition, opts.Datacenter)
|
2021-05-04 14:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *DiscoveryTarget) setID() {
|
2023-03-22 18:56:18 +00:00
|
|
|
t.ID = ChainID(t.ToDiscoveryTargetOpts())
|
2019-08-02 20:34:54 +00:00
|
|
|
}
|
2019-06-27 18:38:21 +00:00
|
|
|
|
2019-08-02 20:34:54 +00:00
|
|
|
func (t *DiscoveryTarget) String() string {
|
|
|
|
return t.ID
|
2019-06-27 18:38:21 +00:00
|
|
|
}
|
2020-01-24 15:04:58 +00:00
|
|
|
|
|
|
|
func (t *DiscoveryTarget) ServiceID() ServiceID {
|
|
|
|
return NewServiceID(t.Service, t.GetEnterpriseMetadata())
|
|
|
|
}
|
2022-06-27 19:37:18 +00:00
|
|
|
|
|
|
|
func (t *DiscoveryTarget) ServiceName() ServiceName {
|
|
|
|
return NewServiceName(t.Service, t.GetEnterpriseMetadata())
|
|
|
|
}
|