使用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()
相关推荐
techdashen21 天前
性能比拼: Linkerd vs. Istio
云原生·istio
zhojiew1 个月前
istio in action之服务网格和istio组件
云原生·istio
zhojiew1 个月前
istio in action之Gateway流量入口与安全
安全·gateway·istio
欧先生^_^1 个月前
深入理解 Istio v1.25.2
云原生·istio
zhojiew1 个月前
istio in action之流量控制与路由
java·数据库·istio
zhojiew1 个月前
Istio in action之Envoy Proxy详解
云原生·istio
程序媛学姐1 个月前
Spring Cloud与Service Mesh集成:Istio服务网格实践
spring cloud·istio·service_mesh
Sirius Wu2 个月前
Service Mesh 深度解析与 Istio+Envoy 实现方案
云原生·istio·service_mesh
lswzw2 个月前
istio 灰度实验
云原生·istio
AC在学习!2 个月前
安装istio遇到的事情
云原生·istio