[Bug]: dynamic-endpoint (named upstream selection) is silently ignored when the API has no upstreamDefinitions

Open
#4,324 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go

Research direction

Start by reading gateway/gateway-controller/pkg/transform/restapi.go and gateway/gateway-controller/pkg/xds/translator.go, where the issue identifies the conditions controlling cluster_header routing. Trace how API and operation policies are represented there, then check the gateway-controller tests for route translation. Done means a named upstream selection without definitions or a sandbox returns 503 instead of reaching the main upstream.

Written by the indexing model from the issue text.

Description

Aspect/API Severity/Major Type/Bug
Please select the area the issue is related to

Gateway

Please select the aspect the issue is related to

Aspect/API (API backends, definitions, contracts, interfaces, OpenAPI)

Description

Problem in the component (gateway-controller)

The gateway-controller only builds an API's routes with Envoy cluster_header routing (the cluster is read from the x-target-upstream header) when the API has upstreamDefinitions or a sandbox upstream:

  • gateway/gateway-controller/pkg/transform/restapi.go: useClusterHeader := hasUpstreamDefinitions || hasSandbox
  • gateway/gateway-controller/pkg/xds/translator.go: useClusterHeader := hasUpstreamDefinitions || hasSandboxForClusterHeader

When neither exists, every route is bound to the static main cluster and x-target-upstream is ignored. A policy that selects an upstream by name therefore has no effect, and the request is silently answered by the main upstream.

This is a gateway routing problem, not a policy problem: any policy that selects an upstream by name through UpstreamName is affected, and dynamic-endpoint is where it was found.

How the problem was found

While building an integration test suite for the dynamic-endpoint policy (gateway-controllers repo), one scenario checks that an API whose operation names an upstream that cannot be resolved never falls back to the main backend. The same check passes when the API has upstreamDefinitions (an unknown or malformed name returns 503 and the main upstream never receives the request). It fails when the API has no upstreamDefinitions: the main upstream answers with 200. It fails on gateway 1.2.0 and on the current latest image.

Current behaviour of the dynamic-endpoint policy

  • With upstreamDefinitions present: the policy sets UpstreamName, the policy engine resolves it to the cluster upstream_<Kind>_<apiId>_<name>, sets x-target-upstream, and the route uses it. A name with no matching definition gives 503.
  • Without upstreamDefinitions (and without a sandbox): the selection is ignored and the main upstream serves the request (200). Nothing in the response or at deploy time tells the caller the policy did nothing.

Expected behaviour

A request must never reach the main upstream because a named selection could not be honoured. With a dynamic-endpoint policy attached and no matching upstream definition, the gateway should refuse the request (503), the same as it does when definitions exist but the name does not match.

Suggested fix

Also enable cluster_header routing when the API, or any of its operations, attaches the dynamic-endpoint policy. A small patch for this exists and makes the scenario pass; it can be raised as a PR.

Steps to Reproduce
  1. Start a gateway (for example the 1.2.0 images) and two simple HTTP backends that echo the request path: backend-main:5000 and backend-alt:5001.

  2. Deploy an API that has no upstreamDefinitions (the management API base is /api/management/v1 on 1.2.0 and latest; 1.1.0 and older use /api/management/v0.9):

    curl -u admin:<password> -X POST http://localhost:9090/api/management/v1/rest-apis \
      -H 'Content-Type: application/yaml' --data-binary @- <<'YAML'
    apiVersion: gateway.api-platform.wso2.com/v1
    kind: RestApi
    metadata:
      name: dynamic-no-definitions
    spec:
      displayName: dynamic-no-definitions
      version: v1.0
      context: /dynamic-no-definitions/$version
      upstream:
        main:
          url: http://backend-main:5000
      operations:
        - method: GET
          path: /probe
          policies:
            - name: dynamic-endpoint
              version: v1
              params:
                targetUpstream: alt-upstream
    YAML
    
  3. Call the route:

    curl -i http://localhost:8080/dynamic-no-definitions/v1.0/probe
    

    Actual: 200, served by backend-main.
    Expected: 503; backend-main must not receive the request.

  4. For contrast, update the same API (PUT /api/management/v1/rest-apis/dynamic-no-definitions) with
    upstreamDefinitions: [{name: alt-upstream, basePath: /alt, upstreams: [{url: http://backend-alt:5001}]}] added under spec.
    The request is now routed to backend-alt with the /alt base path, and changing targetUpstream to a name that is not defined returns 503. So the route behaves correctly once cluster_header routing is on.

Severity Level of the Issue

Severity/Major

Environment Details (with versions)

Gateway 1.2.0 and latest (main) images, Docker, sqlite. Reproduced with the gateway-controller management API.

Dominant language
Go
Stars
85
Forks
137
Avg merge
1d 12h
Merged PRs (30d)
247

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from wso2/api-platform

All issues in wso2/api-platform

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.