Istio Ingress 正则表达式否定

Istio Ingress Regex Negation

我将 Kubernetes 与 Istio 一起使用,它带有流量管理功能。除前端服务外,所有后端 api 端点都以 /api/** 开头,后跟特定的 uri。前端服务没有任何通用 uri 前缀。

我想要实现的是在 istio VirtualService 中使用一个正则表达式,基本上说,如果请求的 uri 不是以 /api/ 开头,让它由前端服务提供服务。

这是我的VirtualService

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: {{ .Release.Name }}-frontend-ingress
  namespace: default
spec:
  hosts:
  {{ include "application.domain" . }}
  gateways:
  - iprocure-gateway
  http:
  - match:
    - uri:
        regex: '^(?!\/api\/).*'
    route:
    - destination:  
        host: {{ printf "%s.%s.svc.cluster.local" .Values.frontendService.serviceName .Release.Name }}
        port:
          number: {{ .Values.frontendService.service.port }}

我可以使用什么 regex 值来使所有不以 /api/ 开头的请求都由 frontend-service

提供

请尝试从其他 github 帖子中删除 regex: 字段中的单引号,它们未被使用。

Virtual Service uses ECMAscript style 因此,当您添加单引号时,它会逐字搜索它们之间的字符串

请参阅标题 "Regex engine changes" 下的 Istio 1.4.x upgrade notes。 Envoy 已移至不支持否定 look-ahead 的 Google Re2 "safe" 正则表达式引擎。 opt-out 通过环境变量到 Pilot 是可能的,但将在未来版本中删除。除了编写长正则表达式之外,我还没有找到 long-term 解决方案。

针对您的具体情况尝试

regex: "^/((a$)|(ap$)|(api$)|([^a].*)|(a[^p].*)|(ap[^i].*)|(api[^/].*))"