Skip to content

Commit

Permalink
feat(control): expose services(#5271)
Browse files Browse the repository at this point in the history
  • Loading branch information
bisakhmondal committed Oct 20, 2021
1 parent 50fed63 commit e68e03f
Show file tree
Hide file tree
Showing 3 changed files with 319 additions and 2 deletions.
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:
-
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}

0 comments on commit e68e03f

Please sign in to comment.