使用envoyfilter添加请求头

该envoyfilter实现了这样一个功能,如果请求头中含有Sw8,则添加请求头HasSw8: true。

1. 内嵌lua脚本

apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: add-header-filter
  namespace: demo-bookinfo  # 可根据实际情况调整命名空间
spec:
  workloadSelector:
    # 应用到的工作负载,若要应用到所有工作负载,可省略 workloadSelector
    labels:
      app: servicea
  configPatches:
    - applyTo: HTTP_FILTER
      match:
        context: SIDECAR_INBOUND  # 应用到入方向流量
        proxy:
          proxyVersion: '^1\.19.*'  # 匹配特定版本的 Istio
        listener:
          filterChain:
            filter:
              name: "envoy.filters.network.http_connection_manager"
              subFilter:
                name: "envoy.filters.http.router"
      patch:
        operation: INSERT_BEFORE
        value:
          name: envoy.filters.http.lua
          typed_config:
            "@type": "type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua"
            inlineCode: |
              function envoy_on_request(request_handle)
                  request_handle:headers():add("Example-Header", "default-value")
                  local sw8_header = request_handle:headers():get("Sw8")
                  if sw8_header then
                      request_handle:headers():add("HasSw8", "true")
                  end
              end

2. 引入外部文件

先将lua脚本文件放到istio-proxy容器指定目录下(可以通过configmap)。

lua

-- 文件路径: /etc/envoy/lua/add_header.lua

function envoy_on_request(request_handle)
    -- 添加一个默认的 header,例如 Example-Header: default-value
    request_handle:headers():add("Example-Header", "default-value")
    
    -- 检查是否存在 Sw8 header
    local sw8_header = request_handle:headers():get("Sw8")
    if sw8_header then
        -- 如果存在 Sw8,添加 HasSw8: true
        request_handle:headers():add("HasSw8", "true")
    end
end

envoyfilter:

apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: lua-header-filter
  namespace: demo-bookinfo
spec:
  workloadSelector:
    labels:
      app: servicea
  configPatches:
    - applyTo: HTTP_FILTER
      match:
        context: SIDECAR_INBOUND  # 流量方向:入方向
        listener:
          filterChain:
            filter:
              name: "envoy.filters.network.http_connection_manager"
              subFilter:
                name: "envoy.filters.http.router"
      patch:
        operation: INSERT_BEFORE
        value:
          name: envoy.filters.http.lua
          typed_config:
            "@type": type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua
            inline_code: |
              -- 加载外部 Lua 文件
              local lua_script = assert(loadfile("/var/lua/add_header.lua"))
              lua_script()
相关推荐
运维开发王义杰1 天前
Kubernetes:EKS 中 Istio Ingress Gateway 负载均衡器配置及常见问题解析
kubernetes·gateway·istio
桂月二二4 天前
基于Istio Ambient Mesh的无边车架构:实现零侵入式服务网格的云原生革命
云原生·架构·istio
小诸葛的博客1 个月前
istio-proxy oom问题排查步骤
云原生·istio
小诸葛的博客1 个月前
istio-proxy内存指标
云原生·istio
小诸葛的博客1 个月前
istio-proxy日志字段解释
云原生·istio
liuxuzxx2 个月前
Istio-2:流量治理之简单负载均衡
云原生·kubernetes·istio
liuxuzxx2 个月前
1.24.1-Istio安装
kubernetes·istio·service mesh
小诸葛的博客2 个月前
istio-proxy不打印访问日志怎么解决?
云原生·istio
知本知至2 个月前
istio配置重复的svc报错
k8s·istio