2022-05-19 19:21:29 +00:00
|
|
|
package peering
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/ptypes"
|
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"google.golang.org/genproto/googleapis/rpc/code"
|
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/cache"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
"github.com/hashicorp/consul/proto/pbpeering"
|
|
|
|
"github.com/hashicorp/consul/proto/pbservice"
|
|
|
|
"github.com/hashicorp/consul/proto/pbstatus"
|
|
|
|
)
|
|
|
|
|
2022-05-19 21:37:52 +00:00
|
|
|
/*
|
|
|
|
TODO(peering):
|
|
|
|
|
|
|
|
At the start of each peering stream establishment (not initiation, but the
|
|
|
|
thing that reconnects) we need to do a little bit of light differential
|
|
|
|
snapshot correction to initially synchronize the local state store.
|
|
|
|
|
|
|
|
Then if we ever fail to apply a replication message we should either tear
|
|
|
|
down the entire connection (and thus force a resync on reconnect) or
|
|
|
|
request a resync operation.
|
|
|
|
*/
|
|
|
|
|
2022-05-19 19:21:29 +00:00
|
|
|
// pushService response handles sending exported service instance updates to the peer cluster.
|
|
|
|
// Each cache.UpdateEvent will contain all instances for a service name.
|
|
|
|
// If there are no instances in the event, we consider that to be a de-registration.
|
2022-05-19 21:37:52 +00:00
|
|
|
func pushServiceResponse(
|
|
|
|
logger hclog.Logger,
|
|
|
|
stream BidirectionalStream,
|
|
|
|
status *lockableStreamStatus,
|
|
|
|
update cache.UpdateEvent,
|
|
|
|
) error {
|
2022-05-19 19:21:29 +00:00
|
|
|
csn, ok := update.Result.(*pbservice.IndexedCheckServiceNodes)
|
|
|
|
if !ok {
|
|
|
|
logger.Error(fmt.Sprintf("invalid type for response: %T, expected *pbservice.IndexedCheckServiceNodes", update.Result))
|
|
|
|
|
|
|
|
// Skip this update to avoid locking up peering due to a bad service update.
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-19 21:37:52 +00:00
|
|
|
|
2022-05-25 15:13:23 +00:00
|
|
|
var serviceName string
|
|
|
|
if strings.HasPrefix(update.CorrelationID, subExportedService) {
|
|
|
|
serviceName = strings.TrimPrefix(update.CorrelationID, subExportedService)
|
|
|
|
} else {
|
|
|
|
serviceName = strings.TrimPrefix(update.CorrelationID, subExportedProxyService) + syntheticProxyNameSuffix
|
|
|
|
}
|
2022-05-19 19:21:29 +00:00
|
|
|
|
|
|
|
// If no nodes are present then it's due to one of:
|
|
|
|
// 1. The service is newly registered or exported and yielded a transient empty update.
|
|
|
|
// 2. All instances of the service were de-registered.
|
|
|
|
// 3. The service was un-exported.
|
|
|
|
//
|
|
|
|
// We don't distinguish when these three things occurred, but it's safe to send a DELETE Op in all cases, so we do that.
|
|
|
|
// Case #1 is a no-op for the importing peer.
|
|
|
|
if len(csn.Nodes) == 0 {
|
|
|
|
resp := &pbpeering.ReplicationMessage{
|
|
|
|
Payload: &pbpeering.ReplicationMessage_Response_{
|
|
|
|
Response: &pbpeering.ReplicationMessage_Response{
|
|
|
|
ResourceURL: pbpeering.TypeURLService,
|
|
|
|
// TODO(peering): Nonce management
|
|
|
|
Nonce: "",
|
|
|
|
ResourceID: serviceName,
|
|
|
|
Operation: pbpeering.ReplicationMessage_Response_DELETE,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
logTraceSend(logger, resp)
|
|
|
|
if err := stream.Send(resp); err != nil {
|
|
|
|
status.trackSendError(err.Error())
|
|
|
|
return fmt.Errorf("failed to send to stream: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are nodes in the response, we push them as an UPSERT operation.
|
|
|
|
any, err := ptypes.MarshalAny(csn)
|
|
|
|
if err != nil {
|
|
|
|
// Log the error and skip this response to avoid locking up peering due to a bad update event.
|
|
|
|
logger.Error("failed to marshal service endpoints", "error", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
resp := &pbpeering.ReplicationMessage{
|
|
|
|
Payload: &pbpeering.ReplicationMessage_Response_{
|
|
|
|
Response: &pbpeering.ReplicationMessage_Response{
|
|
|
|
ResourceURL: pbpeering.TypeURLService,
|
|
|
|
// TODO(peering): Nonce management
|
|
|
|
Nonce: "",
|
|
|
|
ResourceID: serviceName,
|
|
|
|
Operation: pbpeering.ReplicationMessage_Response_UPSERT,
|
|
|
|
Resource: any,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
logTraceSend(logger, resp)
|
|
|
|
if err := stream.Send(resp); err != nil {
|
|
|
|
status.trackSendError(err.Error())
|
|
|
|
return fmt.Errorf("failed to send to stream: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) processResponse(peerName string, partition string, resp *pbpeering.ReplicationMessage_Response) (*pbpeering.ReplicationMessage, error) {
|
|
|
|
if resp.ResourceURL != pbpeering.TypeURLService {
|
2022-05-19 21:37:52 +00:00
|
|
|
err := fmt.Errorf("received response for unknown resource type %q", resp.ResourceURL)
|
|
|
|
return makeReply(
|
|
|
|
resp.ResourceURL,
|
|
|
|
resp.Nonce,
|
|
|
|
code.Code_INVALID_ARGUMENT,
|
|
|
|
err.Error(),
|
|
|
|
), err
|
2022-05-19 19:21:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch resp.Operation {
|
|
|
|
case pbpeering.ReplicationMessage_Response_UPSERT:
|
|
|
|
if resp.Resource == nil {
|
2022-05-19 21:37:52 +00:00
|
|
|
err := fmt.Errorf("received upsert response with no content")
|
|
|
|
return makeReply(
|
|
|
|
resp.ResourceURL,
|
|
|
|
resp.Nonce,
|
|
|
|
code.Code_INVALID_ARGUMENT,
|
|
|
|
err.Error(),
|
|
|
|
), err
|
2022-05-19 19:21:29 +00:00
|
|
|
}
|
2022-05-19 21:37:52 +00:00
|
|
|
|
|
|
|
if err := s.handleUpsert(peerName, partition, resp.ResourceURL, resp.ResourceID, resp.Resource); err != nil {
|
|
|
|
return makeReply(
|
|
|
|
resp.ResourceURL,
|
|
|
|
resp.Nonce,
|
|
|
|
code.Code_INTERNAL,
|
|
|
|
fmt.Sprintf("upsert error, ResourceURL: %q, ResourceID: %q: %v", resp.ResourceURL, resp.ResourceID, err),
|
|
|
|
), fmt.Errorf("upsert error: %w", err)
|
2022-05-19 19:21:29 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 21:37:52 +00:00
|
|
|
return makeReply(resp.ResourceURL, resp.Nonce, code.Code_OK, ""), nil
|
|
|
|
|
2022-05-19 19:21:29 +00:00
|
|
|
case pbpeering.ReplicationMessage_Response_DELETE:
|
2022-05-19 21:37:52 +00:00
|
|
|
if err := s.handleDelete(peerName, partition, resp.ResourceURL, resp.ResourceID); err != nil {
|
|
|
|
return makeReply(
|
|
|
|
resp.ResourceURL,
|
|
|
|
resp.Nonce,
|
|
|
|
code.Code_INTERNAL,
|
|
|
|
fmt.Sprintf("delete error, ResourceURL: %q, ResourceID: %q: %v", resp.ResourceURL, resp.ResourceID, err),
|
|
|
|
), fmt.Errorf("delete error: %w", err)
|
2022-05-19 19:21:29 +00:00
|
|
|
}
|
2022-05-19 21:37:52 +00:00
|
|
|
return makeReply(resp.ResourceURL, resp.Nonce, code.Code_OK, ""), nil
|
2022-05-19 19:21:29 +00:00
|
|
|
|
|
|
|
default:
|
2022-05-19 21:37:52 +00:00
|
|
|
var errMsg string
|
|
|
|
if op := pbpeering.ReplicationMessage_Response_Operation_name[int32(resp.Operation)]; op != "" {
|
|
|
|
errMsg = fmt.Sprintf("unsupported operation: %q", op)
|
|
|
|
} else {
|
|
|
|
errMsg = fmt.Sprintf("unsupported operation: %d", resp.Operation)
|
|
|
|
}
|
|
|
|
return makeReply(
|
|
|
|
resp.ResourceURL,
|
|
|
|
resp.Nonce,
|
|
|
|
code.Code_INVALID_ARGUMENT,
|
|
|
|
errMsg,
|
|
|
|
), errors.New(errMsg)
|
|
|
|
}
|
|
|
|
}
|
2022-05-19 19:21:29 +00:00
|
|
|
|
2022-05-19 21:37:52 +00:00
|
|
|
func (s *Service) handleUpsert(
|
|
|
|
peerName string,
|
|
|
|
partition string,
|
|
|
|
resourceURL string,
|
|
|
|
resourceID string,
|
|
|
|
resource *anypb.Any,
|
|
|
|
) error {
|
|
|
|
switch resourceURL {
|
|
|
|
case pbpeering.TypeURLService:
|
|
|
|
sn := structs.ServiceNameFromString(resourceID)
|
|
|
|
sn.OverridePartition(partition)
|
|
|
|
|
|
|
|
csn := &pbservice.IndexedCheckServiceNodes{}
|
|
|
|
if err := ptypes.UnmarshalAny(resource, csn); err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal resource: %w", err)
|
2022-05-19 19:21:29 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 21:37:52 +00:00
|
|
|
return s.handleUpsertService(peerName, partition, sn, csn)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unexpected resourceURL: %s", resourceURL)
|
2022-05-19 19:21:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-19 21:37:52 +00:00
|
|
|
func (s *Service) handleUpsertService(
|
|
|
|
peerName string,
|
|
|
|
partition string,
|
|
|
|
sn structs.ServiceName,
|
|
|
|
csn *pbservice.IndexedCheckServiceNodes,
|
|
|
|
) error {
|
2022-05-19 19:21:29 +00:00
|
|
|
if csn == nil || len(csn.Nodes) == 0 {
|
2022-05-19 21:37:52 +00:00
|
|
|
return s.handleDeleteService(peerName, partition, sn)
|
2022-05-19 19:21:29 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 21:37:52 +00:00
|
|
|
// Convert exported data into structs format.
|
|
|
|
structsNodes := make([]structs.CheckServiceNode, 0, len(csn.Nodes))
|
|
|
|
for _, pb := range csn.Nodes {
|
|
|
|
instance, err := pbservice.CheckServiceNodeToStructs(pb)
|
2022-05-19 19:21:29 +00:00
|
|
|
if err != nil {
|
2022-05-19 21:37:52 +00:00
|
|
|
return fmt.Errorf("failed to convert instance: %w", err)
|
2022-05-19 19:21:29 +00:00
|
|
|
}
|
2022-05-19 21:37:52 +00:00
|
|
|
structsNodes = append(structsNodes, *instance)
|
2022-05-19 19:21:29 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 21:37:52 +00:00
|
|
|
// Normalize the data into a convenient form for operation.
|
|
|
|
snap := newHealthSnapshot(structsNodes, partition, peerName)
|
2022-05-19 19:21:29 +00:00
|
|
|
|
2022-05-19 21:37:52 +00:00
|
|
|
for _, nodeSnap := range snap.Nodes {
|
2022-05-19 19:21:29 +00:00
|
|
|
// First register the node
|
2022-05-19 21:37:52 +00:00
|
|
|
req := nodeSnap.Node.ToRegisterRequest()
|
2022-05-19 19:21:29 +00:00
|
|
|
if err := s.Backend.Apply().CatalogRegister(&req); err != nil {
|
2022-05-19 21:37:52 +00:00
|
|
|
return fmt.Errorf("failed to register node: %w", err)
|
2022-05-19 19:21:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Then register all services on that node
|
2022-05-19 21:37:52 +00:00
|
|
|
for _, svcSnap := range nodeSnap.Services {
|
|
|
|
req.Service = svcSnap.Service
|
2022-05-19 19:21:29 +00:00
|
|
|
if err := s.Backend.Apply().CatalogRegister(&req); err != nil {
|
2022-05-19 21:37:52 +00:00
|
|
|
return fmt.Errorf("failed to register service: %w", err)
|
2022-05-19 19:21:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
req.Service = nil
|
|
|
|
|
|
|
|
// Then register all checks on that node
|
|
|
|
var chks structs.HealthChecks
|
2022-05-19 21:37:52 +00:00
|
|
|
for _, svcSnap := range nodeSnap.Services {
|
|
|
|
for _, c := range svcSnap.Checks {
|
|
|
|
chks = append(chks, c)
|
|
|
|
}
|
2022-05-19 19:21:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req.Checks = chks
|
|
|
|
if err := s.Backend.Apply().CatalogRegister(&req); err != nil {
|
2022-05-19 21:37:52 +00:00
|
|
|
return fmt.Errorf("failed to register check: %w", err)
|
2022-05-19 19:21:29 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-19 21:37:52 +00:00
|
|
|
|
|
|
|
// TODO(peering): cleanup and deregister existing data that is now missing safely somehow
|
|
|
|
|
2022-05-19 19:21:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 21:37:52 +00:00
|
|
|
func (s *Service) handleDelete(
|
|
|
|
peerName string,
|
|
|
|
partition string,
|
|
|
|
resourceURL string,
|
|
|
|
resourceID string,
|
|
|
|
) error {
|
|
|
|
switch resourceURL {
|
|
|
|
case pbpeering.TypeURLService:
|
|
|
|
sn := structs.ServiceNameFromString(resourceID)
|
|
|
|
sn.OverridePartition(partition)
|
|
|
|
return s.handleDeleteService(peerName, partition, sn)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unexpected resourceURL: %s", resourceURL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) handleDeleteService(
|
|
|
|
peerName string,
|
|
|
|
partition string,
|
|
|
|
sn structs.ServiceName,
|
|
|
|
) error {
|
|
|
|
// Deregister: ServiceID == DeleteService ANd checks
|
|
|
|
// Deregister: ServiceID(empty) CheckID(empty) == DeleteNode
|
|
|
|
|
2022-05-19 19:21:29 +00:00
|
|
|
// TODO(peering): implement
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeReply(resourceURL, nonce string, errCode code.Code, errMsg string) *pbpeering.ReplicationMessage {
|
|
|
|
var rpcErr *pbstatus.Status
|
|
|
|
if errCode != code.Code_OK || errMsg != "" {
|
|
|
|
rpcErr = &pbstatus.Status{
|
|
|
|
Code: int32(errCode),
|
|
|
|
Message: errMsg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-19 21:37:52 +00:00
|
|
|
// TODO: shouldn't this be response?
|
|
|
|
return &pbpeering.ReplicationMessage{
|
2022-05-19 19:21:29 +00:00
|
|
|
Payload: &pbpeering.ReplicationMessage_Request_{
|
|
|
|
Request: &pbpeering.ReplicationMessage_Request{
|
|
|
|
ResourceURL: resourceURL,
|
|
|
|
Nonce: nonce,
|
|
|
|
Error: rpcErr,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|