Skip to content

Commit

Permalink
feat(control): add hosts/reload to control api
Browse files Browse the repository at this point in the history
  • Loading branch information
louxiu authored and louyl committed Nov 5, 2024
1 parent 45679ae commit d7c8580
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
9 changes: 7 additions & 2 deletions apisix/control/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,15 @@ local function reload_plugins()
plugin_mod.load()
end

local function reload_hosts()
core.log.info("start to hot reload hosts")
core.resolver.reload_hosts()
end


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

return _M
34 changes: 31 additions & 3 deletions apisix/control/v1.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ local events = require("apisix.events")

local _M = {}

_M.RELOAD_EVENT = 'control-api-plugin-reload'
_M.RELOAD_PLUGINS_EVENT = 'control-api-plugins-reload'
_M.RELOAD_HOSTS_EVENT = 'control-api-hosts-reload'

function _M.schema()
local http_plugins, stream_plugins = plugin.get_all({
Expand Down Expand Up @@ -404,14 +405,28 @@ function _M.dump_plugin_metadata()
end

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

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

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

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

function _M.get_hosts()
local hosts = core.resolver.get_hosts()
return 200, hosts
end

return {
-- /v1/schema
{
Expand Down Expand Up @@ -491,6 +506,19 @@ return {
uris = {"/plugins/reload"},
handler = _M.post_reload_plugins,
},
-- /v1/hosts/reload
{
methods = {"PUT"},
uris = {"/hosts/reload"},
handler = _M.post_reload_hosts,
},
-- /v1/hosts/reload
{
methods = {"GET"},
uris = {"/hosts"},
handler = _M.get_hosts,
},
get_health_checkers = _get_health_checkers,
reload_event = _M.RELOAD_EVENT,
reload_plugins_event = _M.RELOAD_PLUGINS_EVENT,
reload_hosts_event = _M.RELOAD_HOSTS_EVENT,
}
10 changes: 10 additions & 0 deletions apisix/core/resolver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ local function init_hosts_ip()
end


function _M.reload_hosts()
init_hosts_ip()
end


function _M.get_hosts()
return HOSTS_IP_MATCH_CACHE
end


function _M.init_resolver(args)
-- initialize /etc/hosts
init_hosts_ip()
Expand Down
8 changes: 8 additions & 0 deletions docs/en/latest/control-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -485,3 +485,11 @@ Triggers a hot reload of the plugins.
```shell
curl "http://127.0.0.1:9090/v1/plugins/reload" -X PUT
```

### PUT /v1/hosts/reload

Triggers a hot reload of the hosts.

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

0 comments on commit d7c8580

Please sign in to comment.