open-consul/agent/consul/state/events.go
Daniel Upton 688dfe3138 proxycfg-glue: server-local implementation of ServiceList
This is the OSS portion of enterprise PR 2242.

This PR introduces a server-local implementation of the proxycfg.ServiceList
interface, backed by streaming events and a local materializer.
2022-07-14 18:22:12 +01:00

64 lines
2.1 KiB
Go

package state
import (
"errors"
"fmt"
"github.com/hashicorp/consul/acl"
"github.com/hashicorp/consul/agent/consul/stream"
"github.com/hashicorp/consul/proto/pbsubscribe"
)
func PBToStreamSubscribeRequest(req *pbsubscribe.SubscribeRequest, entMeta acl.EnterpriseMeta) (*stream.SubscribeRequest, error) {
var subject stream.Subject
if req.GetWildcardSubject() {
subject = stream.SubjectWildcard
} else {
named := req.GetNamedSubject()
// Support the (deprcated) top-level Key, Partition, Namespace, and PeerName fields.
if named == nil {
named = &pbsubscribe.NamedSubject{
Key: req.Key, // nolint:staticcheck // SA1019 intentional use of deprecated field
Partition: req.Partition, // nolint:staticcheck // SA1019 intentional use of deprecated field
Namespace: req.Namespace, // nolint:staticcheck // SA1019 intentional use of deprecated field
PeerName: req.PeerName, // nolint:staticcheck // SA1019 intentional use of deprecated field
}
}
if named.Key == "" {
return nil, errors.New("either WildcardSubject or NamedSubject.Key is required")
}
switch req.Topic {
case EventTopicServiceHealth, EventTopicServiceHealthConnect:
subject = EventSubjectService{
Key: named.Key,
EnterpriseMeta: entMeta,
PeerName: named.PeerName,
}
case EventTopicMeshConfig, EventTopicServiceResolver, EventTopicIngressGateway, EventTopicServiceIntentions:
subject = EventSubjectConfigEntry{
Name: named.Key,
EnterpriseMeta: &entMeta,
}
case EventTopicServiceList:
// Events on this topic are published to SubjectNone, but rather than
// exposing this in (and further complicating) the streaming API we rely
// on consumers passing WildcardSubject instead, which is functionally the
// same for this purpose.
return nil, fmt.Errorf("topic %s can only be consumed using WildcardSubject", EventTopicServiceList)
default:
return nil, fmt.Errorf("cannot construct subject for topic %s", req.Topic)
}
}
return &stream.SubscribeRequest{
Topic: req.Topic,
Subject: subject,
Token: req.Token,
Index: req.Index,
}, nil
}