Skip to content

Commit

Permalink
Merge branch 'upstream/master' into github/master
Browse files Browse the repository at this point in the history
* upstream/master:
  feat(elasticsearch-logger): support multi elasticsearch endpoints (apache#8604)
  chore: use operator # instead of string.len (apache#8751)
  chore: hi 2023 (apache#8748)
  refactor(admin): stream_routes/upstreams/protos/services/global_rules/consumer_groups/plugin_configs (apache#8661)
  feat: support send error-log to kafka brokers (apache#8693)
  chore: upgrade `casbin` to `1.41.5` (apache#8744)
  • Loading branch information
hongbinhsu committed Feb 3, 2023
2 parents 2e3529a + 0e2b925 commit 93942f5
Show file tree
Hide file tree
Showing 22 changed files with 625 additions and 985 deletions.
2 changes: 1 addition & 1 deletion NOTICE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Apache APISIX
Copyright 2019-2022 The Apache Software Foundation
Copyright 2019-2023 The Apache Software Foundation

This product includes software developed at
The Apache Software Foundation (http://www.apache.org/).
155 changes: 13 additions & 142 deletions apisix/admin/consumer_group.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,15 @@
--
local core = require("apisix.core")
local consumers = require("apisix.consumer").consumers
local utils = require("apisix.admin.utils")
local resource = require("apisix.admin.resource")
local schema_plugin = require("apisix.admin.plugins").check_schema
local type = type
local tostring = tostring
local ipairs = ipairs


local _M = {
need_v3_filter = true,
}


local function check_conf(id, conf, need_id)
if not conf then
return nil, {error_msg = "missing configurations"}
end

id = id or conf.id
if need_id and not id then
return nil, {error_msg = "missing id"}
end

if not need_id and id then
return nil, {error_msg = "wrong id, do not need it"}
end

if need_id and conf.id and tostring(conf.id) ~= tostring(id) then
return nil, {error_msg = "wrong id"}
end

conf.id = id

core.log.info("conf: ", core.json.delay_encode(conf))
local ok, err = core.schema.check(core.schema.consumer_group, conf)
local function check_conf(id, conf, need_id, schema)
local ok, err = core.schema.check(schema, conf)
if not ok then
return nil, {error_msg = "invalid configuration: " .. err}
end
Expand All @@ -63,50 +38,7 @@ local function check_conf(id, conf, need_id)
end


function _M.put(id, conf)
local ok, err = check_conf(id, conf, true)
if not ok then
return 400, err
end

local key = "/consumer_groups/" .. id

local ok, err = utils.inject_conf_with_prev_conf("consumer_group", key, conf)
if not ok then
return 503, {error_msg = err}
end

local res, err = core.etcd.set(key, conf)
if not res then
core.log.error("failed to put consumer group[", key, "]: ", err)
return 503, {error_msg = err}
end

return res.status, res.body
end


function _M.get(id)
local key = "/consumer_groups"
if id then
key = key .. "/" .. id
end
local res, err = core.etcd.get(key, not id)
if not res then
core.log.error("failed to get consumer group[", key, "]: ", err)
return 503, {error_msg = err}
end

utils.fix_count(res.body, id)
return res.status, res.body
end


function _M.delete(id)
if not id then
return 400, {error_msg = "missing consumer group id"}
end

local function delete_checker(id)
local consumers, consumers_ver = consumers()
if consumers_ver and consumers then
for _, consumer in ipairs(consumers) do
Expand All @@ -120,76 +52,15 @@ function _M.delete(id)
end
end

local key = "/consumer_groups/" .. id
local res, err = core.etcd.delete(key)
if not res then
core.log.error("failed to delete consumer group[", key, "]: ", err)
return 503, {error_msg = err}
end


return res.status, res.body
end


function _M.patch(id, conf, sub_path)
if not id then
return 400, {error_msg = "missing consumer group id"}
end

if not conf then
return 400, {error_msg = "missing new configuration"}
end

if not sub_path or sub_path == "" then
if type(conf) ~= "table" then
return 400, {error_msg = "invalid configuration"}
end
end

local key = "/consumer_groups/" .. id
local res_old, err = core.etcd.get(key)
if not res_old then
core.log.error("failed to get consumer group [", key, "]: ", err)
return 503, {error_msg = err}
end

if res_old.status ~= 200 then
return res_old.status, res_old.body
end
core.log.info("key: ", key, " old value: ",
core.json.delay_encode(res_old, true))

local node_value = res_old.body.node.value
local modified_index = res_old.body.node.modifiedIndex

if sub_path and sub_path ~= "" then
local code, err, node_val = core.table.patch(node_value, sub_path, conf)
node_value = node_val
if code then
return code, err
end
utils.inject_timestamp(node_value, nil, true)
else
node_value = core.table.merge(node_value, conf)
utils.inject_timestamp(node_value, nil, conf)
end

core.log.info("new conf: ", core.json.delay_encode(node_value, true))

local ok, err = check_conf(id, node_value, true)
if not ok then
return 400, err
end

local res, err = core.etcd.atomic_set(key, node_value, nil, modified_index)
if not res then
core.log.error("failed to set new consumer group[", key, "]: ", err)
return 503, {error_msg = err}
end

return res.status, res.body
return nil, nil
end


return _M
return resource.new({
name = "consumer_groups",
kind = "consumer group",
schema = core.schema.consumer_group,
checker = check_conf,
unsupported_methods = {"post"},
delete_checker = delete_checker
})
155 changes: 10 additions & 145 deletions apisix/admin/global_rules.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,12 @@
-- limitations under the License.
--
local core = require("apisix.core")
local utils = require("apisix.admin.utils")
local resource = require("apisix.admin.resource")
local schema_plugin = require("apisix.admin.plugins").check_schema
local type = type
local tostring = tostring


local _M = {
version = 0.1,
need_v3_filter = true,
}


local function check_conf(id, conf, need_id)
if not conf then
return nil, {error_msg = "missing configurations"}
end

id = id or conf.id
if need_id and not id then
return nil, {error_msg = "missing route id"}
end

if not need_id and id then
return nil, {error_msg = "wrong route id, do not need it"}
end

if need_id and conf.id and tostring(conf.id) ~= tostring(id) then
return nil, {error_msg = "wrong route id"}
end

conf.id = id

core.log.info("schema: ", core.json.delay_encode(core.schema.global_rule))
core.log.info("conf : ", core.json.delay_encode(conf))
local ok, err = core.schema.check(core.schema.global_rule, conf)
local function check_conf(id, conf, need_id, schema)
local ok, err = core.schema.check(schema, conf)
if not ok then
return nil, {error_msg = "invalid configuration: " .. err}
end
Expand All @@ -63,116 +34,10 @@ local function check_conf(id, conf, need_id)
end


function _M.put(id, conf)
local ok, err = check_conf(id, conf, true)
if not ok then
return 400, err
end

local key = "/global_rules/" .. id

local ok, err = utils.inject_conf_with_prev_conf("global_rule", key, conf)
if not ok then
return 503, {error_msg = err}
end

local res, err = core.etcd.set(key, conf)
if not res then
core.log.error("failed to put global rule[", key, "]: ", err)
return 503, {error_msg = err}
end

return res.status, res.body
end


function _M.get(id)
local key = "/global_rules"
if id then
key = key .. "/" .. id
end
local res, err = core.etcd.get(key, not id)
if not res then
core.log.error("failed to get global rule[", key, "]: ", err)
return 503, {error_msg = err}
end

utils.fix_count(res.body, id)
return res.status, res.body
end


function _M.delete(id)
local key = "/global_rules/" .. id
-- core.log.info("key: ", key)
local res, err = core.etcd.delete(key)
if not res then
core.log.error("failed to delete global rule[", key, "]: ", err)
return 503, {error_msg = err}
end

return res.status, res.body
end


function _M.patch(id, conf, sub_path)
if not id then
return 400, {error_msg = "missing global rule id"}
end

if not conf then
return 400, {error_msg = "missing new configuration"}
end

if not sub_path or sub_path == "" then
if type(conf) ~= "table" then
return 400, {error_msg = "invalid configuration"}
end
end

local key = "/global_rules/" .. id
local res_old, err = core.etcd.get(key)
if not res_old then
core.log.error("failed to get global rule [", key, "]: ", err)
return 503, {error_msg = err}
end

if res_old.status ~= 200 then
return res_old.status, res_old.body
end
core.log.info("key: ", key, " old value: ",
core.json.delay_encode(res_old, true))

local node_value = res_old.body.node.value
local modified_index = res_old.body.node.modifiedIndex

if sub_path and sub_path ~= "" then
local code, err, node_val = core.table.patch(node_value, sub_path, conf)
node_value = node_val
if code then
return code, err
end
utils.inject_timestamp(node_value, nil, true)
else
node_value = core.table.merge(node_value, conf)
utils.inject_timestamp(node_value, nil, conf)
end

core.log.info("new conf: ", core.json.delay_encode(node_value, true))

local ok, err = check_conf(id, node_value, true)
if not ok then
return 400, err
end

local res, err = core.etcd.atomic_set(key, node_value, nil, modified_index)
if not res then
core.log.error("failed to set new global rule[", key, "]: ", err)
return 503, {error_msg = err}
end

return res.status, res.body
end


return _M
return resource.new({
name = "global_rules",
kind = "global rule",
schema = core.schema.global_rule,
checker = check_conf,
unsupported_methods = {"post"}
})
12 changes: 11 additions & 1 deletion apisix/admin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,17 @@ local function run()
end

local code, data
if seg_res == "routes" then
local refactored_resources = {
"routes",
"stream_routes",
"upstreams",
"protos",
"global_rules",
"services",
"consumer_groups",
"plugin_configs",
}
if core.table.array_find(refactored_resources, seg_res) then
code, data = resource[method](resource, seg_id, req_body, seg_sub_path, uri_args)
else
code, data = resource[method](seg_id, req_body, seg_sub_path, uri_args)
Expand Down
Loading

0 comments on commit 93942f5

Please sign in to comment.