fix(policy-engine): resolve upstream base path for dynamic routing#2000
fix(policy-engine): resolve upstream base path for dynamic routing#2000renuka-fernando wants to merge 2 commits into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (7)
✅ Files skipped from review due to trivial changes (1)
📝 WalkthroughChanges OverviewThis PR fixes dynamic-endpoint routing so named upstream definitions' base paths are applied when forwarding requests, and adds integration tests covering the behavior. Code changes
Tests
ImpactRequests routed via the dynamic-endpoint policy to named upstream definitions now include the upstream's base path (fixes WalkthroughThis pull request fixes upstream base path handling in the dynamic-endpoint policy. The core issue involved two bugs in request translation: incorrect lookup of upstream definition paths (using raw upstream names instead of cluster keys) and improper path computation (naive concatenation that retained API context). The fix updates the translator to use cluster names for path lookup and introduces Sequence Diagram(s)sequenceDiagram
participant RestAPITransformer
participant TranslateRuntimeConfigs
participant Gateway_PolicyEngine as PolicyEngine.TranslateRequestHeaderActions
participant Backend
RestAPITransformer->>TranslateRuntimeConfigs: create UpstreamCluster{Name: def.Name, BasePath, Endpoints}
TranslateRuntimeConfigs->>PolicyEngine.TranslateRequestHeaderActions: publish upstream_definition_paths keyed by UpstreamCluster.Name
Client->>PolicyEngine.TranslateRequestHeaderActions: HTTP request (operation with targetUpstream)
PolicyEngine.TranslateRequestHeaderActions->>PolicyEngine.TranslateRequestHeaderActions: computeUpstreamPath(apiPath, apiContext, targetUpstreamBase)
PolicyEngine.TranslateRequestHeaderActions->>Backend: forward request to <computed upstream base> + <operation path>
Backend-->>Client: response
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (2.12.2)level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain modules listed in go.work or their selected dependencies" Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Two bugs prevented the dynamic-endpoint policy from applying a target upstream definition's base path when routing via UpstreamName: - The controller exposed upstream base paths keyed by an internal cluster key, but the policy engine looked them up by the upstream definition name (the policy's targetUpstream value), so the lookup always missed and the base path defaulted to "/". Key the map by definition name in the controller so both sides agree, and add a Name field to UpstreamCluster to carry it. - Even once resolved, the routed path was built by naive concatenation that left the API context in place (e.g. /foo/ctx/v1/items). Use computeUpstreamPath to strip the context before prepending the upstream base path. Add gateway integration test features/dynamic-endpoint.feature covering named-upstream routing, per-operation base paths, and the required targetUpstream parameter, and register it in the default suite.
Add a root-upstream definition with no URL path to the per-operation routing scenario, exercising the basePath="/" default in the controller and the empty-base-path branch of computeUpstreamPath in the policy engine, which strips the API context but prepends nothing.
5850937 to
29a0bc2
Compare
Purpose
The
dynamic-endpointpolicy routes a request to a namedupstreamDefinitionsentry via the SDKUpstreamNamefield. When the targeted upstream definition had a base path (e.g.http://backend:9080/foo), the gateway switched to the correct upstream cluster but failed to apply the upstream's base path — the request reached the backend at the wrong path. This PR fixes that and adds integration coverage for the policy.Approach
upstreamDefinitionPathslookup intranslator.go: it was keyed by the raw upstream-definition name, but the controller keys the map by the full cluster key (upstream_<Kind>_<UUID>_<name>); look it up by the constructed cluster name so the base path is actually resolved./foo/<context>/v1.0/items) with the existingcomputeUpstreamPathhelper, which strips the API context before prepending the upstream base path. This also wires in a previously unused function.RouteMetadata.UpstreamDefinitionPaths(it holds cluster keys, not names).gateway/it/features/dynamic-endpoint.featurecovering named-upstream routing, per-operation base paths, and the requiredtargetUpstreamparameter, and register it in the default IT suite.Related Issues
Fixes #1999
Checklist