2022-07-08 17:01:13 +00:00
|
|
|
package peerstream
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-05-26 19:24:09 +00:00
|
|
|
"errors"
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
"fmt"
|
2022-08-22 14:19:00 +00:00
|
|
|
"strconv"
|
2022-05-19 19:21:44 +00:00
|
|
|
"strings"
|
2022-09-21 15:55:19 +00:00
|
|
|
"time"
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
|
2022-05-19 19:21:44 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2022-09-21 15:55:19 +00:00
|
|
|
"github.com/hashicorp/go-memdb"
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
|
2022-10-24 16:48:02 +00:00
|
|
|
"github.com/hashicorp/consul/ipaddr"
|
|
|
|
"github.com/hashicorp/consul/lib/retry"
|
|
|
|
|
2022-05-19 19:21:44 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
"github.com/hashicorp/consul/agent/cache"
|
2022-05-25 17:37:44 +00:00
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
2022-08-22 14:19:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/autopilotevents"
|
2022-05-26 19:24:09 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
|
|
|
"github.com/hashicorp/consul/agent/consul/stream"
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
"github.com/hashicorp/consul/agent/submatview"
|
2022-05-19 19:21:44 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/proto/pbcommon"
|
2022-05-26 19:24:09 +00:00
|
|
|
"github.com/hashicorp/consul/proto/pbpeering"
|
2022-07-15 20:03:40 +00:00
|
|
|
"github.com/hashicorp/consul/proto/pbpeerstream"
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
"github.com/hashicorp/consul/proto/pbservice"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MaterializedViewStore interface {
|
|
|
|
Get(ctx context.Context, req submatview.Request) (submatview.Result, error)
|
|
|
|
Notify(ctx context.Context, req submatview.Request, cID string, ch chan<- cache.UpdateEvent) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type SubscriptionBackend interface {
|
|
|
|
Subscriber
|
|
|
|
}
|
|
|
|
|
|
|
|
// subscriptionManager handlers requests to subscribe to events from an events publisher.
|
|
|
|
type subscriptionManager struct {
|
2022-07-15 20:03:40 +00:00
|
|
|
logger hclog.Logger
|
|
|
|
config Config
|
|
|
|
trustDomain string
|
|
|
|
viewStore MaterializedViewStore
|
|
|
|
backend SubscriptionBackend
|
|
|
|
getStore func() StateStore
|
|
|
|
serviceSubReady <-chan struct{}
|
|
|
|
trustBundlesSubReady <-chan struct{}
|
2022-08-22 14:19:00 +00:00
|
|
|
serverAddrsSubReady <-chan struct{}
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(peering): Maybe centralize so that there is a single manager per datacenter, rather than per peering.
|
2022-05-25 17:37:44 +00:00
|
|
|
func newSubscriptionManager(
|
|
|
|
ctx context.Context,
|
|
|
|
logger hclog.Logger,
|
|
|
|
config Config,
|
|
|
|
trustDomain string,
|
|
|
|
backend SubscriptionBackend,
|
2022-07-08 17:01:13 +00:00
|
|
|
getStore func() StateStore,
|
2022-07-15 20:03:40 +00:00
|
|
|
remoteSubTracker *resourceSubscriptionTracker,
|
2022-05-25 17:37:44 +00:00
|
|
|
) *subscriptionManager {
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
logger = logger.Named("subscriptions")
|
|
|
|
store := submatview.NewStore(logger.Named("viewstore"))
|
|
|
|
go store.Run(ctx)
|
|
|
|
|
|
|
|
return &subscriptionManager{
|
2022-07-15 20:03:40 +00:00
|
|
|
logger: logger,
|
|
|
|
config: config,
|
|
|
|
trustDomain: trustDomain,
|
|
|
|
viewStore: store,
|
|
|
|
backend: backend,
|
|
|
|
getStore: getStore,
|
|
|
|
serviceSubReady: remoteSubTracker.SubscribedChan(pbpeerstream.TypeURLExportedService),
|
|
|
|
trustBundlesSubReady: remoteSubTracker.SubscribedChan(pbpeerstream.TypeURLPeeringTrustBundle),
|
2022-08-22 14:21:20 +00:00
|
|
|
serverAddrsSubReady: remoteSubTracker.SubscribedChan(pbpeerstream.TypeURLPeeringServerAddresses),
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// subscribe returns a channel that will contain updates to exported service instances for a given peer.
|
2022-05-25 17:37:44 +00:00
|
|
|
func (m *subscriptionManager) subscribe(ctx context.Context, peerID, peerName, partition string) <-chan cache.UpdateEvent {
|
2022-05-19 19:21:44 +00:00
|
|
|
var (
|
|
|
|
updateCh = make(chan cache.UpdateEvent, 1)
|
|
|
|
publicUpdateCh = make(chan cache.UpdateEvent, 1)
|
|
|
|
)
|
|
|
|
|
2022-05-25 17:37:44 +00:00
|
|
|
state := newSubscriptionState(peerName, partition)
|
2022-05-19 19:21:44 +00:00
|
|
|
state.publicUpdateCh = publicUpdateCh
|
|
|
|
state.updateCh = updateCh
|
|
|
|
|
|
|
|
// Wrap our bare state store queries in goroutines that emit events.
|
|
|
|
go m.notifyExportedServicesForPeerID(ctx, state, peerID)
|
2022-08-22 14:19:00 +00:00
|
|
|
go m.notifyServerAddrUpdates(ctx, state.updateCh)
|
2022-05-26 19:24:09 +00:00
|
|
|
if m.config.ConnectEnabled {
|
2022-06-09 16:05:18 +00:00
|
|
|
go m.notifyMeshGatewaysForPartition(ctx, state, state.partition)
|
|
|
|
// If connect is enabled, watch for updates to CA roots.
|
2022-06-10 21:15:22 +00:00
|
|
|
go m.notifyRootCAUpdatesForPartition(ctx, state.updateCh, state.partition)
|
2022-05-26 19:24:09 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 19:21:44 +00:00
|
|
|
// This goroutine is the only one allowed to manipulate protected
|
|
|
|
// subscriptionManager fields.
|
|
|
|
go m.handleEvents(ctx, state, updateCh)
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
|
2022-05-19 19:21:44 +00:00
|
|
|
return publicUpdateCh
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 19:21:44 +00:00
|
|
|
func (m *subscriptionManager) handleEvents(ctx context.Context, state *subscriptionState, updateCh <-chan cache.UpdateEvent) {
|
|
|
|
for {
|
|
|
|
// TODO(peering): exponential backoff
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case update := <-updateCh:
|
|
|
|
if err := m.handleEvent(ctx, state, update); err != nil {
|
|
|
|
m.logger.Error("Failed to handle update from watch",
|
|
|
|
"id", update.CorrelationID, "error", err,
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
}
|
2022-05-19 19:21:44 +00:00
|
|
|
}
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
|
2022-05-19 19:21:44 +00:00
|
|
|
func (m *subscriptionManager) handleEvent(ctx context.Context, state *subscriptionState, u cache.UpdateEvent) error {
|
|
|
|
if u.Err != nil {
|
|
|
|
return fmt.Errorf("received error event: %w", u.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case u.CorrelationID == subExportedServiceList:
|
|
|
|
// Everything starts with the exported service list coming from
|
|
|
|
// our state store watchset loop.
|
|
|
|
evt, ok := u.Result.(*structs.ExportedServiceList)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 19:21:44 +00:00
|
|
|
state.exportList = evt
|
|
|
|
|
|
|
|
pending := &pendingPayload{}
|
2022-09-29 19:37:19 +00:00
|
|
|
m.syncNormalServices(ctx, state, evt.Services)
|
2022-06-09 16:05:18 +00:00
|
|
|
if m.config.ConnectEnabled {
|
2022-10-24 16:48:02 +00:00
|
|
|
m.syncDiscoveryChains(state, pending, evt.ListAllDiscoveryChains())
|
2022-05-25 15:13:23 +00:00
|
|
|
}
|
2022-09-29 19:37:19 +00:00
|
|
|
|
|
|
|
err := pending.Add(
|
|
|
|
exportedServiceListID,
|
|
|
|
subExportedServiceList,
|
|
|
|
pbpeerstream.ExportedServiceListFromStruct(evt),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-19 19:21:44 +00:00
|
|
|
state.sendPendingEvents(ctx, m.logger, pending)
|
|
|
|
|
|
|
|
// cleanup event versions too
|
|
|
|
state.cleanupEventVersions(m.logger)
|
|
|
|
|
|
|
|
case strings.HasPrefix(u.CorrelationID, subExportedService):
|
|
|
|
csn, ok := u.Result.(*pbservice.IndexedCheckServiceNodes)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 19:21:44 +00:00
|
|
|
// TODO(peering): is it safe to edit these protobufs in place?
|
|
|
|
|
|
|
|
// Clear this raft index before exporting.
|
|
|
|
csn.Index = 0
|
|
|
|
|
2022-06-09 16:05:18 +00:00
|
|
|
// Ensure that connect things are scrubbed so we don't mix-and-match
|
|
|
|
// with the synthetic entries that point to mesh gateways.
|
|
|
|
filterConnectReferences(csn)
|
|
|
|
|
|
|
|
// Flatten health checks
|
|
|
|
for _, instance := range csn.Nodes {
|
|
|
|
instance.Checks = flattenChecks(
|
|
|
|
instance.Node.Node,
|
|
|
|
instance.Service.ID,
|
|
|
|
instance.Service.Service,
|
|
|
|
instance.Service.EnterpriseMeta,
|
|
|
|
instance.Checks,
|
|
|
|
)
|
2022-05-19 19:21:44 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 17:37:44 +00:00
|
|
|
// Scrub raft indexes
|
|
|
|
for _, instance := range csn.Nodes {
|
|
|
|
instance.Node.RaftIndex = nil
|
|
|
|
instance.Service.RaftIndex = nil
|
|
|
|
// skip checks since we just generated one from scratch
|
|
|
|
}
|
|
|
|
|
2022-05-19 19:21:44 +00:00
|
|
|
id := servicePayloadIDPrefix + strings.TrimPrefix(u.CorrelationID, subExportedService)
|
|
|
|
|
|
|
|
// Just ferry this one directly along to the destination.
|
|
|
|
pending := &pendingPayload{}
|
|
|
|
if err := pending.Add(id, u.CorrelationID, csn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
state.sendPendingEvents(ctx, m.logger, pending)
|
|
|
|
|
|
|
|
case strings.HasPrefix(u.CorrelationID, subMeshGateway):
|
|
|
|
csn, ok := u.Result.(*pbservice.IndexedCheckServiceNodes)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
|
|
|
|
partition := strings.TrimPrefix(u.CorrelationID, subMeshGateway)
|
|
|
|
|
2022-06-09 16:05:18 +00:00
|
|
|
if !m.config.ConnectEnabled {
|
2022-05-25 15:13:23 +00:00
|
|
|
return nil // ignore event
|
|
|
|
}
|
|
|
|
|
2022-05-19 19:21:44 +00:00
|
|
|
if !acl.EqualPartitions(partition, state.partition) {
|
|
|
|
return nil // ignore event
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear this raft index before exporting.
|
|
|
|
csn.Index = 0
|
|
|
|
|
2022-05-25 17:37:44 +00:00
|
|
|
// Flatten health checks
|
|
|
|
for _, instance := range csn.Nodes {
|
|
|
|
instance.Checks = flattenChecks(
|
|
|
|
instance.Node.Node,
|
|
|
|
instance.Service.ID,
|
|
|
|
instance.Service.Service,
|
|
|
|
instance.Service.EnterpriseMeta,
|
|
|
|
instance.Checks,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scrub raft indexes
|
|
|
|
for _, instance := range csn.Nodes {
|
|
|
|
instance.Node.RaftIndex = nil
|
|
|
|
instance.Service.RaftIndex = nil
|
|
|
|
// skip checks since we just generated one from scratch
|
|
|
|
|
|
|
|
// Remove connect things like native mode.
|
|
|
|
if instance.Service.Connect != nil || instance.Service.Proxy != nil {
|
|
|
|
instance.Service.Connect = nil
|
|
|
|
instance.Service.Proxy = nil
|
2022-07-13 16:12:06 +00:00
|
|
|
|
|
|
|
// VirtualIPs assigned in this cluster won't make sense on the importing side
|
|
|
|
delete(instance.Service.TaggedAddresses, structs.TaggedAddressVirtualIP)
|
2022-05-25 17:37:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-19 19:21:44 +00:00
|
|
|
state.meshGateway = csn
|
|
|
|
|
|
|
|
pending := &pendingPayload{}
|
|
|
|
|
|
|
|
if state.exportList != nil {
|
|
|
|
// Trigger public events for all synthetic discovery chain replies.
|
2022-06-27 19:37:18 +00:00
|
|
|
for chainName, info := range state.connectServices {
|
2022-10-24 16:48:02 +00:00
|
|
|
m.collectPendingEventForDiscoveryChain(state, pending, chainName, info)
|
2022-05-19 19:21:44 +00:00
|
|
|
}
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
}
|
2022-05-19 19:21:44 +00:00
|
|
|
|
|
|
|
// TODO(peering): should we ship this down verbatim to the consumer?
|
|
|
|
state.sendPendingEvents(ctx, m.logger, pending)
|
|
|
|
|
2022-05-26 19:24:09 +00:00
|
|
|
case u.CorrelationID == subCARoot:
|
|
|
|
roots, ok := u.Result.(*pbpeering.PeeringTrustBundle)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
pending := &pendingPayload{}
|
|
|
|
if err := pending.Add(caRootsPayloadID, u.CorrelationID, roots); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
state.sendPendingEvents(ctx, m.logger, pending)
|
|
|
|
|
2022-08-22 14:19:00 +00:00
|
|
|
case u.CorrelationID == subServerAddrs:
|
|
|
|
addrs, ok := u.Result.(*pbpeering.PeeringServerAddresses)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type for response: %T", u.Result)
|
|
|
|
}
|
|
|
|
pending := &pendingPayload{}
|
|
|
|
if err := pending.Add(serverAddrsPayloadID, u.CorrelationID, addrs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
state.sendPendingEvents(ctx, m.logger, pending)
|
2022-05-19 19:21:44 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown correlation ID: %s", u.CorrelationID)
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
}
|
2022-05-19 19:21:44 +00:00
|
|
|
return nil
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 19:21:44 +00:00
|
|
|
func filterConnectReferences(orig *pbservice.IndexedCheckServiceNodes) {
|
|
|
|
newNodes := make([]*pbservice.CheckServiceNode, 0, len(orig.Nodes))
|
|
|
|
for i := range orig.Nodes {
|
|
|
|
csn := orig.Nodes[i]
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
|
2022-05-19 19:21:44 +00:00
|
|
|
if csn.Service.Kind != string(structs.ServiceKindTypical) {
|
|
|
|
continue // skip non-typical services
|
|
|
|
}
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
|
2022-05-19 19:21:44 +00:00
|
|
|
if strings.HasSuffix(csn.Service.Service, syntheticProxyNameSuffix) {
|
|
|
|
// Skip things that might LOOK like a proxy so we don't get a
|
|
|
|
// collision with the ones we generate.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove connect things like native mode.
|
|
|
|
if csn.Service.Connect != nil || csn.Service.Proxy != nil {
|
|
|
|
csn = proto.Clone(csn).(*pbservice.CheckServiceNode)
|
|
|
|
csn.Service.Connect = nil
|
|
|
|
csn.Service.Proxy = nil
|
2022-07-13 16:12:06 +00:00
|
|
|
|
|
|
|
// VirtualIPs assigned in this cluster won't make sense on the importing side
|
|
|
|
delete(csn.Service.TaggedAddresses, structs.TaggedAddressVirtualIP)
|
2022-05-19 19:21:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newNodes = append(newNodes, csn)
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
}
|
2022-05-19 19:21:44 +00:00
|
|
|
orig.Nodes = newNodes
|
|
|
|
}
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
|
2022-06-10 21:15:22 +00:00
|
|
|
func (m *subscriptionManager) notifyRootCAUpdatesForPartition(
|
|
|
|
ctx context.Context,
|
|
|
|
updateCh chan<- cache.UpdateEvent,
|
|
|
|
partition string,
|
|
|
|
) {
|
2022-07-15 20:03:40 +00:00
|
|
|
// Wait until this is subscribed-to.
|
|
|
|
select {
|
|
|
|
case <-m.trustBundlesSubReady:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-26 19:24:09 +00:00
|
|
|
var idx uint64
|
|
|
|
// TODO(peering): retry logic; fail past a threshold
|
|
|
|
for {
|
|
|
|
var err error
|
|
|
|
// Typically, this function will block inside `m.subscribeCARoots` and only return on error.
|
|
|
|
// Errors are logged and the watch is retried.
|
2022-06-10 21:15:22 +00:00
|
|
|
idx, err = m.subscribeCARoots(ctx, idx, updateCh, partition)
|
2022-05-26 19:24:09 +00:00
|
|
|
if errors.Is(err, stream.ErrSubForceClosed) {
|
|
|
|
m.logger.Trace("subscription force-closed due to an ACL change or snapshot restore, will attempt resume")
|
|
|
|
} else if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
|
|
|
|
m.logger.Warn("failed to subscribe to CA roots, will attempt resume", "error", err.Error())
|
|
|
|
} else {
|
|
|
|
m.logger.Trace(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 14:19:00 +00:00
|
|
|
const subCARoot = "roots"
|
|
|
|
|
2022-05-26 19:24:09 +00:00
|
|
|
// subscribeCARoots subscribes to state.EventTopicCARoots for changes to CA roots.
|
|
|
|
// Upon receiving an event it will send the payload in updateCh.
|
2022-06-10 21:15:22 +00:00
|
|
|
func (m *subscriptionManager) subscribeCARoots(
|
|
|
|
ctx context.Context,
|
|
|
|
idx uint64,
|
|
|
|
updateCh chan<- cache.UpdateEvent,
|
|
|
|
partition string,
|
|
|
|
) (uint64, error) {
|
2022-05-26 19:24:09 +00:00
|
|
|
// following code adapted from connectca/watch_roots.go
|
|
|
|
sub, err := m.backend.Subscribe(&stream.SubscribeRequest{
|
|
|
|
Topic: state.EventTopicCARoots,
|
|
|
|
Subject: stream.SubjectNone,
|
|
|
|
Token: "", // using anonymous token for now
|
|
|
|
Index: idx,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("failed to subscribe to CA Roots events: %w", err)
|
|
|
|
}
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
for {
|
|
|
|
event, err := sub.Next(ctx)
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, stream.ErrSubForceClosed):
|
|
|
|
// If the subscription was closed because the state store was abandoned (e.g.
|
|
|
|
// following a snapshot restore) reset idx to ensure we don't skip over the
|
|
|
|
// new store's events.
|
|
|
|
select {
|
2022-07-08 17:01:13 +00:00
|
|
|
case <-m.getStore().AbandonCh():
|
2022-05-26 19:24:09 +00:00
|
|
|
idx = 0
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return idx, err
|
|
|
|
case errors.Is(err, context.Canceled):
|
|
|
|
return 0, err
|
|
|
|
case errors.Is(err, context.DeadlineExceeded):
|
|
|
|
return 0, err
|
|
|
|
case err != nil:
|
|
|
|
return idx, fmt.Errorf("failed to read next event: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: this check isn't strictly necessary because the event publishing
|
|
|
|
// machinery will ensure the index increases monotonically, but it can be
|
|
|
|
// tricky to faithfully reproduce this in tests (e.g. the EventPublisher
|
|
|
|
// garbage collects topic buffers and snapshots aggressively when streams
|
|
|
|
// disconnect) so this avoids a bunch of confusing setup code.
|
|
|
|
if event.Index <= idx {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
idx = event.Index
|
|
|
|
|
|
|
|
// We do not send framing events (e.g. EndOfSnapshot, NewSnapshotToFollow)
|
|
|
|
// because we send a full list of roots on every event, rather than expecting
|
|
|
|
// clients to maintain a state-machine in the way they do for service health.
|
|
|
|
if event.IsFramingEvent() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
payload, ok := event.Payload.(state.EventPayloadCARoots)
|
|
|
|
if !ok {
|
|
|
|
return 0, fmt.Errorf("unexpected event payload type: %T", payload)
|
|
|
|
}
|
|
|
|
|
|
|
|
var rootPems []string
|
|
|
|
for _, root := range payload.CARoots {
|
|
|
|
rootPems = append(rootPems, root.RootCert)
|
|
|
|
}
|
|
|
|
|
|
|
|
updateCh <- cache.UpdateEvent{
|
|
|
|
CorrelationID: subCARoot,
|
|
|
|
Result: &pbpeering.PeeringTrustBundle{
|
2022-06-10 21:15:22 +00:00
|
|
|
TrustDomain: m.trustDomain,
|
|
|
|
RootPEMs: rootPems,
|
|
|
|
ExportedPartition: partition,
|
|
|
|
// TODO(peering): revisit decision not to validate datacenter in RBAC
|
2022-05-26 19:24:09 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-19 19:21:44 +00:00
|
|
|
func (m *subscriptionManager) syncNormalServices(
|
|
|
|
ctx context.Context,
|
|
|
|
state *subscriptionState,
|
|
|
|
services []structs.ServiceName,
|
|
|
|
) {
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
// seen contains the set of exported service names and is used to reconcile the list of watched services.
|
|
|
|
seen := make(map[structs.ServiceName]struct{})
|
|
|
|
|
|
|
|
// Ensure there is a subscription for each service exported to the peer.
|
|
|
|
for _, svc := range services {
|
|
|
|
seen[svc] = struct{}{}
|
|
|
|
|
2022-05-19 19:21:44 +00:00
|
|
|
if _, ok := state.watchedServices[svc]; ok {
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
// Exported service is already being watched, nothing to do.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
notifyCtx, cancel := context.WithCancel(ctx)
|
2022-05-19 19:21:44 +00:00
|
|
|
if err := m.NotifyStandardService(notifyCtx, svc, state.updateCh); err != nil {
|
|
|
|
cancel()
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
m.logger.Error("failed to subscribe to service", "service", svc.String())
|
|
|
|
continue
|
|
|
|
}
|
2022-05-19 19:21:44 +00:00
|
|
|
|
|
|
|
state.watchedServices[svc] = cancel
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// For every subscription without an exported service, call the associated cancel fn.
|
2022-05-19 19:21:44 +00:00
|
|
|
for svc, cancel := range state.watchedServices {
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
if _, ok := seen[svc]; !ok {
|
|
|
|
cancel()
|
2022-05-19 19:21:44 +00:00
|
|
|
delete(state.watchedServices, svc)
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-19 19:21:44 +00:00
|
|
|
}
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
|
2022-05-19 19:21:44 +00:00
|
|
|
func (m *subscriptionManager) syncDiscoveryChains(
|
|
|
|
state *subscriptionState,
|
|
|
|
pending *pendingPayload,
|
2022-06-27 19:37:18 +00:00
|
|
|
chainsByName map[structs.ServiceName]structs.ExportedDiscoveryChainInfo,
|
2022-05-19 19:21:44 +00:00
|
|
|
) {
|
|
|
|
// if it was newly added, then try to emit an UPDATE event
|
2022-06-27 19:37:18 +00:00
|
|
|
for chainName, info := range chainsByName {
|
|
|
|
if oldInfo, ok := state.connectServices[chainName]; ok && info.Equal(oldInfo) {
|
2022-05-19 19:21:44 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-06-27 19:37:18 +00:00
|
|
|
state.connectServices[chainName] = info
|
2022-05-19 19:21:44 +00:00
|
|
|
|
2022-10-24 16:48:02 +00:00
|
|
|
m.collectPendingEventForDiscoveryChain(state, pending, chainName, info)
|
2022-05-19 19:21:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if it was dropped, try to emit an DELETE event
|
|
|
|
for chainName := range state.connectServices {
|
|
|
|
if _, ok := chainsByName[chainName]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(state.connectServices, chainName)
|
|
|
|
|
|
|
|
if state.meshGateway != nil {
|
|
|
|
// Only need to clean this up if we know we may have ever sent it in the first place.
|
|
|
|
proxyName := generateProxyNameForDiscoveryChain(chainName)
|
|
|
|
err := pending.Add(
|
|
|
|
discoveryChainPayloadIDPrefix+chainName.String(),
|
|
|
|
subExportedService+proxyName.String(),
|
|
|
|
&pbservice.IndexedCheckServiceNodes{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Error("failed to send event for discovery chain", "service", chainName.String(), "error", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 15:55:19 +00:00
|
|
|
func (m *subscriptionManager) collectPendingEventForDiscoveryChain(
|
2022-05-19 19:21:44 +00:00
|
|
|
state *subscriptionState,
|
|
|
|
pending *pendingPayload,
|
|
|
|
chainName structs.ServiceName,
|
2022-06-27 19:37:18 +00:00
|
|
|
info structs.ExportedDiscoveryChainInfo,
|
2022-05-19 19:21:44 +00:00
|
|
|
) {
|
|
|
|
if _, ok := state.connectServices[chainName]; !ok {
|
|
|
|
return // not found
|
|
|
|
}
|
|
|
|
|
|
|
|
if state.exportList == nil || state.meshGateway == nil {
|
|
|
|
return // skip because we don't have the data to do it yet
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit event with fake data
|
|
|
|
proxyName := generateProxyNameForDiscoveryChain(chainName)
|
|
|
|
|
|
|
|
err := pending.Add(
|
|
|
|
discoveryChainPayloadIDPrefix+chainName.String(),
|
|
|
|
subExportedService+proxyName.String(),
|
|
|
|
createDiscoChainHealth(
|
2022-05-25 17:37:44 +00:00
|
|
|
state.peerName,
|
|
|
|
m.config.Datacenter,
|
|
|
|
m.trustDomain,
|
2022-05-19 19:21:44 +00:00
|
|
|
chainName,
|
2022-06-27 19:37:18 +00:00
|
|
|
info,
|
2022-05-19 19:21:44 +00:00
|
|
|
state.meshGateway,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Error("failed to send event for discovery chain", "service", chainName.String(), "error", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-25 17:37:44 +00:00
|
|
|
func createDiscoChainHealth(
|
|
|
|
peerName string,
|
|
|
|
datacenter, trustDomain string,
|
|
|
|
sn structs.ServiceName,
|
2022-06-27 19:37:18 +00:00
|
|
|
info structs.ExportedDiscoveryChainInfo,
|
2022-05-25 17:37:44 +00:00
|
|
|
pb *pbservice.IndexedCheckServiceNodes,
|
|
|
|
) *pbservice.IndexedCheckServiceNodes {
|
2022-05-19 19:21:44 +00:00
|
|
|
fakeProxyName := sn.Name + syntheticProxyNameSuffix
|
|
|
|
|
2022-05-25 17:37:44 +00:00
|
|
|
var peerMeta *pbservice.PeeringServiceMeta
|
|
|
|
{
|
|
|
|
spiffeID := connect.SpiffeIDService{
|
|
|
|
Host: trustDomain,
|
|
|
|
Partition: sn.PartitionOrDefault(),
|
|
|
|
Namespace: sn.NamespaceOrDefault(),
|
|
|
|
Datacenter: datacenter,
|
|
|
|
Service: sn.Name,
|
|
|
|
}
|
2022-06-27 19:37:18 +00:00
|
|
|
mainSpiffeIDString := spiffeID.URI().String()
|
2022-05-25 17:37:44 +00:00
|
|
|
|
|
|
|
sni := connect.PeeredServiceSNI(
|
|
|
|
sn.Name,
|
|
|
|
sn.NamespaceOrDefault(),
|
|
|
|
sn.PartitionOrDefault(),
|
|
|
|
peerName,
|
|
|
|
trustDomain,
|
|
|
|
)
|
|
|
|
|
2022-07-12 16:38:13 +00:00
|
|
|
gwSpiffeID := connect.SpiffeIDMeshGateway{
|
|
|
|
Host: trustDomain,
|
|
|
|
Partition: sn.PartitionOrDefault(),
|
|
|
|
Datacenter: datacenter,
|
|
|
|
}
|
|
|
|
|
2022-05-25 17:37:44 +00:00
|
|
|
// Create common peer meta.
|
|
|
|
//
|
|
|
|
// TODO(peering): should this be replicated by service and not by instance?
|
|
|
|
peerMeta = &pbservice.PeeringServiceMeta{
|
2022-06-27 19:37:18 +00:00
|
|
|
SNI: []string{sni},
|
|
|
|
SpiffeID: []string{
|
|
|
|
mainSpiffeIDString,
|
2022-07-12 16:38:13 +00:00
|
|
|
// Always include the gateway id here to facilitate error-free
|
|
|
|
// L4/L7 upgrade/downgrade scenarios.
|
|
|
|
gwSpiffeID.URI().String(),
|
2022-06-27 19:37:18 +00:00
|
|
|
},
|
|
|
|
Protocol: info.Protocol,
|
|
|
|
}
|
|
|
|
|
2022-07-12 16:38:13 +00:00
|
|
|
if !structs.IsProtocolHTTPLike(info.Protocol) {
|
2022-06-27 19:37:18 +00:00
|
|
|
for _, target := range info.TCPTargets {
|
|
|
|
targetSpiffeID := connect.SpiffeIDService{
|
|
|
|
Host: trustDomain,
|
|
|
|
Partition: target.Partition,
|
|
|
|
Namespace: target.Namespace,
|
|
|
|
Datacenter: target.Datacenter,
|
|
|
|
Service: target.Service,
|
|
|
|
}
|
|
|
|
targetSpiffeIDString := targetSpiffeID.URI().String()
|
|
|
|
if targetSpiffeIDString != mainSpiffeIDString {
|
|
|
|
peerMeta.SpiffeID = append(peerMeta.SpiffeID, targetSpiffeIDString)
|
|
|
|
}
|
|
|
|
}
|
2022-05-25 17:37:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-19 19:21:44 +00:00
|
|
|
newNodes := make([]*pbservice.CheckServiceNode, 0, len(pb.Nodes))
|
|
|
|
for i := range pb.Nodes {
|
|
|
|
gwNode := pb.Nodes[i].Node
|
|
|
|
gwService := pb.Nodes[i].Service
|
|
|
|
gwChecks := pb.Nodes[i].Checks
|
|
|
|
|
|
|
|
pbEntMeta := pbcommon.NewEnterpriseMetaFromStructs(sn.EnterpriseMeta)
|
|
|
|
|
|
|
|
fakeProxyID := fakeProxyName
|
2022-05-25 17:37:44 +00:00
|
|
|
destServiceID := sn.Name
|
2022-05-19 19:21:44 +00:00
|
|
|
if gwService.ID != "" {
|
|
|
|
// This is only going to be relevant if multiple mesh gateways are
|
|
|
|
// on the same exporting node.
|
|
|
|
fakeProxyID = fmt.Sprintf("%s-instance-%d", fakeProxyName, i)
|
2022-05-25 17:37:44 +00:00
|
|
|
destServiceID = fmt.Sprintf("%s-instance-%d", sn.Name, i)
|
2022-05-19 19:21:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
csn := &pbservice.CheckServiceNode{
|
|
|
|
Node: gwNode,
|
|
|
|
Service: &pbservice.NodeService{
|
|
|
|
Kind: string(structs.ServiceKindConnectProxy),
|
|
|
|
Service: fakeProxyName,
|
|
|
|
ID: fakeProxyID,
|
|
|
|
EnterpriseMeta: pbEntMeta,
|
|
|
|
PeerName: structs.DefaultPeerKeyword,
|
|
|
|
Proxy: &pbservice.ConnectProxyConfig{
|
|
|
|
DestinationServiceName: sn.Name,
|
2022-05-25 17:37:44 +00:00
|
|
|
DestinationServiceID: destServiceID,
|
2022-05-19 19:21:44 +00:00
|
|
|
},
|
|
|
|
// direct
|
|
|
|
Address: gwService.Address,
|
|
|
|
TaggedAddresses: gwService.TaggedAddresses,
|
|
|
|
Port: gwService.Port,
|
|
|
|
SocketPath: gwService.SocketPath,
|
|
|
|
Weights: gwService.Weights,
|
2022-05-25 17:37:44 +00:00
|
|
|
Connect: &pbservice.ServiceConnect{
|
|
|
|
PeerMeta: peerMeta,
|
|
|
|
},
|
2022-05-19 19:21:44 +00:00
|
|
|
},
|
|
|
|
Checks: flattenChecks(gwNode.Node, fakeProxyID, fakeProxyName, pbEntMeta, gwChecks),
|
|
|
|
}
|
|
|
|
newNodes = append(newNodes, csn)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pbservice.IndexedCheckServiceNodes{
|
|
|
|
Index: 0,
|
|
|
|
Nodes: newNodes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func flattenChecks(
|
|
|
|
nodeName string,
|
|
|
|
serviceID string,
|
|
|
|
serviceName string,
|
|
|
|
entMeta *pbcommon.EnterpriseMeta,
|
|
|
|
checks []*pbservice.HealthCheck,
|
|
|
|
) []*pbservice.HealthCheck {
|
|
|
|
if len(checks) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
healthStatus := api.HealthPassing
|
|
|
|
for _, chk := range checks {
|
|
|
|
if chk.Status != api.HealthPassing {
|
|
|
|
healthStatus = chk.Status
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if serviceID == "" {
|
|
|
|
serviceID = serviceName
|
|
|
|
}
|
|
|
|
|
|
|
|
return []*pbservice.HealthCheck{
|
|
|
|
{
|
|
|
|
CheckID: serviceID + ":overall-check",
|
|
|
|
Name: "overall-check",
|
|
|
|
Status: healthStatus,
|
|
|
|
Node: nodeName,
|
|
|
|
ServiceID: serviceID,
|
|
|
|
ServiceName: serviceName,
|
|
|
|
EnterpriseMeta: entMeta,
|
|
|
|
PeerName: structs.DefaultPeerKeyword,
|
|
|
|
},
|
|
|
|
}
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2022-06-09 16:05:18 +00:00
|
|
|
subExportedServiceList = "exported-service-list"
|
|
|
|
subExportedService = "exported-service:"
|
|
|
|
subMeshGateway = "mesh-gateway:"
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
)
|
|
|
|
|
2022-05-19 19:21:44 +00:00
|
|
|
// NotifyStandardService will notify the given channel when there are updates
|
|
|
|
// to the requested service of the same name in the catalog.
|
|
|
|
func (m *subscriptionManager) NotifyStandardService(
|
|
|
|
ctx context.Context,
|
|
|
|
svc structs.ServiceName,
|
|
|
|
updateCh chan<- cache.UpdateEvent,
|
|
|
|
) error {
|
|
|
|
sr := newExportedStandardServiceRequest(m.logger, svc, m.backend)
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
return m.viewStore.Notify(ctx, sr, subExportedService+svc.String(), updateCh)
|
|
|
|
}
|
2022-05-19 19:21:44 +00:00
|
|
|
|
|
|
|
// syntheticProxyNameSuffix is the suffix to add to synthetic proxies we
|
|
|
|
// replicate to route traffic to an exported discovery chain through the mesh
|
|
|
|
// gateways.
|
|
|
|
//
|
|
|
|
// This name was chosen to match existing "sidecar service" generation logic
|
|
|
|
// and similar logic in the Service Identity synthetic ACL policies.
|
|
|
|
const syntheticProxyNameSuffix = "-sidecar-proxy"
|
|
|
|
|
|
|
|
func generateProxyNameForDiscoveryChain(sn structs.ServiceName) structs.ServiceName {
|
|
|
|
return structs.NewServiceName(sn.Name+syntheticProxyNameSuffix, &sn.EnterpriseMeta)
|
|
|
|
}
|
2022-08-22 14:19:00 +00:00
|
|
|
|
|
|
|
const subServerAddrs = "server-addrs"
|
|
|
|
|
|
|
|
func (m *subscriptionManager) notifyServerAddrUpdates(
|
|
|
|
ctx context.Context,
|
|
|
|
updateCh chan<- cache.UpdateEvent,
|
|
|
|
) {
|
2022-09-21 15:55:19 +00:00
|
|
|
// Wait until server address updates are subscribed-to.
|
2022-08-22 14:19:00 +00:00
|
|
|
select {
|
|
|
|
case <-m.serverAddrsSubReady:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 15:55:19 +00:00
|
|
|
configNotifyCh := m.notifyMeshConfigUpdates(ctx)
|
|
|
|
|
|
|
|
// Intentionally initialized to empty values.
|
|
|
|
// These are set after the first mesh config entry update arrives.
|
|
|
|
var queryCtx context.Context
|
|
|
|
cancel := func() {}
|
|
|
|
|
|
|
|
useGateways := false
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
cancel()
|
|
|
|
return
|
|
|
|
|
|
|
|
case event := <-configNotifyCh:
|
|
|
|
entry, ok := event.Result.(*structs.MeshConfigEntry)
|
|
|
|
if event.Result != nil && !ok {
|
|
|
|
m.logger.Error(fmt.Sprintf("saw unexpected type %T for mesh config entry: falling back to pushing direct server addresses", event.Result))
|
|
|
|
}
|
|
|
|
if entry != nil && entry.Peering != nil && entry.Peering.PeerThroughMeshGateways {
|
|
|
|
useGateways = true
|
|
|
|
} else {
|
|
|
|
useGateways = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel and re-set watches based on the updated config entry.
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
queryCtx, cancel = context.WithCancel(ctx)
|
|
|
|
|
|
|
|
if useGateways {
|
|
|
|
go m.notifyServerMeshGatewayAddresses(queryCtx, updateCh)
|
|
|
|
} else {
|
|
|
|
go m.ensureServerAddrSubscription(queryCtx, updateCh)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *subscriptionManager) notifyMeshConfigUpdates(ctx context.Context) <-chan cache.UpdateEvent {
|
|
|
|
const meshConfigWatch = "mesh-config-entry"
|
|
|
|
|
|
|
|
notifyCh := make(chan cache.UpdateEvent, 1)
|
2022-10-24 16:48:02 +00:00
|
|
|
go m.syncViaBlockingQuery(ctx, meshConfigWatch, func(_ context.Context, store StateStore, ws memdb.WatchSet) (interface{}, error) {
|
2022-09-21 15:55:19 +00:00
|
|
|
_, rawEntry, err := store.ConfigEntry(ws, structs.MeshConfig, structs.MeshConfigMesh, acl.DefaultEnterpriseMeta())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get mesh config entry: %w", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
return rawEntry, nil
|
|
|
|
}, meshConfigWatch, notifyCh)
|
|
|
|
|
|
|
|
return notifyCh
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *subscriptionManager) notifyServerMeshGatewayAddresses(ctx context.Context, updateCh chan<- cache.UpdateEvent) {
|
|
|
|
m.syncViaBlockingQuery(ctx, "mesh-gateways", func(ctx context.Context, store StateStore, ws memdb.WatchSet) (interface{}, error) {
|
|
|
|
_, nodes, err := store.ServiceDump(ws, structs.ServiceKindMeshGateway, true, acl.DefaultEnterpriseMeta(), structs.DefaultPeerKeyword)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to watch mesh gateways services for servers: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var gatewayAddrs []string
|
|
|
|
for _, csn := range nodes {
|
|
|
|
_, addr, port := csn.BestAddress(true)
|
|
|
|
gatewayAddrs = append(gatewayAddrs, ipaddr.FormatAddressPort(addr, port))
|
|
|
|
}
|
|
|
|
if len(gatewayAddrs) == 0 {
|
|
|
|
return nil, errors.New("configured to peer through mesh gateways but no mesh gateways are registered")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We may return an empty list if there are no gateway addresses.
|
|
|
|
return &pbpeering.PeeringServerAddresses{
|
|
|
|
Addresses: gatewayAddrs,
|
|
|
|
}, nil
|
|
|
|
}, subServerAddrs, updateCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *subscriptionManager) ensureServerAddrSubscription(ctx context.Context, updateCh chan<- cache.UpdateEvent) {
|
|
|
|
waiter := &retry.Waiter{
|
|
|
|
MinFailures: 1,
|
|
|
|
Factor: 500 * time.Millisecond,
|
|
|
|
MaxWait: 60 * time.Second,
|
|
|
|
Jitter: retry.NewJitter(100),
|
|
|
|
}
|
|
|
|
|
|
|
|
logger := m.logger.With("queryType", "server-addresses")
|
|
|
|
|
2022-08-22 14:19:00 +00:00
|
|
|
var idx uint64
|
|
|
|
for {
|
|
|
|
var err error
|
2022-09-21 15:55:19 +00:00
|
|
|
|
2022-08-22 14:19:00 +00:00
|
|
|
idx, err = m.subscribeServerAddrs(ctx, idx, updateCh)
|
2022-10-12 18:38:09 +00:00
|
|
|
if err == nil {
|
|
|
|
waiter.Reset()
|
|
|
|
} else if errors.Is(err, stream.ErrSubForceClosed) {
|
2022-09-21 15:55:19 +00:00
|
|
|
logger.Trace("subscription force-closed due to an ACL change or snapshot restore, will attempt resume")
|
|
|
|
|
2022-08-22 14:19:00 +00:00
|
|
|
} else if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
|
2022-09-21 15:55:19 +00:00
|
|
|
logger.Warn("failed to subscribe to server addresses, will attempt resume", "error", err.Error())
|
2022-08-22 14:19:00 +00:00
|
|
|
|
2022-09-21 15:55:19 +00:00
|
|
|
} else if err != nil {
|
|
|
|
logger.Trace(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := waiter.Wait(ctx); err != nil {
|
2022-08-22 14:19:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *subscriptionManager) subscribeServerAddrs(
|
|
|
|
ctx context.Context,
|
|
|
|
idx uint64,
|
|
|
|
updateCh chan<- cache.UpdateEvent,
|
|
|
|
) (uint64, error) {
|
|
|
|
// following code adapted from serverdiscovery/watch_servers.go
|
|
|
|
sub, err := m.backend.Subscribe(&stream.SubscribeRequest{
|
|
|
|
Topic: autopilotevents.EventTopicReadyServers,
|
|
|
|
Subject: stream.SubjectNone,
|
|
|
|
Token: "", // using anonymous token for now
|
|
|
|
Index: idx,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("failed to subscribe to ReadyServers events: %w", err)
|
|
|
|
}
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
for {
|
|
|
|
event, err := sub.Next(ctx)
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, context.Canceled):
|
|
|
|
return 0, err
|
|
|
|
case err != nil:
|
|
|
|
return idx, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We do not send framing events (e.g. EndOfSnapshot, NewSnapshotToFollow)
|
|
|
|
// because we send a full list of ready servers on every event, rather than expecting
|
|
|
|
// clients to maintain a state-machine in the way they do for service health.
|
|
|
|
if event.IsFramingEvent() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: this check isn't strictly necessary because the event publishing
|
|
|
|
// machinery will ensure the index increases monotonically, but it can be
|
|
|
|
// tricky to faithfully reproduce this in tests (e.g. the EventPublisher
|
|
|
|
// garbage collects topic buffers and snapshots aggressively when streams
|
|
|
|
// disconnect) so this avoids a bunch of confusing setup code.
|
|
|
|
if event.Index <= idx {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
idx = event.Index
|
|
|
|
|
|
|
|
payload, ok := event.Payload.(autopilotevents.EventPayloadReadyServers)
|
|
|
|
if !ok {
|
|
|
|
return 0, fmt.Errorf("unexpected event payload type: %T", payload)
|
|
|
|
}
|
|
|
|
|
|
|
|
var serverAddrs = make([]string, 0, len(payload))
|
|
|
|
|
|
|
|
for _, srv := range payload {
|
|
|
|
if srv.ExtGRPCPort == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2022-10-24 19:44:57 +00:00
|
|
|
addr := srv.Address
|
|
|
|
|
|
|
|
// wan address is preferred
|
|
|
|
if v, ok := srv.TaggedAddresses[structs.TaggedAddressWAN]; ok && v != "" {
|
|
|
|
addr = v
|
|
|
|
}
|
|
|
|
grpcAddr := addr + ":" + strconv.Itoa(srv.ExtGRPCPort)
|
2022-08-22 14:19:00 +00:00
|
|
|
serverAddrs = append(serverAddrs, grpcAddr)
|
|
|
|
}
|
2022-08-24 17:30:52 +00:00
|
|
|
if len(serverAddrs) == 0 {
|
|
|
|
m.logger.Warn("did not find any server addresses with external gRPC ports to publish")
|
|
|
|
continue
|
|
|
|
}
|
2022-08-22 14:19:00 +00:00
|
|
|
|
2022-09-21 15:55:19 +00:00
|
|
|
u := cache.UpdateEvent{
|
2022-08-22 14:19:00 +00:00
|
|
|
CorrelationID: subServerAddrs,
|
|
|
|
Result: &pbpeering.PeeringServerAddresses{
|
|
|
|
Addresses: serverAddrs,
|
|
|
|
},
|
|
|
|
}
|
2022-09-21 15:55:19 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return 0, ctx.Err()
|
|
|
|
case updateCh <- u:
|
|
|
|
}
|
2022-08-22 14:19:00 +00:00
|
|
|
}
|
|
|
|
}
|