2018-10-03 12:36:38 +00:00
|
|
|
package proxycfg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-06-03 21:28:45 +00:00
|
|
|
"net"
|
2018-10-03 12:36:38 +00:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-11-10 23:57:35 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/mitchellh/copystructure"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
|
2018-10-03 12:36:38 +00:00
|
|
|
"github.com/hashicorp/consul/agent/cache"
|
|
|
|
cachetype "github.com/hashicorp/consul/agent/cache-types"
|
2021-06-09 20:34:17 +00:00
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
2018-10-03 12:36:38 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/hashicorp/consul/logging"
|
2018-10-03 12:36:38 +00:00
|
|
|
)
|
|
|
|
|
2019-07-12 21:19:37 +00:00
|
|
|
type CacheNotifier interface {
|
|
|
|
Notify(ctx context.Context, t string, r cache.Request,
|
|
|
|
correlationID string, ch chan<- cache.UpdateEvent) error
|
|
|
|
}
|
|
|
|
|
2021-02-23 17:52:54 +00:00
|
|
|
type Health interface {
|
|
|
|
Notify(ctx context.Context, req structs.ServiceSpecificRequest, correlationID string, ch chan<- cache.UpdateEvent) error
|
|
|
|
}
|
|
|
|
|
2018-10-03 12:36:38 +00:00
|
|
|
const (
|
2020-03-09 20:59:02 +00:00
|
|
|
coalesceTimeout = 200 * time.Millisecond
|
|
|
|
rootsWatchID = "roots"
|
|
|
|
leafWatchID = "leaf"
|
|
|
|
intentionsWatchID = "intentions"
|
|
|
|
serviceListWatchID = "service-list"
|
|
|
|
federationStateListGatewaysWatchID = "federation-state-list-mesh-gateways"
|
|
|
|
consulServerListWatchID = "consul-server-list"
|
|
|
|
datacentersWatchID = "datacenters"
|
|
|
|
serviceResolversWatchID = "service-resolvers"
|
2020-04-16 21:00:48 +00:00
|
|
|
gatewayServicesWatchID = "gateway-services"
|
2020-04-27 23:36:20 +00:00
|
|
|
gatewayConfigWatchID = "gateway-config"
|
2020-04-20 19:42:33 +00:00
|
|
|
externalServiceIDPrefix = "external-service:"
|
|
|
|
serviceLeafIDPrefix = "service-leaf:"
|
2020-08-27 17:20:58 +00:00
|
|
|
serviceConfigIDPrefix = "service-config:"
|
2020-04-20 19:42:33 +00:00
|
|
|
serviceResolverIDPrefix = "service-resolver:"
|
|
|
|
serviceIntentionsIDPrefix = "service-intentions:"
|
2021-03-17 19:40:39 +00:00
|
|
|
intentionUpstreamsID = "intention-upstreams"
|
2021-04-28 22:13:29 +00:00
|
|
|
meshConfigEntryID = "mesh"
|
2020-03-09 20:59:02 +00:00
|
|
|
svcChecksWatchIDPrefix = cachetype.ServiceHTTPChecksName + ":"
|
|
|
|
serviceIDPrefix = string(structs.UpstreamDestTypeService) + ":"
|
|
|
|
preparedQueryIDPrefix = string(structs.UpstreamDestTypePreparedQuery) + ":"
|
|
|
|
defaultPreparedQueryPollInterval = 30 * time.Second
|
2018-10-03 12:36:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// state holds all the state needed to maintain the config for a registered
|
|
|
|
// connect-proxy service. When a proxy registration is changed, the entire state
|
|
|
|
// is discarded and a new one created.
|
|
|
|
type state struct {
|
|
|
|
// logger, source and cache are required to be set before calling Watch.
|
2020-08-27 17:20:58 +00:00
|
|
|
logger hclog.Logger
|
|
|
|
source *structs.QuerySource
|
|
|
|
cache CacheNotifier
|
2021-02-23 17:52:54 +00:00
|
|
|
health Health
|
2020-08-27 17:20:58 +00:00
|
|
|
dnsConfig DNSConfig
|
|
|
|
serverSNIFn ServerSNIFunc
|
|
|
|
intentionDefaultAllow bool
|
2018-10-03 12:36:38 +00:00
|
|
|
|
2020-12-23 22:12:36 +00:00
|
|
|
// cancel is set by Watch and called by Close to stop the goroutine started
|
|
|
|
// in Watch.
|
2018-10-03 12:36:38 +00:00
|
|
|
cancel func()
|
|
|
|
|
2019-06-18 00:52:01 +00:00
|
|
|
kind structs.ServiceKind
|
|
|
|
service string
|
2020-01-24 15:04:58 +00:00
|
|
|
proxyID structs.ServiceID
|
2019-06-18 00:52:01 +00:00
|
|
|
address string
|
|
|
|
port int
|
2020-03-09 20:59:02 +00:00
|
|
|
meta map[string]string
|
2019-06-18 00:52:01 +00:00
|
|
|
taggedAddresses map[string]structs.ServiceAddress
|
|
|
|
proxyCfg structs.ConnectProxyConfig
|
|
|
|
token string
|
2018-10-03 12:36:38 +00:00
|
|
|
|
|
|
|
ch chan cache.UpdateEvent
|
|
|
|
snapCh chan ConfigSnapshot
|
|
|
|
reqCh chan chan *ConfigSnapshot
|
|
|
|
}
|
|
|
|
|
2020-04-27 23:36:20 +00:00
|
|
|
type DNSConfig struct {
|
|
|
|
Domain string
|
|
|
|
AltDomain string
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
type ServerSNIFunc func(dc, nodeName string) string
|
|
|
|
|
2020-02-06 15:52:25 +00:00
|
|
|
func copyProxyConfig(ns *structs.NodeService) (structs.ConnectProxyConfig, error) {
|
|
|
|
if ns == nil {
|
|
|
|
return structs.ConnectProxyConfig{}, nil
|
|
|
|
}
|
|
|
|
// Copy the config map
|
|
|
|
proxyCfgRaw, err := copystructure.Copy(ns.Proxy)
|
|
|
|
if err != nil {
|
|
|
|
return structs.ConnectProxyConfig{}, err
|
|
|
|
}
|
|
|
|
proxyCfg, ok := proxyCfgRaw.(structs.ConnectProxyConfig)
|
|
|
|
if !ok {
|
|
|
|
return structs.ConnectProxyConfig{}, errors.New("failed to copy proxy config")
|
|
|
|
}
|
|
|
|
|
|
|
|
// we can safely modify these since we just copied them
|
2020-06-16 17:19:31 +00:00
|
|
|
for idx := range proxyCfg.Upstreams {
|
2020-02-06 15:52:25 +00:00
|
|
|
us := &proxyCfg.Upstreams[idx]
|
|
|
|
if us.DestinationType != structs.UpstreamDestTypePreparedQuery && us.DestinationNamespace == "" {
|
|
|
|
// default the upstreams target namespace to the namespace of the proxy
|
|
|
|
// doing this here prevents needing much more complex logic a bunch of other
|
|
|
|
// places and makes tracking these upstreams simpler as we can dedup them
|
|
|
|
// with the maps tracking upstream ids being watched.
|
|
|
|
proxyCfg.Upstreams[idx].DestinationNamespace = ns.EnterpriseMeta.NamespaceOrDefault()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return proxyCfg, nil
|
|
|
|
}
|
|
|
|
|
2018-10-03 12:36:38 +00:00
|
|
|
// newState populates the state struct by copying relevant fields from the
|
|
|
|
// NodeService and Token. We copy so that we can use them in a separate
|
|
|
|
// goroutine later without reasoning about races with the NodeService passed
|
|
|
|
// (especially for embedded fields like maps and slices).
|
|
|
|
//
|
2019-07-02 03:10:51 +00:00
|
|
|
// The returned state needs its required dependencies to be set before Watch
|
2018-10-03 12:36:38 +00:00
|
|
|
// can be called.
|
|
|
|
func newState(ns *structs.NodeService, token string) (*state, error) {
|
2020-04-16 21:00:48 +00:00
|
|
|
switch ns.Kind {
|
|
|
|
case structs.ServiceKindConnectProxy:
|
2020-04-10 18:06:08 +00:00
|
|
|
case structs.ServiceKindTerminatingGateway:
|
2020-04-16 21:00:48 +00:00
|
|
|
case structs.ServiceKindMeshGateway:
|
|
|
|
case structs.ServiceKindIngressGateway:
|
|
|
|
default:
|
2020-04-10 18:06:08 +00:00
|
|
|
return nil, errors.New("not a connect-proxy, terminating-gateway, mesh-gateway, or ingress-gateway")
|
2018-10-03 12:36:38 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 15:52:25 +00:00
|
|
|
proxyCfg, err := copyProxyConfig(ns)
|
2018-10-03 12:36:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-18 00:52:01 +00:00
|
|
|
taggedAddresses := make(map[string]structs.ServiceAddress)
|
|
|
|
for k, v := range ns.TaggedAddresses {
|
|
|
|
taggedAddresses[k] = v
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
meta := make(map[string]string)
|
|
|
|
for k, v := range ns.Meta {
|
|
|
|
meta[k] = v
|
|
|
|
}
|
|
|
|
|
2018-10-03 12:36:38 +00:00
|
|
|
return &state{
|
2019-06-18 00:52:01 +00:00
|
|
|
kind: ns.Kind,
|
|
|
|
service: ns.Service,
|
2020-01-24 15:04:58 +00:00
|
|
|
proxyID: ns.CompoundServiceID(),
|
2019-06-18 00:52:01 +00:00
|
|
|
address: ns.Address,
|
|
|
|
port: ns.Port,
|
2020-03-09 20:59:02 +00:00
|
|
|
meta: meta,
|
2019-06-18 00:52:01 +00:00
|
|
|
taggedAddresses: taggedAddresses,
|
|
|
|
proxyCfg: proxyCfg,
|
|
|
|
token: token,
|
2020-11-10 23:57:35 +00:00
|
|
|
|
2018-10-03 12:36:38 +00:00
|
|
|
// 10 is fairly arbitrary here but allow for the 3 mandatory and a
|
|
|
|
// reasonable number of upstream watches to all deliver their initial
|
|
|
|
// messages in parallel without blocking the cache.Notify loops. It's not a
|
|
|
|
// huge deal if we do for a short period so we don't need to be more
|
|
|
|
// conservative to handle larger numbers of upstreams correctly but gives
|
|
|
|
// some head room for normal operation to be non-blocking in most typical
|
|
|
|
// cases.
|
|
|
|
ch: make(chan cache.UpdateEvent, 10),
|
|
|
|
snapCh: make(chan ConfigSnapshot, 1),
|
|
|
|
reqCh: make(chan chan *ConfigSnapshot, 1),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-06 17:13:28 +00:00
|
|
|
// Watch initialized watches on all necessary cache data for the current proxy
|
2018-10-03 12:36:38 +00:00
|
|
|
// registration state and returns a chan to observe updates to the
|
|
|
|
// ConfigSnapshot that contains all necessary config state. The chan is closed
|
|
|
|
// when the state is Closed.
|
|
|
|
func (s *state) Watch() (<-chan ConfigSnapshot, error) {
|
2020-12-23 22:12:36 +00:00
|
|
|
var ctx context.Context
|
|
|
|
ctx, s.cancel = context.WithCancel(context.Background())
|
2018-10-03 12:36:38 +00:00
|
|
|
|
2021-03-17 19:40:39 +00:00
|
|
|
snap := s.initialConfigSnapshot()
|
2020-12-23 22:12:36 +00:00
|
|
|
err := s.initWatches(ctx, &snap)
|
2018-10-03 12:36:38 +00:00
|
|
|
if err != nil {
|
|
|
|
s.cancel()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-23 22:12:36 +00:00
|
|
|
go s.run(ctx, &snap)
|
2018-10-03 12:36:38 +00:00
|
|
|
|
|
|
|
return s.snapCh, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close discards the state and stops any long-running watches.
|
|
|
|
func (s *state) Close() error {
|
|
|
|
if s.cancel != nil {
|
|
|
|
s.cancel()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-24 19:05:36 +00:00
|
|
|
// initWatches sets up the watches needed for the particular service
|
2020-12-23 22:12:36 +00:00
|
|
|
func (s *state) initWatches(ctx context.Context, snap *ConfigSnapshot) error {
|
2019-06-24 19:05:36 +00:00
|
|
|
switch s.kind {
|
|
|
|
case structs.ServiceKindConnectProxy:
|
2020-12-23 22:12:36 +00:00
|
|
|
return s.initWatchesConnectProxy(ctx, snap)
|
2020-04-10 18:06:08 +00:00
|
|
|
case structs.ServiceKindTerminatingGateway:
|
2020-12-23 22:12:36 +00:00
|
|
|
return s.initWatchesTerminatingGateway(ctx)
|
2019-06-18 00:52:01 +00:00
|
|
|
case structs.ServiceKindMeshGateway:
|
2020-12-23 22:12:36 +00:00
|
|
|
return s.initWatchesMeshGateway(ctx)
|
2020-04-16 21:00:48 +00:00
|
|
|
case structs.ServiceKindIngressGateway:
|
2020-12-23 22:12:36 +00:00
|
|
|
return s.initWatchesIngressGateway(ctx)
|
2019-06-24 19:05:36 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unsupported service kind")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 18:30:35 +00:00
|
|
|
func (s *state) watchMeshGateway(ctx context.Context, dc string, upstreamID string) error {
|
|
|
|
return s.cache.Notify(ctx, cachetype.InternalServiceDumpName, &structs.ServiceDumpRequest{
|
|
|
|
Datacenter: dc,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
ServiceKind: structs.ServiceKindMeshGateway,
|
|
|
|
UseServiceKind: true,
|
|
|
|
Source: *s.source,
|
2020-01-24 15:04:58 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMeta(),
|
2019-08-05 18:30:35 +00:00
|
|
|
}, "mesh-gateway:"+dc+":"+upstreamID, s.ch)
|
|
|
|
}
|
|
|
|
|
2019-06-24 19:05:36 +00:00
|
|
|
// initWatchesConnectProxy sets up the watches needed based on current proxy registration
|
|
|
|
// state.
|
2020-12-23 22:12:36 +00:00
|
|
|
func (s *state) initWatchesConnectProxy(ctx context.Context, snap *ConfigSnapshot) error {
|
2018-10-03 12:36:38 +00:00
|
|
|
// Watch for root changes
|
2020-12-23 22:12:36 +00:00
|
|
|
err := s.cache.Notify(ctx, cachetype.ConnectCARootName, &structs.DCSpecificRequest{
|
2018-10-03 12:36:38 +00:00
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
2019-07-04 15:17:49 +00:00
|
|
|
Source: *s.source,
|
2018-10-03 12:36:38 +00:00
|
|
|
}, rootsWatchID, s.ch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch the leaf cert
|
2020-12-23 22:12:36 +00:00
|
|
|
err = s.cache.Notify(ctx, cachetype.ConnectCALeafName, &cachetype.ConnectCALeafRequest{
|
2020-01-24 15:04:58 +00:00
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
Token: s.token,
|
|
|
|
Service: s.proxyCfg.DestinationServiceName,
|
|
|
|
EnterpriseMeta: s.proxyID.EnterpriseMeta,
|
2018-10-03 12:36:38 +00:00
|
|
|
}, leafWatchID, s.ch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch for intention updates
|
2020-12-23 22:12:36 +00:00
|
|
|
err = s.cache.Notify(ctx, cachetype.IntentionMatchName, &structs.IntentionQueryRequest{
|
2018-10-03 12:36:38 +00:00
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
Match: &structs.IntentionQueryMatch{
|
|
|
|
Type: structs.IntentionMatchDestination,
|
|
|
|
Entries: []structs.IntentionMatchEntry{
|
|
|
|
{
|
2020-01-24 15:04:58 +00:00
|
|
|
Namespace: s.proxyID.NamespaceOrDefault(),
|
2018-10-03 12:36:38 +00:00
|
|
|
Name: s.proxyCfg.DestinationServiceName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, intentionsWatchID, s.ch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-26 02:55:52 +00:00
|
|
|
// Watch for service check updates
|
2020-12-23 22:12:36 +00:00
|
|
|
err = s.cache.Notify(ctx, cachetype.ServiceHTTPChecksName, &cachetype.ServiceHTTPChecksRequest{
|
2020-01-24 15:04:58 +00:00
|
|
|
ServiceID: s.proxyCfg.DestinationServiceID,
|
|
|
|
EnterpriseMeta: s.proxyID.EnterpriseMeta,
|
|
|
|
}, svcChecksWatchIDPrefix+structs.ServiceIDString(s.proxyCfg.DestinationServiceID, &s.proxyID.EnterpriseMeta), s.ch)
|
2019-09-26 02:55:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-06 15:52:25 +00:00
|
|
|
// default the namespace to the namespace of this proxy service
|
|
|
|
currentNamespace := s.proxyID.NamespaceOrDefault()
|
2019-08-02 03:03:34 +00:00
|
|
|
|
2021-04-12 15:35:14 +00:00
|
|
|
if s.proxyCfg.Mode == structs.ProxyModeTransparent {
|
2021-03-17 19:40:39 +00:00
|
|
|
// When in transparent proxy we will infer upstreams from intentions with this source
|
2020-12-23 22:12:36 +00:00
|
|
|
err := s.cache.Notify(ctx, cachetype.IntentionUpstreamsName, &structs.ServiceSpecificRequest{
|
2021-03-17 19:40:39 +00:00
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
ServiceName: s.proxyCfg.DestinationServiceName,
|
|
|
|
EnterpriseMeta: structs.NewEnterpriseMeta(s.proxyID.NamespaceOrEmpty()),
|
|
|
|
}, intentionUpstreamsID, s.ch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-06 18:19:59 +00:00
|
|
|
|
2020-12-23 22:12:36 +00:00
|
|
|
err = s.cache.Notify(ctx, cachetype.ConfigEntryName, &structs.ConfigEntryQuery{
|
2021-04-28 22:13:29 +00:00
|
|
|
Kind: structs.MeshConfig,
|
|
|
|
Name: structs.MeshConfigMesh,
|
2021-04-06 18:19:59 +00:00
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMeta(),
|
2021-04-28 22:13:29 +00:00
|
|
|
}, meshConfigEntryID, s.ch)
|
2021-04-06 18:19:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-17 19:40:39 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 12:36:38 +00:00
|
|
|
// Watch for updates to service endpoints for all upstreams
|
2021-03-17 22:18:56 +00:00
|
|
|
for i := range s.proxyCfg.Upstreams {
|
|
|
|
u := s.proxyCfg.Upstreams[i]
|
|
|
|
|
2021-04-06 20:48:54 +00:00
|
|
|
// Store defaults keyed under wildcard so they can be applied to centrally configured upstreams
|
|
|
|
if u.DestinationName == structs.WildcardSpecifier {
|
|
|
|
snap.ConnectProxy.UpstreamConfig[u.DestinationID().String()] = &u
|
2021-04-08 17:16:03 +00:00
|
|
|
continue
|
2021-04-06 20:48:54 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 19:40:39 +00:00
|
|
|
// This can be true if the upstream is a synthetic entry populated from centralized upstream config.
|
|
|
|
// Watches should not be created for them.
|
|
|
|
if u.CentrallyConfigured {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
snap.ConnectProxy.UpstreamConfig[u.Identifier()] = &u
|
|
|
|
|
2018-10-03 12:36:38 +00:00
|
|
|
dc := s.source.Datacenter
|
|
|
|
if u.Datacenter != "" {
|
|
|
|
dc = u.Datacenter
|
|
|
|
}
|
2021-04-12 15:35:14 +00:00
|
|
|
if s.proxyCfg.Mode == structs.ProxyModeTransparent && (dc == "" || dc == s.source.Datacenter) {
|
|
|
|
// In transparent proxy mode, watches for upstreams in the local DC are handled by the IntentionUpstreams watch.
|
2021-03-17 19:40:39 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-10-03 12:36:38 +00:00
|
|
|
|
2019-08-02 03:03:34 +00:00
|
|
|
ns := currentNamespace
|
|
|
|
if u.DestinationNamespace != "" {
|
|
|
|
ns = u.DestinationNamespace
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg, err := parseReducedUpstreamConfig(u.Config)
|
|
|
|
if err != nil {
|
|
|
|
// Don't hard fail on a config typo, just warn. We'll fall back on
|
|
|
|
// the plain discovery chain 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 config",
|
|
|
|
"upstream", u.Identifier(),
|
|
|
|
"error", err,
|
|
|
|
)
|
2019-08-02 03:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 12:36:38 +00:00
|
|
|
switch u.DestinationType {
|
|
|
|
case structs.UpstreamDestTypePreparedQuery:
|
2020-12-23 22:12:36 +00:00
|
|
|
err = s.cache.Notify(ctx, cachetype.PreparedQueryName, &structs.PreparedQueryExecuteRequest{
|
2019-01-18 17:44:04 +00:00
|
|
|
Datacenter: dc,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token, MaxAge: defaultPreparedQueryPollInterval},
|
|
|
|
QueryIDOrName: u.DestinationName,
|
|
|
|
Connect: true,
|
2019-07-04 15:17:49 +00:00
|
|
|
Source: *s.source,
|
2019-04-29 16:27:57 +00:00
|
|
|
}, "upstream:"+u.Identifier(), s.ch)
|
2020-02-18 13:41:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-02 03:03:34 +00:00
|
|
|
|
2018-10-03 12:36:38 +00:00
|
|
|
case structs.UpstreamDestTypeService:
|
|
|
|
fallthrough
|
2019-07-02 03:10:51 +00:00
|
|
|
|
2019-08-02 03:03:34 +00:00
|
|
|
case "": // Treat unset as the default Service type
|
2020-12-23 22:12:36 +00:00
|
|
|
err = s.cache.Notify(ctx, cachetype.CompiledDiscoveryChainName, &structs.DiscoveryChainRequest{
|
2019-08-02 03:03:34 +00:00
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
Name: u.DestinationName,
|
|
|
|
EvaluateInDatacenter: dc,
|
|
|
|
EvaluateInNamespace: ns,
|
|
|
|
OverrideMeshGateway: s.proxyCfg.MeshGateway.OverlayWith(u.MeshGateway),
|
|
|
|
OverrideProtocol: cfg.Protocol,
|
|
|
|
OverrideConnectTimeout: cfg.ConnectTimeout(),
|
|
|
|
}, "discovery-chain:"+u.Identifier(), s.ch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-10-03 12:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown upstream type: %q", u.DestinationType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-02 03:03:34 +00:00
|
|
|
// reducedProxyConfig represents the basic opaque config values that are now
|
|
|
|
// managed with the discovery chain but for backwards compatibility reasons
|
|
|
|
// should still affect how the proxy is configured.
|
|
|
|
//
|
|
|
|
// The full-blown config is agent/xds.UpstreamConfig
|
|
|
|
type reducedUpstreamConfig struct {
|
|
|
|
Protocol string `mapstructure:"protocol"`
|
|
|
|
ConnectTimeoutMs int `mapstructure:"connect_timeout_ms"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *reducedUpstreamConfig) ConnectTimeout() time.Duration {
|
|
|
|
return time.Duration(c.ConnectTimeoutMs) * time.Millisecond
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseReducedUpstreamConfig(m map[string]interface{}) (reducedUpstreamConfig, error) {
|
|
|
|
var cfg reducedUpstreamConfig
|
|
|
|
err := mapstructure.WeakDecode(m, &cfg)
|
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
|
2020-04-10 18:06:08 +00:00
|
|
|
// initWatchesTerminatingGateway sets up the initial watches needed based on the terminating-gateway registration
|
2020-12-23 22:12:36 +00:00
|
|
|
func (s *state) initWatchesTerminatingGateway(ctx context.Context) error {
|
2020-04-10 18:06:08 +00:00
|
|
|
// Watch for root changes
|
2020-12-23 22:12:36 +00:00
|
|
|
err := s.cache.Notify(ctx, cachetype.ConnectCARootName, &structs.DCSpecificRequest{
|
2020-04-10 18:06:08 +00:00
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
Source: *s.source,
|
|
|
|
}, rootsWatchID, s.ch)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Named(logging.TerminatingGateway).
|
|
|
|
Error("failed to register watch for root changes", "error", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch for the terminating-gateway's linked services
|
2020-12-23 22:12:36 +00:00
|
|
|
err = s.cache.Notify(ctx, cachetype.GatewayServicesName, &structs.ServiceSpecificRequest{
|
2020-04-10 18:06:08 +00:00
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
ServiceName: s.service,
|
|
|
|
EnterpriseMeta: s.proxyID.EnterpriseMeta,
|
|
|
|
}, gatewayServicesWatchID, s.ch)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Named(logging.TerminatingGateway).
|
|
|
|
Error("failed to register watch for linked services", "error", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-18 00:52:01 +00:00
|
|
|
// initWatchesMeshGateway sets up the watches needed based on the current mesh gateway registration
|
2020-12-23 22:12:36 +00:00
|
|
|
func (s *state) initWatchesMeshGateway(ctx context.Context) error {
|
2019-06-18 00:52:01 +00:00
|
|
|
// Watch for root changes
|
2020-12-23 22:12:36 +00:00
|
|
|
err := s.cache.Notify(ctx, cachetype.ConnectCARootName, &structs.DCSpecificRequest{
|
2019-06-18 00:52:01 +00:00
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
2019-07-04 15:17:49 +00:00
|
|
|
Source: *s.source,
|
2019-06-18 00:52:01 +00:00
|
|
|
}, rootsWatchID, s.ch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch for all services
|
2020-12-23 22:12:36 +00:00
|
|
|
err = s.cache.Notify(ctx, cachetype.CatalogServiceListName, &structs.DCSpecificRequest{
|
2020-01-24 15:04:58 +00:00
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
Source: *s.source,
|
|
|
|
EnterpriseMeta: *structs.WildcardEnterpriseMeta(),
|
2019-06-18 00:52:01 +00:00
|
|
|
}, serviceListWatchID, s.ch)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
if s.meta[structs.MetaWANFederationKey] == "1" {
|
|
|
|
// Conveniently we can just use this service meta attribute in one
|
|
|
|
// place here to set the machinery in motion and leave the conditional
|
|
|
|
// behavior out of the rest of the package.
|
2020-12-23 22:12:36 +00:00
|
|
|
err = s.cache.Notify(ctx, cachetype.FederationStateListMeshGatewaysName, &structs.DCSpecificRequest{
|
2020-03-09 20:59:02 +00:00
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
Source: *s.source,
|
|
|
|
}, federationStateListGatewaysWatchID, s.ch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-23 22:12:36 +00:00
|
|
|
err = s.health.Notify(ctx, structs.ServiceSpecificRequest{
|
2020-03-09 20:59:02 +00:00
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
ServiceName: structs.ConsulServiceName,
|
|
|
|
}, consulServerListWatchID, s.ch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 00:52:01 +00:00
|
|
|
// Eventually we will have to watch connect enable instances for each service as well as the
|
|
|
|
// destination services themselves but those notifications will be setup later. However we
|
|
|
|
// cannot setup those watches until we know what the services are. from the service list
|
|
|
|
// watch above
|
|
|
|
|
2020-12-23 22:12:36 +00:00
|
|
|
err = s.cache.Notify(ctx, cachetype.CatalogDatacentersName, &structs.DatacentersRequest{
|
2019-06-18 00:52:01 +00:00
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token, MaxAge: 30 * time.Second},
|
|
|
|
}, datacentersWatchID, s.ch)
|
2020-02-18 13:41:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-18 00:52:01 +00:00
|
|
|
|
|
|
|
// Once we start getting notified about the datacenters we will setup watches on the
|
|
|
|
// gateways within those other datacenters. We cannot do that here because we don't
|
|
|
|
// know what they are yet.
|
|
|
|
|
2020-01-10 15:30:13 +00:00
|
|
|
// Watch service-resolvers so we can setup service subset clusters
|
2020-12-23 22:12:36 +00:00
|
|
|
err = s.cache.Notify(ctx, cachetype.ConfigEntriesName, &structs.ConfigEntryQuery{
|
2020-01-24 15:04:58 +00:00
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
EnterpriseMeta: *structs.WildcardEnterpriseMeta(),
|
2020-01-10 15:30:13 +00:00
|
|
|
}, serviceResolversWatchID, s.ch)
|
|
|
|
|
|
|
|
if err != nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Named(logging.MeshGateway).
|
|
|
|
Error("failed to register watch for service-resolver config entries", "error", err)
|
2020-01-10 15:30:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-18 00:52:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-23 22:12:36 +00:00
|
|
|
func (s *state) initWatchesIngressGateway(ctx context.Context) error {
|
2020-04-16 21:00:48 +00:00
|
|
|
// Watch for root changes
|
2020-12-23 22:12:36 +00:00
|
|
|
err := s.cache.Notify(ctx, cachetype.ConnectCARootName, &structs.DCSpecificRequest{
|
2020-04-16 21:00:48 +00:00
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
Source: *s.source,
|
|
|
|
}, rootsWatchID, s.ch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-27 23:36:20 +00:00
|
|
|
// Watch this ingress gateway's config entry
|
2020-12-23 22:12:36 +00:00
|
|
|
err = s.cache.Notify(ctx, cachetype.ConfigEntryName, &structs.ConfigEntryQuery{
|
2020-05-07 15:22:57 +00:00
|
|
|
Kind: structs.IngressGateway,
|
|
|
|
Name: s.service,
|
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
EnterpriseMeta: s.proxyID.EnterpriseMeta,
|
2020-04-27 23:36:20 +00:00
|
|
|
}, gatewayConfigWatchID, s.ch)
|
2020-04-16 21:00:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch the ingress-gateway's list of upstreams
|
2020-12-23 22:12:36 +00:00
|
|
|
err = s.cache.Notify(ctx, cachetype.GatewayServicesName, &structs.ServiceSpecificRequest{
|
2020-04-16 21:00:48 +00:00
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
ServiceName: s.service,
|
|
|
|
EnterpriseMeta: s.proxyID.EnterpriseMeta,
|
|
|
|
}, gatewayServicesWatchID, s.ch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-12 21:19:37 +00:00
|
|
|
func (s *state) initialConfigSnapshot() ConfigSnapshot {
|
2018-10-03 12:36:38 +00:00
|
|
|
snap := ConfigSnapshot{
|
2020-08-27 17:20:58 +00:00
|
|
|
Kind: s.kind,
|
|
|
|
Service: s.service,
|
|
|
|
ProxyID: s.proxyID,
|
|
|
|
Address: s.address,
|
|
|
|
Port: s.port,
|
|
|
|
ServiceMeta: s.meta,
|
|
|
|
TaggedAddresses: s.taggedAddresses,
|
|
|
|
Proxy: s.proxyCfg,
|
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
ServerSNIFn: s.serverSNIFn,
|
|
|
|
IntentionDefaultAllow: s.intentionDefaultAllow,
|
2018-10-03 12:36:38 +00:00
|
|
|
}
|
2019-06-24 19:05:36 +00:00
|
|
|
|
|
|
|
switch s.kind {
|
|
|
|
case structs.ServiceKindConnectProxy:
|
2019-07-02 03:10:51 +00:00
|
|
|
snap.ConnectProxy.DiscoveryChain = make(map[string]*structs.CompiledDiscoveryChain)
|
2021-03-17 19:40:39 +00:00
|
|
|
snap.ConnectProxy.WatchedDiscoveryChains = make(map[string]context.CancelFunc)
|
2019-08-02 20:34:54 +00:00
|
|
|
snap.ConnectProxy.WatchedUpstreams = make(map[string]map[string]context.CancelFunc)
|
|
|
|
snap.ConnectProxy.WatchedUpstreamEndpoints = make(map[string]map[string]structs.CheckServiceNodes)
|
2019-08-05 18:30:35 +00:00
|
|
|
snap.ConnectProxy.WatchedGateways = make(map[string]map[string]context.CancelFunc)
|
|
|
|
snap.ConnectProxy.WatchedGatewayEndpoints = make(map[string]map[string]structs.CheckServiceNodes)
|
2020-01-24 15:04:58 +00:00
|
|
|
snap.ConnectProxy.WatchedServiceChecks = make(map[structs.ServiceID][]structs.CheckType)
|
2020-03-09 20:59:02 +00:00
|
|
|
snap.ConnectProxy.PreparedQueryEndpoints = make(map[string]structs.CheckServiceNodes)
|
2021-03-17 19:40:39 +00:00
|
|
|
snap.ConnectProxy.UpstreamConfig = make(map[string]*structs.Upstream)
|
2021-06-09 20:34:17 +00:00
|
|
|
snap.ConnectProxy.PassthroughUpstreams = make(map[string]ServicePassthroughAddrs)
|
2020-04-10 18:06:08 +00:00
|
|
|
case structs.ServiceKindTerminatingGateway:
|
2020-06-12 14:57:41 +00:00
|
|
|
snap.TerminatingGateway.WatchedServices = make(map[structs.ServiceName]context.CancelFunc)
|
|
|
|
snap.TerminatingGateway.WatchedIntentions = make(map[structs.ServiceName]context.CancelFunc)
|
2020-08-27 17:20:58 +00:00
|
|
|
snap.TerminatingGateway.Intentions = make(map[structs.ServiceName]structs.Intentions)
|
|
|
|
snap.TerminatingGateway.WatchedLeaves = make(map[structs.ServiceName]context.CancelFunc)
|
2020-06-12 14:57:41 +00:00
|
|
|
snap.TerminatingGateway.ServiceLeaves = make(map[structs.ServiceName]*structs.IssuedCert)
|
2020-08-27 17:20:58 +00:00
|
|
|
snap.TerminatingGateway.WatchedConfigs = make(map[structs.ServiceName]context.CancelFunc)
|
|
|
|
snap.TerminatingGateway.ServiceConfigs = make(map[structs.ServiceName]*structs.ServiceConfigResponse)
|
|
|
|
snap.TerminatingGateway.WatchedResolvers = make(map[structs.ServiceName]context.CancelFunc)
|
2020-06-12 14:57:41 +00:00
|
|
|
snap.TerminatingGateway.ServiceResolvers = make(map[structs.ServiceName]*structs.ServiceResolverConfigEntry)
|
2020-08-27 17:20:58 +00:00
|
|
|
snap.TerminatingGateway.ServiceResolversSet = make(map[structs.ServiceName]bool)
|
|
|
|
snap.TerminatingGateway.ServiceGroups = make(map[structs.ServiceName]structs.CheckServiceNodes)
|
2020-06-12 14:57:41 +00:00
|
|
|
snap.TerminatingGateway.GatewayServices = make(map[structs.ServiceName]structs.GatewayService)
|
|
|
|
snap.TerminatingGateway.HostnameServices = make(map[structs.ServiceName]structs.CheckServiceNodes)
|
2019-06-18 00:52:01 +00:00
|
|
|
case structs.ServiceKindMeshGateway:
|
2020-06-12 14:57:41 +00:00
|
|
|
snap.MeshGateway.WatchedServices = make(map[structs.ServiceName]context.CancelFunc)
|
2019-06-18 00:52:01 +00:00
|
|
|
snap.MeshGateway.WatchedDatacenters = make(map[string]context.CancelFunc)
|
2020-06-12 14:57:41 +00:00
|
|
|
snap.MeshGateway.ServiceGroups = make(map[structs.ServiceName]structs.CheckServiceNodes)
|
2019-06-18 00:52:01 +00:00
|
|
|
snap.MeshGateway.GatewayGroups = make(map[string]structs.CheckServiceNodes)
|
2020-06-12 14:57:41 +00:00
|
|
|
snap.MeshGateway.ServiceResolvers = make(map[structs.ServiceName]*structs.ServiceResolverConfigEntry)
|
2020-06-03 21:28:45 +00:00
|
|
|
snap.MeshGateway.HostnameDatacenters = make(map[string]structs.CheckServiceNodes)
|
2019-07-02 13:43:35 +00:00
|
|
|
// there is no need to initialize the map of service resolvers as we
|
|
|
|
// fully rebuild it every time we get updates
|
2020-04-16 21:00:48 +00:00
|
|
|
case structs.ServiceKindIngressGateway:
|
|
|
|
snap.IngressGateway.WatchedDiscoveryChains = make(map[string]context.CancelFunc)
|
|
|
|
snap.IngressGateway.DiscoveryChain = make(map[string]*structs.CompiledDiscoveryChain)
|
|
|
|
snap.IngressGateway.WatchedUpstreams = make(map[string]map[string]context.CancelFunc)
|
|
|
|
snap.IngressGateway.WatchedUpstreamEndpoints = make(map[string]map[string]structs.CheckServiceNodes)
|
|
|
|
snap.IngressGateway.WatchedGateways = make(map[string]map[string]context.CancelFunc)
|
|
|
|
snap.IngressGateway.WatchedGatewayEndpoints = make(map[string]map[string]structs.CheckServiceNodes)
|
2019-06-24 19:05:36 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 21:19:37 +00:00
|
|
|
return snap
|
|
|
|
}
|
|
|
|
|
2020-12-23 22:12:36 +00:00
|
|
|
func (s *state) run(ctx context.Context, snap *ConfigSnapshot) {
|
2019-07-12 21:19:37 +00:00
|
|
|
// Close the channel we return from Watch when we stop so consumers can stop
|
|
|
|
// watching and clean up their goroutines. It's important we do this here and
|
|
|
|
// not in Close since this routine sends on this chan and so might panic if it
|
|
|
|
// gets closed from another goroutine.
|
|
|
|
defer close(s.snapCh)
|
|
|
|
|
2018-10-03 12:36:38 +00:00
|
|
|
// This turns out to be really fiddly/painful by just using time.Timer.C
|
|
|
|
// directly in the code below since you can't detect when a timer is stopped
|
|
|
|
// vs waiting in order to know to reset it. So just use a chan to send
|
|
|
|
// ourselves messages.
|
|
|
|
sendCh := make(chan struct{})
|
|
|
|
var coalesceTimer *time.Timer
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2020-12-23 22:12:36 +00:00
|
|
|
case <-ctx.Done():
|
2018-10-03 12:36:38 +00:00
|
|
|
return
|
|
|
|
case u := <-s.ch:
|
2021-02-05 22:14:49 +00:00
|
|
|
s.logger.Trace("A blocking query returned; handling snapshot update")
|
2021-02-02 19:26:38 +00:00
|
|
|
|
2020-12-23 22:12:36 +00:00
|
|
|
if err := s.handleUpdate(ctx, u, snap); err != nil {
|
2021-02-05 22:14:49 +00:00
|
|
|
s.logger.Error("Failed to handle update from watch",
|
2021-02-02 19:26:38 +00:00
|
|
|
"id", u.CorrelationID, "error", err,
|
2020-01-28 23:50:41 +00:00
|
|
|
)
|
2018-10-03 12:36:38 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-sendCh:
|
|
|
|
// Make a deep copy of snap so we don't mutate any of the embedded structs
|
|
|
|
// etc on future updates.
|
|
|
|
snapCopy, err := snap.Clone()
|
|
|
|
if err != nil {
|
2021-02-05 22:14:49 +00:00
|
|
|
s.logger.Error("Failed to copy config snapshot for proxy",
|
|
|
|
"error", err,
|
2020-01-28 23:50:41 +00:00
|
|
|
)
|
2018-10-03 12:36:38 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-02-02 18:31:14 +00:00
|
|
|
|
|
|
|
select {
|
2021-02-08 16:45:45 +00:00
|
|
|
// Try to send
|
2021-02-02 18:31:14 +00:00
|
|
|
case s.snapCh <- *snapCopy:
|
2021-02-05 22:14:49 +00:00
|
|
|
s.logger.Trace("Delivered new snapshot to proxy config watchers")
|
2021-02-02 19:26:38 +00:00
|
|
|
|
2021-02-06 01:00:59 +00:00
|
|
|
// Allow the next change to trigger a send
|
|
|
|
coalesceTimer = nil
|
|
|
|
|
|
|
|
// Skip rest of loop - there is nothing to send since nothing changed on
|
|
|
|
// this iteration
|
|
|
|
continue
|
|
|
|
|
2021-02-08 16:45:45 +00:00
|
|
|
// Avoid blocking if a snapshot is already buffered in snapCh as this can result in a deadlock.
|
|
|
|
// See PR #9689 for more details.
|
2021-02-02 18:31:14 +00:00
|
|
|
default:
|
2021-02-05 22:14:49 +00:00
|
|
|
s.logger.Trace("Failed to deliver new snapshot to proxy config watchers")
|
2021-02-02 18:31:14 +00:00
|
|
|
|
2021-02-08 16:45:45 +00:00
|
|
|
// Reset the timer to retry later. This is to ensure we attempt to redeliver the updated snapshot shortly.
|
2021-02-06 01:00:59 +00:00
|
|
|
if coalesceTimer == nil {
|
|
|
|
coalesceTimer = time.AfterFunc(coalesceTimeout, func() {
|
|
|
|
sendCh <- struct{}{}
|
|
|
|
})
|
|
|
|
}
|
2018-10-03 12:36:38 +00:00
|
|
|
|
2021-02-06 01:00:59 +00:00
|
|
|
// Do not reset coalesceTimer since we just queued a timer-based refresh
|
|
|
|
continue
|
|
|
|
}
|
2018-10-03 12:36:38 +00:00
|
|
|
|
|
|
|
case replyCh := <-s.reqCh:
|
2021-02-05 22:14:49 +00:00
|
|
|
s.logger.Trace("A proxy config snapshot was requested")
|
2021-02-02 19:26:38 +00:00
|
|
|
|
2018-10-03 12:36:38 +00:00
|
|
|
if !snap.Valid() {
|
|
|
|
// Not valid yet just respond with nil and move on to next task.
|
|
|
|
replyCh <- nil
|
2021-02-02 19:26:38 +00:00
|
|
|
|
2021-02-05 22:14:49 +00:00
|
|
|
s.logger.Trace("The proxy's config snapshot is not valid yet")
|
2018-10-03 12:36:38 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Make a deep copy of snap so we don't mutate any of the embedded structs
|
|
|
|
// etc on future updates.
|
|
|
|
snapCopy, err := snap.Clone()
|
|
|
|
if err != nil {
|
2021-02-05 22:14:49 +00:00
|
|
|
s.logger.Error("Failed to copy config snapshot for proxy",
|
|
|
|
"error", err,
|
2020-01-28 23:50:41 +00:00
|
|
|
)
|
2018-10-03 12:36:38 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
replyCh <- snapCopy
|
|
|
|
|
|
|
|
// Skip rest of loop - there is nothing to send since nothing changed on
|
|
|
|
// this iteration
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if snap is complete enough to be a valid config to deliver to a
|
|
|
|
// proxy yet.
|
|
|
|
if snap.Valid() {
|
|
|
|
// Don't send it right away, set a short timer that will wait for updates
|
|
|
|
// from any of the other cache values and deliver them all together.
|
|
|
|
if coalesceTimer == nil {
|
|
|
|
coalesceTimer = time.AfterFunc(coalesceTimeout, func() {
|
|
|
|
// This runs in another goroutine so we can't just do the send
|
|
|
|
// directly here as access to snap is racy. Instead, signal the main
|
|
|
|
// loop above.
|
|
|
|
sendCh <- struct{}{}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 22:12:36 +00:00
|
|
|
func (s *state) handleUpdate(ctx context.Context, u cache.UpdateEvent, snap *ConfigSnapshot) error {
|
2019-06-24 19:05:36 +00:00
|
|
|
switch s.kind {
|
|
|
|
case structs.ServiceKindConnectProxy:
|
2020-12-23 22:12:36 +00:00
|
|
|
return s.handleUpdateConnectProxy(ctx, u, snap)
|
2020-04-10 18:06:08 +00:00
|
|
|
case structs.ServiceKindTerminatingGateway:
|
2020-12-23 22:12:36 +00:00
|
|
|
return s.handleUpdateTerminatingGateway(ctx, u, snap)
|
2019-06-18 00:52:01 +00:00
|
|
|
case structs.ServiceKindMeshGateway:
|
2020-12-23 22:12:36 +00:00
|
|
|
return s.handleUpdateMeshGateway(ctx, u, snap)
|
2020-04-16 21:00:48 +00:00
|
|
|
case structs.ServiceKindIngressGateway:
|
2020-12-23 22:12:36 +00:00
|
|
|
return s.handleUpdateIngressGateway(ctx, u, snap)
|
2019-06-24 19:05:36 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unsupported service kind")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 22:12:36 +00:00
|
|
|
func (s *state) handleUpdateConnectProxy(ctx context.Context, u cache.UpdateEvent, snap *ConfigSnapshot) error {
|
2019-09-26 15:42:17 +00:00
|
|
|
if u.Err != nil {
|
|
|
|
return fmt.Errorf("error filling agent cache: %v", u.Err)
|
|
|
|
}
|
|
|
|
|
2019-07-02 03:10:51 +00:00
|
|
|
switch {
|
|
|
|
case u.CorrelationID == rootsWatchID:
|
2018-10-03 12:36:38 +00:00
|
|
|
roots, ok := u.Result.(*structs.IndexedCARoots)
|
|
|
|
if !ok {
|
2019-09-26 15:42:17 +00:00
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
2018-10-03 12:36:38 +00:00
|
|
|
}
|
|
|
|
snap.Roots = roots
|
2020-04-16 21:00:48 +00:00
|
|
|
case u.CorrelationID == intentionsWatchID:
|
2020-08-27 17:20:58 +00:00
|
|
|
resp, ok := u.Result.(*structs.IndexedIntentionMatches)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
if len(resp.Matches) > 0 {
|
|
|
|
// RPC supports matching multiple services at once but we only ever
|
|
|
|
// query with the one service we represent currently so just pick
|
|
|
|
// the one result set up.
|
|
|
|
snap.ConnectProxy.Intentions = resp.Matches[0]
|
|
|
|
}
|
|
|
|
snap.ConnectProxy.IntentionsSet = true
|
2020-04-16 21:00:48 +00:00
|
|
|
|
2021-03-17 19:40:39 +00:00
|
|
|
case u.CorrelationID == intentionUpstreamsID:
|
|
|
|
resp, ok := u.Result.(*structs.IndexedServiceList)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response %T", u.Result)
|
|
|
|
}
|
|
|
|
|
|
|
|
seenServices := make(map[string]struct{})
|
|
|
|
for _, svc := range resp.Services {
|
|
|
|
seenServices[svc.String()] = struct{}{}
|
|
|
|
|
|
|
|
cfgMap := make(map[string]interface{})
|
|
|
|
u, ok := snap.ConnectProxy.UpstreamConfig[svc.String()]
|
|
|
|
if ok {
|
|
|
|
cfgMap = u.Config
|
2021-04-06 20:48:54 +00:00
|
|
|
} else {
|
|
|
|
// Use the centralized upstream defaults if they exist and there isn't specific configuration for this upstream
|
|
|
|
// This is only relevant to upstreams from intentions because for explicit upstreams the defaulting is handled
|
|
|
|
// by the ResolveServiceConfig endpoint.
|
|
|
|
wildcardSID := structs.NewServiceID(structs.WildcardSpecifier, structs.WildcardEnterpriseMeta())
|
|
|
|
defaults, ok := snap.ConnectProxy.UpstreamConfig[wildcardSID.String()]
|
|
|
|
if ok {
|
2021-04-06 21:16:58 +00:00
|
|
|
u = defaults
|
2021-04-06 20:48:54 +00:00
|
|
|
cfgMap = defaults.Config
|
|
|
|
snap.ConnectProxy.UpstreamConfig[svc.String()] = defaults
|
|
|
|
}
|
2021-03-17 19:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg, err := parseReducedUpstreamConfig(cfgMap)
|
|
|
|
if err != nil {
|
|
|
|
// Don't hard fail on a config typo, just warn. We'll fall back on
|
|
|
|
// the plain discovery chain if there is an error so it's safe to
|
|
|
|
// continue.
|
|
|
|
s.logger.Warn("failed to parse upstream config",
|
|
|
|
"upstream", u.Identifier(),
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-06 21:16:58 +00:00
|
|
|
meshGateway := s.proxyCfg.MeshGateway
|
|
|
|
if u != nil {
|
|
|
|
meshGateway = meshGateway.OverlayWith(u.MeshGateway)
|
|
|
|
}
|
|
|
|
watchOpts := discoveryChainWatchOpts{
|
|
|
|
id: svc.String(),
|
|
|
|
name: svc.Name,
|
|
|
|
namespace: svc.NamespaceOrDefault(),
|
|
|
|
cfg: cfg,
|
|
|
|
meshGateway: meshGateway,
|
|
|
|
}
|
2020-12-23 22:12:36 +00:00
|
|
|
err = s.watchDiscoveryChain(ctx, snap, watchOpts)
|
2021-03-17 19:40:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to watch discovery chain for %s: %v", svc.String(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up data from services that were not in the update
|
|
|
|
for sn := range snap.ConnectProxy.WatchedUpstreams {
|
|
|
|
if _, ok := seenServices[sn]; !ok {
|
|
|
|
delete(snap.ConnectProxy.WatchedUpstreams, sn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for sn := range snap.ConnectProxy.WatchedUpstreamEndpoints {
|
|
|
|
if _, ok := seenServices[sn]; !ok {
|
|
|
|
delete(snap.ConnectProxy.WatchedUpstreamEndpoints, sn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for sn := range snap.ConnectProxy.WatchedGateways {
|
|
|
|
if _, ok := seenServices[sn]; !ok {
|
|
|
|
delete(snap.ConnectProxy.WatchedGateways, sn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for sn := range snap.ConnectProxy.WatchedGatewayEndpoints {
|
|
|
|
if _, ok := seenServices[sn]; !ok {
|
|
|
|
delete(snap.ConnectProxy.WatchedGatewayEndpoints, sn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for sn, cancelFn := range snap.ConnectProxy.WatchedDiscoveryChains {
|
|
|
|
if _, ok := seenServices[sn]; !ok {
|
|
|
|
cancelFn()
|
|
|
|
delete(snap.ConnectProxy.WatchedDiscoveryChains, sn)
|
|
|
|
delete(snap.ConnectProxy.DiscoveryChain, sn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:00:48 +00:00
|
|
|
case strings.HasPrefix(u.CorrelationID, "upstream:"+preparedQueryIDPrefix):
|
|
|
|
resp, ok := u.Result.(*structs.PreparedQueryExecuteResponse)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
pq := strings.TrimPrefix(u.CorrelationID, "upstream:")
|
|
|
|
snap.ConnectProxy.PreparedQueryEndpoints[pq] = resp.Nodes
|
2019-07-02 03:10:51 +00:00
|
|
|
|
2020-04-16 21:00:48 +00:00
|
|
|
case strings.HasPrefix(u.CorrelationID, svcChecksWatchIDPrefix):
|
|
|
|
resp, ok := u.Result.([]structs.CheckType)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for service checks response: %T, want: []structs.CheckType", u.Result)
|
|
|
|
}
|
|
|
|
svcID := structs.ServiceIDFromString(strings.TrimPrefix(u.CorrelationID, svcChecksWatchIDPrefix))
|
|
|
|
snap.ConnectProxy.WatchedServiceChecks[svcID] = resp
|
2021-04-06 18:19:59 +00:00
|
|
|
|
2021-04-28 22:13:29 +00:00
|
|
|
case u.CorrelationID == meshConfigEntryID:
|
2021-04-06 18:19:59 +00:00
|
|
|
resp, ok := u.Result.(*structs.ConfigEntryResponse)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Entry != nil {
|
2021-04-28 22:13:29 +00:00
|
|
|
meshConf, ok := resp.Entry.(*structs.MeshConfigEntry)
|
2021-04-06 18:19:59 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for config entry: %T", resp.Entry)
|
|
|
|
}
|
2021-04-28 22:13:29 +00:00
|
|
|
snap.ConnectProxy.MeshConfig = meshConf
|
2021-04-06 18:19:59 +00:00
|
|
|
} else {
|
2021-04-28 22:13:29 +00:00
|
|
|
snap.ConnectProxy.MeshConfig = nil
|
2021-04-06 18:19:59 +00:00
|
|
|
}
|
2021-04-28 22:13:29 +00:00
|
|
|
snap.ConnectProxy.MeshConfigSet = true
|
2021-04-06 18:19:59 +00:00
|
|
|
|
2020-04-16 21:00:48 +00:00
|
|
|
default:
|
2020-12-23 22:12:36 +00:00
|
|
|
return s.handleUpdateUpstreams(ctx, u, snap)
|
2020-04-16 21:00:48 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-23 22:12:36 +00:00
|
|
|
func (s *state) handleUpdateUpstreams(ctx context.Context, u cache.UpdateEvent, snap *ConfigSnapshot) error {
|
2020-04-16 21:00:48 +00:00
|
|
|
if u.Err != nil {
|
|
|
|
return fmt.Errorf("error filling agent cache: %v", u.Err)
|
|
|
|
}
|
|
|
|
|
2021-06-09 20:34:17 +00:00
|
|
|
upstreamsSnapshot := &snap.ConnectProxy.ConfigSnapshotUpstreams
|
|
|
|
if snap.Kind == structs.ServiceKindIngressGateway {
|
|
|
|
upstreamsSnapshot = &snap.IngressGateway.ConfigSnapshotUpstreams
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:00:48 +00:00
|
|
|
switch {
|
2019-07-02 03:10:51 +00:00
|
|
|
case u.CorrelationID == leafWatchID:
|
2018-10-03 12:36:38 +00:00
|
|
|
leaf, ok := u.Result.(*structs.IssuedCert)
|
|
|
|
if !ok {
|
2019-09-26 15:42:17 +00:00
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
2018-10-03 12:36:38 +00:00
|
|
|
}
|
2021-06-09 20:34:17 +00:00
|
|
|
upstreamsSnapshot.Leaf = leaf
|
2019-07-02 03:10:51 +00:00
|
|
|
|
|
|
|
case strings.HasPrefix(u.CorrelationID, "discovery-chain:"):
|
|
|
|
resp, ok := u.Result.(*structs.DiscoveryChainResponse)
|
|
|
|
if !ok {
|
2019-09-26 15:42:17 +00:00
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
2019-07-02 03:10:51 +00:00
|
|
|
}
|
|
|
|
svc := strings.TrimPrefix(u.CorrelationID, "discovery-chain:")
|
2021-06-09 20:34:17 +00:00
|
|
|
upstreamsSnapshot.DiscoveryChain[svc] = resp.Chain
|
2019-07-02 03:10:51 +00:00
|
|
|
|
2020-12-23 22:12:36 +00:00
|
|
|
if err := s.resetWatchesFromChain(ctx, svc, resp.Chain, upstreamsSnapshot); err != nil {
|
2019-07-02 03:10:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case strings.HasPrefix(u.CorrelationID, "upstream-target:"):
|
|
|
|
resp, ok := u.Result.(*structs.IndexedCheckServiceNodes)
|
|
|
|
if !ok {
|
2019-09-26 15:42:17 +00:00
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
2019-07-02 03:10:51 +00:00
|
|
|
}
|
|
|
|
correlationID := strings.TrimPrefix(u.CorrelationID, "upstream-target:")
|
2019-08-02 20:34:54 +00:00
|
|
|
targetID, svc, ok := removeColonPrefix(correlationID)
|
2019-07-02 03:10:51 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid correlation id %q", u.CorrelationID)
|
|
|
|
}
|
|
|
|
|
2021-06-09 20:34:17 +00:00
|
|
|
if _, ok := upstreamsSnapshot.WatchedUpstreamEndpoints[svc]; !ok {
|
|
|
|
upstreamsSnapshot.WatchedUpstreamEndpoints[svc] = make(map[string]structs.CheckServiceNodes)
|
|
|
|
}
|
|
|
|
upstreamsSnapshot.WatchedUpstreamEndpoints[svc][targetID] = resp.Nodes
|
|
|
|
|
|
|
|
var passthroughAddrs map[string]ServicePassthroughAddrs
|
|
|
|
|
|
|
|
for _, node := range resp.Nodes {
|
|
|
|
if snap.Proxy.Mode == structs.ProxyModeTransparent && node.Service.Proxy.TransparentProxy.DialedDirectly {
|
|
|
|
if passthroughAddrs == nil {
|
|
|
|
passthroughAddrs = make(map[string]ServicePassthroughAddrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
svc := node.Service.CompoundServiceName()
|
|
|
|
|
|
|
|
// Overwrite the name if it's a connect proxy (as opposed to Connect native).
|
|
|
|
// We don't reference the proxy name directly for things like SNI, but rather the name
|
|
|
|
// of the destination. The enterprise meta of a proxy will always be the same as that of
|
|
|
|
// the destination service, so that remains intact.
|
|
|
|
if node.Service.Kind == structs.ServiceKindConnectProxy {
|
|
|
|
dst := node.Service.Proxy.DestinationServiceName
|
|
|
|
if dst == "" {
|
|
|
|
dst = node.Service.Proxy.DestinationServiceID
|
|
|
|
}
|
|
|
|
svc.Name = dst
|
|
|
|
}
|
|
|
|
|
|
|
|
sni := connect.ServiceSNI(
|
|
|
|
svc.Name,
|
|
|
|
"",
|
|
|
|
svc.NamespaceOrDefault(),
|
|
|
|
snap.Datacenter,
|
|
|
|
snap.Roots.TrustDomain)
|
|
|
|
|
|
|
|
if _, ok := upstreamsSnapshot.PassthroughUpstreams[svc.String()]; !ok {
|
|
|
|
upstreamsSnapshot.PassthroughUpstreams[svc.String()] = ServicePassthroughAddrs{
|
|
|
|
SNI: sni,
|
|
|
|
|
|
|
|
// Stored in a set because it's possible for these to be duplicated
|
|
|
|
// when the upstream-target is targeted by multiple discovery chains.
|
|
|
|
Addrs: make(map[string]struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addr, _ := node.BestAddress(false)
|
|
|
|
upstreamsSnapshot.PassthroughUpstreams[svc.String()].Addrs[addr] = struct{}{}
|
|
|
|
}
|
2019-07-02 03:10:51 +00:00
|
|
|
}
|
|
|
|
|
2019-08-05 18:30:35 +00:00
|
|
|
case strings.HasPrefix(u.CorrelationID, "mesh-gateway:"):
|
2020-09-14 22:12:21 +00:00
|
|
|
resp, ok := u.Result.(*structs.IndexedNodesWithGateways)
|
2019-08-05 18:30:35 +00:00
|
|
|
if !ok {
|
2019-09-26 15:42:17 +00:00
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
2019-08-05 18:30:35 +00:00
|
|
|
}
|
|
|
|
correlationID := strings.TrimPrefix(u.CorrelationID, "mesh-gateway:")
|
|
|
|
dc, svc, ok := removeColonPrefix(correlationID)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid correlation id %q", u.CorrelationID)
|
|
|
|
}
|
2021-06-09 20:34:17 +00:00
|
|
|
if _, ok = upstreamsSnapshot.WatchedGatewayEndpoints[svc]; !ok {
|
|
|
|
upstreamsSnapshot.WatchedGatewayEndpoints[svc] = make(map[string]structs.CheckServiceNodes)
|
2019-08-05 18:30:35 +00:00
|
|
|
}
|
2021-06-09 20:34:17 +00:00
|
|
|
upstreamsSnapshot.WatchedGatewayEndpoints[svc][dc] = resp.Nodes
|
2018-10-03 12:36:38 +00:00
|
|
|
default:
|
2019-09-26 15:42:17 +00:00
|
|
|
return fmt.Errorf("unknown correlation ID: %s", u.CorrelationID)
|
2019-07-02 03:10:51 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeColonPrefix(s string) (string, string, bool) {
|
|
|
|
idx := strings.Index(s, ":")
|
|
|
|
if idx == -1 {
|
|
|
|
return "", "", false
|
|
|
|
}
|
|
|
|
return s[0:idx], s[idx+1:], true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *state) resetWatchesFromChain(
|
2020-12-23 22:12:36 +00:00
|
|
|
ctx context.Context,
|
2019-07-02 03:10:51 +00:00
|
|
|
id string,
|
|
|
|
chain *structs.CompiledDiscoveryChain,
|
2020-04-16 21:00:48 +00:00
|
|
|
snap *ConfigSnapshotUpstreams,
|
2019-07-02 03:10:51 +00:00
|
|
|
) error {
|
2020-02-03 14:26:47 +00:00
|
|
|
s.logger.Trace("resetting watches for discovery chain", "id", id)
|
2019-07-02 03:10:51 +00:00
|
|
|
if chain == nil {
|
|
|
|
return fmt.Errorf("not possible to arrive here with no discovery chain")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize relevant sub maps.
|
2020-04-16 21:00:48 +00:00
|
|
|
if _, ok := snap.WatchedUpstreams[id]; !ok {
|
|
|
|
snap.WatchedUpstreams[id] = make(map[string]context.CancelFunc)
|
2019-07-02 03:10:51 +00:00
|
|
|
}
|
2020-04-16 21:00:48 +00:00
|
|
|
if _, ok := snap.WatchedUpstreamEndpoints[id]; !ok {
|
|
|
|
snap.WatchedUpstreamEndpoints[id] = make(map[string]structs.CheckServiceNodes)
|
2019-07-02 03:10:51 +00:00
|
|
|
}
|
2020-04-16 21:00:48 +00:00
|
|
|
if _, ok := snap.WatchedGateways[id]; !ok {
|
|
|
|
snap.WatchedGateways[id] = make(map[string]context.CancelFunc)
|
2019-08-05 18:30:35 +00:00
|
|
|
}
|
2020-04-16 21:00:48 +00:00
|
|
|
if _, ok := snap.WatchedGatewayEndpoints[id]; !ok {
|
|
|
|
snap.WatchedGatewayEndpoints[id] = make(map[string]structs.CheckServiceNodes)
|
2019-08-05 18:30:35 +00:00
|
|
|
}
|
2019-07-02 03:10:51 +00:00
|
|
|
|
|
|
|
// We could invalidate this selectively based on a hash of the relevant
|
|
|
|
// resolver information, but for now just reset anything about this
|
|
|
|
// upstream when the chain changes in any way.
|
|
|
|
//
|
|
|
|
// TODO(rb): content hash based add/remove
|
2020-04-16 21:00:48 +00:00
|
|
|
for targetID, cancelFn := range snap.WatchedUpstreams[id] {
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Trace("stopping watch of target",
|
|
|
|
"upstream", id,
|
|
|
|
"chain", chain.ServiceName,
|
|
|
|
"target", targetID,
|
|
|
|
)
|
2020-04-16 21:00:48 +00:00
|
|
|
delete(snap.WatchedUpstreams[id], targetID)
|
|
|
|
delete(snap.WatchedUpstreamEndpoints[id], targetID)
|
2019-07-02 03:10:51 +00:00
|
|
|
cancelFn()
|
|
|
|
}
|
|
|
|
|
2021-05-04 14:45:19 +00:00
|
|
|
var (
|
|
|
|
watchedChainEndpoints bool
|
|
|
|
needGateways = make(map[string]struct{})
|
|
|
|
)
|
|
|
|
|
|
|
|
chainID := chain.ID()
|
2019-08-02 20:34:54 +00:00
|
|
|
for _, target := range chain.Targets {
|
2021-05-04 14:45:19 +00:00
|
|
|
if target.ID == chainID {
|
|
|
|
watchedChainEndpoints = true
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := targetWatchOpts{
|
|
|
|
upstreamID: id,
|
|
|
|
chainID: target.ID,
|
|
|
|
service: target.Service,
|
|
|
|
filter: target.Subset.Filter,
|
|
|
|
datacenter: target.Datacenter,
|
|
|
|
entMeta: target.GetEnterpriseMetadata(),
|
|
|
|
}
|
2020-12-23 22:12:36 +00:00
|
|
|
err := s.watchUpstreamTarget(ctx, snap, opts)
|
2021-05-04 14:45:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to watch target %q for upstream %q", target.ID, id)
|
|
|
|
}
|
2019-07-02 03:10:51 +00:00
|
|
|
|
2019-08-05 18:30:35 +00:00
|
|
|
// We'll get endpoints from the gateway query, but the health still has
|
|
|
|
// to come from the backing service query.
|
|
|
|
switch target.MeshGateway.Mode {
|
|
|
|
case structs.MeshGatewayModeRemote:
|
|
|
|
needGateways[target.Datacenter] = struct{}{}
|
|
|
|
case structs.MeshGatewayModeLocal:
|
|
|
|
needGateways[s.source.Datacenter] = struct{}{}
|
2019-07-02 03:10:51 +00:00
|
|
|
}
|
2021-05-04 14:45:19 +00:00
|
|
|
}
|
2019-07-02 03:10:51 +00:00
|
|
|
|
2021-05-04 14:45:19 +00:00
|
|
|
// If the discovery chain's targets do not lead to watching all endpoints
|
|
|
|
// for the upstream, then create a separate watch for those too.
|
|
|
|
// This is needed in transparent mode because if there is some service A that
|
|
|
|
// redirects to service B, the dialing proxy needs to associate A's virtual IP
|
|
|
|
// with A's discovery chain.
|
|
|
|
//
|
|
|
|
// Outside of transparent mode we only watch the chain target, B,
|
|
|
|
// since A is a virtual service and traffic will not be sent to it.
|
|
|
|
if !watchedChainEndpoints && s.proxyCfg.Mode == structs.ProxyModeTransparent {
|
|
|
|
chainEntMeta := structs.NewEnterpriseMeta(chain.Namespace)
|
|
|
|
|
|
|
|
opts := targetWatchOpts{
|
|
|
|
upstreamID: id,
|
|
|
|
chainID: chainID,
|
|
|
|
service: chain.ServiceName,
|
|
|
|
filter: "",
|
|
|
|
datacenter: chain.Datacenter,
|
|
|
|
entMeta: &chainEntMeta,
|
|
|
|
}
|
2020-12-23 22:12:36 +00:00
|
|
|
err := s.watchUpstreamTarget(ctx, snap, opts)
|
2019-07-02 03:10:51 +00:00
|
|
|
if err != nil {
|
2021-05-04 14:45:19 +00:00
|
|
|
return fmt.Errorf("failed to watch target %q for upstream %q", chainID, id)
|
2019-07-02 03:10:51 +00:00
|
|
|
}
|
2018-10-03 12:36:38 +00:00
|
|
|
}
|
2019-07-02 03:10:51 +00:00
|
|
|
|
2020-06-16 17:19:31 +00:00
|
|
|
for dc := range needGateways {
|
2020-04-16 21:00:48 +00:00
|
|
|
if _, ok := snap.WatchedGateways[id][dc]; ok {
|
2019-08-05 18:30:35 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Trace("initializing watch of mesh gateway in datacenter",
|
|
|
|
"upstream", id,
|
|
|
|
"chain", chain.ServiceName,
|
|
|
|
"datacenter", dc,
|
|
|
|
)
|
2019-08-05 18:30:35 +00:00
|
|
|
|
2020-12-23 22:12:36 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2019-08-05 18:30:35 +00:00
|
|
|
err := s.watchMeshGateway(ctx, dc, id)
|
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:00:48 +00:00
|
|
|
snap.WatchedGateways[id][dc] = cancel
|
2019-08-05 18:30:35 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 21:00:48 +00:00
|
|
|
for dc, cancelFn := range snap.WatchedGateways[id] {
|
2019-08-05 18:30:35 +00:00
|
|
|
if _, ok := needGateways[dc]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Trace("stopping watch of mesh gateway in datacenter",
|
|
|
|
"upstream", id,
|
|
|
|
"chain", chain.ServiceName,
|
|
|
|
"datacenter", dc,
|
|
|
|
)
|
2020-04-16 21:00:48 +00:00
|
|
|
delete(snap.WatchedGateways[id], dc)
|
|
|
|
delete(snap.WatchedGatewayEndpoints[id], dc)
|
2019-08-05 18:30:35 +00:00
|
|
|
cancelFn()
|
|
|
|
}
|
|
|
|
|
2018-10-03 12:36:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-04 14:45:19 +00:00
|
|
|
type targetWatchOpts struct {
|
|
|
|
upstreamID string
|
|
|
|
chainID string
|
|
|
|
service string
|
|
|
|
filter string
|
|
|
|
datacenter string
|
|
|
|
entMeta *structs.EnterpriseMeta
|
|
|
|
}
|
|
|
|
|
2020-12-23 22:12:36 +00:00
|
|
|
func (s *state) watchUpstreamTarget(ctx context.Context, snap *ConfigSnapshotUpstreams, opts targetWatchOpts) error {
|
2021-05-04 14:45:19 +00:00
|
|
|
s.logger.Trace("initializing watch of target",
|
|
|
|
"upstream", opts.upstreamID,
|
|
|
|
"chain", opts.service,
|
|
|
|
"target", opts.chainID,
|
|
|
|
)
|
|
|
|
|
|
|
|
var finalMeta structs.EnterpriseMeta
|
|
|
|
finalMeta.Merge(opts.entMeta)
|
|
|
|
|
|
|
|
correlationID := "upstream-target:" + opts.chainID + ":" + opts.upstreamID
|
|
|
|
|
2020-12-23 22:12:36 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2021-05-04 14:45:19 +00:00
|
|
|
err := s.health.Notify(ctx, structs.ServiceSpecificRequest{
|
|
|
|
Datacenter: opts.datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Token: s.token,
|
|
|
|
Filter: opts.filter,
|
|
|
|
},
|
|
|
|
ServiceName: opts.service,
|
|
|
|
Connect: true,
|
|
|
|
// Note that Identifier doesn't type-prefix for service any more as it's
|
|
|
|
// the default and makes metrics and other things much cleaner. It's
|
|
|
|
// simpler for us if we have the type to make things unambiguous.
|
|
|
|
Source: *s.source,
|
|
|
|
EnterpriseMeta: finalMeta,
|
|
|
|
}, correlationID, s.ch)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
snap.WatchedUpstreams[opts.upstreamID][opts.chainID] = cancel
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-23 22:12:36 +00:00
|
|
|
func (s *state) handleUpdateTerminatingGateway(ctx context.Context, u cache.UpdateEvent, snap *ConfigSnapshot) error {
|
2020-04-10 18:06:08 +00:00
|
|
|
if u.Err != nil {
|
|
|
|
return fmt.Errorf("error filling agent cache: %v", u.Err)
|
|
|
|
}
|
|
|
|
logger := s.logger.Named(logging.TerminatingGateway)
|
|
|
|
|
2020-04-14 22:50:03 +00:00
|
|
|
switch {
|
|
|
|
case u.CorrelationID == rootsWatchID:
|
2020-04-10 18:06:08 +00:00
|
|
|
roots, ok := u.Result.(*structs.IndexedCARoots)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
snap.Roots = roots
|
|
|
|
|
|
|
|
// Update watches based on the current list of services associated with the terminating-gateway
|
2020-04-14 22:50:03 +00:00
|
|
|
case u.CorrelationID == gatewayServicesWatchID:
|
2020-04-10 18:06:08 +00:00
|
|
|
services, ok := u.Result.(*structs.IndexedGatewayServices)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
|
2020-06-12 14:57:41 +00:00
|
|
|
svcMap := make(map[structs.ServiceName]struct{})
|
2020-04-10 18:06:08 +00:00
|
|
|
for _, svc := range services.Services {
|
|
|
|
// Make sure to add every service to this map, we use it to cancel watches below.
|
|
|
|
svcMap[svc.Service] = struct{}{}
|
|
|
|
|
2020-04-27 22:25:37 +00:00
|
|
|
// Store the gateway <-> service mapping for TLS origination
|
|
|
|
snap.TerminatingGateway.GatewayServices[svc.Service] = *svc
|
|
|
|
|
2020-04-10 18:06:08 +00:00
|
|
|
// Watch the health endpoint to discover endpoints for the service
|
|
|
|
if _, ok := snap.TerminatingGateway.WatchedServices[svc.Service]; !ok {
|
2020-12-23 22:12:36 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2021-02-23 17:52:54 +00:00
|
|
|
err := s.health.Notify(ctx, structs.ServiceSpecificRequest{
|
2020-04-10 18:06:08 +00:00
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
2020-06-12 14:57:41 +00:00
|
|
|
ServiceName: svc.Service.Name,
|
2020-04-10 18:06:08 +00:00
|
|
|
EnterpriseMeta: svc.Service.EnterpriseMeta,
|
|
|
|
|
|
|
|
// The gateway acts as the service's proxy, so we do NOT want to discover other proxies
|
|
|
|
Connect: false,
|
2020-04-20 19:42:33 +00:00
|
|
|
}, externalServiceIDPrefix+svc.Service.String(), s.ch)
|
2020-04-10 18:06:08 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("failed to register watch for external-service",
|
|
|
|
"service", svc.Service.String(),
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
snap.TerminatingGateway.WatchedServices[svc.Service] = cancel
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch intentions with this service as their destination
|
|
|
|
// The gateway will enforce intentions for connections to the service
|
|
|
|
if _, ok := snap.TerminatingGateway.WatchedIntentions[svc.Service]; !ok {
|
2020-12-23 22:12:36 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2020-04-10 18:06:08 +00:00
|
|
|
err := s.cache.Notify(ctx, cachetype.IntentionMatchName, &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
Match: &structs.IntentionQueryMatch{
|
|
|
|
Type: structs.IntentionMatchDestination,
|
|
|
|
Entries: []structs.IntentionMatchEntry{
|
|
|
|
{
|
|
|
|
Namespace: svc.Service.NamespaceOrDefault(),
|
2020-06-12 14:57:41 +00:00
|
|
|
Name: svc.Service.Name,
|
2020-04-10 18:06:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-04-20 19:42:33 +00:00
|
|
|
}, serviceIntentionsIDPrefix+svc.Service.String(), s.ch)
|
2020-04-10 18:06:08 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("failed to register watch for service-intentions",
|
|
|
|
"service", svc.Service.String(),
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
snap.TerminatingGateway.WatchedIntentions[svc.Service] = cancel
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch leaf certificate for the service
|
|
|
|
// This cert is used to terminate mTLS connections on the service's behalf
|
|
|
|
if _, ok := snap.TerminatingGateway.WatchedLeaves[svc.Service]; !ok {
|
2020-12-23 22:12:36 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2020-04-10 18:06:08 +00:00
|
|
|
err := s.cache.Notify(ctx, cachetype.ConnectCALeafName, &cachetype.ConnectCALeafRequest{
|
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
Token: s.token,
|
2020-06-12 14:57:41 +00:00
|
|
|
Service: svc.Service.Name,
|
2020-04-10 18:06:08 +00:00
|
|
|
EnterpriseMeta: svc.Service.EnterpriseMeta,
|
2020-04-20 19:42:33 +00:00
|
|
|
}, serviceLeafIDPrefix+svc.Service.String(), s.ch)
|
2020-04-10 18:06:08 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("failed to register watch for a service-leaf",
|
|
|
|
"service", svc.Service.String(),
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
snap.TerminatingGateway.WatchedLeaves[svc.Service] = cancel
|
|
|
|
}
|
2020-04-14 14:59:23 +00:00
|
|
|
|
2020-08-27 17:20:58 +00:00
|
|
|
// Watch service configs for the service.
|
|
|
|
// These are used to determine the protocol for the target service.
|
|
|
|
if _, ok := snap.TerminatingGateway.WatchedConfigs[svc.Service]; !ok {
|
2020-12-23 22:12:36 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2020-08-27 17:20:58 +00:00
|
|
|
err := s.cache.Notify(ctx, cachetype.ResolvedServiceConfigName, &structs.ServiceConfigRequest{
|
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
Name: svc.Service.Name,
|
|
|
|
EnterpriseMeta: svc.Service.EnterpriseMeta,
|
|
|
|
}, serviceConfigIDPrefix+svc.Service.String(), s.ch)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("failed to register watch for a resolved service config",
|
|
|
|
"service", svc.Service.String(),
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
snap.TerminatingGateway.WatchedConfigs[svc.Service] = cancel
|
|
|
|
}
|
|
|
|
|
2020-04-14 14:59:23 +00:00
|
|
|
// Watch service resolvers for the service
|
|
|
|
// These are used to create clusters and endpoints for the service subsets
|
|
|
|
if _, ok := snap.TerminatingGateway.WatchedResolvers[svc.Service]; !ok {
|
2020-12-23 22:12:36 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2020-04-14 14:59:23 +00:00
|
|
|
err := s.cache.Notify(ctx, cachetype.ConfigEntriesName, &structs.ConfigEntryQuery{
|
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
Kind: structs.ServiceResolver,
|
2020-06-12 14:57:41 +00:00
|
|
|
Name: svc.Service.Name,
|
2020-04-14 14:59:23 +00:00
|
|
|
EnterpriseMeta: svc.Service.EnterpriseMeta,
|
2020-04-20 19:42:33 +00:00
|
|
|
}, serviceResolverIDPrefix+svc.Service.String(), s.ch)
|
2020-04-14 14:59:23 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("failed to register watch for a service-resolver",
|
|
|
|
"service", svc.Service.String(),
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
snap.TerminatingGateway.WatchedResolvers[svc.Service] = cancel
|
|
|
|
}
|
2020-04-10 18:06:08 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 22:25:37 +00:00
|
|
|
// Delete gateway service mapping for services that were not in the update
|
2020-06-16 17:19:31 +00:00
|
|
|
for sn := range snap.TerminatingGateway.GatewayServices {
|
2020-06-12 14:57:41 +00:00
|
|
|
if _, ok := svcMap[sn]; !ok {
|
|
|
|
delete(snap.TerminatingGateway.GatewayServices, sn)
|
2020-04-27 22:25:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 21:28:45 +00:00
|
|
|
// Clean up services with hostname mapping for services that were not in the update
|
2020-06-16 17:19:31 +00:00
|
|
|
for sn := range snap.TerminatingGateway.HostnameServices {
|
2020-06-12 14:57:41 +00:00
|
|
|
if _, ok := svcMap[sn]; !ok {
|
|
|
|
delete(snap.TerminatingGateway.HostnameServices, sn)
|
2020-06-03 21:28:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 18:06:08 +00:00
|
|
|
// Cancel service instance watches for services that were not in the update
|
2020-06-12 14:57:41 +00:00
|
|
|
for sn, cancelFn := range snap.TerminatingGateway.WatchedServices {
|
|
|
|
if _, ok := svcMap[sn]; !ok {
|
|
|
|
logger.Debug("canceling watch for service", "service", sn.String())
|
|
|
|
delete(snap.TerminatingGateway.WatchedServices, sn)
|
|
|
|
delete(snap.TerminatingGateway.ServiceGroups, sn)
|
2020-04-10 18:06:08 +00:00
|
|
|
cancelFn()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel leaf cert watches for services that were not in the update
|
2020-06-12 14:57:41 +00:00
|
|
|
for sn, cancelFn := range snap.TerminatingGateway.WatchedLeaves {
|
|
|
|
if _, ok := svcMap[sn]; !ok {
|
|
|
|
logger.Debug("canceling watch for leaf cert", "service", sn.String())
|
|
|
|
delete(snap.TerminatingGateway.WatchedLeaves, sn)
|
|
|
|
delete(snap.TerminatingGateway.ServiceLeaves, sn)
|
2020-04-10 18:06:08 +00:00
|
|
|
cancelFn()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:20:58 +00:00
|
|
|
// Cancel service config watches for services that were not in the update
|
|
|
|
for sn, cancelFn := range snap.TerminatingGateway.WatchedConfigs {
|
|
|
|
if _, ok := svcMap[sn]; !ok {
|
|
|
|
logger.Debug("canceling watch for resolved service config", "service", sn.String())
|
|
|
|
delete(snap.TerminatingGateway.WatchedConfigs, sn)
|
|
|
|
delete(snap.TerminatingGateway.ServiceConfigs, sn)
|
|
|
|
cancelFn()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 14:59:23 +00:00
|
|
|
// Cancel service-resolver watches for services that were not in the update
|
2020-06-12 14:57:41 +00:00
|
|
|
for sn, cancelFn := range snap.TerminatingGateway.WatchedResolvers {
|
|
|
|
if _, ok := svcMap[sn]; !ok {
|
|
|
|
logger.Debug("canceling watch for service-resolver", "service", sn.String())
|
|
|
|
delete(snap.TerminatingGateway.WatchedResolvers, sn)
|
|
|
|
delete(snap.TerminatingGateway.ServiceResolvers, sn)
|
2020-08-27 17:20:58 +00:00
|
|
|
delete(snap.TerminatingGateway.ServiceResolversSet, sn)
|
2020-04-14 14:59:23 +00:00
|
|
|
cancelFn()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 18:06:08 +00:00
|
|
|
// Cancel intention watches for services that were not in the update
|
2020-06-12 14:57:41 +00:00
|
|
|
for sn, cancelFn := range snap.TerminatingGateway.WatchedIntentions {
|
|
|
|
if _, ok := svcMap[sn]; !ok {
|
|
|
|
logger.Debug("canceling watch for intention", "service", sn.String())
|
|
|
|
delete(snap.TerminatingGateway.WatchedIntentions, sn)
|
2020-08-27 17:20:58 +00:00
|
|
|
delete(snap.TerminatingGateway.Intentions, sn)
|
2020-04-10 18:06:08 +00:00
|
|
|
cancelFn()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 19:42:33 +00:00
|
|
|
case strings.HasPrefix(u.CorrelationID, externalServiceIDPrefix):
|
2020-04-14 22:50:03 +00:00
|
|
|
resp, ok := u.Result.(*structs.IndexedCheckServiceNodes)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
2020-04-10 18:06:08 +00:00
|
|
|
|
2020-06-12 14:57:41 +00:00
|
|
|
sn := structs.ServiceNameFromString(strings.TrimPrefix(u.CorrelationID, externalServiceIDPrefix))
|
|
|
|
delete(snap.TerminatingGateway.ServiceGroups, sn)
|
|
|
|
delete(snap.TerminatingGateway.HostnameServices, sn)
|
2020-04-10 18:06:08 +00:00
|
|
|
|
2020-04-14 22:50:03 +00:00
|
|
|
if len(resp.Nodes) > 0 {
|
2020-06-12 14:57:41 +00:00
|
|
|
snap.TerminatingGateway.ServiceGroups[sn] = resp.Nodes
|
|
|
|
snap.TerminatingGateway.HostnameServices[sn] = s.hostnameEndpoints(logging.TerminatingGateway, snap.Datacenter, resp.Nodes)
|
2020-04-14 22:50:03 +00:00
|
|
|
}
|
2020-04-10 18:06:08 +00:00
|
|
|
|
2020-04-14 22:50:03 +00:00
|
|
|
// Store leaf cert for watched service
|
2020-04-20 19:42:33 +00:00
|
|
|
case strings.HasPrefix(u.CorrelationID, serviceLeafIDPrefix):
|
2020-04-14 22:50:03 +00:00
|
|
|
leaf, ok := u.Result.(*structs.IssuedCert)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
2020-04-10 18:06:08 +00:00
|
|
|
|
2020-06-12 14:57:41 +00:00
|
|
|
sn := structs.ServiceNameFromString(strings.TrimPrefix(u.CorrelationID, serviceLeafIDPrefix))
|
|
|
|
snap.TerminatingGateway.ServiceLeaves[sn] = leaf
|
2020-04-10 18:06:08 +00:00
|
|
|
|
2020-08-27 17:20:58 +00:00
|
|
|
case strings.HasPrefix(u.CorrelationID, serviceConfigIDPrefix):
|
|
|
|
serviceConfig, ok := u.Result.(*structs.ServiceConfigResponse)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
|
|
|
|
sn := structs.ServiceNameFromString(strings.TrimPrefix(u.CorrelationID, serviceConfigIDPrefix))
|
|
|
|
snap.TerminatingGateway.ServiceConfigs[sn] = serviceConfig
|
|
|
|
|
|
|
|
case strings.HasPrefix(u.CorrelationID, serviceResolverIDPrefix):
|
2020-04-14 22:50:03 +00:00
|
|
|
configEntries, ok := u.Result.(*structs.IndexedConfigEntries)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
2020-08-27 17:20:58 +00:00
|
|
|
sn := structs.ServiceNameFromString(strings.TrimPrefix(u.CorrelationID, serviceResolverIDPrefix))
|
2020-04-14 22:50:03 +00:00
|
|
|
// There should only ever be one entry for a service resolver within a namespace
|
|
|
|
if len(configEntries.Entries) == 1 {
|
|
|
|
if resolver, ok := configEntries.Entries[0].(*structs.ServiceResolverConfigEntry); ok {
|
2020-08-27 17:20:58 +00:00
|
|
|
snap.TerminatingGateway.ServiceResolvers[sn] = resolver
|
2020-04-14 14:59:23 +00:00
|
|
|
}
|
2020-04-14 22:50:03 +00:00
|
|
|
}
|
2020-08-27 17:20:58 +00:00
|
|
|
snap.TerminatingGateway.ServiceResolversSet[sn] = true
|
2020-04-14 14:59:23 +00:00
|
|
|
|
2020-04-20 19:42:33 +00:00
|
|
|
case strings.HasPrefix(u.CorrelationID, serviceIntentionsIDPrefix):
|
2020-08-27 17:20:58 +00:00
|
|
|
resp, ok := u.Result.(*structs.IndexedIntentionMatches)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
|
|
|
|
sn := structs.ServiceNameFromString(strings.TrimPrefix(u.CorrelationID, serviceIntentionsIDPrefix))
|
|
|
|
|
|
|
|
if len(resp.Matches) > 0 {
|
|
|
|
// RPC supports matching multiple services at once but we only ever
|
|
|
|
// query with the one service we represent currently so just pick
|
|
|
|
// the one result set up.
|
|
|
|
snap.TerminatingGateway.Intentions[sn] = resp.Matches[0]
|
|
|
|
}
|
2020-04-10 18:06:08 +00:00
|
|
|
|
2020-04-14 22:50:03 +00:00
|
|
|
default:
|
|
|
|
// do nothing
|
2020-04-10 18:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-23 22:12:36 +00:00
|
|
|
func (s *state) handleUpdateMeshGateway(ctx context.Context, u cache.UpdateEvent, snap *ConfigSnapshot) error {
|
2019-09-26 15:42:17 +00:00
|
|
|
if u.Err != nil {
|
|
|
|
return fmt.Errorf("error filling agent cache: %v", u.Err)
|
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
meshLogger := s.logger.Named(logging.MeshGateway)
|
|
|
|
|
2019-06-18 00:52:01 +00:00
|
|
|
switch u.CorrelationID {
|
|
|
|
case rootsWatchID:
|
|
|
|
roots, ok := u.Result.(*structs.IndexedCARoots)
|
|
|
|
if !ok {
|
2019-09-26 15:42:17 +00:00
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
|
|
|
snap.Roots = roots
|
2020-03-09 20:59:02 +00:00
|
|
|
case federationStateListGatewaysWatchID:
|
|
|
|
dcIndexedNodes, ok := u.Result.(*structs.DatacenterIndexedCheckServiceNodes)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
snap.MeshGateway.FedStateGateways = dcIndexedNodes.DatacenterNodes
|
2020-06-03 21:28:45 +00:00
|
|
|
|
|
|
|
for dc, nodes := range dcIndexedNodes.DatacenterNodes {
|
|
|
|
snap.MeshGateway.HostnameDatacenters[dc] = s.hostnameEndpoints(logging.MeshGateway, snap.Datacenter, nodes)
|
|
|
|
}
|
|
|
|
|
2020-06-16 17:19:31 +00:00
|
|
|
for dc := range snap.MeshGateway.HostnameDatacenters {
|
2020-06-03 21:28:45 +00:00
|
|
|
if _, ok := dcIndexedNodes.DatacenterNodes[dc]; !ok {
|
|
|
|
delete(snap.MeshGateway.HostnameDatacenters, dc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 00:52:01 +00:00
|
|
|
case serviceListWatchID:
|
2020-01-24 15:04:58 +00:00
|
|
|
services, ok := u.Result.(*structs.IndexedServiceList)
|
2019-06-18 00:52:01 +00:00
|
|
|
if !ok {
|
2019-09-26 15:42:17 +00:00
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 14:57:41 +00:00
|
|
|
svcMap := make(map[structs.ServiceName]struct{})
|
2020-01-24 15:04:58 +00:00
|
|
|
for _, svc := range services.Services {
|
2020-04-02 15:12:13 +00:00
|
|
|
// Make sure to add every service to this map, we use it to cancel
|
|
|
|
// watches below.
|
2020-06-12 14:57:41 +00:00
|
|
|
svcMap[svc] = struct{}{}
|
2020-04-02 15:12:13 +00:00
|
|
|
|
2020-06-12 14:57:41 +00:00
|
|
|
if _, ok := snap.MeshGateway.WatchedServices[svc]; !ok {
|
2020-12-23 22:12:36 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2021-02-23 17:52:54 +00:00
|
|
|
err := s.health.Notify(ctx, structs.ServiceSpecificRequest{
|
2020-01-24 15:04:58 +00:00
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
ServiceName: svc.Name,
|
|
|
|
Connect: true,
|
2020-06-12 14:57:41 +00:00
|
|
|
EnterpriseMeta: svc.EnterpriseMeta,
|
|
|
|
}, fmt.Sprintf("connect-service:%s", svc.String()), s.ch)
|
2019-06-18 00:52:01 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
meshLogger.Error("failed to register watch for connect-service",
|
2020-06-12 14:57:41 +00:00
|
|
|
"service", svc.String(),
|
2020-01-28 23:50:41 +00:00
|
|
|
"error", err,
|
|
|
|
)
|
2019-06-18 00:52:01 +00:00
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
2020-06-12 14:57:41 +00:00
|
|
|
snap.MeshGateway.WatchedServices[svc] = cancel
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
for sid, cancelFn := range snap.MeshGateway.WatchedServices {
|
|
|
|
if _, ok := svcMap[sid]; !ok {
|
2020-04-02 15:12:13 +00:00
|
|
|
meshLogger.Debug("canceling watch for service", "service", sid.String())
|
2020-04-10 18:06:08 +00:00
|
|
|
// TODO (gateways) Should the sid also be deleted from snap.MeshGateway.ServiceGroups?
|
|
|
|
// Do those endpoints get cleaned up some other way?
|
2020-01-24 15:04:58 +00:00
|
|
|
delete(snap.MeshGateway.WatchedServices, sid)
|
2019-06-18 00:52:01 +00:00
|
|
|
cancelFn()
|
|
|
|
}
|
|
|
|
}
|
2019-10-17 21:46:49 +00:00
|
|
|
|
|
|
|
snap.MeshGateway.WatchedServicesSet = true
|
2019-06-18 00:52:01 +00:00
|
|
|
case datacentersWatchID:
|
|
|
|
datacentersRaw, ok := u.Result.(*[]string)
|
|
|
|
if !ok {
|
2019-09-26 15:42:17 +00:00
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
|
|
|
if datacentersRaw == nil {
|
|
|
|
return fmt.Errorf("invalid response with a nil datacenter list")
|
|
|
|
}
|
|
|
|
|
|
|
|
datacenters := *datacentersRaw
|
|
|
|
|
|
|
|
for _, dc := range datacenters {
|
|
|
|
if dc == s.source.Datacenter {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := snap.MeshGateway.WatchedDatacenters[dc]; !ok {
|
2020-12-23 22:12:36 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2019-06-18 00:52:01 +00:00
|
|
|
err := s.cache.Notify(ctx, cachetype.InternalServiceDumpName, &structs.ServiceDumpRequest{
|
|
|
|
Datacenter: dc,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
ServiceKind: structs.ServiceKindMeshGateway,
|
|
|
|
UseServiceKind: true,
|
2019-07-04 15:17:49 +00:00
|
|
|
Source: *s.source,
|
2020-01-24 15:04:58 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMeta(),
|
2019-06-18 00:52:01 +00:00
|
|
|
}, fmt.Sprintf("mesh-gateway:%s", dc), s.ch)
|
|
|
|
|
|
|
|
if err != nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
meshLogger.Error("failed to register watch for mesh-gateway",
|
|
|
|
"datacenter", dc,
|
|
|
|
"error", err,
|
|
|
|
)
|
2019-06-18 00:52:01 +00:00
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
snap.MeshGateway.WatchedDatacenters[dc] = cancel
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for dc, cancelFn := range snap.MeshGateway.WatchedDatacenters {
|
|
|
|
found := false
|
|
|
|
for _, dcCurrent := range datacenters {
|
|
|
|
if dcCurrent == dc {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
delete(snap.MeshGateway.WatchedDatacenters, dc)
|
|
|
|
cancelFn()
|
|
|
|
}
|
|
|
|
}
|
2019-07-02 13:43:35 +00:00
|
|
|
case serviceResolversWatchID:
|
|
|
|
configEntries, ok := u.Result.(*structs.IndexedConfigEntries)
|
|
|
|
if !ok {
|
2019-09-26 15:42:17 +00:00
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
2019-07-02 13:43:35 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 14:57:41 +00:00
|
|
|
resolvers := make(map[structs.ServiceName]*structs.ServiceResolverConfigEntry)
|
2019-07-02 13:43:35 +00:00
|
|
|
for _, entry := range configEntries.Entries {
|
|
|
|
if resolver, ok := entry.(*structs.ServiceResolverConfigEntry); ok {
|
2020-06-12 14:57:41 +00:00
|
|
|
resolvers[structs.NewServiceName(resolver.Name, &resolver.EnterpriseMeta)] = resolver
|
2019-07-02 13:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
snap.MeshGateway.ServiceResolvers = resolvers
|
2020-03-09 20:59:02 +00:00
|
|
|
|
|
|
|
case consulServerListWatchID:
|
|
|
|
resp, ok := u.Result.(*structs.IndexedCheckServiceNodes)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do some initial sanity checks to avoid doing something dumb.
|
|
|
|
for _, csn := range resp.Nodes {
|
|
|
|
if csn.Service.Service != structs.ConsulServiceName {
|
|
|
|
return fmt.Errorf("expected service name %q but got %q",
|
|
|
|
structs.ConsulServiceName, csn.Service.Service)
|
|
|
|
}
|
|
|
|
if csn.Node.Datacenter != snap.Datacenter {
|
|
|
|
return fmt.Errorf("expected datacenter %q but got %q",
|
|
|
|
snap.Datacenter, csn.Node.Datacenter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
snap.MeshGateway.ConsulServers = resp.Nodes
|
|
|
|
|
2019-06-18 00:52:01 +00:00
|
|
|
default:
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(u.CorrelationID, "connect-service:"):
|
|
|
|
resp, ok := u.Result.(*structs.IndexedCheckServiceNodes)
|
|
|
|
if !ok {
|
2019-09-26 15:42:17 +00:00
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 14:57:41 +00:00
|
|
|
sn := structs.ServiceNameFromString(strings.TrimPrefix(u.CorrelationID, "connect-service:"))
|
2019-06-18 00:52:01 +00:00
|
|
|
|
|
|
|
if len(resp.Nodes) > 0 {
|
2020-06-12 14:57:41 +00:00
|
|
|
snap.MeshGateway.ServiceGroups[sn] = resp.Nodes
|
|
|
|
} else if _, ok := snap.MeshGateway.ServiceGroups[sn]; ok {
|
|
|
|
delete(snap.MeshGateway.ServiceGroups, sn)
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
|
|
|
case strings.HasPrefix(u.CorrelationID, "mesh-gateway:"):
|
2020-09-14 22:12:21 +00:00
|
|
|
resp, ok := u.Result.(*structs.IndexedNodesWithGateways)
|
2019-06-18 00:52:01 +00:00
|
|
|
if !ok {
|
2019-09-26 15:42:17 +00:00
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dc := strings.TrimPrefix(u.CorrelationID, "mesh-gateway:")
|
2020-06-03 21:28:45 +00:00
|
|
|
delete(snap.MeshGateway.GatewayGroups, dc)
|
|
|
|
delete(snap.MeshGateway.HostnameDatacenters, dc)
|
2019-06-18 00:52:01 +00:00
|
|
|
|
|
|
|
if len(resp.Nodes) > 0 {
|
|
|
|
snap.MeshGateway.GatewayGroups[dc] = resp.Nodes
|
2020-06-03 21:28:45 +00:00
|
|
|
snap.MeshGateway.HostnameDatacenters[dc] = s.hostnameEndpoints(logging.MeshGateway, snap.Datacenter, resp.Nodes)
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
// do nothing for now
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-04-16 21:00:48 +00:00
|
|
|
}
|
|
|
|
|
2020-12-23 22:12:36 +00:00
|
|
|
func (s *state) handleUpdateIngressGateway(ctx context.Context, u cache.UpdateEvent, snap *ConfigSnapshot) error {
|
2020-04-16 21:00:48 +00:00
|
|
|
if u.Err != nil {
|
|
|
|
return fmt.Errorf("error filling agent cache: %v", u.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case u.CorrelationID == rootsWatchID:
|
|
|
|
roots, ok := u.Result.(*structs.IndexedCARoots)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
snap.Roots = roots
|
2020-04-27 23:36:20 +00:00
|
|
|
case u.CorrelationID == gatewayConfigWatchID:
|
|
|
|
resp, ok := u.Result.(*structs.ConfigEntryResponse)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
gatewayConf, ok := resp.Entry.(*structs.IngressGatewayConfigEntry)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for config entry: %T", resp.Entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
snap.IngressGateway.TLSEnabled = gatewayConf.TLS.Enabled
|
|
|
|
snap.IngressGateway.TLSSet = true
|
|
|
|
|
2020-12-23 22:12:36 +00:00
|
|
|
if err := s.watchIngressLeafCert(ctx, snap); err != nil {
|
2020-04-27 23:36:20 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:00:48 +00:00
|
|
|
case u.CorrelationID == gatewayServicesWatchID:
|
|
|
|
services, ok := u.Result.(*structs.IndexedGatewayServices)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
|
2020-04-27 23:36:20 +00:00
|
|
|
// Update our upstreams and watches.
|
2020-05-07 15:22:57 +00:00
|
|
|
var hosts []string
|
2020-04-16 21:00:48 +00:00
|
|
|
watchedSvcs := make(map[string]struct{})
|
2020-04-16 23:24:11 +00:00
|
|
|
upstreamsMap := make(map[IngressListenerKey]structs.Upstreams)
|
2020-04-16 21:00:48 +00:00
|
|
|
for _, service := range services.Services {
|
2020-05-21 14:08:12 +00:00
|
|
|
u := makeUpstream(service)
|
2020-04-16 21:00:48 +00:00
|
|
|
|
2021-04-06 21:16:58 +00:00
|
|
|
watchOpts := discoveryChainWatchOpts{
|
|
|
|
id: u.Identifier(),
|
|
|
|
name: u.DestinationName,
|
|
|
|
namespace: u.DestinationNamespace,
|
|
|
|
}
|
2020-12-23 22:12:36 +00:00
|
|
|
err := s.watchDiscoveryChain(ctx, snap, watchOpts)
|
2020-04-16 21:00:48 +00:00
|
|
|
if err != nil {
|
2021-03-17 19:40:39 +00:00
|
|
|
return fmt.Errorf("failed to watch discovery chain for %s: %v", u.Identifier(), err)
|
2020-04-16 21:00:48 +00:00
|
|
|
}
|
|
|
|
watchedSvcs[u.Identifier()] = struct{}{}
|
2020-04-16 23:24:11 +00:00
|
|
|
|
2020-05-07 15:22:57 +00:00
|
|
|
hosts = append(hosts, service.Hosts...)
|
|
|
|
|
2020-04-16 23:24:11 +00:00
|
|
|
id := IngressListenerKey{Protocol: service.Protocol, Port: service.Port}
|
|
|
|
upstreamsMap[id] = append(upstreamsMap[id], u)
|
2020-04-16 21:00:48 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 15:22:57 +00:00
|
|
|
snap.IngressGateway.Upstreams = upstreamsMap
|
2020-04-27 23:36:20 +00:00
|
|
|
snap.IngressGateway.Hosts = hosts
|
|
|
|
snap.IngressGateway.HostsSet = true
|
|
|
|
|
2020-04-16 21:00:48 +00:00
|
|
|
for id, cancelFn := range snap.IngressGateway.WatchedDiscoveryChains {
|
|
|
|
if _, ok := watchedSvcs[id]; !ok {
|
|
|
|
cancelFn()
|
|
|
|
delete(snap.IngressGateway.WatchedDiscoveryChains, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 22:12:36 +00:00
|
|
|
if err := s.watchIngressLeafCert(ctx, snap); err != nil {
|
2020-04-27 23:36:20 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:00:48 +00:00
|
|
|
default:
|
2020-12-23 22:12:36 +00:00
|
|
|
return s.handleUpdateUpstreams(ctx, u, snap)
|
2020-04-16 21:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-26 20:00:44 +00:00
|
|
|
// Note: Ingress gateways are always bound to ports and never unix sockets.
|
|
|
|
// This means LocalBindPort is the only possibility
|
2020-05-21 14:08:12 +00:00
|
|
|
func makeUpstream(g *structs.GatewayService) structs.Upstream {
|
2020-04-16 21:00:48 +00:00
|
|
|
upstream := structs.Upstream{
|
2020-06-12 14:57:41 +00:00
|
|
|
DestinationName: g.Service.Name,
|
2020-04-16 21:00:48 +00:00
|
|
|
DestinationNamespace: g.Service.NamespaceOrDefault(),
|
|
|
|
LocalBindPort: g.Port,
|
2020-04-23 15:06:19 +00:00
|
|
|
IngressHosts: g.Hosts,
|
2020-04-21 21:06:23 +00:00
|
|
|
// Pass the protocol that was configured on the ingress listener in order
|
|
|
|
// to force that protocol on the Envoy listener.
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"protocol": g.Protocol,
|
|
|
|
},
|
2020-04-16 21:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return upstream
|
|
|
|
}
|
|
|
|
|
2021-04-06 21:16:58 +00:00
|
|
|
type discoveryChainWatchOpts struct {
|
|
|
|
id string
|
|
|
|
name string
|
|
|
|
namespace string
|
|
|
|
cfg reducedUpstreamConfig
|
|
|
|
meshGateway structs.MeshGatewayConfig
|
|
|
|
}
|
|
|
|
|
2020-12-23 22:12:36 +00:00
|
|
|
func (s *state) watchDiscoveryChain(ctx context.Context, snap *ConfigSnapshot, opts discoveryChainWatchOpts) error {
|
2021-04-06 21:16:58 +00:00
|
|
|
if _, ok := snap.ConnectProxy.WatchedDiscoveryChains[opts.id]; ok {
|
2020-04-16 21:00:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-23 22:12:36 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2020-04-16 21:00:48 +00:00
|
|
|
err := s.cache.Notify(ctx, cachetype.CompiledDiscoveryChainName, &structs.DiscoveryChainRequest{
|
2021-03-17 19:40:39 +00:00
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
2021-04-06 21:16:58 +00:00
|
|
|
Name: opts.name,
|
2021-03-17 19:40:39 +00:00
|
|
|
EvaluateInDatacenter: s.source.Datacenter,
|
2021-04-06 21:16:58 +00:00
|
|
|
EvaluateInNamespace: opts.namespace,
|
|
|
|
OverrideProtocol: opts.cfg.Protocol,
|
|
|
|
OverrideConnectTimeout: opts.cfg.ConnectTimeout(),
|
|
|
|
OverrideMeshGateway: opts.meshGateway,
|
|
|
|
}, "discovery-chain:"+opts.id, s.ch)
|
2020-04-16 21:00:48 +00:00
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-17 19:40:39 +00:00
|
|
|
switch s.kind {
|
|
|
|
case structs.ServiceKindIngressGateway:
|
2021-04-06 21:16:58 +00:00
|
|
|
snap.IngressGateway.WatchedDiscoveryChains[opts.id] = cancel
|
2021-03-17 19:40:39 +00:00
|
|
|
case structs.ServiceKindConnectProxy:
|
2021-04-06 21:16:58 +00:00
|
|
|
snap.ConnectProxy.WatchedDiscoveryChains[opts.id] = cancel
|
2021-03-17 19:40:39 +00:00
|
|
|
default:
|
2021-03-18 03:44:14 +00:00
|
|
|
cancel()
|
2021-03-17 19:40:39 +00:00
|
|
|
return fmt.Errorf("unsupported kind %s", s.kind)
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:00:48 +00:00
|
|
|
return nil
|
2020-04-27 23:36:20 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 22:04:06 +00:00
|
|
|
func (s *state) generateIngressDNSSANs(snap *ConfigSnapshot) []string {
|
|
|
|
// Update our leaf cert watch with wildcard entries for our DNS domains as well as any
|
|
|
|
// configured custom hostnames from the service.
|
|
|
|
if !snap.IngressGateway.TLSEnabled {
|
2020-04-27 23:36:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var dnsNames []string
|
2020-05-07 22:04:06 +00:00
|
|
|
namespaces := make(map[string]struct{})
|
|
|
|
for _, upstreams := range snap.IngressGateway.Upstreams {
|
|
|
|
for _, u := range upstreams {
|
|
|
|
namespaces[u.DestinationNamespace] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for ns := range namespaces {
|
|
|
|
// The default namespace is special cased in DNS resolution, so special
|
|
|
|
// case it here.
|
|
|
|
if ns == structs.IntentionDefaultNamespace {
|
|
|
|
ns = ""
|
|
|
|
} else {
|
|
|
|
ns = ns + "."
|
|
|
|
}
|
|
|
|
|
|
|
|
dnsNames = append(dnsNames, fmt.Sprintf("*.ingress.%s%s", ns, s.dnsConfig.Domain))
|
|
|
|
dnsNames = append(dnsNames, fmt.Sprintf("*.ingress.%s%s.%s", ns, s.source.Datacenter, s.dnsConfig.Domain))
|
2020-04-27 23:36:20 +00:00
|
|
|
if s.dnsConfig.AltDomain != "" {
|
2020-05-07 22:04:06 +00:00
|
|
|
dnsNames = append(dnsNames, fmt.Sprintf("*.ingress.%s%s", ns, s.dnsConfig.AltDomain))
|
|
|
|
dnsNames = append(dnsNames, fmt.Sprintf("*.ingress.%s%s.%s", ns, s.source.Datacenter, s.dnsConfig.AltDomain))
|
2020-04-27 23:36:20 +00:00
|
|
|
}
|
2020-05-07 22:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dnsNames = append(dnsNames, snap.IngressGateway.Hosts...)
|
|
|
|
|
|
|
|
return dnsNames
|
|
|
|
}
|
|
|
|
|
2020-12-23 22:12:36 +00:00
|
|
|
func (s *state) watchIngressLeafCert(ctx context.Context, snap *ConfigSnapshot) error {
|
2020-05-07 22:04:06 +00:00
|
|
|
if !snap.IngressGateway.TLSSet || !snap.IngressGateway.HostsSet {
|
|
|
|
return nil
|
2020-04-27 23:36:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Watch the leaf cert
|
|
|
|
if snap.IngressGateway.LeafCertWatchCancel != nil {
|
|
|
|
snap.IngressGateway.LeafCertWatchCancel()
|
|
|
|
}
|
2020-12-23 22:12:36 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2020-04-27 23:36:20 +00:00
|
|
|
err := s.cache.Notify(ctx, cachetype.ConnectCALeafName, &cachetype.ConnectCALeafRequest{
|
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
Token: s.token,
|
|
|
|
Service: s.service,
|
2020-05-07 22:04:06 +00:00
|
|
|
DNSSAN: s.generateIngressDNSSANs(snap),
|
2020-04-27 23:36:20 +00:00
|
|
|
EnterpriseMeta: s.proxyID.EnterpriseMeta,
|
|
|
|
}, leafWatchID, s.ch)
|
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
snap.IngressGateway.LeafCertWatchCancel = cancel
|
|
|
|
|
|
|
|
return nil
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 12:36:38 +00:00
|
|
|
// CurrentSnapshot synchronously returns the current ConfigSnapshot if there is
|
|
|
|
// one ready. If we don't have one yet because not all necessary parts have been
|
|
|
|
// returned (i.e. both roots and leaf cert), nil is returned.
|
|
|
|
func (s *state) CurrentSnapshot() *ConfigSnapshot {
|
|
|
|
// Make a chan for the response to be sent on
|
|
|
|
ch := make(chan *ConfigSnapshot, 1)
|
|
|
|
s.reqCh <- ch
|
|
|
|
// Wait for the response
|
|
|
|
return <-ch
|
|
|
|
}
|
|
|
|
|
|
|
|
// Changed returns whether or not the passed NodeService has had any of the
|
|
|
|
// fields we care about for config state watching changed or a different token.
|
|
|
|
func (s *state) Changed(ns *structs.NodeService, token string) bool {
|
|
|
|
if ns == nil {
|
|
|
|
return true
|
|
|
|
}
|
2020-02-06 15:52:25 +00:00
|
|
|
|
|
|
|
proxyCfg, err := copyProxyConfig(ns)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Warn("Failed to parse proxy config and will treat the new service as unchanged")
|
|
|
|
}
|
|
|
|
|
2019-06-24 19:05:36 +00:00
|
|
|
return ns.Kind != s.kind ||
|
2020-01-24 15:04:58 +00:00
|
|
|
s.proxyID != ns.CompoundServiceID() ||
|
2018-10-03 12:36:38 +00:00
|
|
|
s.address != ns.Address ||
|
|
|
|
s.port != ns.Port ||
|
2020-02-06 15:52:25 +00:00
|
|
|
!reflect.DeepEqual(s.proxyCfg, proxyCfg) ||
|
2018-10-03 12:36:38 +00:00
|
|
|
s.token != token
|
|
|
|
}
|
2020-06-03 21:28:45 +00:00
|
|
|
|
|
|
|
// hostnameEndpoints returns all CheckServiceNodes that have hostnames instead of IPs as the address.
|
|
|
|
// Envoy cannot resolve hostnames provided through EDS, so we exclusively use CDS for these clusters.
|
|
|
|
// If there is a mix of hostnames and addresses we exclusively use the hostnames, since clusters cannot discover
|
|
|
|
// services with both EDS and DNS.
|
|
|
|
func (s *state) hostnameEndpoints(loggerName string, localDC string, nodes structs.CheckServiceNodes) structs.CheckServiceNodes {
|
|
|
|
var (
|
|
|
|
hasIP bool
|
|
|
|
hasHostname bool
|
|
|
|
resp structs.CheckServiceNodes
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, n := range nodes {
|
|
|
|
addr, _ := n.BestAddress(localDC != n.Node.Datacenter)
|
|
|
|
if net.ParseIP(addr) != nil {
|
|
|
|
hasIP = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
hasHostname = true
|
|
|
|
resp = append(resp, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasHostname && hasIP {
|
|
|
|
dc := nodes[0].Node.Datacenter
|
2020-06-12 14:57:41 +00:00
|
|
|
sn := nodes[0].Service.CompoundServiceName()
|
2020-06-03 21:28:45 +00:00
|
|
|
|
|
|
|
s.logger.Named(loggerName).
|
2020-06-12 19:46:17 +00:00
|
|
|
Warn("service contains instances with mix of hostnames and IP addresses; only hostnames will be passed to Envoy",
|
2020-06-12 14:57:41 +00:00
|
|
|
"dc", dc, "service", sn.String())
|
2020-06-03 21:28:45 +00:00
|
|
|
}
|
|
|
|
return resp
|
|
|
|
}
|