使用 Istio 的 (1.6.0) envoy lua 过滤器添加自定义响应头

我正在运行 Istio 1.6.0。 我想要为所有从我的服务发出的出站响应添加一些自定义标头。所以我尝试使用 envoyfilter 来实现。然而,我没有看到我的代理得到正确配置。

我正在尝试使用的 envoy 过滤器配置是

kind: EnvoyFilter
metadata:
  name: lua-filter
  namespace: istio-system
spec:
  workloadSelector:
    labels:
      istio: ingressgateway
  configPatches:
  - applyTo: HTTP_FILTER
    match:
      context: GATEWAY
      listener:
        filterChain:
          filter:
            name: "envoy.http_connection_manager"
            subFilter:
              name: "envoy.router"
    patch:
      operation: INSERT_BEFORE
      value:
       name: envoy.lua
       typed_config:
         "@type": "type.googleapis.com/envoy.config.filter.http.lua.v2.Lua"
         inlineCode: |
            function envoy_on_response(response_handle)
                response_handle:logInfo(" ========= XXXXX ========== ")
                response_handle:headers():add("X-User-Header", "worked")
            end

我确实在 istio-system 命名空间中运行着我的入口网关 pod

❯ kgp -l istio=ingressgateway -n istio-system
NAME                              READY   STATUS    RESTARTS   AGE
ingress-gateway-b4b5cffc9-wz75r   1/1     Running   0          3d12h
ingress-gateway-b4b5cffc9-znx9b   1/1     Running   0          28h

我希望能在 curl 我的服务时看到 X-User-Header。 不幸的是,我没有看到任何自定义标头。

我尝试检查 istio-system 中 ingress-gateway pod 的 proxy-configs,但我根本没有看到 envoy.lua 被配置。 我不确定我是否在正确地进行调试。

 istioctl proxy-config listener ingress-gateway-b4b5cffc9-wz75r.istio-system  -n istio-system --port 443 -o json | grep "name"
        "name": "0.0.0.0_443",
                        "name": "istio.stats",
                        "name": "envoy.tcp_proxy",
                        "name": "istio.stats",
                        "name": "envoy.tcp_proxy",
                "name": "envoy.listener.tls_inspector",

请让我知道我错过了什么或者配置不正确。 任何关于如何进一步调试的建议也会非常有帮助。

非常感谢。

点赞
用户11977760
用户11977760

根据我的 istio 集群版本 1.6.3 和 1.6.4 的检查,你的示例完全正常。请看下面来自我的集群的代码。

我使用 curl 进行了检查:

$ curl -s -I -X HEAD x.x.x.x/
HTTP/1.1 200 OK
server: istio-envoy
date: Mon, 06 Jul 2020 08:35:37 GMT
content-type: text/html
content-length: 13
last-modified: Thu, 02 Jul 2020 12:11:16 GMT
etag: "5efdcee4-d"
accept-ranges: bytes
x-envoy-upstream-service-time: 2
x-user-header: worked

并且

我使用 istio ingress-gateway pod 的 config_dump 进行了检查。

我在那里使用以下命令:

kubectl exec -ti istio-ingressgateway-78db9f457d-xfhl7  -n istio-system -- /bin/bash

从 config_dump 得到的结果:

curl 0:15000/config_dump | grep X-User-Header
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  128k    0  128k    0     0  9162k      0 --:--:-- --:--:-- --:--:-- 9162k
               "inline_code": "function envoy_on_response(response_handle)\n    response_handle:logInfo(\" ========= XXXXX ========== \")\n    response_handle:headers():add(\"X-User-Header\", \"worked\")\nend\n"

所以,正如您所看到的,它可以工作,请求已添加头,并且函数在 istio ingress gateway 中处于活动状态。


您是否可以尝试使用上述 curl 再次检查它,检查 istio ingress-gateway tcp_dump,然后让我知道它是否适用于您?

2020-07-06 08:43:59
用户13821160
用户13821160

更新示例筛选器(修复警告,更新版本)截至 Lua v3 和 Istio v1.10.3:

apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: my-test-filter
  namespace: istio-system
spec:
  workloadSelector:
    labels:
      istio: ingressgateway
  configPatches:
  - applyTo: HTTP_FILTER
    match:
      # 上下文省略以便适用于侧边车和网关
      listener:
        filterChain:
          filter:
            name: "envoy.filters.network.http_connection_manager"
            subFilter:
              name: "envoy.filters.http.router"
    patch:
      operation: INSERT_BEFORE
      value: # lua filter specification
        name: envoy.lua
        typed_config:
          "@type": "type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua"
          inlineCode: |
            function envoy_on_response(response_handle)
              response_handle:headers():add("my-oidc-endpoint", "http://localhost:8080/auth/realms/my-realm/.well-known/openid-configuration")
            end

2021-07-20 21:18:36