使用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()
相关推荐
昕光xg4 天前
Istio笔记04-基于Jaeger的分布式链路追踪
笔记·分布式·istio
运维大师19 天前
【云原生与DevOps】07-Istio服务网格落地:从试点到全量的踩坑记录
云原生·istio·devops
heimeiyingwang22 天前
【架构实战】API网关设计与演进:从Nginx到自研网关
架构·istio·service_mesh
IT策士2 个月前
第49篇 k8s之服务网格入门:Istio 简介
容器·kubernetes·istio
Waay2 个月前
从 0 到 1 实操 K8s Gateway API+Istio:告别 Ingress,用新标准实现域名访问
kubernetes·gateway·istio
codeejun2 个月前
每日一Go-68、基于 Kind 的 Istio 本地实战(完整可跑)
golang·istio·kind
LCG元2 个月前
Istio - 服务网格流量治理深度解析:灰度发布 / 故障注入配置实践
java·数据库·istio
切糕师学AI2 个月前
Envoy 详解:云原生时代的高性能网络代理
网络·云原生·istio·网络代理·envoy·sidecar·网格服务
Gc9umsbL12 个月前
Istio 架构全景解析:控制面 vs 数据面、核心组件与流量路径深度拆解
云原生·架构·istio
telllong3 个月前
3分钟理解服务网格Istio
云原生·istio