How to add multiple request matcher and rewrite under virtualservice (istio)?

7.1k views Asked by At

I am using istio and Kubernetes for my development. I have searched many article and post but not found the expected answer. Below is my virtual service script.

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  annotations:
    helm.fluxcd.io/antecedent: default:helmrelease/hello-11
  creationTimestamp: "2020-10-15T11:52:02Z"
  generation: 2
  name: hello-11
  namespace: default
  resourceVersion: "4178089"
  selfLink: /apis/networking.istio.io/v1beta1/namespaces/default/virtualservices/hello-11
  uid: kfk9580d-0fedc-11eb-b8a2-pp155d6a1e13
spec:
  gateways:
  - istio-system/istio-ingressgateway
  - istio-system/istio-ingressgateway
  hosts:
  - ext-auth-host
  - no-ext-auth-host
  http:
  - gateways:
    - istio-system/istio-ingressgateway
    match:
    - headers:
        x-jwt-extracted-xx-id:
          exact: "9980098"
    name: hello-11-ext-auth
    route:
    - destination:
        host: hello-11.default.svc.cluster.local
  - gateways:
    - istio-system/istio-ingressgateway
    headers:
      request:
        add:
          x-jwt-extracted-xx-id: "9980098"
    match:
    - uri:
        prefix: /9980098/api/show    
    name: hello-11-no-ext-auth
    rewrite:
        uri: /api/show
    route:
    - destination:
        host: hello-11.default.svc.cluster.local

I want to add multiple matches along with multiple rewrites. But, it's not working as expected. My expected virtual service should looks like below.

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  annotations:
    helm.fluxcd.io/antecedent: default:helmrelease/hello-11
  creationTimestamp: "2020-10-15T11:52:02Z"
  generation: 2
  name: hello-11
  namespace: default
  resourceVersion: "4178089"
  selfLink: /apis/networking.istio.io/v1beta1/namespaces/default/virtualservices/hello-11
  uid: kfk9580d-0fedc-11eb-b8a2-pp155d6a1e13
spec:
  gateways:
  - istio-system/istio-ingressgateway
  - istio-system/istio-ingressgateway
  hosts:
  - ext-auth-host
  - no-ext-auth-host
  http:
  - gateways:
    - istio-system/istio-ingressgateway
    match:
    - headers:
        x-jwt-extracted-xx-id:
          exact: "9980098"
    name: hello-11-ext-auth
    route:
    - destination:
        host: hello-11.default.svc.cluster.local
  - gateways:
    - istio-system/istio-ingressgateway
    headers:
      request:
        add:
          x-jwt-extracted-xx-id: "9980098"
    match:
    - uri:
        prefix: /9980098/api/show    
    name: hello-11-no-ext-auth
    rewrite:
        uri: /api/show
    route:
    - destination:
        host: hello-11.default.svc.cluster.local
    match:
    - uri:
        prefix: /9980098/api/send    
    name: hello-11-no-ext-auth
    rewrite:
        uri: /api/send
    route:
    - destination:
        host: hello-11.default.svc.cluster.local

The multiple url rewrites are :

uri: /api/send
uri: /api/show

Any suggestions or references would be helpful and thankful.

Thanks

1

There are 1 answers

0
Jakub On

It's hard to say what's wrong here, for me the whole virtual service is written incorrectly. I would start with fixing multiple issues in your virtual service, like multiple gateways or name in http section.


Follow below docs and examples to fix your virtual service.


apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews-route
spec:
  gateways:  
  - mygateway
  http:
  - name: "example-1"  
    match:
    - uri:
        prefix: "/wpcatalog"
    rewrite:
      uri: "/newcatalog"
    route:
    - destination:
        host: reviews


  - name: "example-2"  
    match:
    - headers:
        end-user:
          exact: jason   
    route:
    - destination:
        host: reviews
  

  - name: "example-3"
    headers:
      response:
        add:
          foo: "bar"
    match:
    - uri:
        prefix: /a
    rewrite:
      uri: /
    route:
    - destination:
        host: reviews