通过遵循特使过滤器拦截上游响应,我能够创建特使过滤器,它也部分工作。我可以看到我的https请求正在被特使过滤器解释,并且可以看到更新的响应被打印在日志中。
然而,在邮差中,我没有得到响应,它无限期地等待。访问日志显示正在记录400响应。想知道为什么邮递员没有收到响应体。
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
name: test-http-filter
namespace: test
spec:
workloadSelector:
labels:
app: istio
configPatches:
- applyTo: HTTP_FILTER
match:
context: GATEWAY
listener:
filterChain:
filter:
name: envoy.filters.network.http_connection_manager
subfilter:
name: envoy.filters.http.router
patch:
operation: INSERT_AFTER # INSER_BEFORE also tried
value:
name: envoy.custom-resp
typed_config:
"@type": "type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua"
inlineCode: |
function envoy_on_response(response_handle)
if response_handle:headers():get(":status") == "404" or response_handle:headers():get(":status") == "400" then
local body = response_handle:body()
local jsonString = tostring(body:getBytes(0, body:length()))
response_handle:logErr("Status: "..response_handle:headers():get(":status"))
response_handle:logErr("JSONString: "..jsonString)
jsonString = jsonString:gsub("mystring", "myUpdatedstring")
response_handle:body():setBytes(jsonString)
end
end
有人在response_bandle:body():setBytes()
中遇到过类似的问题吗?原来的帖子有response_handle():set()
,在我的情况下抛出错误。
请将补丁操作从" operation: INSERT_AFTER"操作:insert_before;在发送响应后应用过滤器时。更新envoy过滤器如下
patch:
operation: INSERT_BEFORE
修正特使过滤器
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
name: test-http-filter
namespace: istio-system
spec:
workloadSelector:
labels:
istio: ingressgateway
configPatches:
- applyTo: HTTP_FILTER
match:
context: GATEWAY
listener:
filterChain:
filter:
name: envoy.filters.network.http_connection_manager
patch:
operation: INSERT_BEFORE
value:
name: envoy.lua
typed_config:
"@type": "type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua"
inlineCode: |
function envoy_on_response(response_handle)
if response_handle:headers():get(":status") == "404" or response_handle:headers():get(":status") == "400" then
local body = response_handle:body()
local jsonString = tostring(body:getBytes(0, body:length()))
response_handle:logErr("Status: "..response_handle:headers():get(":status"))
response_handle:logErr("JSONString: "..jsonString)
jsonString = jsonString:gsub("mystring", "myUpdatedstring")
response_handle:body():setBytes(jsonString)
end
end