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
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-06 00:18:37 +00:00
|
|
|
func (e EventPayloadCheckServiceNode) MatchesKey(key, namespace string) bool {
|
2020-10-27 18:40:06 +00:00
|
|
|
if key == "" && namespace == "" {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return (key == "" || strings.EqualFold(key, name)) &&
|
|
|
|
(namespace == "" || strings.EqualFold(namespace, ns))
|
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-02-02 23:04:01 +00:00
|
|
|
entMeta := structs.NewEnterpriseMeta(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-07-21 23:39:36 +00:00
|
|
|
var nodeChanges map[string]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
|
|
|
|
2020-07-21 23:39:36 +00:00
|
|
|
markNode := func(node string, typ changeType) {
|
2020-07-15 15:54:50 +00:00
|
|
|
if nodeChanges == nil {
|
2020-07-21 23:39:36 +00:00
|
|
|
nodeChanges = make(map[string]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 {
|
|
|
|
case "nodes":
|
|
|
|
// 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)
|
|
|
|
markNode(n.Node, changeTypeFromChange(change))
|
2020-07-15 15:54:50 +00:00
|
|
|
|
|
|
|
case "services":
|
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
|
|
|
|
|
|
|
case "checks":
|
|
|
|
// 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
|
2020-07-21 23:39:36 +00:00
|
|
|
markNode(after.Node, 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
|
2020-07-22 01:02:22 +00:00
|
|
|
markNode(obj.Node, 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
|
2020-07-22 01:02:22 +00:00
|
|
|
es, err := newServiceHealthEventsForNode(tx, changes.Index, node)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := nodeChanges[tuple.Node]; ok {
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2021-01-11 22:12:51 +00:00
|
|
|
for gatewayName, serviceChanges := range termGatewayChanges {
|
|
|
|
for serviceName, gsChange := range serviceChanges {
|
|
|
|
gs := changeObject(gsChange.change).(*structs.GatewayService)
|
|
|
|
|
2021-01-20 20:10:48 +00:00
|
|
|
_, nodes, err := serviceNodesTxn(tx, nil, gs.Gateway.Name, false, &gatewayName.EnterpriseMeta)
|
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-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)
|
|
|
|
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-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
|
|
|
|
iter, err := gatewayServices(tx, node.Service.Service, &node.Service.EnterpriseMeta)
|
|
|
|
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-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.
|
2020-07-22 01:02:22 +00:00
|
|
|
func newServiceHealthEventsForNode(tx ReadTxn, idx uint64, node string) ([]stream.Event, error) {
|
2021-02-12 22:31:02 +00:00
|
|
|
services, err := tx.Get(tableServices, indexNode, Query{Value: node})
|
2020-07-15 15:54:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-22 01:33:50 +00:00
|
|
|
n, checksFunc, err := getNodeAndChecks(tx, node)
|
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.
|
|
|
|
func getNodeAndChecks(tx ReadTxn, node string) (*structs.Node, serviceChecksFunc, error) {
|
2020-07-15 15:54:50 +00:00
|
|
|
// Fetch the node
|
2021-02-19 23:18:25 +00:00
|
|
|
nodeRaw, err := tx.First(tableNodes, indexID, Query{Value: node})
|
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-02-12 22:31:02 +00:00
|
|
|
iter, err := tx.Get(tableChecks, indexNode, Query{Value: node})
|
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) {
|
2020-07-22 01:33:50 +00:00
|
|
|
n, checksFunc, err := getNodeAndChecks(tx, tuple.Node)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
svc, err := getCompoundWithTxn(tx, "services", "id", &tuple.EntMeta, tuple.Node, tuple.ServiceID)
|
|
|
|
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.
|
|
|
|
csn := &structs.CheckServiceNode{
|
|
|
|
Node: &structs.Node{
|
|
|
|
Node: sn.Node,
|
|
|
|
},
|
|
|
|
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
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|