Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(control): add plugins/reload to control api #10905

Merged
merged 10 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion apisix/control/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ local pairs = pairs
local type = type
local ngx = ngx
local get_method = ngx.req.get_method

local events = require("apisix.events")

local _M = {}

Expand Down Expand Up @@ -198,5 +198,15 @@ end

end -- do

local function reload_plugins()
core.log.info("start to hot reload plugins")
plugin_mod.load()
end


function _M.init_worker()
-- register reload plugin handler
events:register(reload_plugins, builtin_v1_routes.reload_event, "PUT")
end

return _M
18 changes: 18 additions & 0 deletions apisix/control/v1.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ local collectgarbage = collectgarbage
local ipairs = ipairs
local pcall = pcall
local str_format = string.format
local ngx = ngx
local ngx_var = ngx.var
local events = require("apisix.events")


local _M = {}

_M.RELOAD_EVENT = 'control-api-plugin-reload'

function _M.schema()
local http_plugins, stream_plugins = plugin.get_all({
Expand Down Expand Up @@ -400,6 +403,14 @@ function _M.dump_plugin_metadata()
return 200, metadata.value
end

function _M.post_reload_plugins()
local success, err = events:post(_M.RELOAD_EVENT, ngx.req.get_method(), ngx.time())
if not success then
core.response.exit(503, err)
end

core.response.exit(200, "done")
end

return {
-- /v1/schema
Expand Down Expand Up @@ -474,5 +485,12 @@ return {
uris = {"/plugin_metadata/*"},
handler = _M.dump_plugin_metadata,
},
-- /v1/plugins/reload
{
methods = {"PUT"},
uris = {"/plugins/reload"},
handler = _M.post_reload_plugins,
},
get_health_checkers = _get_health_checkers,
reload_event = _M.RELOAD_EVENT,
}
1 change: 1 addition & 0 deletions apisix/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ function _M.http_init_worker()
apisix_upstream.init_worker()
require("apisix.plugins.ext-plugin.init").init_worker()

control_api_router.init_worker()
local_conf = core.config.local_conf()

if local_conf.apisix and local_conf.apisix.enable_server_tokens == false then
Expand Down
10 changes: 10 additions & 0 deletions docs/en/latest/control-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -475,3 +475,13 @@ Dumps the metadata with the specified `plugin_name`:
"id": "file-logger"
}
```

### PUT /v1/plugins/reload

Introduced in [v3.9.0](https://github.com/apache/apisix/releases/tag/3.9.0)

Triggers a hot reload of the plugins.

```shell
curl "http://127.0.0.1:9090/v1/plugins/reload" -X PUT
```
Loading
Loading