Consul ingress controller regex rewrite

Sorry-I take liberty in posing a hypothetical rewrite situation

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-fanout-namespace-xyz
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  namespace: namespace-xyz
spec:
  ingressClassName: nginx
  rules:
    - http:
        paths:
          - path: /analytics/spark/master(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: spark-master-svc
                port:
                  number: 80
          - path: /analytics/jupyter/lab(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: jupyter-proxy-public
                port:
                  number: 80

Question:

  1. Does consul-ingress support rewrites like above?
  2. Does newly published api-gw supports them as well?

Have we got rich documentation with some examples?

Thank you.

1 Like

Hi @vchaudh3,

Consul’s ingress gateway and service mesh sidecars do not currently support configuring regex rewrites in Envoy, although I think this would be a worthwhile feature to support.

In order to implement similar functionality with the string replacement functionality of the prefix rewrite feature, you would need to use the following configuration.

---
apiVersion: consul.hashicorp.com/v1alpha1
kind: ServiceRouter
metadata:
  name: ingress-fanout-virtual-service
spec:
  routes:
    - match:
        http:
          pathExact: /analytics/spark/master
      destination:
        service: spark-master-svc
        prefixRewrite: /
    - match:
        http:
          pathPrefix: /analytics/spark/master/
      destination:
        service: spark-master-svc
        # This will replace '/analytics/spark/master/' in the path with '/'.
        # Any values proceeding the trailing slash will remain in the path.
        # For example, a request to  '/analytics/spark/master/foo' will be written to '/foo'
        prefixRewrite: /
    - match:
        http:
          pathExact: /analytics/jupyter/lab
      destination:
        service: jupyter-proxy-public
        prefixRewrite: /
    - match:
        http:
          pathPrefix: /analytics/jupyter/lab/
      destination:
        service: jupyter-proxy-public
        prefixRewrite: /

---
apiVersion: consul.hashicorp.com/v1alpha1
kind: IngressGateway
metadata:
  name: ingress-fanout-gateway
spec:
  listeners:
    - port: 80
      protocol: http
      services:
        - name: ingress-fanout-virtual-service

Envoy’s documentation for the prefix_rewrite parameter explains the reason that multiple match directives are required.

I’m not sure whether Consul’s API Gateway currently supports using regular expressions to rewrite paths. In looking at the code, it appears it might support regex path matching, but not rewriting. My colleague @Jeff-Apple can confirm these details.