Skip to content

Commit

Permalink
change: Plugins from plugin_config should not override those in the r…
Browse files Browse the repository at this point in the history
…oute (apache#7614)

Signed-off-by: spacewander <[email protected]>
  • Loading branch information
spacewander authored and Liu-Junlin committed Nov 4, 2022
1 parent 1ad8166 commit 3c4b2c0
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 6 deletions.
4 changes: 3 additions & 1 deletion apisix/plugin_config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ function _M.merge(route_conf, plugin_config)
route_conf.value.plugins = core.table.clone(route_conf.value.plugins)

for name, value in pairs(plugin_config.value.plugins) do
route_conf.value.plugins[name] = value
if not route_conf.value.plugins[name] then
route_conf.value.plugins[name] = value
end
end

route_conf.update_count = route_conf.update_count + 1
Expand Down
4 changes: 2 additions & 2 deletions docs/en/latest/terminology/plugin-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f13

When APISIX can't find the Plugin Config with the `id`, the requests reaching this Route are terminated with a status code of 503.

If a Route already has the `plugins` field configured, the plugins in the Plugin Config will effectively be merged to it. The same plugin in the Plugin Config will override the ones configured directly in the Route.
If a Route already has the `plugins` field configured, the plugins in the Plugin Config will effectively be merged to it. The same plugin in the Plugin Config will not override the ones configured directly in the Route.

For example, if we configure a Plugin Config as shown below

Expand Down Expand Up @@ -135,7 +135,7 @@ the effective configuration will be as the one shown below:
"host": "apisix.iresty.com"
},
"limit-count": {
"count": 2,
"count": 20,
"time_window": 60,
"rejected_code": 503
}
Expand Down
4 changes: 2 additions & 2 deletions docs/zh/latest/architecture-design/plugin-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ $ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f
如果找不到对应的 Plugin config,该路由上的请求会报 503 错误。

如果这个路由已经配置了 `plugins`,那么 Plugin config 里面的插件配置会合并进去。
相同的插件会覆盖掉 `plugins` 原有的插件。
相同的插件不会覆盖掉 `plugins` 原有的插件。

举个例子:

Expand Down Expand Up @@ -131,7 +131,7 @@ $ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f
"host": "apisix.iresty.com"
},
"limit-count": {
"count": 2,
"count": 20,
"time_window": 60,
"rejected_code": 503
}
Expand Down
2 changes: 1 addition & 1 deletion t/config-center-yaml/plugin-configs.t
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ routes:
--- request
GET /echo
--- response_body
hello
world
--- response_headers
in: out
--- error_log eval
Expand Down
61 changes: 61 additions & 0 deletions t/node/plugin-configs.t
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,64 @@ property "block_rules" validation failed
--- response_body
hello
hello world
=== TEST 5: don't override the plugin in the route
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, err = t('/apisix/admin/plugin_configs/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-rewrite": {
"uri": "/hello"
},
"response-rewrite": {
"body": "hello"
}
}
}]]
)
if code > 300 then
ngx.log(ngx.ERR, err)
return
end
local code, err = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"uri": "/helloaa",
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"plugin_config_id": 1,
"plugins": {
"response-rewrite": {
"body": "world"
}
}
}]]
)
if code > 300 then
ngx.log(ngx.ERR, err)
return
end
ngx.sleep(0.1)
local code, err, org_body = t('/helloaa')
if code > 300 then
ngx.log(ngx.ERR, err)
return
end
ngx.say(org_body)
}
}
--- response_body
world

0 comments on commit 3c4b2c0

Please sign in to comment.