This is an automated email from the ASF dual-hosted git repository.

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 3c01202  fix(plugin_config): recover plugin when plugin_config changed 
(#4888)
3c01202 is described below

commit 3c01202d704722b97751f111af6e6bb656d56d63
Author: jackstraw <[email protected]>
AuthorDate: Mon Aug 30 10:54:32 2021 +0800

    fix(plugin_config): recover plugin when plugin_config changed (#4888)
    
    Co-authored-by: spacewander <[email protected]>
---
 apisix/plugin_config.lua | 11 ++++++--
 t/node/plugin-configs.t  | 73 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 82 insertions(+), 2 deletions(-)

diff --git a/apisix/plugin_config.lua b/apisix/plugin_config.lua
index 958ccae..903ea6e 100644
--- a/apisix/plugin_config.lua
+++ b/apisix/plugin_config.lua
@@ -52,11 +52,18 @@ function _M.merge(route_conf, plugin_config)
 
     if not route_conf.value.plugins then
         route_conf.value.plugins = {}
-    elseif not route_conf.orig_plugins then
+    end
+
+    if route_conf.orig_plugins then
+        -- recover
+        route_conf.value.plugins = route_conf.orig_plugins
+    else
+        -- backup in the first time
         route_conf.orig_plugins = route_conf.value.plugins
-        route_conf.value.plugins = core.table.clone(route_conf.value.plugins)
     end
 
+    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
     end
diff --git a/t/node/plugin-configs.t b/t/node/plugin-configs.t
index 9808738..16e3178 100644
--- a/t/node/plugin-configs.t
+++ b/t/node/plugin-configs.t
@@ -180,3 +180,76 @@ GET /t
 GET /t
 --- error_log
 property "block_rules" validation failed
+
+
+
+=== TEST 4: recover plugin when plugin_config changed
+--- 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": {
+                        "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": "/hello",
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    },
+                    "plugin_config_id": 1
+                }]]
+            )
+            if code > 300 then
+                ngx.log(ngx.ERR, err)
+                return
+            end
+            ngx.sleep(0.1)
+
+            local code, err, org_body = t('/hello')
+            if code > 300 then
+                ngx.log(ngx.ERR, err)
+                return
+            end
+            ngx.say(org_body)
+
+            local code, err = t('/apisix/admin/plugin_configs/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "plugins": {
+                    }
+                }]]
+            )
+            if code > 300 then
+                ngx.log(ngx.ERR, err)
+                return
+            end
+
+            local code, err, org_body = t('/hello')
+            if code > 300 then
+                ngx.log(ngx.ERR, err)
+                return
+            end
+            ngx.print(org_body)
+        }
+    }
+--- response_body
+hello
+hello world

Reply via email to