2018-10-03 12:36:38 +00:00
|
|
|
package proxycfg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2021-10-22 21:22:55 +00:00
|
|
|
"fmt"
|
2020-06-03 21:28:45 +00:00
|
|
|
"net"
|
2018-10-03 12:36:38 +00:00
|
|
|
"reflect"
|
|
|
|
"time"
|
|
|
|
|
2020-11-10 23:57:35 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/mitchellh/copystructure"
|
|
|
|
|
2018-10-03 12:36:38 +00:00
|
|
|
"github.com/hashicorp/consul/agent/cache"
|
|
|
|
cachetype "github.com/hashicorp/consul/agent/cache-types"
|
|
|
|
"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 + ":"
|
|
|
|
preparedQueryIDPrefix = string(structs.UpstreamDestTypePreparedQuery) + ":"
|
|
|
|
defaultPreparedQueryPollInterval = 30 * time.Second
|
2018-10-03 12:36:38 +00:00
|
|
|
)
|
|
|
|
|
2020-12-23 22:29:20 +00:00
|
|
|
type stateConfig struct {
|
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
|
2020-12-23 22:29:20 +00:00
|
|
|
}
|
2018-10-03 12:36:38 +00:00
|
|
|
|
2020-12-23 22:29:20 +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 {
|
2020-12-23 23:03:30 +00:00
|
|
|
logger hclog.Logger
|
|
|
|
serviceInstance serviceInstance
|
|
|
|
handler kindHandler
|
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()
|
|
|
|
|
|
|
|
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-12-23 22:29:20 +00:00
|
|
|
type serviceInstance struct {
|
|
|
|
kind structs.ServiceKind
|
|
|
|
service string
|
|
|
|
proxyID structs.ServiceID
|
|
|
|
address string
|
|
|
|
port int
|
|
|
|
meta map[string]string
|
|
|
|
taggedAddresses map[string]structs.ServiceAddress
|
|
|
|
proxyCfg structs.ConnectProxyConfig
|
|
|
|
token 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.
|
2020-12-23 23:03:30 +00:00
|
|
|
func newState(ns *structs.NodeService, token string, config stateConfig) (*state, error) {
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
s, err := newServiceInstanceFromNodeService(ns, token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var handler kindHandler
|
2021-06-17 16:53:25 +00:00
|
|
|
h := handlerState{stateConfig: config, serviceInstance: s, ch: ch}
|
|
|
|
|
2020-04-16 21:00:48 +00:00
|
|
|
switch ns.Kind {
|
|
|
|
case structs.ServiceKindConnectProxy:
|
2021-06-17 16:53:25 +00:00
|
|
|
handler = &handlerConnectProxy{handlerState: h}
|
2020-04-10 18:06:08 +00:00
|
|
|
case structs.ServiceKindTerminatingGateway:
|
2021-06-17 16:53:25 +00:00
|
|
|
h.stateConfig.logger = config.logger.Named(logging.TerminatingGateway)
|
|
|
|
handler = &handlerTerminatingGateway{handlerState: h}
|
2020-04-16 21:00:48 +00:00
|
|
|
case structs.ServiceKindMeshGateway:
|
2021-06-17 16:53:25 +00:00
|
|
|
h.stateConfig.logger = config.logger.Named(logging.MeshGateway)
|
|
|
|
handler = &handlerMeshGateway{handlerState: h}
|
2020-04-16 21:00:48 +00:00
|
|
|
case structs.ServiceKindIngressGateway:
|
2021-06-17 16:53:25 +00:00
|
|
|
handler = &handlerIngressGateway{handlerState: h}
|
2020-04-16 21:00:48 +00:00
|
|
|
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-12-23 22:29:20 +00:00
|
|
|
return &state{
|
2020-12-23 23:03:30 +00:00
|
|
|
logger: config.logger.With("proxy", s.proxyID, "kind", s.kind),
|
2020-12-23 22:29:20 +00:00
|
|
|
serviceInstance: s,
|
2020-12-23 23:03:30 +00:00
|
|
|
handler: handler,
|
|
|
|
ch: ch,
|
|
|
|
snapCh: make(chan ConfigSnapshot, 1),
|
|
|
|
reqCh: make(chan chan *ConfigSnapshot, 1),
|
2020-12-23 22:29:20 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newServiceInstanceFromNodeService(ns *structs.NodeService, token string) (serviceInstance, error) {
|
|
|
|
proxyCfg, err := copyProxyConfig(ns)
|
|
|
|
if err != nil {
|
|
|
|
return serviceInstance{}, 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
|
|
|
|
}
|
|
|
|
|
2020-12-23 22:29:20 +00:00
|
|
|
return serviceInstance{
|
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,
|
2018-10-03 12:36:38 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-12-23 23:03:30 +00:00
|
|
|
type kindHandler interface {
|
|
|
|
initialize(ctx context.Context) (ConfigSnapshot, error)
|
|
|
|
handleUpdate(ctx context.Context, u cache.UpdateEvent, snap *ConfigSnapshot) error
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-12-23 23:03:30 +00:00
|
|
|
snap, err := s.handler.initialize(ctx)
|
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
|
|
|
|
}
|
|
|
|
|
2021-06-17 16:53:25 +00:00
|
|
|
type handlerState struct {
|
2020-12-23 23:03:30 +00:00
|
|
|
stateConfig // TODO: un-embed
|
|
|
|
serviceInstance // TODO: un-embed
|
|
|
|
ch chan cache.UpdateEvent
|
2019-06-24 19:05:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-23 23:03:30 +00:00
|
|
|
func newConfigSnapshotFromServiceInstance(s serviceInstance, config stateConfig) ConfigSnapshot {
|
|
|
|
// TODO: use serviceInstance type in ConfigSnapshot
|
|
|
|
return 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,
|
2020-12-23 23:03:30 +00:00
|
|
|
Datacenter: config.source.Datacenter,
|
2021-10-29 00:47:42 +00:00
|
|
|
Locality: GatewayKey{Datacenter: config.source.Datacenter, Partition: s.proxyID.PartitionOrDefault()},
|
2020-12-23 23:03:30 +00:00
|
|
|
ServerSNIFn: config.serverSNIFn,
|
|
|
|
IntentionDefaultAllow: config.intentionDefaultAllow,
|
2018-10-03 12:36:38 +00:00
|
|
|
}
|
2019-07-12 21:19:37 +00:00
|
|
|
}
|
|
|
|
|
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-07-23 22:36:17 +00:00
|
|
|
s.logger.Trace("A blocking query returned; handling snapshot update", "correlationID", u.CorrelationID)
|
2021-02-02 19:26:38 +00:00
|
|
|
|
2020-12-23 23:03:30 +00:00
|
|
|
if err := s.handler.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 {
|
2020-12-23 23:03:30 +00:00
|
|
|
s.logger.Error("Failed to copy config snapshot for proxy", "error", err)
|
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 {
|
2020-12-23 23:03:30 +00:00
|
|
|
s.logger.Error("Failed to copy config snapshot for proxy", "error", err)
|
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{}{}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
2020-12-23 23:03:30 +00:00
|
|
|
i := s.serviceInstance
|
|
|
|
return ns.Kind != i.kind ||
|
|
|
|
i.proxyID != ns.CompoundServiceID() ||
|
|
|
|
i.address != ns.Address ||
|
|
|
|
i.port != ns.Port ||
|
|
|
|
!reflect.DeepEqual(i.proxyCfg, proxyCfg) ||
|
|
|
|
i.token != token
|
2018-10-03 12:36:38 +00:00
|
|
|
}
|
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.
|
2021-10-29 00:41:48 +00:00
|
|
|
func hostnameEndpoints(logger hclog.Logger, localKey GatewayKey, nodes structs.CheckServiceNodes) structs.CheckServiceNodes {
|
2020-06-03 21:28:45 +00:00
|
|
|
var (
|
|
|
|
hasIP bool
|
|
|
|
hasHostname bool
|
|
|
|
resp structs.CheckServiceNodes
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, n := range nodes {
|
2021-10-29 00:41:48 +00:00
|
|
|
addr, _ := n.BestAddress(!localKey.Matches(n.Node.Datacenter, n.Node.PartitionOrDefault()))
|
2020-06-03 21:28:45 +00:00
|
|
|
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
|
|
|
|
2020-12-23 19:17:15 +00:00
|
|
|
logger.Warn("service contains instances with mix of hostnames and IP addresses; only hostnames will be passed to Envoy",
|
|
|
|
"dc", dc, "service", sn.String())
|
2020-06-03 21:28:45 +00:00
|
|
|
}
|
|
|
|
return resp
|
|
|
|
}
|
2021-10-22 21:22:55 +00:00
|
|
|
|
|
|
|
type gatewayWatchOpts struct {
|
|
|
|
notifier CacheNotifier
|
|
|
|
notifyCh chan cache.UpdateEvent
|
|
|
|
source structs.QuerySource
|
|
|
|
token string
|
|
|
|
key GatewayKey
|
|
|
|
upstreamID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func watchMeshGateway(ctx context.Context, opts gatewayWatchOpts) error {
|
|
|
|
return opts.notifier.Notify(ctx, cachetype.InternalServiceDumpName, &structs.ServiceDumpRequest{
|
|
|
|
Datacenter: opts.key.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: opts.token},
|
|
|
|
ServiceKind: structs.ServiceKindMeshGateway,
|
|
|
|
UseServiceKind: true,
|
|
|
|
Source: opts.source,
|
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInPartition(opts.key.Partition),
|
|
|
|
}, fmt.Sprintf("mesh-gateway:%s:%s", opts.key.String(), opts.upstreamID), opts.notifyCh)
|
|
|
|
}
|