2020-07-15 15:54:50 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
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.
|
|
|
|
key string
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
if e.key != "" {
|
|
|
|
name = e.key
|
|
|
|
}
|
2020-10-28 23:25:15 +00:00
|
|
|
ns := e.Value.Service.EnterpriseMeta.GetNamespace()
|
|
|
|
return (key == "" || key == name) && (namespace == "" || 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.
|
|
|
|
//
|
|
|
|
// TODO: no tests for this yet
|
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
|
2020-10-21 20:41:22 +00:00
|
|
|
entMeta := structs.EnterpriseMetaInitializer(req.Namespace)
|
|
|
|
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,
|
2020-09-08 22:13:24 +00:00
|
|
|
Payload: EventPayloadCheckServiceNode{
|
|
|
|
Op: pbsubscribe.CatalogOp_Register,
|
|
|
|
Value: &n,
|
2020-07-21 22:42:26 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-08-07 17:00:39 +00:00
|
|
|
// append each event as a separate item so that they can be serialized
|
|
|
|
// separately, to prevent the encoding of one massive message.
|
2020-07-15 15:54:50 +00:00
|
|
|
buf.Append([]stream.Event{event})
|
|
|
|
}
|
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
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2020-07-15 15:54:50 +00:00
|
|
|
// change may be nil if there was a change that _affected_ the service
|
|
|
|
// like a change to checks but it didn't actually change the service
|
|
|
|
// record itself.
|
2020-07-22 22:41:22 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Duplicate any events that affected connect-enabled instances (proxies or
|
|
|
|
// native apps) to the relevant Connect topic.
|
2020-07-22 01:02:22 +00:00
|
|
|
events = append(events, serviceHealthToConnectEvents(events...)...)
|
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)
|
|
|
|
payload.key = payload.Value.Service.Proxy.DestinationServiceName
|
|
|
|
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-07-22 01:02:22 +00:00
|
|
|
func serviceHealthToConnectEvents(events ...stream.Event) []stream.Event {
|
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-09-15 19:04:33 +00:00
|
|
|
if event.Topic != topicServiceHealth {
|
2020-07-15 15:54:50 +00:00
|
|
|
// Skip non-health or any events already emitted to Connect topic
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
node := getPayloadCheckServiceNode(event.Payload)
|
2020-08-07 18:18:24 +00:00
|
|
|
if node.Service == nil {
|
2020-07-15 15:54:50 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
connectEvent := event
|
2020-09-15 19:04:33 +00:00
|
|
|
connectEvent.Topic = topicServiceHealthConnect
|
2020-07-15 15:54:50 +00:00
|
|
|
|
2020-08-07 18:18:24 +00:00
|
|
|
switch {
|
|
|
|
case node.Service.Connect.Native:
|
|
|
|
result = append(result, connectEvent)
|
|
|
|
|
|
|
|
case node.Service.Kind == structs.ServiceKindConnectProxy:
|
2020-10-27 18:40:06 +00:00
|
|
|
payload := event.Payload.(EventPayloadCheckServiceNode)
|
|
|
|
payload.key = node.Service.Proxy.DestinationServiceName
|
|
|
|
connectEvent.Payload = payload
|
2020-08-07 18:18:24 +00:00
|
|
|
result = append(result, connectEvent)
|
2020-07-15 15:54:50 +00:00
|
|
|
|
2020-08-07 18:18:24 +00:00
|
|
|
default:
|
|
|
|
// ServiceKindTerminatingGateway changes are handled separately.
|
|
|
|
// All other cases are not relevant to the connect topic
|
|
|
|
}
|
2020-07-15 15:54:50 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 18:18:24 +00:00
|
|
|
return result
|
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) {
|
2020-10-21 20:41:22 +00:00
|
|
|
services, err := catalogServiceListByNode(tx, node, structs.WildcardEnterpriseMeta(), true)
|
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
|
|
|
|
nodeRaw, err := tx.First("nodes", "id", node)
|
|
|
|
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)
|
|
|
|
|
2020-10-21 20:41:22 +00:00
|
|
|
iter, err := catalogListChecksByNode(tx, node, structs.WildcardEnterpriseMeta())
|
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
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|