From 0cbc83112bad56ad166de6437f805ae3ee66ab18 Mon Sep 17 00:00:00 2001 From: spacewander Date: Fri, 5 Aug 2022 15:02:53 +0800 Subject: [PATCH] change: Plugins from plugin_config should not override those in the route Signed-off-by: spacewander --- apisix/plugin_config.lua | 4 +- docs/en/latest/terminology/plugin-config.md | 4 +- .../architecture-design/plugin-config.md | 4 +- t/node/plugin-configs.t | 61 +++++++++++++++++++ 4 files changed, 68 insertions(+), 5 deletions(-) diff --git a/apisix/plugin_config.lua b/apisix/plugin_config.lua index 903ea6ec19132..cc5a6ff38456d 100644 --- a/apisix/plugin_config.lua +++ b/apisix/plugin_config.lua @@ -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 diff --git a/docs/en/latest/terminology/plugin-config.md b/docs/en/latest/terminology/plugin-config.md index 95d69df0cf0c6..02ee1106a9221 100644 --- a/docs/en/latest/terminology/plugin-config.md +++ b/docs/en/latest/terminology/plugin-config.md @@ -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 @@ -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 } diff --git a/docs/zh/latest/architecture-design/plugin-config.md b/docs/zh/latest/architecture-design/plugin-config.md index d3bac196e36e3..171cda39b0cad 100644 --- a/docs/zh/latest/architecture-design/plugin-config.md +++ b/docs/zh/latest/architecture-design/plugin-config.md @@ -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` 原有的插件。 举个例子: @@ -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 } diff --git a/t/node/plugin-configs.t b/t/node/plugin-configs.t index 770392276a779..d6c0cb75c7627 100644 --- a/t/node/plugin-configs.t +++ b/t/node/plugin-configs.t @@ -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