2020-12-24 19:11:13 +00:00
|
|
|
package proxycfg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-09-26 16:50:17 +00:00
|
|
|
"net"
|
2022-06-06 19:20:41 +00:00
|
|
|
"sort"
|
2022-09-26 16:50:17 +00:00
|
|
|
"strconv"
|
2020-12-24 19:11:13 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-10-13 22:54:14 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2022-09-26 16:50:17 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
|
2022-06-15 19:36:18 +00:00
|
|
|
cachetype "github.com/hashicorp/consul/agent/cache-types"
|
2022-09-23 01:24:13 +00:00
|
|
|
"github.com/hashicorp/consul/agent/proxycfg/internal/watch"
|
2020-12-24 19:11:13 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2022-06-06 19:20:41 +00:00
|
|
|
"github.com/hashicorp/consul/lib/maps"
|
2020-12-24 19:11:13 +00:00
|
|
|
"github.com/hashicorp/consul/logging"
|
2022-06-15 19:36:18 +00:00
|
|
|
"github.com/hashicorp/consul/proto/pbpeering"
|
2020-12-24 19:11:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type handlerMeshGateway struct {
|
|
|
|
handlerState
|
|
|
|
}
|
|
|
|
|
2022-09-26 16:50:17 +00:00
|
|
|
type peerAddressType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
undefinedAddressType peerAddressType = ""
|
|
|
|
ipAddressType peerAddressType = "ip"
|
|
|
|
hostnameAddressType peerAddressType = "hostname"
|
|
|
|
)
|
|
|
|
|
2020-12-24 19:11:13 +00:00
|
|
|
// initialize sets up the watches needed based on the current mesh gateway registration
|
|
|
|
func (s *handlerMeshGateway) initialize(ctx context.Context) (ConfigSnapshot, error) {
|
|
|
|
snap := newConfigSnapshotFromServiceInstance(s.serviceInstance, s.stateConfig)
|
2022-09-26 16:50:17 +00:00
|
|
|
snap.MeshGateway.WatchedLocalServers = watch.NewMap[string, structs.CheckServiceNodes]()
|
2022-09-23 01:24:13 +00:00
|
|
|
|
2020-12-24 19:11:13 +00:00
|
|
|
// Watch for root changes
|
2022-06-01 15:18:06 +00:00
|
|
|
err := s.dataSources.CARoots.Notify(ctx, &structs.DCSpecificRequest{
|
2020-12-24 19:11:13 +00:00
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
Source: *s.source,
|
|
|
|
}, rootsWatchID, s.ch)
|
|
|
|
if err != nil {
|
|
|
|
return snap, err
|
|
|
|
}
|
|
|
|
|
2022-06-15 19:36:18 +00:00
|
|
|
// Watch for all peer trust bundles we may need.
|
2022-07-12 23:18:05 +00:00
|
|
|
err = s.dataSources.TrustBundleList.Notify(ctx, &cachetype.TrustBundleListRequest{
|
|
|
|
Request: &pbpeering.TrustBundleListByServiceRequest{
|
|
|
|
Kind: string(structs.ServiceKindMeshGateway),
|
|
|
|
ServiceName: s.service,
|
|
|
|
Namespace: s.proxyID.NamespaceOrDefault(),
|
|
|
|
Partition: s.proxyID.PartitionOrDefault(),
|
|
|
|
},
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
2022-06-15 19:36:18 +00:00
|
|
|
}, peeringTrustBundlesWatchID, s.ch)
|
|
|
|
if err != nil {
|
|
|
|
return snap, err
|
|
|
|
}
|
|
|
|
|
2021-09-17 23:36:20 +00:00
|
|
|
wildcardEntMeta := s.proxyID.WithWildcardNamespace()
|
2021-08-19 20:09:42 +00:00
|
|
|
|
2021-10-23 19:19:51 +00:00
|
|
|
// Watch for all services.
|
2021-10-26 21:58:23 +00:00
|
|
|
// Eventually we will have to watch connect enabled instances for each service as well as the
|
2021-10-23 19:19:51 +00:00
|
|
|
// destination services themselves but those notifications will be setup later.
|
|
|
|
// We cannot setup those watches until we know what the services are.
|
2022-06-01 15:18:06 +00:00
|
|
|
err = s.dataSources.ServiceList.Notify(ctx, &structs.DCSpecificRequest{
|
2020-12-24 19:11:13 +00:00
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
Source: *s.source,
|
2021-08-19 20:09:42 +00:00
|
|
|
EnterpriseMeta: *wildcardEntMeta,
|
2020-12-24 19:11:13 +00:00
|
|
|
}, serviceListWatchID, s.ch)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return snap, err
|
|
|
|
}
|
|
|
|
|
2021-10-23 19:19:51 +00:00
|
|
|
// Watch service-resolvers so we can setup service subset clusters
|
2022-06-01 15:18:06 +00:00
|
|
|
err = s.dataSources.ConfigEntryList.Notify(ctx, &structs.ConfigEntryQuery{
|
2021-10-23 19:19:51 +00:00
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
Kind: structs.ServiceResolver,
|
|
|
|
EnterpriseMeta: *wildcardEntMeta,
|
|
|
|
}, serviceResolversWatchID, s.ch)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Named(logging.MeshGateway).
|
|
|
|
Error("failed to register watch for service-resolver config entries", "error", err)
|
|
|
|
return snap, err
|
|
|
|
}
|
|
|
|
|
2021-10-26 22:10:30 +00:00
|
|
|
if s.proxyID.InDefaultPartition() {
|
2022-09-23 01:24:13 +00:00
|
|
|
if err := s.initializeCrossDCWatches(ctx, &snap); err != nil {
|
2021-10-23 19:19:51 +00:00
|
|
|
return snap, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeEntWatches(ctx); err != nil {
|
|
|
|
return snap, err
|
|
|
|
}
|
|
|
|
|
2022-06-15 19:36:18 +00:00
|
|
|
// Get information about the entire service mesh.
|
|
|
|
err = s.dataSources.ConfigEntry.Notify(ctx, &structs.ConfigEntryQuery{
|
|
|
|
Kind: structs.MeshConfig,
|
|
|
|
Name: structs.MeshConfigMesh,
|
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInPartition(s.proxyID.PartitionOrDefault()),
|
|
|
|
}, meshConfigEntryID, s.ch)
|
|
|
|
if err != nil {
|
|
|
|
return snap, err
|
|
|
|
}
|
|
|
|
|
2022-06-06 19:20:41 +00:00
|
|
|
// Watch for all exported services from this mesh gateway's partition in any peering.
|
|
|
|
err = s.dataSources.ExportedPeeredServices.Notify(ctx, &structs.DCSpecificRequest{
|
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
Source: *s.source,
|
|
|
|
EnterpriseMeta: s.proxyID.EnterpriseMeta,
|
|
|
|
}, exportedServiceListWatchID, s.ch)
|
|
|
|
if err != nil {
|
|
|
|
return snap, err
|
|
|
|
}
|
|
|
|
|
2021-10-23 19:19:51 +00:00
|
|
|
snap.MeshGateway.WatchedServices = make(map[structs.ServiceName]context.CancelFunc)
|
|
|
|
snap.MeshGateway.WatchedGateways = make(map[string]context.CancelFunc)
|
|
|
|
snap.MeshGateway.ServiceGroups = make(map[structs.ServiceName]structs.CheckServiceNodes)
|
|
|
|
snap.MeshGateway.GatewayGroups = make(map[string]structs.CheckServiceNodes)
|
|
|
|
snap.MeshGateway.ServiceResolvers = make(map[structs.ServiceName]*structs.ServiceResolverConfigEntry)
|
|
|
|
snap.MeshGateway.HostnameDatacenters = make(map[string]structs.CheckServiceNodes)
|
2022-06-06 19:20:41 +00:00
|
|
|
snap.MeshGateway.ExportedServicesWithPeers = make(map[structs.ServiceName][]string)
|
|
|
|
snap.MeshGateway.DiscoveryChain = make(map[structs.ServiceName]*structs.CompiledDiscoveryChain)
|
|
|
|
snap.MeshGateway.WatchedDiscoveryChains = make(map[structs.ServiceName]context.CancelFunc)
|
2022-10-06 13:54:14 +00:00
|
|
|
snap.MeshGateway.WatchedPeeringServices = make(map[string]map[structs.ServiceName]context.CancelFunc)
|
|
|
|
snap.MeshGateway.WatchedPeers = make(map[string]context.CancelFunc)
|
|
|
|
snap.MeshGateway.PeeringServices = make(map[string]map[structs.ServiceName]PeeringServiceValue)
|
2021-10-23 19:19:51 +00:00
|
|
|
|
|
|
|
// there is no need to initialize the map of service resolvers as we
|
|
|
|
// fully rebuild it every time we get updates
|
|
|
|
return snap, err
|
|
|
|
}
|
|
|
|
|
2022-09-23 01:24:13 +00:00
|
|
|
func (s *handlerMeshGateway) initializeCrossDCWatches(ctx context.Context, snap *ConfigSnapshot) error {
|
2020-12-24 19:11:13 +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.
|
2022-06-01 15:18:06 +00:00
|
|
|
err := s.dataSources.FederationStateListMeshGateways.Notify(ctx, &structs.DCSpecificRequest{
|
2020-12-24 19:11:13 +00:00
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
Source: *s.source,
|
|
|
|
}, federationStateListGatewaysWatchID, s.ch)
|
|
|
|
if err != nil {
|
2021-10-23 19:19:51 +00:00
|
|
|
return err
|
2020-12-24 19:11:13 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 15:18:06 +00:00
|
|
|
err = s.dataSources.Health.Notify(ctx, &structs.ServiceSpecificRequest{
|
2020-12-24 19:11:13 +00:00
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
ServiceName: structs.ConsulServiceName,
|
|
|
|
}, consulServerListWatchID, s.ch)
|
|
|
|
if err != nil {
|
2021-10-23 19:19:51 +00:00
|
|
|
return err
|
2020-12-24 19:11:13 +00:00
|
|
|
}
|
2022-09-26 16:50:17 +00:00
|
|
|
snap.MeshGateway.WatchedLocalServers.InitWatch(structs.ConsulServiceName, nil)
|
2020-12-24 19:11:13 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 15:18:06 +00:00
|
|
|
err := s.dataSources.Datacenters.Notify(ctx, &structs.DatacentersRequest{
|
2020-12-24 19:11:13 +00:00
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token, MaxAge: 30 * time.Second},
|
|
|
|
}, datacentersWatchID, s.ch)
|
|
|
|
if err != nil {
|
2021-10-23 19:19:51 +00:00
|
|
|
return err
|
2020-12-24 19:11:13 +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.
|
|
|
|
|
2021-10-23 19:19:51 +00:00
|
|
|
return nil
|
2020-12-24 19:11:13 +00:00
|
|
|
}
|
|
|
|
|
2022-05-20 14:47:40 +00:00
|
|
|
func (s *handlerMeshGateway) handleUpdate(ctx context.Context, u UpdateEvent, snap *ConfigSnapshot) error {
|
2020-12-24 19:11:13 +00:00
|
|
|
if u.Err != nil {
|
|
|
|
return fmt.Errorf("error filling agent cache: %v", u.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
meshLogger := s.logger.Named(logging.MeshGateway)
|
|
|
|
|
|
|
|
switch u.CorrelationID {
|
|
|
|
case rootsWatchID:
|
|
|
|
roots, ok := u.Result.(*structs.IndexedCARoots)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
snap.Roots = roots
|
2021-10-23 19:19:51 +00:00
|
|
|
|
2020-12-24 19:11:13 +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
|
|
|
|
|
|
|
|
for dc, nodes := range dcIndexedNodes.DatacenterNodes {
|
|
|
|
snap.MeshGateway.HostnameDatacenters[dc] = hostnameEndpoints(
|
2021-10-29 00:41:48 +00:00
|
|
|
s.logger.Named(logging.MeshGateway),
|
2021-10-29 00:47:42 +00:00
|
|
|
snap.Locality,
|
2021-10-29 00:41:48 +00:00
|
|
|
nodes,
|
|
|
|
)
|
2020-12-24 19:11:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for dc := range snap.MeshGateway.HostnameDatacenters {
|
|
|
|
if _, ok := dcIndexedNodes.DatacenterNodes[dc]; !ok {
|
|
|
|
delete(snap.MeshGateway.HostnameDatacenters, dc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case serviceListWatchID:
|
|
|
|
services, ok := u.Result.(*structs.IndexedServiceList)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
|
|
|
|
svcMap := make(map[structs.ServiceName]struct{})
|
|
|
|
for _, svc := range services.Services {
|
|
|
|
// Make sure to add every service to this map, we use it to cancel
|
|
|
|
// watches below.
|
|
|
|
svcMap[svc] = struct{}{}
|
|
|
|
|
|
|
|
if _, ok := snap.MeshGateway.WatchedServices[svc]; !ok {
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2022-06-01 15:18:06 +00:00
|
|
|
err := s.dataSources.Health.Notify(ctx, &structs.ServiceSpecificRequest{
|
2020-12-24 19:11:13 +00:00
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
ServiceName: svc.Name,
|
|
|
|
Connect: true,
|
|
|
|
EnterpriseMeta: svc.EnterpriseMeta,
|
|
|
|
}, fmt.Sprintf("connect-service:%s", svc.String()), s.ch)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
meshLogger.Error("failed to register watch for connect-service",
|
|
|
|
"service", svc.String(),
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
snap.MeshGateway.WatchedServices[svc] = cancel
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for sid, cancelFn := range snap.MeshGateway.WatchedServices {
|
|
|
|
if _, ok := svcMap[sid]; !ok {
|
|
|
|
meshLogger.Debug("canceling watch for service", "service", sid.String())
|
|
|
|
// TODO (gateways) Should the sid also be deleted from snap.MeshGateway.ServiceGroups?
|
|
|
|
// Do those endpoints get cleaned up some other way?
|
|
|
|
delete(snap.MeshGateway.WatchedServices, sid)
|
|
|
|
cancelFn()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
snap.MeshGateway.WatchedServicesSet = true
|
2021-10-23 19:19:51 +00:00
|
|
|
|
2020-12-24 19:11:13 +00:00
|
|
|
case datacentersWatchID:
|
|
|
|
datacentersRaw, ok := u.Result.(*[]string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-10-23 19:19:51 +00:00
|
|
|
entMeta := structs.DefaultEnterpriseMetaInDefaultPartition()
|
|
|
|
gk := GatewayKey{Datacenter: dc, Partition: entMeta.PartitionOrDefault()}
|
|
|
|
|
|
|
|
if _, ok := snap.MeshGateway.WatchedGateways[gk.String()]; !ok {
|
2020-12-24 19:11:13 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2022-06-01 15:18:06 +00:00
|
|
|
err := s.dataSources.InternalServiceDump.Notify(ctx, &structs.ServiceDumpRequest{
|
2020-12-24 19:11:13 +00:00
|
|
|
Datacenter: dc,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
ServiceKind: structs.ServiceKindMeshGateway,
|
|
|
|
UseServiceKind: true,
|
|
|
|
Source: *s.source,
|
2021-10-23 19:19:51 +00:00
|
|
|
EnterpriseMeta: *entMeta,
|
|
|
|
}, fmt.Sprintf("mesh-gateway:%s", gk.String()), s.ch)
|
2020-12-24 19:11:13 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
meshLogger.Error("failed to register watch for mesh-gateway",
|
|
|
|
"datacenter", dc,
|
2021-10-23 19:19:51 +00:00
|
|
|
"partition", entMeta.PartitionOrDefault(),
|
2020-12-24 19:11:13 +00:00
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
2021-10-23 19:19:51 +00:00
|
|
|
snap.MeshGateway.WatchedGateways[gk.String()] = cancel
|
2020-12-24 19:11:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 19:19:51 +00:00
|
|
|
for key, cancelFn := range snap.MeshGateway.WatchedGateways {
|
|
|
|
gk := gatewayKeyFromString(key)
|
|
|
|
if gk.Datacenter == s.source.Datacenter {
|
|
|
|
// Only cross-DC watches are managed by the datacenters watch.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-12-24 19:11:13 +00:00
|
|
|
found := false
|
|
|
|
for _, dcCurrent := range datacenters {
|
2021-10-23 19:19:51 +00:00
|
|
|
if dcCurrent == gk.Datacenter {
|
2020-12-24 19:11:13 +00:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2021-10-23 19:19:51 +00:00
|
|
|
delete(snap.MeshGateway.WatchedGateways, key)
|
2020-12-24 19:11:13 +00:00
|
|
|
cancelFn()
|
|
|
|
}
|
|
|
|
}
|
2021-10-23 19:19:51 +00:00
|
|
|
|
2020-12-24 19:11:13 +00:00
|
|
|
case serviceResolversWatchID:
|
|
|
|
configEntries, ok := u.Result.(*structs.IndexedConfigEntries)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
|
|
|
|
resolvers := make(map[structs.ServiceName]*structs.ServiceResolverConfigEntry)
|
|
|
|
for _, entry := range configEntries.Entries {
|
|
|
|
if resolver, ok := entry.(*structs.ServiceResolverConfigEntry); ok {
|
|
|
|
resolvers[structs.NewServiceName(resolver.Name, &resolver.EnterpriseMeta)] = resolver
|
|
|
|
}
|
|
|
|
}
|
|
|
|
snap.MeshGateway.ServiceResolvers = resolvers
|
|
|
|
|
|
|
|
case consulServerListWatchID:
|
|
|
|
resp, ok := u.Result.(*structs.IndexedCheckServiceNodes)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-26 16:50:17 +00:00
|
|
|
snap.MeshGateway.WatchedLocalServers.Set(structs.ConsulServiceName, resp.Nodes)
|
2020-12-24 19:11:13 +00:00
|
|
|
|
2022-06-06 19:20:41 +00:00
|
|
|
case exportedServiceListWatchID:
|
|
|
|
exportedServices, ok := u.Result.(*structs.IndexedExportedServiceList)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
|
|
|
|
seenServices := make(map[structs.ServiceName][]string) // svc -> peername slice
|
|
|
|
for peerName, services := range exportedServices.Services {
|
|
|
|
for _, svc := range services {
|
|
|
|
seenServices[svc] = append(seenServices[svc], peerName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Sort the peer names so ultimately xDS has a stable output.
|
|
|
|
for svc := range seenServices {
|
|
|
|
sort.Strings(seenServices[svc])
|
|
|
|
}
|
|
|
|
peeredServiceList := maps.SliceOfKeys(seenServices)
|
|
|
|
structs.ServiceList(peeredServiceList).Sort()
|
|
|
|
|
|
|
|
snap.MeshGateway.ExportedServicesSlice = peeredServiceList
|
|
|
|
snap.MeshGateway.ExportedServicesWithPeers = seenServices
|
2022-06-15 19:36:18 +00:00
|
|
|
snap.MeshGateway.ExportedServicesSet = true
|
|
|
|
|
|
|
|
// Decide if we do or do not need our leaf.
|
|
|
|
hasExports := len(snap.MeshGateway.ExportedServicesSlice) > 0
|
|
|
|
if hasExports && snap.MeshGateway.LeafCertWatchCancel == nil {
|
|
|
|
// no watch and we need one
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
err := s.dataSources.LeafCertificate.Notify(ctx, &cachetype.ConnectCALeafRequest{
|
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
Token: s.token,
|
|
|
|
Kind: structs.ServiceKindMeshGateway,
|
|
|
|
EnterpriseMeta: s.proxyID.EnterpriseMeta,
|
|
|
|
}, leafWatchID, s.ch)
|
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
snap.MeshGateway.LeafCertWatchCancel = cancel
|
|
|
|
} else if !hasExports && snap.MeshGateway.LeafCertWatchCancel != nil {
|
|
|
|
// has watch and shouldn't
|
|
|
|
snap.MeshGateway.LeafCertWatchCancel()
|
|
|
|
snap.MeshGateway.LeafCertWatchCancel = nil
|
|
|
|
snap.MeshGateway.Leaf = nil
|
|
|
|
}
|
2022-06-06 19:20:41 +00:00
|
|
|
|
|
|
|
// For each service that we should be exposing, also watch disco chains
|
|
|
|
// in the same manner as an ingress gateway would.
|
|
|
|
|
|
|
|
for _, svc := range snap.MeshGateway.ExportedServicesSlice {
|
|
|
|
if _, ok := snap.MeshGateway.WatchedDiscoveryChains[svc]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
err := s.dataSources.CompiledDiscoveryChain.Notify(ctx, &structs.DiscoveryChainRequest{
|
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
Name: svc.Name,
|
|
|
|
EvaluateInDatacenter: s.source.Datacenter,
|
|
|
|
EvaluateInNamespace: svc.NamespaceOrDefault(),
|
|
|
|
EvaluateInPartition: svc.PartitionOrDefault(),
|
|
|
|
}, "discovery-chain:"+svc.String(), s.ch)
|
|
|
|
if err != nil {
|
|
|
|
meshLogger.Error("failed to register watch for discovery chain",
|
|
|
|
"service", svc.String(),
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
snap.MeshGateway.WatchedDiscoveryChains[svc] = cancel
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up data from services that were not in the update
|
|
|
|
|
|
|
|
for svc, cancelFn := range snap.MeshGateway.WatchedDiscoveryChains {
|
|
|
|
if _, ok := seenServices[svc]; !ok {
|
|
|
|
cancelFn()
|
|
|
|
delete(snap.MeshGateway.WatchedDiscoveryChains, svc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// These entries are intentionally handled separately from the
|
|
|
|
// WatchedDiscoveryChains above. There have been situations where a
|
|
|
|
// discovery watch was cancelled, then fired. That update event then
|
|
|
|
// re-populated the DiscoveryChain map entry, which wouldn't get
|
|
|
|
// cleaned up since there was no known watch for it.
|
|
|
|
|
|
|
|
for svc := range snap.MeshGateway.DiscoveryChain {
|
|
|
|
if _, ok := seenServices[svc]; !ok {
|
|
|
|
delete(snap.MeshGateway.DiscoveryChain, svc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-15 19:36:18 +00:00
|
|
|
case leafWatchID:
|
|
|
|
leaf, ok := u.Result.(*structs.IssuedCert)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasExports := len(snap.MeshGateway.ExportedServicesSlice) > 0; !hasExports {
|
|
|
|
return nil // ignore this update, it's stale
|
|
|
|
}
|
|
|
|
|
|
|
|
snap.MeshGateway.Leaf = leaf
|
|
|
|
|
|
|
|
case peeringTrustBundlesWatchID:
|
|
|
|
resp, ok := u.Result.(*pbpeering.TrustBundleListByServiceResponse)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
if len(resp.Bundles) > 0 {
|
|
|
|
snap.MeshGateway.PeeringTrustBundles = resp.Bundles
|
|
|
|
}
|
|
|
|
snap.MeshGateway.PeeringTrustBundlesSet = true
|
|
|
|
|
2022-10-06 13:54:14 +00:00
|
|
|
wildcardEntMeta := s.proxyID.WithWildcardNamespace()
|
|
|
|
|
|
|
|
// For each peer, fetch the imported services to support mesh gateway local
|
|
|
|
// mode.
|
|
|
|
for _, tb := range resp.Bundles {
|
|
|
|
entMeta := structs.DefaultEnterpriseMetaInDefaultPartition()
|
|
|
|
|
|
|
|
if _, ok := snap.MeshGateway.WatchedPeers[tb.PeerName]; !ok {
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
err := s.dataSources.ServiceList.Notify(ctx, &structs.DCSpecificRequest{
|
|
|
|
PeerName: tb.PeerName,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
Source: *s.source,
|
|
|
|
EnterpriseMeta: *wildcardEntMeta,
|
|
|
|
}, peeringServiceListWatchID+tb.PeerName, s.ch)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
meshLogger.Error("failed to register watch for mesh-gateway",
|
|
|
|
"peer", tb.PeerName,
|
|
|
|
"partition", entMeta.PartitionOrDefault(),
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
snap.MeshGateway.WatchedPeers[tb.PeerName] = cancel
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for peerName, cancelFn := range snap.MeshGateway.WatchedPeers {
|
|
|
|
found := false
|
|
|
|
for _, bundle := range resp.Bundles {
|
|
|
|
if peerName == bundle.PeerName {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
delete(snap.MeshGateway.PeeringServices, peerName)
|
|
|
|
delete(snap.MeshGateway.WatchedPeers, peerName)
|
|
|
|
delete(snap.MeshGateway.WatchedPeeringServices, peerName)
|
|
|
|
cancelFn()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-15 19:36:18 +00:00
|
|
|
case meshConfigEntryID:
|
|
|
|
resp, ok := u.Result.(*structs.ConfigEntryResponse)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
|
2022-09-26 16:50:17 +00:00
|
|
|
meshConf, ok := resp.Entry.(*structs.MeshConfigEntry)
|
|
|
|
if resp.Entry != nil && !ok {
|
|
|
|
return fmt.Errorf("invalid type for config entry: %T", resp.Entry)
|
|
|
|
}
|
|
|
|
snap.MeshGateway.MeshConfig = meshConf
|
|
|
|
snap.MeshGateway.MeshConfigSet = true
|
2022-09-23 01:24:13 +00:00
|
|
|
|
2022-10-07 14:54:37 +00:00
|
|
|
// If we're peering through mesh gateways it means the config entry may be deleted
|
|
|
|
// or the flag was disabled. Here we clean up related watches if they exist.
|
2022-09-26 16:50:17 +00:00
|
|
|
if !meshConf.PeerThroughMeshGateways() {
|
|
|
|
// We avoid canceling server watches when WAN federation is enabled since it
|
2022-10-07 14:54:37 +00:00
|
|
|
// always requires a watch to the local servers.
|
2022-09-23 01:24:13 +00:00
|
|
|
if s.meta[structs.MetaWANFederationKey] != "1" {
|
|
|
|
// If the entry was deleted we cancel watches that may have existed because of
|
|
|
|
// PeerThroughMeshGateways being set in the past.
|
2022-09-26 16:50:17 +00:00
|
|
|
snap.MeshGateway.WatchedLocalServers.CancelWatch(structs.ConsulServiceName)
|
|
|
|
}
|
|
|
|
if snap.MeshGateway.PeerServersWatchCancel != nil {
|
|
|
|
snap.MeshGateway.PeerServersWatchCancel()
|
|
|
|
snap.MeshGateway.PeerServersWatchCancel = nil
|
|
|
|
|
|
|
|
snap.MeshGateway.PeerServers = nil
|
2022-09-23 01:24:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-06-15 19:36:18 +00:00
|
|
|
}
|
2022-09-23 01:24:13 +00:00
|
|
|
|
2022-09-26 16:50:17 +00:00
|
|
|
// If PeerThroughMeshGateways is enabled, and we are in the default partition,
|
|
|
|
// we need to start watching the list of peering connections in all partitions
|
|
|
|
// to set up outbound routes for the control plane. Consul servers are in the default partition,
|
|
|
|
// so only mesh gateways here have his responsibility.
|
2022-10-07 14:54:37 +00:00
|
|
|
if snap.ProxyID.InDefaultPartition() &&
|
2022-09-26 16:50:17 +00:00
|
|
|
snap.MeshGateway.PeerServersWatchCancel == nil {
|
|
|
|
|
|
|
|
peeringListCtx, cancel := context.WithCancel(ctx)
|
|
|
|
err := s.dataSources.PeeringList.Notify(peeringListCtx, &cachetype.PeeringListRequest{
|
|
|
|
Request: &pbpeering.PeeringListRequest{
|
2022-10-13 22:54:14 +00:00
|
|
|
Partition: acl.WildcardPartitionName,
|
2022-09-26 16:50:17 +00:00
|
|
|
},
|
2022-11-07 15:00:11 +00:00
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
2022-09-26 16:50:17 +00:00
|
|
|
}, peerServersWatchID, s.ch)
|
|
|
|
if err != nil {
|
|
|
|
meshLogger.Error("failed to register watch for peering list", "error", err)
|
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
snap.MeshGateway.PeerServersWatchCancel = cancel
|
2022-09-23 01:24:13 +00:00
|
|
|
}
|
2022-09-26 16:50:17 +00:00
|
|
|
|
2022-10-07 14:54:37 +00:00
|
|
|
// We avoid initializing Consul server watches when WAN federation is enabled since it
|
2022-09-23 01:24:13 +00:00
|
|
|
// always requires server watches.
|
|
|
|
if s.meta[structs.MetaWANFederationKey] == "1" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-26 16:50:17 +00:00
|
|
|
if snap.MeshGateway.WatchedLocalServers.IsWatched(structs.ConsulServiceName) {
|
2022-09-23 01:24:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
notifyCtx, cancel := context.WithCancel(ctx)
|
|
|
|
err := s.dataSources.Health.Notify(notifyCtx, &structs.ServiceSpecificRequest{
|
|
|
|
Datacenter: s.source.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
ServiceName: structs.ConsulServiceName,
|
|
|
|
}, consulServerListWatchID, s.ch)
|
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
return fmt.Errorf("failed to watch local consul servers: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-09-26 16:50:17 +00:00
|
|
|
snap.MeshGateway.WatchedLocalServers.InitWatch(structs.ConsulServiceName, cancel)
|
|
|
|
|
|
|
|
case peerServersWatchID:
|
|
|
|
resp, ok := u.Result.(*pbpeering.PeeringListResponse)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
|
|
|
|
peerServers := make(map[string]PeerServersValue)
|
|
|
|
for _, peering := range resp.Peerings {
|
|
|
|
// We only need to keep track of outbound establish connections
|
|
|
|
// for mesh gateway.
|
|
|
|
if !peering.ShouldDial() || !peering.IsActive() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if existing, ok := peerServers[peering.PeerServerName]; ok && existing.Index >= peering.ModifyIndex {
|
|
|
|
// Multiple peerings can reference the same set of Consul servers, since there can be
|
|
|
|
// multiple partitions in a datacenter. Rather than randomly overwriting, we attempt to
|
|
|
|
// use the latest addresses by checking the Raft index associated with the peering.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
hostnames, ips := peerHostnamesAndIPs(meshLogger, peering.Name, peering.PeerServerAddresses)
|
|
|
|
if len(hostnames) > 0 {
|
|
|
|
peerServers[peering.PeerServerName] = PeerServersValue{
|
|
|
|
Addresses: hostnames,
|
|
|
|
Index: peering.ModifyIndex,
|
|
|
|
UseCDS: true,
|
|
|
|
}
|
|
|
|
} else if len(ips) > 0 {
|
|
|
|
peerServers[peering.PeerServerName] = PeerServersValue{
|
|
|
|
Addresses: ips,
|
|
|
|
Index: peering.ModifyIndex,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
snap.MeshGateway.PeerServers = peerServers
|
2022-09-23 01:24:13 +00:00
|
|
|
|
2020-12-24 19:11:13 +00:00
|
|
|
default:
|
|
|
|
switch {
|
2022-10-06 13:54:14 +00:00
|
|
|
case strings.HasPrefix(u.CorrelationID, peeringServiceListWatchID):
|
|
|
|
services, ok := u.Result.(*structs.IndexedServiceList)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
|
|
|
|
peerName := strings.TrimPrefix(u.CorrelationID, peeringServiceListWatchID)
|
|
|
|
|
|
|
|
svcMap := make(map[structs.ServiceName]struct{})
|
|
|
|
|
|
|
|
if _, ok := snap.MeshGateway.WatchedPeeringServices[peerName]; !ok {
|
|
|
|
snap.MeshGateway.WatchedPeeringServices[peerName] = make(map[structs.ServiceName]context.CancelFunc)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, svc := range services.Services {
|
|
|
|
// Make sure to add every service to this map, we use it to cancel
|
|
|
|
// watches below.
|
|
|
|
svcMap[svc] = struct{}{}
|
|
|
|
|
|
|
|
if _, ok := snap.MeshGateway.WatchedPeeringServices[peerName][svc]; !ok {
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
err := s.dataSources.Health.Notify(ctx, &structs.ServiceSpecificRequest{
|
|
|
|
PeerName: peerName,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: s.token},
|
|
|
|
ServiceName: svc.Name,
|
|
|
|
Connect: true,
|
|
|
|
EnterpriseMeta: svc.EnterpriseMeta,
|
|
|
|
}, fmt.Sprintf("peering-connect-service:%s:%s", peerName, svc.String()), s.ch)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
meshLogger.Error("failed to register watch for connect-service",
|
|
|
|
"service", svc.String(),
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
cancel()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
snap.MeshGateway.WatchedPeeringServices[peerName][svc] = cancel
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
watchedServices := snap.MeshGateway.WatchedPeeringServices[peerName]
|
|
|
|
for sn, cancelFn := range watchedServices {
|
|
|
|
if _, ok := svcMap[sn]; !ok {
|
|
|
|
meshLogger.Debug("canceling watch for service", "service", sn.String())
|
|
|
|
delete(snap.MeshGateway.WatchedPeeringServices[peerName], sn)
|
|
|
|
delete(snap.MeshGateway.PeeringServices[peerName], sn)
|
|
|
|
cancelFn()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 19:11:13 +00:00
|
|
|
case strings.HasPrefix(u.CorrelationID, "connect-service:"):
|
|
|
|
resp, ok := u.Result.(*structs.IndexedCheckServiceNodes)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
|
|
|
|
sn := structs.ServiceNameFromString(strings.TrimPrefix(u.CorrelationID, "connect-service:"))
|
|
|
|
|
|
|
|
if len(resp.Nodes) > 0 {
|
|
|
|
snap.MeshGateway.ServiceGroups[sn] = resp.Nodes
|
|
|
|
} else if _, ok := snap.MeshGateway.ServiceGroups[sn]; ok {
|
|
|
|
delete(snap.MeshGateway.ServiceGroups, sn)
|
|
|
|
}
|
2022-10-06 13:54:14 +00:00
|
|
|
case strings.HasPrefix(u.CorrelationID, "peering-connect-service:"):
|
|
|
|
resp, ok := u.Result.(*structs.IndexedCheckServiceNodes)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
|
|
|
|
key := strings.TrimPrefix(u.CorrelationID, "peering-connect-service:")
|
|
|
|
peer, snString, ok := strings.Cut(key, ":")
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
sn := structs.ServiceNameFromString(snString)
|
|
|
|
|
|
|
|
if len(resp.Nodes) > 0 {
|
|
|
|
if _, ok := snap.MeshGateway.PeeringServices[peer]; !ok {
|
|
|
|
snap.MeshGateway.PeeringServices[peer] = make(map[structs.ServiceName]PeeringServiceValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
if eps := hostnameEndpoints(s.logger, GatewayKey{}, resp.Nodes); len(eps) > 0 {
|
|
|
|
snap.MeshGateway.PeeringServices[peer][sn] = PeeringServiceValue{
|
|
|
|
Nodes: eps,
|
|
|
|
UseCDS: true,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
snap.MeshGateway.PeeringServices[peer][sn] = PeeringServiceValue{
|
|
|
|
Nodes: resp.Nodes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if _, ok := snap.MeshGateway.PeeringServices[peer]; ok {
|
|
|
|
delete(snap.MeshGateway.PeeringServices[peer], sn)
|
|
|
|
}
|
|
|
|
}
|
2021-10-23 19:19:51 +00:00
|
|
|
|
2020-12-24 19:11:13 +00:00
|
|
|
case strings.HasPrefix(u.CorrelationID, "mesh-gateway:"):
|
2022-09-01 14:46:30 +00:00
|
|
|
resp, ok := u.Result.(*structs.IndexedCheckServiceNodes)
|
2020-12-24 19:11:13 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
|
2021-10-23 19:19:51 +00:00
|
|
|
key := strings.TrimPrefix(u.CorrelationID, "mesh-gateway:")
|
|
|
|
delete(snap.MeshGateway.GatewayGroups, key)
|
|
|
|
delete(snap.MeshGateway.HostnameDatacenters, key)
|
2020-12-24 19:11:13 +00:00
|
|
|
|
|
|
|
if len(resp.Nodes) > 0 {
|
2021-10-23 19:19:51 +00:00
|
|
|
snap.MeshGateway.GatewayGroups[key] = resp.Nodes
|
|
|
|
snap.MeshGateway.HostnameDatacenters[key] = hostnameEndpoints(
|
2021-10-29 00:41:48 +00:00
|
|
|
s.logger.Named(logging.MeshGateway),
|
2021-10-29 00:47:42 +00:00
|
|
|
snap.Locality,
|
2021-10-29 00:41:48 +00:00
|
|
|
resp.Nodes,
|
|
|
|
)
|
2020-12-24 19:11:13 +00:00
|
|
|
}
|
2021-10-23 19:19:51 +00:00
|
|
|
|
2022-06-06 19:20:41 +00:00
|
|
|
case strings.HasPrefix(u.CorrelationID, "discovery-chain:"):
|
|
|
|
resp, ok := u.Result.(*structs.DiscoveryChainResponse)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
svcString := strings.TrimPrefix(u.CorrelationID, "discovery-chain:")
|
|
|
|
svc := structs.ServiceNameFromString(svcString)
|
|
|
|
|
|
|
|
if !snap.MeshGateway.IsServiceExported(svc) {
|
|
|
|
delete(snap.MeshGateway.DiscoveryChain, svc)
|
|
|
|
s.logger.Trace("discovery-chain watch fired for unknown service", "service", svc)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
snap.MeshGateway.DiscoveryChain[svc] = resp.Chain
|
|
|
|
|
2020-12-24 19:11:13 +00:00
|
|
|
default:
|
2021-10-23 19:19:51 +00:00
|
|
|
if err := s.handleEntUpdate(meshLogger, ctx, u, snap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-24 19:11:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-26 16:50:17 +00:00
|
|
|
|
|
|
|
func peerHostnamesAndIPs(logger hclog.Logger, peerName string, addresses []string) ([]structs.ServiceAddress, []structs.ServiceAddress) {
|
|
|
|
var (
|
|
|
|
hostnames []structs.ServiceAddress
|
|
|
|
ips []structs.ServiceAddress
|
|
|
|
)
|
|
|
|
|
|
|
|
// Sort the input so that the output is also sorted.
|
|
|
|
sort.Strings(addresses)
|
|
|
|
|
|
|
|
for _, addr := range addresses {
|
|
|
|
ip, rawPort, splitErr := net.SplitHostPort(addr)
|
|
|
|
port, convErr := strconv.Atoi(rawPort)
|
|
|
|
|
|
|
|
if splitErr != nil || convErr != nil {
|
|
|
|
logger.Warn("unable to parse ip and port from peer server address. skipping address.",
|
|
|
|
"peer", peerName, "address", addr)
|
|
|
|
}
|
|
|
|
if net.ParseIP(ip) != nil {
|
|
|
|
ips = append(ips, structs.ServiceAddress{
|
|
|
|
Address: ip,
|
|
|
|
Port: port,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
hostnames = append(hostnames, structs.ServiceAddress{
|
|
|
|
Address: ip,
|
|
|
|
Port: port,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(hostnames) > 0 && len(ips) > 0 {
|
|
|
|
logger.Warn("peer server address list contains mix of hostnames and IP addresses; only hostnames will be passed to Envoy",
|
|
|
|
"peer", peerName)
|
|
|
|
}
|
|
|
|
return hostnames, ips
|
|
|
|
}
|