使用 Lua 脚本通过其他 API 获取数据的 Envoy 过滤器

我有一个 Envoy 过滤器,在其中向每个 HTTP 请求添加一个头部。该头部的值来自 API。

让我们假设过滤器的两个配置。在下面的配置中,我添加了一个硬编码版本的头部。它在我的目标应用程序的日志中得到了验证,它可以工作。

apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: lua-filter
spec:
  configPatches:
  - applyTo: HTTP_FILTER
    match:
      context: ANY
      listener:
        portNumber: 7123
        filterChain:
          filter:
            name: "envoy.http_connection_manager"
            subFilter:
              name: "envoy.router"
    patch:
      operation: INSERT_BEFORE
      value:
       name: envoy.lua
       typed_config:
         "@type": "type.googleapis.com/envoy.config.filter.http.lua.v2.Lua"
         inlineCode: |
            function envoy_on_request(request_handle)
                request_handle:headers():add("authorization", "it works!")
            end

这次我希望从我的 API 中获取头部的值。不幸的是,这个设置不起作用,我不知道为什么。我已经在我的本地机器上检查了 Lua 脚本,脚本本身可行,但是一旦我将脚本提供给过滤器,就不会添加任何头部。

 typed_config:
     "@type": "type.googleapis.com/envoy.config.filter.http.lua.v2.Lua"
     inlineCode: |
        function envoy_on_request(request_handle)
          local http = require('socket.http')
          local json = require('json')
          local ltn12 = require "ltn12"
          local reqbody="my request body"
          local respbody = {}
          local  body, code, headers, status = http.request {
          method = "POST",
          url = "http://my-address",
          source = ltn12.source.string(reqbody),
          headers =
          {
          ["Accept"] = "*/*",
          ["Accept-Encoding"] = "gzip, deflate",
          ["Accept-Language"] = "en-us",
          ["Content-Type"] = "application/x-www-form-urlencoded",
          ["content-length"] = string.len(reqbody)
          },
          sink = ltn12.sink.table(respbody)
          }
          respbody = table.concat(respbody)
          parsed = json.decode(respbody)
          token = parsed["token-value"]
        request_handle:headers():add("authorization",token)
         end
点赞