use service datacenter for dns name (#8704)

* Use args.Datacenter instead of configured datacenter
This commit is contained in:
Hans Hasselberg 2020-09-22 20:34:09 +02:00 committed by GitHub
parent cacc7a1d68
commit d48d2bf550
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 5 deletions

3
.changelog/8704.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:bug
ui: show correct datacenter for gateways
```

View File

@ -164,7 +164,7 @@ RPC:
// Generate the summary // Generate the summary
// TODO (gateways) (freddy) Have Internal.ServiceDump return ServiceDump instead. Need to add bexpr filtering for type. // TODO (gateways) (freddy) Have Internal.ServiceDump return ServiceDump instead. Need to add bexpr filtering for type.
return summarizeServices(out.Nodes.ToServiceDump(), s.agent.config), nil return summarizeServices(out.Nodes.ToServiceDump(), s.agent.config, args.Datacenter), nil
} }
// UIGatewayServices is used to query all the nodes for services associated with a gateway along with their gateway config // UIGatewayServices is used to query all the nodes for services associated with a gateway along with their gateway config
@ -199,10 +199,10 @@ RPC:
return nil, err return nil, err
} }
return summarizeServices(out.Dump, s.agent.config), nil return summarizeServices(out.Dump, s.agent.config, args.Datacenter), nil
} }
func summarizeServices(dump structs.ServiceDump, cfg *config.RuntimeConfig) []*ServiceSummary { func summarizeServices(dump structs.ServiceDump, cfg *config.RuntimeConfig, datacenter string) []*ServiceSummary {
// Collect the summary information // Collect the summary information
var services []structs.ServiceID var services []structs.ServiceID
summary := make(map[structs.ServiceID]*ServiceSummary) summary := make(map[structs.ServiceID]*ServiceSummary)
@ -226,7 +226,7 @@ func summarizeServices(dump structs.ServiceDump, cfg *config.RuntimeConfig) []*S
if csn.GatewayService != nil { if csn.GatewayService != nil {
gwsvc := csn.GatewayService gwsvc := csn.GatewayService
sum := getService(gwsvc.Service.ToServiceID()) sum := getService(gwsvc.Service.ToServiceID())
modifySummaryForGatewayService(cfg, sum, gwsvc) modifySummaryForGatewayService(cfg, datacenter, sum, gwsvc)
} }
// Will happen in cases where we only have the GatewayServices mapping // Will happen in cases where we only have the GatewayServices mapping
@ -307,6 +307,7 @@ func summarizeServices(dump structs.ServiceDump, cfg *config.RuntimeConfig) []*S
func modifySummaryForGatewayService( func modifySummaryForGatewayService(
cfg *config.RuntimeConfig, cfg *config.RuntimeConfig,
datacenter string,
sum *ServiceSummary, sum *ServiceSummary,
gwsvc *structs.GatewayService, gwsvc *structs.GatewayService,
) { ) {
@ -319,7 +320,7 @@ func modifySummaryForGatewayService(
} }
dnsAddresses = append(dnsAddresses, serviceIngressDNSName( dnsAddresses = append(dnsAddresses, serviceIngressDNSName(
gwsvc.Service.Name, gwsvc.Service.Name,
cfg.Datacenter, datacenter,
domain, domain,
&gwsvc.Service.EnterpriseMeta, &gwsvc.Service.EnterpriseMeta,
)) ))

View File

@ -11,6 +11,7 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/hashicorp/consul/agent/config"
"github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/sdk/testutil" "github.com/hashicorp/consul/sdk/testutil"
@ -794,3 +795,12 @@ func TestUIGatewayIntentions(t *testing.T) {
} }
assert.ElementsMatch(t, expected, actual) assert.ElementsMatch(t, expected, actual)
} }
func TestUIEndpoint_modifySummaryForGatewayService_UseRequestedDCInsteadOfConfigured(t *testing.T) {
dc := "dc2"
cfg := config.RuntimeConfig{Datacenter: "dc1", DNSDomain: "consul"}
sum := ServiceSummary{GatewayConfig: GatewayConfig{}}
gwsvc := structs.GatewayService{Service: structs.ServiceName{Name: "test"}, Port: 42}
modifySummaryForGatewayService(&cfg, dc, &sum, &gwsvc)
require.Equal(t, "test.ingress.dc2.consul:42", sum.GatewayConfig.Addresses[0])
}