From 088b49be0a5f4310351361e81f3f3d61f90a2df7 Mon Sep 17 00:00:00 2001 From: Nathan Coleman Date: Wed, 5 Jun 2024 11:00:17 -0400 Subject: [PATCH] Use /v1/exported-services instead of config entry list for exportedServices func --- dependency/consul_exported_services.go | 60 ++++++++------------- dependency/consul_exported_services_test.go | 48 +++++++---------- 2 files changed, 42 insertions(+), 66 deletions(-) diff --git a/dependency/consul_exported_services.go b/dependency/consul_exported_services.go index 0984528da..d7d247d77 100644 --- a/dependency/consul_exported_services.go +++ b/dependency/consul_exported_services.go @@ -5,6 +5,7 @@ import ( "log" "net/url" "slices" + "strings" capi "github.com/hashicorp/consul/api" ) @@ -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. @@ -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{ diff --git a/dependency/consul_exported_services_test.go b/dependency/consul_exported_services_test.go index d9295c546..f565b1816 100644 --- a/dependency/consul_exported_services_test.go +++ b/dependency/consul_exported_services_test.go @@ -3,6 +3,7 @@ package dependency import ( "testing" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" capi "github.com/hashicorp/consul/api" @@ -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"}, }, }, }, @@ -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"}, }, }, }, @@ -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"}, }, }, }, @@ -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{}, }, }, }, @@ -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{}, }, }, }, @@ -220,9 +213,7 @@ 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) @@ -230,8 +221,7 @@ func TestListExportedServicesQuery_Fetch(t *testing.T) { 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