Skip to content

Commit

Permalink
feat(core) enable plugin.run_on support on plugins iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
bungle committed Dec 10, 2018
1 parent 40a073e commit 115bc35
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 7 deletions.
29 changes: 22 additions & 7 deletions kong/runloop/plugins_iterator.lua
Expand Up @@ -45,16 +45,31 @@ local function load_plugin_configuration(ctx,
return ngx.exit(ngx.ERROR)
end

if plugin ~= nil and plugin.enabled then
local cfg = plugin.config or {}
if not plugin or not plugin.enabled then
return
end

cfg.api_id = plugin.api and plugin.api.id
cfg.route_id = plugin.route and plugin.route.id
cfg.service_id = plugin.service and plugin.service.id
cfg.consumer_id = plugin.consumer and plugin.consumer.id
if plugin.run_on ~= "all" then
if ctx.is_service_mesh_request then
if plugin.run_on == "first" then
return
end

return cfg
else
if plugin.run_on == "second" then
return
end
end
end

local cfg = plugin.config or {}

cfg.api_id = plugin.api and plugin.api.id
cfg.route_id = plugin.route and plugin.route.id
cfg.service_id = plugin.service and plugin.service.id
cfg.consumer_id = plugin.consumer and plugin.consumer.id

return cfg
end


Expand Down
119 changes: 119 additions & 0 deletions spec/02-integration/05-proxy/03-plugins_triggering_spec.lua
Expand Up @@ -27,6 +27,7 @@ for _, strategy in helpers.each_strategy() do
"keyauth_credentials",
}, {
"error-handler-log",
"error-generator-pre",
})

local consumer1 = bp.consumers:insert {
Expand Down Expand Up @@ -1000,5 +1001,123 @@ for _, strategy in helpers.each_strategy() do
assert.equal("timeout", res.headers["Log-Plugin-Service-Matched"])
end)
end)
describe("plugin with run_on", function()
lazy_setup(function()
if proxy_client then
proxy_client:close()
end
helpers.stop_kong()
db:truncate("routes")
db:truncate("services")
db:truncate("plugins")
do
local service = assert(bp.services:insert {
name = "example",
host = helpers.mock_upstream_host,
port = helpers.mock_upstream_port,
})
local route1 = assert(db.routes:insert {
hosts = { "run-on-first.org" },
protocols = { "http" },
service = service,
})
assert(bp.plugins:insert {
name = "error-generator-pre",
route = { id = route1.id },
config = {
access = true,
},
run_on = "first",
})
local route2 = assert(db.routes:insert {
hosts = { "run-on-second.org" },
protocols = { "http" },
service = service,
})
assert(bp.plugins:insert {
name = "error-generator-pre",
route = { id = route2.id },
config = {
access = true,
},
run_on = "second",
})
local route3 = assert(db.routes:insert {
hosts = { "run-on-all.org" },
protocols = { "http" },
service = service,
})
assert(bp.plugins:insert {
name = "error-generator-pre",
route = { id = route3.id },
config = {
access = true,
},
run_on = "all",
})
end
assert(helpers.start_kong {
database = strategy,
nginx_conf = "spec/fixtures/custom_nginx.template",
})
proxy_client = helpers.proxy_client()
end)
lazy_teardown(function()
if proxy_client then
proxy_client:close()
end
helpers.stop_kong()
end)
it("= first does get executed when running on first", function()
local res = assert(proxy_client:send {
method = "GET",
path = "/status/200",
headers = {
["Host"] = "run-on-first.org",
}
})
assert.res_status(500, res)
end)
it("= second does not get executed when running on first", function()
local res = assert(proxy_client:send {
method = "GET",
path = "/status/200",
headers = {
["Host"] = "run-on-second.org",
}
})
assert.res_status(200, res)
end)
it("= all does get executed when running on first", function()
local res = assert(proxy_client:send {
method = "GET",
path = "/status/200",
headers = {
["Host"] = "run-on-all.org",
}
})
assert.res_status(500, res)
end)
end)
end)
end

0 comments on commit 115bc35

Please sign in to comment.