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 2 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
14 changes: 13 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,17 @@ end

end -- do

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

return 200, "done"
end


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

sheharyaar marked this conversation as resolved.
Show resolved Hide resolved
return _M
16 changes: 16 additions & 0 deletions apisix/control/v1.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ local ipairs = ipairs
local pcall = pcall
local str_format = string.format
local ngx_var = ngx.var
local events = require("apisix.events")


local _M = {}

_M.RELOAD_EVENT = "plugin-reload"

function _M.schema()
local http_plugins, stream_plugins = plugin.get_all({
Expand Down Expand Up @@ -400,6 +402,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 +484,11 @@ return {
uris = {"/plugin_metadata/*"},
handler = _M.dump_plugin_metadata,
},
-- /v1/plugins_reload
{
methods = {"POST"},
uris = {"/plugins_reload"},
starsz marked this conversation as resolved.
Show resolved Hide resolved
handler = _M.post_reload_plugins,
},
get_health_checkers = _get_health_checkers,
}
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
6 changes: 6 additions & 0 deletions docs/en/latest/control-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -475,3 +475,9 @@ Dumps the metadata with the specified `plugin_name`:
"id": "file-logger"
}
```

### POST /v1/plugins_reload
starsz marked this conversation as resolved.
Show resolved Hide resolved

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

Triggers a hot reload of the plugins.
starsz marked this conversation as resolved.
Show resolved Hide resolved
52 changes: 52 additions & 0 deletions t/control/plugins-reload.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
use t::APISIX 'no_plan';

repeat_each(1);
no_long_string();
no_root_location();
no_shuffle();
log_level("info");

our $yaml_config = <<_EOC_;
apisix:
enable_control: true
node_listen: 1984
_EOC_

run_tests();

__DATA__

=== TEST 1: test plugins-reload
--- yaml_config eval: $::yaml_config
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin")

local code, body, res = t.test('/v1/plugins_reload',
ngx.HTTP_POST)
ngx.say(res)
}
}
--- request
GET /t
--- error_code: 200
--- response_body
done
sheharyaar marked this conversation as resolved.
Show resolved Hide resolved
Loading