如何在 Ingress k8s 对象中使用 Kong 的捕获组来重新编写逻辑?

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

我想在Ingress k8s对象中使用Kong的Capturing Group来进行uri重写。 我想实现以下逻辑: https://kong_host:30000/service/audits/health -> (重写)https://kong_host:30000/service/audit/v1/health

入口资源:

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

Kong插件

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

谢谢。

正如文档中所指出的,您无法使用 v1beat1 入口 API 版本来捕获路径中的组。

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

您需要将 k8s 集群升级到 1.19 或更高版本才能使用此功能。

我也遇到了类似的问题,按照配置解决了:

入口资源:

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

Kong插件

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