2020-07-15 15:54:50 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2021-02-03 17:23:10 +00:00
|
|
|
"strings"
|
|
|
|
|
2020-09-15 19:04:33 +00:00
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
|
|
|
|
2020-11-05 22:57:25 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2020-07-15 15:54:50 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/stream"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2020-09-08 22:13:24 +00:00
|
|
|
"github.com/hashicorp/consul/proto/pbsubscribe"
|
2020-07-15 15:54:50 +00:00
|
|
|
)
|
|
|
|
|
2020-09-08 22:13:24 +00:00
|
|
|
// EventPayloadCheckServiceNode is used as the Payload for a stream.Event to
|
|
|
|
// indicates changes to a CheckServiceNode for service health.
|
2020-11-06 18:00:33 +00:00
|
|
|
//
|
|
|
|
// The stream.Payload methods implemented by EventPayloadCheckServiceNode are
|
|
|
|
// do not mutate the payload, making it safe to use in an Event sent to
|
|
|
|
// stream.EventPublisher.Publish.
|
2020-09-08 22:13:24 +00:00
|
|
|
type EventPayloadCheckServiceNode struct {
|
|
|
|
Op pbsubscribe.CatalogOp
|
|
|
|
Value *structs.CheckServiceNode
|
2020-10-27 18:40:06 +00:00
|
|
|
// key is used to override the key used to filter the payload. It is set for
|
|
|
|
// events in the connect topic to specify the name of the underlying service
|
|
|
|
// when the change event is for a sidecar or gateway.
|
2021-01-29 20:40:07 +00:00
|
|
|
overrideKey string
|
|
|
|
overrideNamespace string
|
2021-08-17 21:49:26 +00:00
|
|
|
overridePartition string
|
2020-10-27 18:40:06 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 22:57:25 +00:00
|
|
|
func (e EventPayloadCheckServiceNode) HasReadPermission(authz acl.Authorizer) bool {
|
|
|
|
return e.Value.CanRead(authz) == acl.Allow
|
|
|
|
}
|
|
|
|
|
2021-08-17 21:49:26 +00:00
|
|
|
func (e EventPayloadCheckServiceNode) MatchesKey(key, namespace, partition string) bool {
|
|
|
|
if key == "" && namespace == "" && partition == "" {
|
2020-10-27 18:40:06 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Value.Service == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
name := e.Value.Service.Service
|
2021-01-29 20:40:07 +00:00
|
|
|
if e.overrideKey != "" {
|
|
|
|
name = e.overrideKey
|
2020-10-27 18:40:06 +00:00
|
|
|
}
|
2021-02-22 17:50:52 +00:00
|
|
|
ns := e.Value.Service.EnterpriseMeta.NamespaceOrDefault()
|
2021-01-29 20:40:07 +00:00
|
|
|
if e.overrideNamespace != "" {
|
|
|
|
ns = e.overrideNamespace
|
|
|
|
}
|
2021-08-17 21:49:26 +00:00
|
|
|
ap := e.Value.Service.EnterpriseMeta.PartitionOrDefault()
|
|
|
|
if e.overridePartition != "" {
|
|
|
|
ap = e.overridePartition
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:40:07 +00:00
|
|
|
return (key == "" || strings.EqualFold(key, name)) &&
|
2021-08-17 21:49:26 +00:00
|
|
|
(namespace == "" || strings.EqualFold(namespace, ns)) &&
|
|
|
|
(partition == "" || strings.EqualFold(partition, ap))
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 22:42:26 +00:00
|
|
|
// serviceHealthSnapshot returns a stream.SnapshotFunc that provides a snapshot
|
|
|
|
// of stream.Events that describe the current state of a service health query.
|
2020-10-23 18:43:13 +00:00
|
|
|
func serviceHealthSnapshot(db ReadDB, topic stream.Topic) stream.SnapshotFunc {
|
2020-07-21 22:42:26 +00:00
|
|
|
return func(req stream.SubscribeRequest, buf stream.SnapshotAppender) (index uint64, err error) {
|
2020-10-23 18:43:13 +00:00
|
|
|
tx := db.ReadTxn()
|
2020-07-21 22:42:26 +00:00
|
|
|
defer tx.Abort()
|
2020-07-15 15:54:50 +00:00
|
|
|
|
2020-09-15 19:04:33 +00:00
|
|
|
connect := topic == topicServiceHealthConnect
|
2021-08-17 21:49:26 +00:00
|
|
|
entMeta := structs.NewEnterpriseMetaWithPartition(req.Partition, req.Namespace)
|
2020-10-21 20:41:22 +00:00
|
|
|
idx, nodes, err := checkServiceNodesTxn(tx, nil, req.Key, connect, &entMeta)
|
2020-07-21 22:42:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 19:04:33 +00:00
|
|
|
for i := range nodes {
|
|
|
|
n := nodes[i]
|
2020-07-21 22:42:26 +00:00
|
|
|
event := stream.Event{
|
|
|
|
Index: idx,
|
|
|
|
Topic: topic,
|
2021-02-27 00:39:05 +00:00
|
|
|
Payload: EventPayloadCheckServiceNode{
|
|
|
|
Op: pbsubscribe.CatalogOp_Register,
|
|
|
|
Value: &n,
|
|
|
|
},
|
2020-12-04 21:34:29 +00:00
|
|
|
}
|
|
|
|
|
2021-02-27 00:39:05 +00:00
|
|
|
if !connect {
|
|
|
|
// append each event as a separate item so that they can be serialized
|
|
|
|
// separately, to prevent the encoding of one massive message.
|
|
|
|
buf.Append([]stream.Event{event})
|
|
|
|
continue
|
2020-12-04 21:34:29 +00:00
|
|
|
}
|
|
|
|
|
2021-02-27 00:39:05 +00:00
|
|
|
events, err := connectEventsByServiceKind(tx, event)
|
|
|
|
if err != nil {
|
|
|
|
return idx, err
|
|
|
|
}
|
|
|
|
buf.Append(events)
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
2020-07-21 22:42:26 +00:00
|
|
|
|
|
|
|
return idx, err
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-12 18:39:38 +00:00
|
|
|
// TODO: this could use NodeServiceQuery
|
2020-07-15 15:54:50 +00:00
|
|
|
type nodeServiceTuple struct {
|
|
|
|
Node string
|
|
|
|
ServiceID string
|
|
|
|
EntMeta structs.EnterpriseMeta
|
|
|
|
}
|
|
|
|
|
2020-07-22 22:41:22 +00:00
|
|
|
func newNodeServiceTupleFromServiceNode(sn *structs.ServiceNode) nodeServiceTuple {
|
|
|
|
return nodeServiceTuple{
|
|
|
|
Node: sn.Node,
|
|
|
|
ServiceID: sn.ServiceID,
|
|
|
|
EntMeta: sn.EnterpriseMeta,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newNodeServiceTupleFromServiceHealthCheck(hc *structs.HealthCheck) nodeServiceTuple {
|
|
|
|
return nodeServiceTuple{
|
|
|
|
Node: hc.Node,
|
|
|
|
ServiceID: hc.ServiceID,
|
|
|
|
EntMeta: hc.EnterpriseMeta,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type serviceChange struct {
|
|
|
|
changeType changeType
|
|
|
|
change memdb.Change
|
|
|
|
}
|
|
|
|
|
2021-08-17 21:49:26 +00:00
|
|
|
type nodeTuple struct {
|
|
|
|
Node string
|
|
|
|
Partition string
|
|
|
|
}
|
|
|
|
|
2020-07-22 22:41:22 +00:00
|
|
|
var serviceChangeIndirect = serviceChange{changeType: changeIndirect}
|
|
|
|
|
2020-07-15 15:54:50 +00:00
|
|
|
// ServiceHealthEventsFromChanges returns all the service and Connect health
|
|
|
|
// events that should be emitted given a set of changes to the state store.
|
|
|
|
func ServiceHealthEventsFromChanges(tx ReadTxn, changes Changes) ([]stream.Event, error) {
|
|
|
|
var events []stream.Event
|
|
|
|
|
2021-08-17 21:49:26 +00:00
|
|
|
var nodeChanges map[nodeTuple]changeType
|
2020-07-22 22:41:22 +00:00
|
|
|
var serviceChanges map[nodeServiceTuple]serviceChange
|
2021-01-11 22:12:51 +00:00
|
|
|
var termGatewayChanges map[structs.ServiceName]map[structs.ServiceName]serviceChange
|
2020-07-15 15:54:50 +00:00
|
|
|
|
2021-08-17 21:49:26 +00:00
|
|
|
markNode := func(node nodeTuple, typ changeType) {
|
2020-07-15 15:54:50 +00:00
|
|
|
if nodeChanges == nil {
|
2021-08-17 21:49:26 +00:00
|
|
|
nodeChanges = make(map[nodeTuple]changeType)
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
// If the caller has an actual node mutation ensure we store it even if the
|
|
|
|
// node is already marked. If the caller is just marking the node dirty
|
2020-08-07 17:00:39 +00:00
|
|
|
// without a node change, don't overwrite any existing node change we know
|
2020-07-15 15:54:50 +00:00
|
|
|
// about.
|
2020-07-21 23:39:36 +00:00
|
|
|
if nodeChanges[node] == changeIndirect {
|
|
|
|
nodeChanges[node] = typ
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-22 22:41:22 +00:00
|
|
|
markService := func(key nodeServiceTuple, svcChange serviceChange) {
|
2020-07-15 15:54:50 +00:00
|
|
|
if serviceChanges == nil {
|
2020-07-22 22:41:22 +00:00
|
|
|
serviceChanges = make(map[nodeServiceTuple]serviceChange)
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
// If the caller has an actual service mutation ensure we store it even if
|
|
|
|
// the service is already marked. If the caller is just marking the service
|
2020-08-07 17:00:39 +00:00
|
|
|
// dirty without a service change, don't overwrite any existing service change we
|
2020-07-15 15:54:50 +00:00
|
|
|
// know about.
|
2020-07-22 22:41:22 +00:00
|
|
|
if serviceChanges[key].changeType == changeIndirect {
|
|
|
|
serviceChanges[key] = svcChange
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, change := range changes.Changes {
|
|
|
|
switch change.Table {
|
2021-08-17 21:49:26 +00:00
|
|
|
case tableNodes:
|
2020-07-15 15:54:50 +00:00
|
|
|
// Node changed in some way, if it's not a delete, we'll need to
|
|
|
|
// re-deliver CheckServiceNode results for all services on that node but
|
|
|
|
// we mark it anyway because if it _is_ a delete then we need to know that
|
|
|
|
// later to avoid trying to deliver events when node level checks mark the
|
|
|
|
// node as "changed".
|
2020-07-21 23:39:36 +00:00
|
|
|
n := changeObject(change).(*structs.Node)
|
2021-08-17 21:49:26 +00:00
|
|
|
tuple := newNodeTupleFromNode(n)
|
|
|
|
markNode(tuple, changeTypeFromChange(change))
|
2020-07-15 15:54:50 +00:00
|
|
|
|
2021-03-26 20:04:45 +00:00
|
|
|
case tableServices:
|
2020-07-21 23:39:36 +00:00
|
|
|
sn := changeObject(change).(*structs.ServiceNode)
|
2020-07-22 22:41:22 +00:00
|
|
|
srvChange := serviceChange{changeType: changeTypeFromChange(change), change: change}
|
|
|
|
markService(newNodeServiceTupleFromServiceNode(sn), srvChange)
|
2020-07-15 15:54:50 +00:00
|
|
|
|
2021-03-26 23:33:10 +00:00
|
|
|
case tableChecks:
|
2020-07-15 15:54:50 +00:00
|
|
|
// For health we only care about the scope for now to know if it's just
|
|
|
|
// affecting a single service or every service on a node. There is a
|
|
|
|
// subtle edge case where the check with same ID changes from being node
|
|
|
|
// scoped to service scoped or vice versa, in either case we need to treat
|
|
|
|
// it as affecting all services on the node.
|
|
|
|
switch {
|
|
|
|
case change.Updated():
|
|
|
|
before := change.Before.(*structs.HealthCheck)
|
|
|
|
after := change.After.(*structs.HealthCheck)
|
|
|
|
if after.ServiceID == "" || before.ServiceID == "" {
|
2020-08-07 17:00:39 +00:00
|
|
|
// check before and/or after is node-scoped
|
2021-08-17 21:49:26 +00:00
|
|
|
nt := newNodeTupleFromHealthCheck(after)
|
|
|
|
markNode(nt, changeIndirect)
|
2020-07-15 15:54:50 +00:00
|
|
|
} else {
|
|
|
|
// Check changed which means we just need to emit for the linked
|
|
|
|
// service.
|
2020-07-22 22:41:22 +00:00
|
|
|
markService(newNodeServiceTupleFromServiceHealthCheck(after), serviceChangeIndirect)
|
2020-07-15 15:54:50 +00:00
|
|
|
|
|
|
|
// Edge case - if the check with same ID was updated to link to a
|
|
|
|
// different service ID but the old service with old ID still exists,
|
|
|
|
// then the old service instance needs updating too as it has one
|
|
|
|
// fewer checks now.
|
|
|
|
if before.ServiceID != after.ServiceID {
|
2020-07-22 22:41:22 +00:00
|
|
|
markService(newNodeServiceTupleFromServiceHealthCheck(before), serviceChangeIndirect)
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 01:02:22 +00:00
|
|
|
case change.Deleted(), change.Created():
|
|
|
|
obj := changeObject(change).(*structs.HealthCheck)
|
|
|
|
if obj.ServiceID == "" {
|
2020-07-15 15:54:50 +00:00
|
|
|
// Node level check
|
2021-08-17 21:49:26 +00:00
|
|
|
nt := newNodeTupleFromHealthCheck(obj)
|
|
|
|
markNode(nt, changeIndirect)
|
2020-07-15 15:54:50 +00:00
|
|
|
} else {
|
2020-07-22 22:41:22 +00:00
|
|
|
markService(newNodeServiceTupleFromServiceHealthCheck(obj), serviceChangeIndirect)
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-26 18:34:14 +00:00
|
|
|
case tableGatewayServices:
|
2021-01-11 22:12:51 +00:00
|
|
|
gs := changeObject(change).(*structs.GatewayService)
|
|
|
|
if gs.GatewayKind != structs.ServiceKindTerminatingGateway {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
gsChange := serviceChange{changeType: changeTypeFromChange(change), change: change}
|
2021-01-20 20:10:48 +00:00
|
|
|
|
2021-01-11 22:12:51 +00:00
|
|
|
if termGatewayChanges == nil {
|
|
|
|
termGatewayChanges = make(map[structs.ServiceName]map[structs.ServiceName]serviceChange)
|
|
|
|
}
|
|
|
|
|
2021-01-20 20:10:48 +00:00
|
|
|
_, ok := termGatewayChanges[gs.Gateway]
|
2021-01-11 22:12:51 +00:00
|
|
|
if !ok {
|
|
|
|
termGatewayChanges[gs.Gateway] = map[structs.ServiceName]serviceChange{}
|
|
|
|
}
|
|
|
|
|
2021-01-20 20:10:48 +00:00
|
|
|
switch gsChange.changeType {
|
|
|
|
case changeUpdate:
|
|
|
|
after := gsChange.change.After.(*structs.GatewayService)
|
|
|
|
if gsChange.change.Before.(*structs.GatewayService).IsSame(after) {
|
|
|
|
continue
|
|
|
|
}
|
2021-01-11 22:12:51 +00:00
|
|
|
termGatewayChanges[gs.Gateway][gs.Service] = gsChange
|
2021-01-20 20:10:48 +00:00
|
|
|
case changeDelete, changeCreate:
|
2021-01-11 22:12:51 +00:00
|
|
|
termGatewayChanges[gs.Gateway][gs.Service] = gsChange
|
|
|
|
}
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now act on those marked nodes/services
|
2020-07-21 23:39:36 +00:00
|
|
|
for node, changeType := range nodeChanges {
|
|
|
|
if changeType == changeDelete {
|
2020-07-15 15:54:50 +00:00
|
|
|
// Node deletions are a no-op here since the state store transaction will
|
|
|
|
// have also removed all the service instances which will be handled in
|
|
|
|
// the loop below.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Rebuild events for all services on this node
|
2021-08-17 21:49:26 +00:00
|
|
|
es, err := newServiceHealthEventsForNode(tx, changes.Index, node.Node,
|
|
|
|
structs.WildcardEnterpriseMetaInPartition(node.Partition))
|
2020-07-15 15:54:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
events = append(events, es...)
|
|
|
|
}
|
|
|
|
|
2020-07-22 22:41:22 +00:00
|
|
|
for tuple, srvChange := range serviceChanges {
|
|
|
|
if srvChange.changeType == changeDelete {
|
|
|
|
sn := srvChange.change.Before.(*structs.ServiceNode)
|
2020-07-22 01:02:22 +00:00
|
|
|
e := newServiceHealthEventDeregister(changes.Index, sn)
|
|
|
|
events = append(events, e)
|
2020-07-15 15:54:50 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if this was a service mutation that changed it's name which
|
|
|
|
// requires special handling even if node changed and new events were
|
|
|
|
// already published.
|
2020-07-22 22:41:22 +00:00
|
|
|
if srvChange.changeType == changeUpdate {
|
|
|
|
before := srvChange.change.Before.(*structs.ServiceNode)
|
|
|
|
after := srvChange.change.After.(*structs.ServiceNode)
|
2020-07-15 15:54:50 +00:00
|
|
|
|
|
|
|
if before.ServiceName != after.ServiceName {
|
|
|
|
// Service was renamed, the code below will ensure the new registrations
|
|
|
|
// go out to subscribers to the new service name topic key, but we need
|
|
|
|
// to fix up subscribers that were watching the old name by sending
|
|
|
|
// deregistrations.
|
2020-07-22 01:02:22 +00:00
|
|
|
e := newServiceHealthEventDeregister(changes.Index, before)
|
|
|
|
events = append(events, e)
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 18:18:24 +00:00
|
|
|
if e, ok := isConnectProxyDestinationServiceChange(changes.Index, before, after); ok {
|
|
|
|
events = append(events, e)
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-17 21:49:26 +00:00
|
|
|
if _, ok := nodeChanges[tuple.nodeTuple()]; ok {
|
2020-07-15 15:54:50 +00:00
|
|
|
// We already rebuilt events for everything on this node, no need to send
|
|
|
|
// a duplicate.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Build service event and append it
|
2020-07-22 01:02:22 +00:00
|
|
|
e, err := newServiceHealthEventForService(tx, changes.Index, tuple)
|
2020-07-15 15:54:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-22 01:02:22 +00:00
|
|
|
events = append(events, e)
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
|
2022-01-12 20:08:49 +00:00
|
|
|
for gatewayName, svcChanges := range termGatewayChanges {
|
|
|
|
for serviceName, gsChange := range svcChanges {
|
2021-01-11 22:12:51 +00:00
|
|
|
gs := changeObject(gsChange.change).(*structs.GatewayService)
|
|
|
|
|
2021-08-17 21:49:26 +00:00
|
|
|
q := Query{
|
|
|
|
Value: gs.Gateway.Name,
|
|
|
|
EnterpriseMeta: gatewayName.EnterpriseMeta,
|
|
|
|
}
|
2021-03-26 22:48:55 +00:00
|
|
|
_, nodes, err := serviceNodesTxn(tx, nil, indexService, q)
|
2021-01-11 22:12:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always send deregister events for deletes/updates.
|
|
|
|
if gsChange.changeType != changeCreate {
|
|
|
|
for _, sn := range nodes {
|
|
|
|
e := newServiceHealthEventDeregister(changes.Index, sn)
|
|
|
|
|
|
|
|
e.Topic = topicServiceHealthConnect
|
|
|
|
payload := e.Payload.(EventPayloadCheckServiceNode)
|
2021-01-29 20:40:07 +00:00
|
|
|
payload.overrideKey = serviceName.Name
|
2021-02-22 17:50:52 +00:00
|
|
|
if gatewayName.EnterpriseMeta.NamespaceOrDefault() != serviceName.EnterpriseMeta.NamespaceOrDefault() {
|
|
|
|
payload.overrideNamespace = serviceName.EnterpriseMeta.NamespaceOrDefault()
|
2021-01-29 20:53:45 +00:00
|
|
|
}
|
2021-08-17 21:49:26 +00:00
|
|
|
if gatewayName.EnterpriseMeta.PartitionOrDefault() != serviceName.EnterpriseMeta.PartitionOrDefault() {
|
|
|
|
payload.overridePartition = serviceName.EnterpriseMeta.PartitionOrDefault()
|
|
|
|
}
|
2021-01-11 22:12:51 +00:00
|
|
|
e.Payload = payload
|
|
|
|
|
|
|
|
events = append(events, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if gsChange.changeType == changeDelete {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build service events and append them
|
|
|
|
for _, sn := range nodes {
|
|
|
|
tuple := newNodeServiceTupleFromServiceNode(sn)
|
2022-01-12 20:08:49 +00:00
|
|
|
|
|
|
|
// If we're already sending an event for the service, don't send another.
|
|
|
|
if _, ok := serviceChanges[tuple]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-01-11 22:12:51 +00:00
|
|
|
e, err := newServiceHealthEventForService(tx, changes.Index, tuple)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
e.Topic = topicServiceHealthConnect
|
|
|
|
payload := e.Payload.(EventPayloadCheckServiceNode)
|
2021-01-29 20:40:07 +00:00
|
|
|
payload.overrideKey = serviceName.Name
|
2021-02-22 17:50:52 +00:00
|
|
|
if gatewayName.EnterpriseMeta.NamespaceOrDefault() != serviceName.EnterpriseMeta.NamespaceOrDefault() {
|
|
|
|
payload.overrideNamespace = serviceName.EnterpriseMeta.NamespaceOrDefault()
|
2021-01-29 20:53:45 +00:00
|
|
|
}
|
2021-08-17 21:49:26 +00:00
|
|
|
if gatewayName.EnterpriseMeta.PartitionOrDefault() != serviceName.EnterpriseMeta.PartitionOrDefault() {
|
|
|
|
payload.overridePartition = serviceName.EnterpriseMeta.PartitionOrDefault()
|
|
|
|
}
|
2021-01-11 22:12:51 +00:00
|
|
|
e.Payload = payload
|
|
|
|
|
|
|
|
events = append(events, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 15:54:50 +00:00
|
|
|
// Duplicate any events that affected connect-enabled instances (proxies or
|
|
|
|
// native apps) to the relevant Connect topic.
|
2020-12-17 22:46:24 +00:00
|
|
|
connectEvents, err := serviceHealthToConnectEvents(tx, events...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
events = append(events, connectEvents...)
|
2020-07-15 15:54:50 +00:00
|
|
|
|
|
|
|
return events, nil
|
|
|
|
}
|
|
|
|
|
2020-08-07 18:18:24 +00:00
|
|
|
// isConnectProxyDestinationServiceChange handles the case where a Connect proxy changed
|
|
|
|
// the service it is proxying. We need to issue a de-registration for the old
|
|
|
|
// service on the Connect topic. We don't actually need to deregister this sidecar
|
|
|
|
// service though as it still exists and didn't change its name.
|
|
|
|
func isConnectProxyDestinationServiceChange(idx uint64, before, after *structs.ServiceNode) (stream.Event, bool) {
|
|
|
|
if before.ServiceKind != structs.ServiceKindConnectProxy ||
|
|
|
|
before.ServiceProxy.DestinationServiceName == after.ServiceProxy.DestinationServiceName {
|
|
|
|
return stream.Event{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
e := newServiceHealthEventDeregister(idx, before)
|
2020-09-15 19:04:33 +00:00
|
|
|
e.Topic = topicServiceHealthConnect
|
2020-10-27 18:40:06 +00:00
|
|
|
payload := e.Payload.(EventPayloadCheckServiceNode)
|
2021-01-29 20:40:07 +00:00
|
|
|
payload.overrideKey = payload.Value.Service.Proxy.DestinationServiceName
|
2020-10-27 18:40:06 +00:00
|
|
|
e.Payload = payload
|
2020-08-07 18:18:24 +00:00
|
|
|
return e, true
|
|
|
|
}
|
|
|
|
|
2020-07-21 23:39:36 +00:00
|
|
|
type changeType uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
// changeIndirect indicates some other object changed which has implications
|
|
|
|
// for the target object.
|
|
|
|
changeIndirect changeType = iota
|
|
|
|
changeDelete
|
|
|
|
changeCreate
|
|
|
|
changeUpdate
|
|
|
|
)
|
|
|
|
|
|
|
|
func changeTypeFromChange(change memdb.Change) changeType {
|
|
|
|
switch {
|
|
|
|
case change.Deleted():
|
|
|
|
return changeDelete
|
|
|
|
case change.Created():
|
|
|
|
return changeCreate
|
|
|
|
default:
|
|
|
|
return changeUpdate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 15:54:50 +00:00
|
|
|
// serviceHealthToConnectEvents converts already formatted service health
|
|
|
|
// registration events into the ones needed to publish to the Connect topic.
|
|
|
|
// This essentially means filtering out any instances that are not Connect
|
|
|
|
// enabled and so of no interest to those subscribers but also involves
|
|
|
|
// switching connection details to be the proxy instead of the actual instance
|
|
|
|
// in case of a sidecar.
|
2020-12-17 22:46:24 +00:00
|
|
|
func serviceHealthToConnectEvents(
|
|
|
|
tx ReadTxn,
|
|
|
|
events ...stream.Event,
|
|
|
|
) ([]stream.Event, error) {
|
2020-08-07 18:18:24 +00:00
|
|
|
var result []stream.Event
|
2020-07-15 15:54:50 +00:00
|
|
|
for _, event := range events {
|
2020-12-17 22:46:24 +00:00
|
|
|
if event.Topic != topicServiceHealth { // event.Topic == topicServiceHealthConnect
|
2020-07-15 15:54:50 +00:00
|
|
|
// Skip non-health or any events already emitted to Connect topic
|
|
|
|
continue
|
|
|
|
}
|
2021-02-27 00:39:05 +00:00
|
|
|
|
|
|
|
connectEvents, err := connectEventsByServiceKind(tx, event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
|
2021-02-27 00:39:05 +00:00
|
|
|
result = append(result, connectEvents...)
|
|
|
|
}
|
2020-07-15 15:54:50 +00:00
|
|
|
|
2021-02-27 00:39:05 +00:00
|
|
|
return result, nil
|
|
|
|
}
|
2020-08-07 18:18:24 +00:00
|
|
|
|
2021-02-27 00:39:05 +00:00
|
|
|
func connectEventsByServiceKind(tx ReadTxn, origEvent stream.Event) ([]stream.Event, error) {
|
|
|
|
node := getPayloadCheckServiceNode(origEvent.Payload)
|
|
|
|
if node.Service == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-07-15 15:54:50 +00:00
|
|
|
|
2021-02-27 00:39:05 +00:00
|
|
|
event := origEvent // shallow copy the event
|
|
|
|
event.Topic = topicServiceHealthConnect
|
2020-12-17 22:46:24 +00:00
|
|
|
|
2021-02-27 00:39:05 +00:00
|
|
|
if node.Service.Connect.Native {
|
|
|
|
return []stream.Event{event}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch node.Service.Kind {
|
|
|
|
case structs.ServiceKindConnectProxy:
|
|
|
|
payload := event.Payload.(EventPayloadCheckServiceNode)
|
|
|
|
payload.overrideKey = node.Service.Proxy.DestinationServiceName
|
|
|
|
event.Payload = payload
|
|
|
|
return []stream.Event{event}, nil
|
2020-12-17 22:46:24 +00:00
|
|
|
|
2021-02-27 00:39:05 +00:00
|
|
|
case structs.ServiceKindTerminatingGateway:
|
|
|
|
var result []stream.Event
|
2021-03-16 17:54:05 +00:00
|
|
|
|
|
|
|
sn := structs.ServiceName{
|
|
|
|
Name: node.Service.Service,
|
|
|
|
EnterpriseMeta: node.Service.EnterpriseMeta,
|
|
|
|
}
|
|
|
|
iter, err := tx.Get(tableGatewayServices, indexGateway, sn)
|
2021-02-27 00:39:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-08-07 18:18:24 +00:00
|
|
|
}
|
2020-07-15 15:54:50 +00:00
|
|
|
|
2021-02-27 00:39:05 +00:00
|
|
|
// similar to checkServiceNodesTxn -> serviceGatewayNodes
|
|
|
|
for obj := iter.Next(); obj != nil; obj = iter.Next() {
|
|
|
|
result = append(result, copyEventForService(event, obj.(*structs.GatewayService).Service))
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
default:
|
|
|
|
// All other cases are not relevant to the connect topic
|
|
|
|
}
|
|
|
|
return nil, nil
|
2020-12-17 22:46:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func copyEventForService(event stream.Event, service structs.ServiceName) stream.Event {
|
|
|
|
event.Topic = topicServiceHealthConnect
|
|
|
|
payload := event.Payload.(EventPayloadCheckServiceNode)
|
2021-01-29 20:40:07 +00:00
|
|
|
payload.overrideKey = service.Name
|
2021-02-22 17:50:52 +00:00
|
|
|
if payload.Value.Service.EnterpriseMeta.NamespaceOrDefault() != service.EnterpriseMeta.NamespaceOrDefault() {
|
|
|
|
payload.overrideNamespace = service.EnterpriseMeta.NamespaceOrDefault()
|
2021-01-29 20:53:45 +00:00
|
|
|
}
|
2021-08-17 21:49:26 +00:00
|
|
|
if payload.Value.Service.EnterpriseMeta.PartitionOrDefault() != service.EnterpriseMeta.PartitionOrDefault() {
|
|
|
|
payload.overridePartition = service.EnterpriseMeta.PartitionOrDefault()
|
|
|
|
}
|
2021-01-29 20:53:45 +00:00
|
|
|
|
2021-01-20 19:56:59 +00:00
|
|
|
event.Payload = payload
|
2020-12-17 22:46:24 +00:00
|
|
|
return event
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 18:40:06 +00:00
|
|
|
func getPayloadCheckServiceNode(payload stream.Payload) *structs.CheckServiceNode {
|
2020-09-08 22:13:24 +00:00
|
|
|
ep, ok := payload.(EventPayloadCheckServiceNode)
|
2020-07-15 15:54:50 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-08 22:13:24 +00:00
|
|
|
return ep.Value
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 01:02:22 +00:00
|
|
|
// newServiceHealthEventsForNode returns health events for all services on the
|
2020-07-15 15:54:50 +00:00
|
|
|
// given node. This mirrors some of the the logic in the oddly-named
|
|
|
|
// parseCheckServiceNodes but is more efficient since we know they are all on
|
|
|
|
// the same node.
|
2021-08-17 21:49:26 +00:00
|
|
|
func newServiceHealthEventsForNode(tx ReadTxn, idx uint64, node string, entMeta *structs.EnterpriseMeta) ([]stream.Event, error) {
|
|
|
|
services, err := tx.Get(tableServices, indexNode, Query{
|
|
|
|
Value: node,
|
|
|
|
EnterpriseMeta: *entMeta,
|
|
|
|
})
|
2020-07-15 15:54:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-17 21:49:26 +00:00
|
|
|
n, checksFunc, err := getNodeAndChecks(tx, node, entMeta)
|
2020-07-15 15:54:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var events []stream.Event
|
|
|
|
for service := services.Next(); service != nil; service = services.Next() {
|
|
|
|
sn := service.(*structs.ServiceNode)
|
|
|
|
|
2020-07-22 01:33:50 +00:00
|
|
|
event := newServiceHealthEventRegister(idx, n, sn, checksFunc(sn.ServiceID))
|
2020-07-22 01:02:22 +00:00
|
|
|
events = append(events, event)
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return events, nil
|
|
|
|
}
|
|
|
|
|
2020-07-22 01:33:50 +00:00
|
|
|
// getNodeAndNodeChecks returns a the node structure and a function that returns
|
|
|
|
// the full list of checks for a specific service on that node.
|
2021-08-17 21:49:26 +00:00
|
|
|
func getNodeAndChecks(tx ReadTxn, node string, entMeta *structs.EnterpriseMeta) (*structs.Node, serviceChecksFunc, error) {
|
2020-07-15 15:54:50 +00:00
|
|
|
// Fetch the node
|
2021-08-17 21:49:26 +00:00
|
|
|
nodeRaw, err := tx.First(tableNodes, indexID, Query{
|
|
|
|
Value: node,
|
|
|
|
EnterpriseMeta: *entMeta,
|
|
|
|
})
|
2020-07-15 15:54:50 +00:00
|
|
|
if err != nil {
|
2020-07-22 01:33:50 +00:00
|
|
|
return nil, nil, err
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
if nodeRaw == nil {
|
2020-07-22 01:33:50 +00:00
|
|
|
return nil, nil, ErrMissingNode
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
n := nodeRaw.(*structs.Node)
|
|
|
|
|
2021-08-17 21:49:26 +00:00
|
|
|
iter, err := tx.Get(tableChecks, indexNode, Query{
|
|
|
|
Value: node,
|
|
|
|
EnterpriseMeta: *entMeta,
|
|
|
|
})
|
2020-07-15 15:54:50 +00:00
|
|
|
if err != nil {
|
2020-07-22 01:33:50 +00:00
|
|
|
return nil, nil, err
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var nodeChecks structs.HealthChecks
|
|
|
|
var svcChecks map[string]structs.HealthChecks
|
|
|
|
|
|
|
|
for check := iter.Next(); check != nil; check = iter.Next() {
|
|
|
|
check := check.(*structs.HealthCheck)
|
|
|
|
if check.ServiceID == "" {
|
|
|
|
nodeChecks = append(nodeChecks, check)
|
|
|
|
} else {
|
|
|
|
if svcChecks == nil {
|
|
|
|
svcChecks = make(map[string]structs.HealthChecks)
|
|
|
|
}
|
|
|
|
svcChecks[check.ServiceID] = append(svcChecks[check.ServiceID], check)
|
|
|
|
}
|
|
|
|
}
|
2020-07-22 01:33:50 +00:00
|
|
|
serviceChecks := func(serviceID string) structs.HealthChecks {
|
|
|
|
// Create a new slice so that append does not modify the array backing nodeChecks.
|
|
|
|
result := make(structs.HealthChecks, 0, len(nodeChecks))
|
|
|
|
result = append(result, nodeChecks...)
|
|
|
|
for _, check := range svcChecks[serviceID] {
|
|
|
|
result = append(result, check)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
return n, serviceChecks, nil
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 01:33:50 +00:00
|
|
|
type serviceChecksFunc func(serviceID string) structs.HealthChecks
|
|
|
|
|
2020-07-22 01:02:22 +00:00
|
|
|
func newServiceHealthEventForService(tx ReadTxn, idx uint64, tuple nodeServiceTuple) (stream.Event, error) {
|
2021-08-17 21:49:26 +00:00
|
|
|
n, checksFunc, err := getNodeAndChecks(tx, tuple.Node, &tuple.EntMeta)
|
2020-07-15 15:54:50 +00:00
|
|
|
if err != nil {
|
2020-07-22 01:02:22 +00:00
|
|
|
return stream.Event{}, err
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 21:49:26 +00:00
|
|
|
svc, err := tx.Get(tableServices, indexID, NodeServiceQuery{
|
|
|
|
EnterpriseMeta: tuple.EntMeta,
|
|
|
|
Node: tuple.Node,
|
|
|
|
Service: tuple.ServiceID,
|
|
|
|
})
|
2020-07-15 15:54:50 +00:00
|
|
|
if err != nil {
|
2020-07-22 01:02:22 +00:00
|
|
|
return stream.Event{}, err
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 01:33:50 +00:00
|
|
|
raw := svc.Next()
|
|
|
|
if raw == nil {
|
2020-07-22 01:02:22 +00:00
|
|
|
return stream.Event{}, ErrMissingService
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 01:33:50 +00:00
|
|
|
sn := raw.(*structs.ServiceNode)
|
|
|
|
return newServiceHealthEventRegister(idx, n, sn, checksFunc(sn.ServiceID)), nil
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 01:33:50 +00:00
|
|
|
func newServiceHealthEventRegister(
|
|
|
|
idx uint64,
|
2020-07-15 15:54:50 +00:00
|
|
|
node *structs.Node,
|
|
|
|
sn *structs.ServiceNode,
|
2020-07-22 01:33:50 +00:00
|
|
|
checks structs.HealthChecks,
|
2020-07-22 01:02:22 +00:00
|
|
|
) stream.Event {
|
2020-07-15 15:54:50 +00:00
|
|
|
csn := &structs.CheckServiceNode{
|
|
|
|
Node: node,
|
|
|
|
Service: sn.ToNodeService(),
|
|
|
|
Checks: checks,
|
|
|
|
}
|
2020-07-22 01:02:22 +00:00
|
|
|
return stream.Event{
|
2020-09-15 19:04:33 +00:00
|
|
|
Topic: topicServiceHealth,
|
2020-07-15 15:54:50 +00:00
|
|
|
Index: idx,
|
2020-09-08 22:13:24 +00:00
|
|
|
Payload: EventPayloadCheckServiceNode{
|
|
|
|
Op: pbsubscribe.CatalogOp_Register,
|
|
|
|
Value: csn,
|
2020-07-15 15:54:50 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 01:02:22 +00:00
|
|
|
func newServiceHealthEventDeregister(idx uint64, sn *structs.ServiceNode) stream.Event {
|
2020-07-15 15:54:50 +00:00
|
|
|
// We actually only need the node name populated in the node part as it's only
|
|
|
|
// used as a key to know which service was deregistered so don't bother looking
|
|
|
|
// up the node in the DB. Note that while the ServiceNode does have NodeID
|
|
|
|
// etc. fields, they are never populated in memdb per the comment on that
|
|
|
|
// struct and only filled in when we return copies of the result to users.
|
|
|
|
// This is also important because if the service was deleted as part of a
|
|
|
|
// whole node deregistering then the node record won't actually exist now
|
|
|
|
// anyway and we'd have to plumb it through from the changeset above.
|
2021-08-17 21:49:26 +00:00
|
|
|
|
|
|
|
entMeta := sn.EnterpriseMeta
|
|
|
|
entMeta.Normalize()
|
|
|
|
|
2020-07-15 15:54:50 +00:00
|
|
|
csn := &structs.CheckServiceNode{
|
|
|
|
Node: &structs.Node{
|
2021-08-17 21:49:26 +00:00
|
|
|
Node: sn.Node,
|
|
|
|
Partition: entMeta.PartitionOrEmpty(),
|
2020-07-15 15:54:50 +00:00
|
|
|
},
|
|
|
|
Service: sn.ToNodeService(),
|
|
|
|
}
|
|
|
|
|
2020-07-22 01:02:22 +00:00
|
|
|
return stream.Event{
|
2020-09-15 19:04:33 +00:00
|
|
|
Topic: topicServiceHealth,
|
2020-07-15 15:54:50 +00:00
|
|
|
Index: idx,
|
2020-09-08 22:13:24 +00:00
|
|
|
Payload: EventPayloadCheckServiceNode{
|
|
|
|
Op: pbsubscribe.CatalogOp_Deregister,
|
|
|
|
Value: csn,
|
2020-07-15 15:54:50 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|