ingress-nginx-helm/rootfs/etc/nginx/lua/plugins.lua

62 lines
1.6 KiB
Lua
Raw Normal View History

local require = require
local ngx = ngx
local ipairs = ipairs
2019-02-25 02:32:17 +00:00
local string_format = string.format
local ngx_log = ngx.log
local INFO = ngx.INFO
local ERR = ngx.ERR
local pcall = pcall
2019-02-25 02:32:17 +00:00
local _M = {}
2020-06-10 09:36:56 +00:00
local MAX_NUMBER_OF_PLUGINS = 20
local plugins = {}
2019-02-25 02:32:17 +00:00
local function load_plugin(name)
local path = string_format("plugins.%s.main", name)
local ok, plugin = pcall(require, path)
if not ok then
ngx_log(ERR, string_format("error loading plugin \"%s\": %s", path, plugin))
return
end
local index = #plugins
if (plugin.name == nil or plugin.name == '') then
plugin.name = name
end
plugins[index + 1] = plugin
2019-02-25 02:32:17 +00:00
end
function _M.init(names)
2020-06-10 09:36:56 +00:00
local count = 0
2019-02-25 02:32:17 +00:00
for _, name in ipairs(names) do
2020-06-10 09:36:56 +00:00
if count >= MAX_NUMBER_OF_PLUGINS then
ngx_log(ERR, "the total number of plugins exceed the maximum number: ", MAX_NUMBER_OF_PLUGINS)
break
end
2019-02-25 02:32:17 +00:00
load_plugin(name)
2020-06-10 09:36:56 +00:00
count = count + 1 -- ignore loading failure, just count the total
2019-02-25 02:32:17 +00:00
end
end
function _M.run()
local phase = ngx.get_phase()
for _, plugin in ipairs(plugins) do
2019-02-25 02:32:17 +00:00
if plugin[phase] then
ngx_log(INFO, string_format("running plugin \"%s\" in phase \"%s\"", plugin.name, phase))
2019-02-25 02:32:17 +00:00
-- TODO: consider sandboxing this, should we?
-- probably yes, at least prohibit plugin from accessing env vars etc
-- but since the plugins are going to be installed by ingress-nginx
-- operator they can be assumed to be safe also
2019-02-25 02:32:17 +00:00
local ok, err = pcall(plugin[phase])
if not ok then
ngx_log(ERR, string_format("error while running plugin \"%s\" in phase \"%s\": %s",
plugin.name, phase, err))
2019-02-25 02:32:17 +00:00
end
end
end
end
return _M