Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
Signed-off-by: yiyiyimu <wosoyoung@gmail.com>
  • Loading branch information
Yiyiyimu committed Jan 25, 2021
2 parents c4d02b5 + 8439c8d commit b1c90ef
Show file tree
Hide file tree
Showing 11 changed files with 1,119 additions and 11 deletions.
13 changes: 11 additions & 2 deletions apisix/admin/ssl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function _M.delete(id)
end


function _M.patch(id, conf)
function _M.patch(id, conf, sub_path)
if not id then
return 400, {error_msg = "missing route id"}
end
Expand Down Expand Up @@ -206,7 +206,16 @@ function _M.patch(id, conf)
local node_value = res_old.body.node.value
local modified_index = res_old.body.node.modifiedIndex

node_value = core.table.merge(node_value, conf);
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
else
node_value = core.table.merge(node_value, conf);
end


utils.inject_timestamp(node_value, nil, conf)

Expand Down
64 changes: 60 additions & 4 deletions apisix/plugins/fault-injection.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
-- limitations under the License.
--
local core = require("apisix.core")
local expr = require("resty.expr.v1")

local sleep = core.sleep
local random = math.random
local ipairs = ipairs

local plugin_name = "fault-injection"

Expand All @@ -30,15 +32,23 @@ local schema = {
properties = {
http_status = {type = "integer", minimum = 200},
body = {type = "string", minLength = 0},
percentage = {type = "integer", minimum = 0, maximum = 100}
percentage = {type = "integer", minimum = 0, maximum = 100},
vars = {
type = "array",
maxItems = 20
}
},
required = {"http_status"},
},
delay = {
type = "object",
properties = {
duration = {type = "number", minimum = 0},
percentage = {type = "integer", minimum = 0, maximum = 100}
percentage = {type = "integer", minimum = 0, maximum = 100},
vars = {
type = "array",
maxItems = 20
}
},
required = {"duration"},
}
Expand All @@ -64,24 +74,70 @@ local function sample_hit(percentage)
end


local function vars_match(vars, ctx)
local match_result
for _, var in ipairs(vars) do
local expr, _ = expr.new(var)
match_result = expr:eval(ctx.var)
if match_result then
break
end
end

return match_result
end


function _M.check_schema(conf)
local ok, err = core.schema.check(schema, conf)
if not ok then
return false, err
end

if conf.abort and conf.abort.vars then
for _, var in ipairs(conf.abort.vars) do
local _, err = expr.new(var)
if err then
core.log.error("failed to create vars expression: ", err)
return false, err
end
end
end

if conf.delay and conf.delay.vars then
for _, var in ipairs(conf.delay.vars) do
local _, err = expr.new(var)
if err then
core.log.error("failed to create vars expression: ", err)
return false, err
end
end
end

return true
end


function _M.rewrite(conf, ctx)
core.log.info("plugin rewrite phase, conf: ", core.json.delay_encode(conf))

if conf.delay and sample_hit(conf.delay.percentage) then
local abort_vars = true
if conf.abort and conf.abort.vars then
abort_vars = vars_match(conf.abort.vars, ctx)
end
core.log.info("abort_vars: ", abort_vars)

local delay_vars = true
if conf.delay and conf.delay.vars then
delay_vars = vars_match(conf.delay.vars, ctx)
end
core.log.info("delay_vars: ", delay_vars)

if conf.delay and sample_hit(conf.delay.percentage) and delay_vars then
sleep(conf.delay.duration)
end

if conf.abort and sample_hit(conf.abort.percentage) then
if conf.abort and sample_hit(conf.abort.percentage) and abort_vars then
return conf.abort.http_status, core.utils.resolve_var(conf.abort.body, ctx.var)
end
end
Expand Down
3 changes: 1 addition & 2 deletions conf/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ apisix:
admin_key:
- name: "admin"
# yamllint disable rule:comments-indentation
key: edd1c9f034335f136f87ad84b625c8f1
# using fixed API token has security risk, please update it when you deploy to production environment
key: edd1c9f034335f136f87ad84b625c8f1 # using fixed API token has security risk, please update it when you deploy to production environment
# yamllint enable rule:comments-indentation
role: admin
Loading

0 comments on commit b1c90ef

Please sign in to comment.