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 dump upstream api #5259

Merged
merged 1 commit into from
Oct 19, 2021
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
54 changes: 52 additions & 2 deletions apisix/control/v1.lua
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ end

function _M.dump_all_routes_info()
local routes = get_routes()
local infos, _ = iter_add_get_routes_info(routes, nil)
local infos = iter_add_get_routes_info(routes, nil)
return 200, infos
end

Expand All @@ -193,7 +193,45 @@ function _M.dump_route_info()
return 200, route
end

local function iter_add_get_upstream_info(values, upstream_id)
juzhiyuan marked this conversation as resolved.
Show resolved Hide resolved
if not values then
return nil
end

local infos = {}
for _, upstream in core.config_util.iterate_values(values) do
local new_upstream = core.table.deepcopy(upstream)
core.table.insert(infos, new_upstream)
if new_upstream.value and new_upstream.value.parent then
new_upstream.value.parent = nil
end
-- check the upstream id
if upstream_id and upstream.value.id == upstream_id then
return new_upstream
end
end
if not upstream_id then
return infos
end
return nil
end

function _M.dump_all_upstreams_info()
local upstreams = get_upstreams()
local infos = iter_add_get_upstream_info(upstreams, nil)
return 200, infos
end

function _M.dump_upstream_info()
local upstreams = get_upstreams()
local uri_segs = core.utils.split_uri(ngx_var.uri)
local upstream_id = uri_segs[4]
local upstream = iter_add_get_upstream_info(upstreams, upstream_id)
if not upstream then
return 404, {error_msg = str_format("upstream[%s] not found", upstream_id)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a space like upstream [%s]?

Copy link
Contributor Author

@Xunzhuo Xunzhuo Oct 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this will be changed, the error msg from dump routes should be changed in this format too, so in my view, this is OK for me 😃

end
return 200, upstream
end

function _M.trigger_gc()
-- TODO: find a way to trigger GC in the stream subsystem
Expand Down Expand Up @@ -233,10 +271,22 @@ return {
uris = {"/routes"},
handler = _M.dump_all_routes_info,
},
--- /v1/
--- /v1/route/*
{
methods = {"GET"},
uris = {"/route/*"},
handler = _M.dump_route_info,
},
-- /v1/upstreams
{
methods = {"GET"},
uris = {"/upstreams"},
handler = _M.dump_all_upstreams_info,
},
-- /v1/upstream/*
{
methods = {"GET"},
uris = {"/upstream/*"},
handler = _M.dump_upstream_info,
}
}
80 changes: 80 additions & 0 deletions docs/en/latest/control-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,83 @@ Return specific route info with **route_id** in the format below:
"key": "/routes/1"
}
```

### Get /v1/upstreams

Introduced since `v2.11.0`.

Dump all upstreams in the format below:

```json
[
{
"value":{
"scheme":"http",
"pass_host":"pass",
"nodes":[
{
"host":"127.0.0.1",
"port":80,
"weight":1
},
{
"host":"foo.com",
"port":80,
"weight":2
}
],
"hash_on":"vars",
"update_time":1634543819,
"key":"remote_addr",
"create_time":1634539759,
"id":"1",
"type":"chash"
},
"has_domain":true,
"key":"\/apisix\/upstreams\/1",
"clean_handlers":{
},
"createdIndex":938,
"modifiedIndex":1225
}
]
```

### Get /v1/upstream/{upstream_id}

Introduced since `v2.11.0`.

Dump specific upstream info with **upstream_id** in the format below:

```json
{
"value":{
"scheme":"http",
"pass_host":"pass",
"nodes":[
{
"host":"127.0.0.1",
"port":80,
"weight":1
},
{
"host":"foo.com",
"port":80,
"weight":2
}
],
"hash_on":"vars",
"update_time":1634543819,
"key":"remote_addr",
"create_time":1634539759,
"id":"1",
"type":"chash"
},
"has_domain":true,
"key":"\/apisix\/upstreams\/1",
"clean_handlers":{
},
"createdIndex":938,
"modifiedIndex":1225
}
```
144 changes: 144 additions & 0 deletions t/control/upstreams.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#
# 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");

add_block_preprocessor(sub {
my ($block) = @_;

if (!$block->yaml_config) {
my $yaml_config = <<_EOC_;
apisix:
node_listen: 1984
config_center: yaml
enable_admin: false
_EOC_

$block->set_value("yaml_config", $yaml_config);
}

if (!$block->request) {
$block->set_value("request", "GET /t");
}
});

run_tests;

__DATA__

=== TEST 1: dump all upstreams
--- apisix_yaml
upstreams:
-
Xunzhuo marked this conversation as resolved.
Show resolved Hide resolved
id: 1
nodes:
"127.0.0.1:8001": 1
type: roundrobin
-
id: 2
nodes:
"127.0.0.1:8002": 1
type: roundrobin
#END
--- 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/upstreams',
ngx.HTTP_GET)
res = json.decode(res)
Xunzhuo marked this conversation as resolved.
Show resolved Hide resolved
if res[2] and table.getn(res) == 2 then
local data = {}
data.nodes = res[2].value.nodes
ngx.say(json.encode(data))
end
}
}
--- response_body
{"nodes":[{"host":"127.0.0.1","port":8002,"weight":1}]}



=== TEST 2: dump specific upstream with id 1
--- apisix_yaml
upstreams:
-
id: 1
nodes:
"127.0.0.1:8001": 1
type: roundrobin
-
id: 2
nodes:
"127.0.0.1:8002": 1
type: roundrobin
#END
--- 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/upstream/1',
ngx.HTTP_GET)
res = json.decode(res)
if res then
local data = {}
data.nodes = res.value.nodes
ngx.say(json.encode(data))
end
}
}
--- response_body
{"nodes":[{"host":"127.0.0.1","port":8001,"weight":1}]}



=== TEST 3: upstreams with invalid id
--- apisix_yaml
upstreams:
-
id: 1
nodes:
"127.0.0.1:8001": 1
type: roundrobin
-
id: 2
nodes:
"127.0.0.1:8002": 1
type: roundrobin
#END
--- 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/upstream/3',
ngx.HTTP_GET)
local data = {}
data.status = code
ngx.say(json.encode(data))
return
}
}
--- response_body
{"status":404}