Skip to content

Commit

Permalink
Backport of [API Gateway] Fix use of virtual resolvers in HTTPRoutes …
Browse files Browse the repository at this point in the history
…into release/1.15.x (#17536)

* backport of commit ee9fd90

* backport of commit 6bc25b5

---------

Co-authored-by: Andrew Stucki <[email protected]>
  • Loading branch information
hc-github-team-consul-core and Andrew Stucki authored May 31, 2023
1 parent f9d3f8a commit 7d94518
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/17055.txt
Original file line number Diff line number Diff line change
@@ -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.
```
7 changes: 7 additions & 0 deletions agent/consul/discoverychain/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
112 changes: 112 additions & 0 deletions agent/consul/discoverychain/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 7d94518

Please sign in to comment.