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): expose services #5271

Merged
merged 6 commits into from
Oct 20, 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
53 changes: 51 additions & 2 deletions apisix/control/v1.lua
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ local function iter_add_get_routes_info(values, route_id)
new_route.value.upstream.parent = nil
end
core.table.insert(infos, new_route)
-- check the roude id
-- check the route id
if route_id and route.value.id == route_id then
return new_route
end
Expand Down Expand Up @@ -240,6 +240,43 @@ function _M.trigger_gc()
end


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

function _M.dump_all_services_info()
local services = get_services()
local infos = iter_add_get_services_info(services, nil)
return 200, infos
end

function _M.dump_service_info()
local services = get_services()
local uri_segs = core.utils.split_uri(ngx_var.uri)
local svc_id = uri_segs[4]
local info = iter_add_get_services_info(services, svc_id)
if not info then
return 404, {error_msg = str_format("service[%s] not found", svc_id)}
end
return 200, info
end


return {
-- /v1/schema
{
Expand Down Expand Up @@ -271,12 +308,24 @@ return {
uris = {"/routes"},
handler = _M.dump_all_routes_info,
},
--- /v1/route/*
-- /v1/route/*
{
methods = {"GET"},
uris = {"/route/*"},
handler = _M.dump_route_info,
},
-- /v1/services
{
methods = {"GET"},
uris = {"/services"},
handler = _M.dump_all_services_info
},
-- /v1/service/*
{
methods = {"GET"},
uris = {"/service/*"},
handler = _M.dump_service_info
},
-- /v1/upstreams
{
methods = {"GET"},
Expand Down
82 changes: 82 additions & 0 deletions docs/en/latest/control-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,88 @@ Return specific route info with **route_id** in the format below:
}
```

### Get /v1/services

Introduced since `v2.11`.

Return all services info in the format below:

```json
[
{
"has_domain": false,
"clean_handlers": {},
"modifiedIndex": 671,
"key": "/apisix/services/200",
"createdIndex": 671,
"value": {
"upstream": {
"scheme": "http",
"hash_on": "vars",
"pass_host": "pass",
"type": "roundrobin",
"nodes": [
{
"port": 80,
"weight": 1,
"host": "39.97.63.215"
}
]
},
"create_time": 1634552648,
"id": "200",
"plugins": {
"limit-count": {
"key": "remote_addr",
"time_window": 60,
"redis_timeout": 1000,
"allow_degradation": false,
"show_limit_quota_header": true,
"policy": "local",
"count": 2,
"rejected_code": 503
}
},
"update_time": 1634552648
}
}
]
```

### Get /v1/service/{service_id}

Introduced since `v2.11`.

Return specific service info with **service_id** in the format below:

```json
{
"has_domain": false,
"clean_handlers": {},
"modifiedIndex": 728,
"key": "/apisix/services/5",
"createdIndex": 728,
"value": {
"create_time": 1634554563,
"id": "5",
"upstream": {
"scheme": "http",
"hash_on": "vars",
"pass_host": "pass",
"type": "roundrobin",
"nodes": [
{
"port": 80,
"weight": 1,
"host": "39.97.63.215"
}
]
},
"update_time": 1634554563
}
}
```

### Get /v1/upstreams

Introduced since `v2.11.0`.
Expand Down
186 changes: 186 additions & 0 deletions t/control/services.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
#
# 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: services
--- apisix_yaml
services:
-
Copy link
Member

Choose a reason for hiding this comment

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

Let's test with multiple services

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, sounds good!

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

id: 200
upstream:
nodes:
"127.0.0.1:1980": 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/services',
ngx.HTTP_GET)
res = json.decode(res)
if res[1] then
local data = {}
data.id = res[1].value.id
data.plugins = res[1].value.plugins
data.upstream = res[1].value.upstream
ngx.say(json.encode(data))
end
return
}
}
--- response_body
{"id":"200","upstream":{"hash_on":"vars","nodes":[{"host":"127.0.0.1","port":1980,"weight":1}],"pass_host":"pass","scheme":"http","type":"roundrobin"}}
=== TEST 2: multiple services
--- apisix_yaml
services:
-
id: 200
upstream:
nodes:
"127.0.0.1:1980": 1
type: roundrobin
-
id: 201
upstream:
nodes:
"127.0.0.2:1980": 1
type: roundrobin
#END
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin")
local core = require("apisix.core")
local code, body, res = t.test('/v1/services',
ngx.HTTP_GET)
res = json.decode(res)
local g_data = {}
for _, r in core.config_util.iterate_values(res) do
local data = {}
data.id = r.value.id
data.plugins = r.value.plugins
data.upstream = r.value.upstream
core.table.insert(g_data, data)
end
ngx.say(json.encode(g_data))
return
}
}
--- response_body
[{"id":"200","upstream":{"hash_on":"vars","nodes":[{"host":"127.0.0.1","port":1980,"weight":1}],"pass_host":"pass","scheme":"http","type":"roundrobin"}},{"id":"201","upstream":{"hash_on":"vars","nodes":[{"host":"127.0.0.2","port":1980,"weight":1}],"pass_host":"pass","scheme":"http","type":"roundrobin"}}]
=== TEST 3: get service with id 5
--- apisix_yaml
services:
-
id: 5
plugins:
limit-count:
count: 2
time_window: 60
rejected_code: 503
key: remote_addr
upstream:
nodes:
"127.0.0.1:1980": 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/service/5',
ngx.HTTP_GET)
res = json.decode(res)
if res then
local data = {}
data.id = res.value.id
data.plugins = res.value.plugins
data.upstream = res.value.upstream
ngx.say(json.encode(data))
end
return
}
}
--- response_body
{"id":"5","plugins":{"limit-count":{"allow_degradation":false,"count":2,"key":"remote_addr","policy":"local","rejected_code":503,"show_limit_quota_header":true,"time_window":60}},"upstream":{"hash_on":"vars","nodes":[{"host":"127.0.0.1","port":1980,"weight":1}],"pass_host":"pass","scheme":"http","type":"roundrobin"}}
=== TEST 4: services with invalid id
--- apisix_yaml
services:
-
id: 1
upstream:
nodes:
"127.0.0.1:1980": 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/service/2',
ngx.HTTP_GET)
local data = {}
data.status = code
ngx.say(json.encode(data))
return
}
}
--- response_body
{"status":404}