Skip to content

Commit

Permalink
Use /v1/exported-services instead of config entry list for exportedSe…
Browse files Browse the repository at this point in the history
…rvices func
  • Loading branch information
nathancoleman committed Jun 5, 2024
1 parent 507ac65 commit 088b49b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 66 deletions.
60 changes: 23 additions & 37 deletions dependency/consul_exported_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"net/url"
"slices"
"strings"

capi "github.com/hashicorp/consul/api"
)
Expand Down Expand Up @@ -36,35 +37,28 @@ type ExportedService struct {
}

type ResolvedConsumers struct {
Peers []string
Partitions []string
SamenessGroups []string
Peers []string
Partitions []string
}

func fromConsulExportedService(svc capi.ExportedService) ExportedService {
peers := make([]string, 0, len(svc.Consumers))
partitions := make([]string, 0, len(svc.Consumers))
samenessGroups := make([]string, 0, len(svc.Consumers))
for _, consumer := range svc.Consumers {
if consumer.Peer != "" {
peers = append(peers, consumer.Peer)
}
if consumer.Partition != "" {
partitions = append(partitions, consumer.Partition)
}
if consumer.SamenessGroup != "" {
samenessGroups = append(samenessGroups, consumer.SamenessGroup)
}
}

return ExportedService{
Service: svc.Name,
func fromConsulExportedService(svc capi.ResolvedExportedService) ExportedService {
exportedService := ExportedService{
Service: svc.Service,
Consumers: ResolvedConsumers{
Peers: peers,
Partitions: partitions,
SamenessGroups: samenessGroups,
Partitions: []string{},
Peers: []string{},
},
}

if len(svc.Consumers.Partitions) > 0 {
exportedService.Consumers.Partitions = slices.Clone(svc.Consumers.Partitions)
}

if len(svc.Consumers.Peers) > 0 {
exportedService.Consumers.Peers = slices.Clone(svc.Consumers.Peers)
}

return exportedService
}

// NewListExportedServicesQuery parses a string of the format @dc.
Expand All @@ -87,32 +81,24 @@ func (c *ListExportedServicesQuery) Fetch(clients *ClientSet, opts *QueryOptions
})

log.Printf("[TRACE] %s: GET %s", c, &url.URL{
Path: "/v1/config/exported-services",
Path: "/v1/exported-services",
RawQuery: opts.String(),
})

consulExportedServices, qm, err := clients.Consul().ConfigEntries().List(capi.ExportedServices, opts.ToConsulOpts())
consulExportedServices, qm, err := clients.Consul().ExportedServices(opts.ToConsulOpts())
if err != nil {
return nil, nil, fmt.Errorf("%s: %w", c.String(), err)
}

exportedServices := make([]ExportedService, 0, len(consulExportedServices))
for _, cfgEntry := range consulExportedServices {
svc := cfgEntry.(*capi.ExportedServicesConfigEntry)
for _, svc := range svc.Services {
exportedServices = append(exportedServices, fromConsulExportedService(svc))
}
for _, exportedService := range consulExportedServices {
exportedServices = append(exportedServices, fromConsulExportedService(exportedService))
}

log.Printf("[TRACE] %s: returned %d results", c, len(exportedServices))

slices.SortStableFunc(exportedServices, func(i, j ExportedService) int {
if i.Service < j.Service {
return -1
} else if i.Service > j.Service {
return 1
}
return 0
return strings.Compare(i.Service, j.Service)
})

rm := &ResponseMetadata{
Expand Down
48 changes: 19 additions & 29 deletions dependency/consul_exported_services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dependency
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

capi "github.com/hashicorp/consul/api"
Expand Down Expand Up @@ -41,9 +42,8 @@ func TestListExportedServicesQuery_Fetch(t *testing.T) {
{
Service: "service1",
Consumers: ResolvedConsumers{
Peers: []string{},
Partitions: []string{"foo"},
SamenessGroups: []string{},
Peers: []string{},
Partitions: []string{"foo"},
},
},
},
Expand Down Expand Up @@ -77,17 +77,15 @@ func TestListExportedServicesQuery_Fetch(t *testing.T) {
{
Service: "service1",
Consumers: ResolvedConsumers{
Peers: []string{},
Partitions: []string{"foo"},
SamenessGroups: []string{},
Peers: []string{},
Partitions: []string{"foo"},
},
},
{
Service: "service2",
Consumers: ResolvedConsumers{
Peers: []string{},
Partitions: []string{"foo"},
SamenessGroups: []string{},
Peers: []string{},
Partitions: []string{"foo"},
},
},
},
Expand Down Expand Up @@ -121,17 +119,15 @@ func TestListExportedServicesQuery_Fetch(t *testing.T) {
{
Service: "service1",
Consumers: ResolvedConsumers{
Peers: []string{},
Partitions: []string{"default"},
SamenessGroups: []string{},
Peers: []string{},
Partitions: []string{"default"},
},
},
{
Service: "service2",
Consumers: ResolvedConsumers{
Peers: []string{},
Partitions: []string{"default"},
SamenessGroups: []string{},
Peers: []string{},
Partitions: []string{"default"},
},
},
},
Expand All @@ -157,9 +153,8 @@ func TestListExportedServicesQuery_Fetch(t *testing.T) {
{
Service: "service1",
Consumers: ResolvedConsumers{
Peers: []string{"another"},
Partitions: []string{},
SamenessGroups: []string{},
Peers: []string{"another"},
Partitions: []string{},
},
},
},
Expand Down Expand Up @@ -193,17 +188,15 @@ func TestListExportedServicesQuery_Fetch(t *testing.T) {
{
Service: "service1",
Consumers: ResolvedConsumers{
Peers: []string{"another"},
Partitions: []string{},
SamenessGroups: []string{},
Peers: []string{"another"},
Partitions: []string{},
},
},
{
Service: "service2",
Consumers: ResolvedConsumers{
Peers: []string{"another"},
Partitions: []string{},
SamenessGroups: []string{},
Peers: []string{"another"},
Partitions: []string{},
},
},
},
Expand All @@ -220,18 +213,15 @@ func TestListExportedServicesQuery_Fetch(t *testing.T) {

if tc.exportedServices != nil {
_, _, err := testClients.Consul().ConfigEntries().Set(tc.exportedServices, opts)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
require.NoError(t, err)
}

q, err := NewListExportedServicesQuery(tc.partition)
require.NoError(t, err)

actual, _, err := q.Fetch(testClients, nil)
require.NoError(t, err)

require.ElementsMatch(t, tc.expected, actual)
assert.ElementsMatch(t, tc.expected, actual)

if tc.exportedServices != nil {
// need to clean up because we use a single shared consul instance
Expand Down

0 comments on commit 088b49b

Please sign in to comment.