Backport of [API Gateway] Fix use of virtual resolvers in HTTPRoutes into release/1.15.x (#17536)

* backport of commit ee9fd909fc

* backport of commit 6bc25b5bcc

---------

Co-authored-by: Andrew Stucki <andrew.stucki@hashicorp.com>
pull/17539/head
hc-github-team-consul-core 2023-05-31 17:19:56 -04:00 committed by GitHub
parent f9d3f8a065
commit 7d94518014
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 122 additions and 0 deletions

3
.changelog/17055.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:bug
gateways: Fix an bug where targeting a virtual service defined by a service-resolver was broken for HTTPRoutes.
```

View File

@ -229,6 +229,13 @@ func targetForResolverNode(nodeName string, chains []*structs.CompiledDiscoveryC
splitterName := splitterPrefix + strings.TrimPrefix(nodeName, resolverPrefix)
for _, c := range chains {
targetChainPrefix := resolverPrefix + c.ServiceName + "."
if strings.HasPrefix(nodeName, targetChainPrefix) && len(c.Nodes) == 1 {
// we have a virtual resolver that just maps to another resolver, return
// the given node name
return c.StartNode
}
for name, node := range c.Nodes {
if node.IsSplitter() && strings.HasPrefix(splitterName, name) {
return name

View File

@ -596,6 +596,118 @@ func TestGatewayChainSynthesizer_Synthesize(t *testing.T) {
},
}},
},
"HTTPRoute with virtual resolver": {
synthesizer: NewGatewayChainSynthesizer("dc1", "domain", "suffix", &structs.APIGatewayConfigEntry{
Kind: structs.APIGateway,
Name: "gateway",
}),
httpRoutes: []*structs.HTTPRouteConfigEntry{
{
Kind: structs.HTTPRoute,
Name: "http-route",
Rules: []structs.HTTPRouteRule{{
Services: []structs.HTTPService{{
Name: "foo",
}},
}},
},
},
chain: &structs.CompiledDiscoveryChain{
ServiceName: "foo",
Namespace: "default",
Datacenter: "dc1",
StartNode: "resolver:foo-2.default.default.dc2",
Nodes: map[string]*structs.DiscoveryGraphNode{
"resolver:foo-2.default.default.dc2": {
Type: "resolver",
Name: "foo-2.default.default.dc2",
Resolver: &structs.DiscoveryResolver{
Target: "foo-2.default.default.dc2",
Default: true,
ConnectTimeout: 5000000000,
},
},
},
},
extra: []*structs.CompiledDiscoveryChain{},
expectedIngressServices: []structs.IngressService{{
Name: "gateway-suffix-9b9265b",
Hosts: []string{"*"},
}},
expectedDiscoveryChains: []*structs.CompiledDiscoveryChain{{
ServiceName: "gateway-suffix-9b9265b",
Partition: "default",
Namespace: "default",
Datacenter: "dc1",
Protocol: "http",
StartNode: "router:gateway-suffix-9b9265b.default.default",
Nodes: map[string]*structs.DiscoveryGraphNode{
"router:gateway-suffix-9b9265b.default.default": {
Type: "router",
Name: "gateway-suffix-9b9265b.default.default",
Routes: []*structs.DiscoveryRoute{{
Definition: &structs.ServiceRoute{
Match: &structs.ServiceRouteMatch{
HTTP: &structs.ServiceRouteHTTPMatch{
PathPrefix: "/",
},
},
Destination: &structs.ServiceRouteDestination{
Service: "foo",
Partition: "default",
Namespace: "default",
RequestHeaders: &structs.HTTPHeaderModifiers{
Add: make(map[string]string),
Set: make(map[string]string),
},
},
},
NextNode: "resolver:foo-2.default.default.dc2",
}},
},
"resolver:foo.default.default.dc1": {
Type: "resolver",
Name: "foo.default.default.dc1",
Resolver: &structs.DiscoveryResolver{
Target: "foo.default.default.dc1",
Default: true,
ConnectTimeout: 5000000000,
},
},
"resolver:foo-2.default.default.dc2": {
Type: "resolver",
Name: "foo-2.default.default.dc2",
Resolver: &structs.DiscoveryResolver{
Target: "foo-2.default.default.dc2",
Default: true,
ConnectTimeout: 5000000000,
},
},
},
Targets: map[string]*structs.DiscoveryTarget{
"gateway-suffix-9b9265b.default.default.dc1": {
ID: "gateway-suffix-9b9265b.default.default.dc1",
Service: "gateway-suffix-9b9265b",
Datacenter: "dc1",
Partition: "default",
Namespace: "default",
ConnectTimeout: 5000000000,
SNI: "gateway-suffix-9b9265b.default.dc1.internal.domain",
Name: "gateway-suffix-9b9265b.default.dc1.internal.domain",
},
"foo.default.default.dc1": {
ID: "foo.default.default.dc1",
Service: "foo",
Datacenter: "dc1",
Partition: "default",
Namespace: "default",
ConnectTimeout: 5000000000,
SNI: "foo.default.dc1.internal.domain",
Name: "foo.default.dc1.internal.domain",
},
},
}},
},
}
for name, tc := range cases {