open-consul/agent/proxycfg-glue/service_list.go

128 lines
3.7 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package proxycfgglue
import (
"context"
"sort"
"github.com/hashicorp/consul/acl"
"github.com/hashicorp/consul/agent/cache"
cachetype "github.com/hashicorp/consul/agent/cache-types"
"github.com/hashicorp/consul/agent/proxycfg"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/agent/submatview"
Protobuf Refactoring for Multi-Module Cleanliness (#16302) Protobuf Refactoring for Multi-Module Cleanliness This commit includes the following: Moves all packages that were within proto/ to proto/private Rewrites imports to account for the packages being moved Adds in buf.work.yaml to enable buf workspaces Names the proto-public buf module so that we can override the Go package imports within proto/buf.yaml Bumps the buf version dependency to 1.14.0 (I was trying out the version to see if it would get around an issue - it didn't but it also doesn't break things and it seemed best to keep up with the toolchain changes) Why: In the future we will need to consume other protobuf dependencies such as the Google HTTP annotations for openapi generation or grpc-gateway usage. There were some recent changes to have our own ratelimiting annotations. The two combined were not working when I was trying to use them together (attempting to rebase another branch) Buf workspaces should be the solution to the problem Buf workspaces means that each module will have generated Go code that embeds proto file names relative to the proto dir and not the top level repo root. This resulted in proto file name conflicts in the Go global protobuf type registry. The solution to that was to add in a private/ directory into the path within the proto/ directory. That then required rewriting all the imports. Is this safe? AFAICT yes The gRPC wire protocol doesn't seem to care about the proto file names (although the Go grpc code does tack on the proto file name as Metadata in the ServiceDesc) Other than imports, there were no changes to any generated code as a result of this.
2023-02-17 21:14:46 +00:00
"github.com/hashicorp/consul/proto/private/pbcommon"
"github.com/hashicorp/consul/proto/private/pbsubscribe"
)
// CacheServiceList satisfies the proxycfg.ServiceList interface by sourcing
// data from the agent cache.
func CacheServiceList(c *cache.Cache) proxycfg.ServiceList {
return &cacheProxyDataSource[*structs.DCSpecificRequest]{c, cachetype.CatalogServiceListName}
}
func ServerServiceList(deps ServerDataSourceDeps, remoteSource proxycfg.ServiceList) proxycfg.ServiceList {
return &serverServiceList{deps, remoteSource}
}
type serverServiceList struct {
deps ServerDataSourceDeps
remoteSource proxycfg.ServiceList
}
func (s *serverServiceList) Notify(ctx context.Context, req *structs.DCSpecificRequest, correlationID string, ch chan<- proxycfg.UpdateEvent) error {
if req.Datacenter != s.deps.Datacenter {
return s.remoteSource.Notify(ctx, req, correlationID, ch)
}
return s.deps.ViewStore.NotifyCallback(
ctx,
&serviceListRequest{s.deps, req},
correlationID,
dispatchCacheUpdate(ch),
)
}
type serviceListRequest struct {
deps ServerDataSourceDeps
req *structs.DCSpecificRequest
}
func (r *serviceListRequest) Request(index uint64) *pbsubscribe.SubscribeRequest {
return &pbsubscribe.SubscribeRequest{
Topic: pbsubscribe.Topic_ServiceList,
Subject: &pbsubscribe.SubscribeRequest_WildcardSubject{WildcardSubject: true},
Index: index,
Datacenter: r.req.Datacenter,
Token: r.req.QueryOptions.Token,
}
}
func (r *serviceListRequest) CacheInfo() cache.RequestInfo { return r.req.CacheInfo() }
func (r *serviceListRequest) NewMaterializer() (submatview.Materializer, error) {
return submatview.NewLocalMaterializer(submatview.LocalMaterializerDeps{
Backend: r.deps.EventPublisher,
ACLResolver: r.deps.ACLResolver,
Deps: submatview.Deps{
View: newServiceListView(r.req.EnterpriseMeta),
Logger: r.deps.Logger,
Request: r.Request,
},
}), nil
}
func (serviceListRequest) Type() string { return "proxycfgglue.ServiceList" }
func newServiceListView(entMeta acl.EnterpriseMeta) *serviceListView {
view := &serviceListView{entMeta: entMeta}
view.Reset()
return view
}
type serviceListView struct {
entMeta acl.EnterpriseMeta
state map[string]structs.ServiceName
}
func (v *serviceListView) Reset() { v.state = make(map[string]structs.ServiceName) }
func (v *serviceListView) Update(events []*pbsubscribe.Event) error {
for _, event := range filterByEnterpriseMeta(events, v.entMeta) {
update := event.GetService()
if update == nil {
continue
}
var entMeta acl.EnterpriseMeta
pbcommon.EnterpriseMetaToStructs(update.EnterpriseMeta, &entMeta)
name := structs.NewServiceName(update.Name, &entMeta)
switch update.Op {
case pbsubscribe.CatalogOp_Register:
v.state[name.String()] = name
case pbsubscribe.CatalogOp_Deregister:
delete(v.state, name.String())
}
}
return nil
}
func (v *serviceListView) Result(index uint64) any {
serviceList := make(structs.ServiceList, 0, len(v.state))
for _, name := range v.state {
serviceList = append(serviceList, name)
}
sort.Slice(serviceList, func(a, b int) bool {
return serviceList[a].String() < serviceList[b].String()
})
return &structs.IndexedServiceList{
Services: serviceList,
QueryMeta: structs.QueryMeta{
Backend: structs.QueryBackendStreaming,
Index: index,
},
}
}