使用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()
相关推荐
小北方城市网5 天前
第 5 课:服务网格(Istio)实战|大规模微服务的流量与安全治理体系
大数据·开发语言·人工智能·python·安全·微服务·istio
没有bug.的程序员5 天前
Istio 架构全景解析:控制面 vs 数据面、核心组件与流量路径深度拆解
微服务·云原生·架构·istio·架构设计·envoy·servicemesh
没有bug.的程序员5 天前
为什么会出现 Service Mesh:从 Spring Cloud 到 Sidecar 的演进逻辑
spring cloud·微服务·云原生·istio·service_mesh·架构演进·sidecar
虫小宝7 天前
导购返利APP服务网格实践:基于Istio的微服务流量管理与监控
微服务·云原生·istio
汪碧康13 天前
【k8s-1.34.2安装部署】十.gateway Api v1.4.0和istio安装
云原生·容器·kubernetes·gateway·istio·cilium·xkube
一起养小猫15 天前
【探索实战】Kurator统一流量治理深度实践:基于Istio的跨集群服务网格
java·云原生·istio
一起养小猫23 天前
【前瞻创想】Kurator生态创新展望:AI原生时代的多集群管理范式
云原生·华为云·istio·ai-native·kurator
一起养小猫23 天前
【贡献经历】从零到贡献者:我的Kurator开源社区参与之旅
分布式·物联网·云原生·开源·华为云·istio·kurator
古城小栈1 个月前
微服务网格:Istio 流量管理实战
微服务·架构·istio
聊询QQ:276998851 个月前
Matlab/Simulink 半车主动悬架建模:ADRC 与 PID 的碰撞之旅
istio