Search code examples
kuberneteskubernetes-ingresskongkong-pluginkong-ingress

How can I use Kong’s Capturing Group in Ingress k8s object for rewirting logic?


I want to use Kong’s Capturing Group in Ingress k8s object to perform an uri rewriting. I want to implement the following logic: https://kong_host:30000/service/audits/health -> (rewrite) https://kong_host:30000/service/audit/v1/health

Ingress resource:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: audits
  annotations:
    konghq.com/plugins: audits-rewrite
spec:
  rules:
  - http:
      paths:
      - path: /service/audits/(?<path>\\\S+)
        backend:
          serviceName: audits
          servicePort: 8080

KongPlugin

apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
  name: audits-rewrite
config: 
  replace:
    uri: /service/audit/v1/$(uri_captures["path"])
plugin: request-transformer

Thanks.


Solution

  • As pointed in documentation you are not able to use v1beat1 ingress API version to capture groups in paths.

    https://docs.konghq.com/hub/kong-inc/request-transformer/#examples

    You need to upgrade you k8s cluster to 1.19 or higher version to use this feature.

    I also had similar problem and resolved it will following configuration:

    Ingress resource:

    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      name: audits
      annotations:
        konghq.com/plugins: audits-rewrite
    spec:
      rules:
      - http:
          paths:
          - path: /service/audits/(.*)
            backend:
              serviceName: audits
              servicePort: 8080
    

    KongPlugin

    apiVersion: configuration.konghq.com/v1
    kind: KongPlugin
    metadata:
      name: audits-rewrite
    config: 
      replace:
        uri: /service/audit/v1/$(uri_captures[1])
    plugin: request-transformer