hashicorp / consul-api-gateway

The Consul API Gateway is a dedicated ingress solution for intelligently routing traffic to applications running on a Consul Service Mesh.
Mozilla Public License 2.0
100 stars 16 forks source link

“rules.matches.method” only accepts single entry (string) #581

Closed kds-rune closed 1 year ago

kds-rune commented 1 year ago

Overview of the Issue

Unable to add more than 1 entry in the "rules.matches.method” Documentation states this should be provided as list, but only accepts a string.

See related Discuss

Reproduction Steps

  1. Deploy API Gateway. The following example HTTPRoute configuration only works for 1 x "method" (replace ns/svc as needed):
```yaml apiVersion: gateway.networking.k8s.io/v1beta1 kind: HTTPRoute metadata: name: whoami namespace: default spec: hostnames: [] parentRefs: - name: api-gateway rules: - matches: - method: HEAD path: type: PathPrefix value: /whoareyou headers: - name: Host type: RegularExpression value: "^localhost:.+$" backendRefs: - kind: Service name: whoami namespace: default port: 80 weight: 100 filters: - type: URLRewrite urlRewrite: path: type: ReplacePrefixMatch replacePrefixMatch: /whoami ```

Logs

kubectl output

```bash The HTTPRoute "whoami" is invalid: * spec.rules[1].matches[0].method: Invalid value: "array": spec.rules[1].matches[0].method in body must be of type string: "array" * spec.rules[1].matches[0].method: Unsupported value: []interface {}{"HEAD", "GET"}: supported values: "GET", "HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE", "PATCH" ```

Expected behavior

Able to add more than 1 method, as stated in documentation

Environment details

Additional Context

N/A

mikemorris commented 1 year ago

The way to configure the behavior you're describing would be to add multiple match clauses, as mentioned in https://developer.hashicorp.com/consul/docs/api-gateway/configuration/routes#rules-matches (the mention of a list in the docs you linked is inaccurate and should be updated).

Multiple matches are combined as any/OR, but within a single match, parameters are ANDed together, including in the array fields within a match, such as headers and queryParams, which is why method is signular as it would be inconsistent to support an OR-joined list. This is described in a bit more detail in the upstream Gateway API documentation:

Could you try applying the configuration below to see if it solves your use case?

``` apiVersion: gateway.networking.k8s.io/v1beta1 kind: HTTPRoute metadata: name: whoami namespace: default spec: hostnames: [] parentRefs: - name: api-gateway rules: - matches: - method: HEAD path: type: PathPrefix value: /whoareyou headers: - name: Host type: RegularExpression value: "^localhost:.+$" - method: GET path: type: PathPrefix value: /whoareyou headers: - name: Host type: RegularExpression value: "^localhost:.+$" backendRefs: - kind: Service name: whoami namespace: default port: 80 weight: 100 filters: - type: URLRewrite urlRewrite: path: type: ReplacePrefixMatch replacePrefixMatch: /whoami ```

This is admittedly a bit verbose, so you may want to consider some templating layer for generating this more concisely.

kds-rune commented 1 year ago

Yes, this worked. Thank you! I guess the documentation was/is a bit misleading 👍