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

时间:2020-07-04 08:40:19

标签: kubernetes lua istio envoyproxy

我正在运行Istio 1.6.0。 我想向来自我的服务的所有出站响应添加一些自定义标头。因此,我试图使用lua envoyfilter来实现这一目标。但是,我看不到代理配置正确。

我要使用的特使过滤器配置是

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

我的入口网关Pod确实在istio-system名称空间中运行

❯ 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

我希望在卷曲服务时会看到X-User-Header。 不幸的是,我没有看到任何自定义标题。

我尝试检查istio系统中入口网关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",

请让我知道我缺少什么或配置错误。 关于如何进一步调试的任何建议也将非常有帮助。

非常感谢您。

2 个答案:

答案 0 :(得分:1)

据我检查版本1.6.3和1.6.4的istio群集时,您的示例工作正常。看看下面来自我集群的代码。

我用卷发检查了

$ 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

AND

我在istio入口网关容器中使用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入口网关中处于活动状态。


您能否尝试再次检查上述卷曲,检查istio ingress-gateway tcp_dump并让我知道它是否对您有用?

答案 1 :(得分:0)

从 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:
      # context omitted so that this applies to both sidecars and gateways
      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