0

I was waiting on the Regex Rewrite based on Capture Groups for a long time and it was finally merged in 1.19.4. I cannot find much documentation regarding how to use the uriRegexRewrite and my attempts to make it work all failed so far.

I need the VirtualService to capture something like /captcha-srv(/|$)(.*) and rewrite it to /$2

My current Virtual Service looks like this:

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  labels:
    app.kubernetes.io/instance: authserver-webapi
  name: authserver-webapi
  namespace: istio-full
spec:
  gateways:
  - authserver-webapi
  hosts:
  - auth.example.com
  http:
  - match:
    - uri:
        regex: /captcha-srv(/|$)(.*)
    rewrite:
      uri: /
    route:
    - destination:
        host: captcha
        port:
          number: 80
  - route:
    - destination:
        host: authserver-webapi
        port:
          number: 80

This setup rewrites whatever you write after captcha-srv to my microservice's / however, I need it to send /captcha-srv to / and e.g. /captcha-srv/v1 to /v1. In Nginx Ingress, this is easily done, but apparently, this feature was recently added to Istio, and using the documentation wasn't much help except that the directive used to achieve this is uriRegexrewrite.

Any help?

Sinux
  • 31
  • 1
  • 6

1 Answers1

3

To answer my question, to achieve my goal the correct way of writing a virtualservice according to the Istio reference documentation is as below:

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  labels:
    app.kubernetes.io/instance: authserver-webapi
  name: authserver-webapi
  namespace: istio-full
spec:
  gateways:
  - authserver-webapi
  hosts:
  - auth.example.com
  http:
  - match:
    - uri:
        regex: /captcha-srv(/|$)(.*)
    rewrite:
      uriRegexRewrite:
        match: /captcha-srv(/|$)(.*)
        rewrite: /\2
    route:
    - destination:
        host: captcha
        port:
          number: 80
  - match:
    - uri:
        prefix: /
  - route:
    - destination:
        host: authserver-webapi
        port:
          number: 80

It's essential to know how to read and understand the reference manuals in such cases, e.g. in this case HTTPRewite is a subsection of virtualservice and therefore has an indentation, then, if you use the uriRegexRewrite you'll have to refer to the RegexRewrite section and use the match and rewrite directives with an indentation.

Sinux
  • 31
  • 1
  • 6