Search code examples
istio

Istio VirtualService rule with header and url matching


How could I write rule for my VirtuelService such that traffic with url "/v1/myservice" and header "x-client-id: test" should route to "my-service-v2-dev", otherwise traffic with url "/v1/myservice" and with any header should route to "my-service-dev" Below is my code which is not working as expected and all traffic is going to "my-service-v2-dev".

Can anybody help me and let me know what mistake I am doing here? Thanks In advance.

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: my-public-dev
  namespace: my-dev
spec:
  gateways:
  - my-public-dev
  hosts:
  - my-public-dev.com.digital
  http:
  - match:
    - headers:
        x-client-id:
          exact: test
    - uri:
        prefix: /v1/myservice/
    name: myservice-v2-route
    route:
    - destination:
        host: my-service-v2-dev
        port:
          number: 8080
  - match:
    - uri:
        prefix: /v1/myservice/
    name: myservice-v1-route
    route:
    - destination:
        host: my-service-dev
        port:
          number: 8888

Solution

  • The match in the first route means you have a list of two conditions, combined with logical OR.. If you want to use AND, you have to move that to one condition, which can include a header and a uri condition and is combined with AND.

    See

    https://istio.io/latest/docs/reference/config/networking/virtual-service/#HTTPMatchRequest

    https://istio.io/latest/docs/reference/config/networking/virtual-service/#HTTPRoute

    (in response to comments) For more complex matching, you can split the conditions with logical operations, e.g. first match url1 AND header, second match, url2 AND header, third url1, fourth url2, fifth catch all for the rest.

    Or match the urls with a Regex, so multiple URLs could also be represented by that Regex.