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 55afd7d76 feat: config center will check plugin_metadata (#7315)
55afd7d76 is described below
commit 55afd7d767effb62a9e988d4b1ac52b6c3cbeb6e
Author: fesily <[email protected]>
AuthorDate: Mon Jun 27 09:32:47 2022 +0800
feat: config center will check plugin_metadata (#7315)
---
apisix/plugin.lua | 75 +++++++++++++++++++++-------------
t/config-center-yaml/plugin-metadata.t | 24 ++++++++++-
2 files changed, 70 insertions(+), 29 deletions(-)
diff --git a/apisix/plugin.lua b/apisix/plugin.lua
index b0344eb09..d8f4d538c 100644
--- a/apisix/plugin.lua
+++ b/apisix/plugin.lua
@@ -41,7 +41,7 @@ local merged_route = core.lrucache.new({
ttl = 300, count = 512
})
local local_conf
-
+local check_plugin_metadata
local _M = {
version = 0.3,
@@ -635,7 +635,10 @@ function _M.init_worker()
end
local plugin_metadatas, err = core.config.new("/plugin_metadata",
- {automatic = true}
+ {
+ automatic = true,
+ checker = check_plugin_metadata
+ }
)
if not plugin_metadatas then
error("failed to create etcd instance for fetching /plugin_metadatas :
"
@@ -689,39 +692,55 @@ function _M.conf_version(conf)
end
-local function check_schema(plugins_conf, schema_type, skip_disabled_plugin)
- for name, plugin_conf in pairs(plugins_conf) do
- core.log.info("check plugin schema, name: ", name, ", configurations:
",
- core.json.delay_encode(plugin_conf, true))
- if type(plugin_conf) ~= "table" then
- return false, "invalid plugin conf " ..
- core.json.encode(plugin_conf, true) ..
- " for plugin [" .. name .. "]"
+local function check_single_plugin_schema(name, plugin_conf, schema_type,
skip_disabled_plugin)
+ core.log.info("check plugin schema, name: ", name, ", configurations: ",
+ core.json.delay_encode(plugin_conf, true))
+ if type(plugin_conf) ~= "table" then
+ return false, "invalid plugin conf " ..
+ core.json.encode(plugin_conf, true) ..
+ " for plugin [" .. name .. "]"
+ end
+
+ local plugin_obj = local_plugins_hash[name]
+ if not plugin_obj then
+ if skip_disabled_plugin then
+ return true
+ else
+ return false, "unknown plugin [" .. name .. "]"
end
+ end
- local plugin_obj = local_plugins_hash[name]
- if not plugin_obj then
- if skip_disabled_plugin then
- goto CONTINUE
- else
- return false, "unknown plugin [" .. name .. "]"
- end
+ if plugin_obj.check_schema then
+ local disable = plugin_conf.disable
+ plugin_conf.disable = nil
+
+ local ok, err = plugin_obj.check_schema(plugin_conf, schema_type)
+ if not ok then
+ return false, "failed to check the configuration of plugin "
+ .. name .. " err: " .. err
end
- if plugin_obj.check_schema then
- local disable = plugin_conf.disable
- plugin_conf.disable = nil
+ plugin_conf.disable = disable
+ end
- local ok, err = plugin_obj.check_schema(plugin_conf, schema_type)
- if not ok then
- return false, "failed to check the configuration of plugin "
- .. name .. " err: " .. err
- end
+ return true
+end
- plugin_conf.disable = disable
- end
- ::CONTINUE::
+check_plugin_metadata = function(item)
+ return check_single_plugin_schema(item.id, item,
+ core.schema.TYPE_METADATA, true)
+end
+
+
+
+local function check_schema(plugins_conf, schema_type, skip_disabled_plugin)
+ for name, plugin_conf in pairs(plugins_conf) do
+ local ok, err = check_single_plugin_schema(name, plugin_conf,
+ schema_type, skip_disabled_plugin)
+ if not ok then
+ return false, err
+ end
end
return true
diff --git a/t/config-center-yaml/plugin-metadata.t
b/t/config-center-yaml/plugin-metadata.t
index 0ad0c6c08..6e0a9971e 100644
--- a/t/config-center-yaml/plugin-metadata.t
+++ b/t/config-center-yaml/plugin-metadata.t
@@ -33,7 +33,7 @@ _EOC_
$block->set_value("yaml_config", $yaml_config);
- if (!$block->no_error_log) {
+ if (!$block->no_error_log && !$block->error_log) {
$block->set_value("no_error_log", "[error]");
}
});
@@ -67,3 +67,25 @@ plugin_metadata:
GET /hello
--- error_log
"remote_addr":"127.0.0.1"
+
+
+
+=== TEST 2: sanity
+--- apisix_yaml
+upstreams:
+ - id: 1
+ nodes:
+ "127.0.0.1:1980": 1
+ type: roundrobin
+routes:
+ -
+ uri: /hello
+ upstream_id: 1
+plugin_metadata:
+ - id: authz-casbin
+ model: 123
+#END
+--- request
+GET /hello
+--- error_log
+failed to check item data of [plugin_metadata]