使用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()
相关推荐
codeejun2 天前
每日一Go-68、基于 Kind 的 Istio 本地实战(完整可跑)
golang·istio·kind
LCG元3 天前
Istio - 服务网格流量治理深度解析:灰度发布 / 故障注入配置实践
java·数据库·istio
切糕师学AI8 天前
Envoy 详解:云原生时代的高性能网络代理
网络·云原生·istio·网络代理·envoy·sidecar·网格服务
Gc9umsbL116 天前
Istio 架构全景解析:控制面 vs 数据面、核心组件与流量路径深度拆解
云原生·架构·istio
telllong1 个月前
3分钟理解服务网格Istio
云原生·istio
heimeiyingwang1 个月前
【架构实战】Service Mesh深度对比:Istio vs Linkerd
架构·istio·service_mesh
crossoverJie1 个月前
从企业版 Istio 迁移到社区版:一场给高速行驶汽车换轮胎的实践
云原生·汽车·istio
刘~浪地球1 个月前
云原生与容器--Service Mesh (Istio) 入门实战
云原生·istio·service_mesh
pl4H522a62 个月前
istio初探以及解决http-426的问题
http·kubernetes·istio
人间打气筒(Ada)2 个月前
go实战案例:如何通过 Service Meh 实现熔断和限流
java·开发语言·golang·web·istio·service mesh·熔断限流