Skip to content
Closed
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
64 changes: 64 additions & 0 deletions apisix/admin/stream_routes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,39 @@ local function check_conf(id, conf, need_id, schema, opts)
end
end

local superior_id = conf.protocol and conf.protocol.superior_id
if superior_id and not opts.skip_references_check then
local key = "/stream_routes/" .. superior_id
local res, err = core.etcd.get(key)
if not res then
return nil, {error_msg = "failed to fetch superior stream route info by "
.. "superior id [" .. superior_id .. "]: "
.. err}
end

if res.status ~= 200 then
return nil, {error_msg = "failed to fetch superior stream route info by "
.. "superior id [" .. superior_id .. "], "
.. "response code: " .. res.status}
end

if res.body and res.body.node and res.body.node.value then
local superior_route = res.body.node.value

if not superior_route.protocol or not superior_route.protocol.name then
return nil, {error_msg = "superior stream route [" .. superior_id .. "] "
.. "does not have a valid protocol configuration"}
end

if conf.protocol.name ~= superior_route.protocol.name then
return nil, {error_msg = "protocol name mismatch: subordinate route has protocol "
.. "[" .. conf.protocol.name .. "] but superior route "
.. "[" .. superior_id .. "] has protocol "
.. "[" .. superior_route.protocol.name .. "]"}
end
end
end

local ok, err = stream_route_checker(conf, true)
if not ok then
return nil, {error_msg = err}
Expand All @@ -69,11 +102,42 @@ local function check_conf(id, conf, need_id, schema, opts)
end



local function delete_checker(id)
local key = "/stream_routes"
local res, err = core.etcd.get(key, true)
if not res then
core.log.error("failed to fetch stream routes from etcd: ", err)
return nil, nil
end

if res.status ~= 200 then
return nil, nil
end

if res.body and res.body.list then
for _, item in ipairs(res.body.list) do
if item and item.value and item.value.protocol
and item.value.protocol.superior_id
and tostring(item.value.protocol.superior_id) == id then
return 400, {error_msg = "can not delete this stream route directly, "
.. "subordinate route [" .. item.value.id .. "] "
.. "is still using it now"}
end
end
end

return true
end



return resource.new({
name = "stream_routes",
kind = "stream route",
schema = core.schema.stream_route,
checker = check_conf,
delete_checker = delete_checker,
unsupported_methods = { "patch" },
list_filter_fields = {
service_id = true,
Expand Down
200 changes: 200 additions & 0 deletions t/admin/stream-routes.t
Original file line number Diff line number Diff line change
Expand Up @@ -651,3 +651,203 @@ passed
GET /t
--- response_body
passed



=== TEST 18: cleanup before subordinate route tests
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, message = t('/apisix/admin/stream_routes/1', ngx.HTTP_DELETE)
ngx.say("[delete] code: ", code, " message: cleanup ", message)
}
}
--- request
GET /t
--- response_body
[delete] code: 200 message: cleanup passed



=== TEST 19: set subordinate route with non-existent superior_id
--- extra_yaml_config
xrpc:
protocols:
- name: pingpong
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/stream_routes/1',
ngx.HTTP_PUT,
[[{
"protocol": {
"name": "pingpong",
"superior_id": 999
},
"upstream": {
"nodes": {
"127.0.0.1:1995": 1
},
"type": "roundrobin"
}
}]]
)
ngx.status = code
ngx.say(body)
}
}
--- request
GET /t
--- error_code: 400
--- response_body eval
qr/failed to fetch superior stream route info by superior id \[999\]/



=== TEST 20: set subordinate route with mismatched protocol name
--- extra_yaml_config
xrpc:
protocols:
- name: pingpong
- name: redis
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/stream_routes/1',
ngx.HTTP_PUT,
[[{
"protocol": {
"name": "pingpong"
},
"upstream": {
"nodes": {
"127.0.0.1:1995": 1
},
"type": "roundrobin"
}
}]]
)

if code >= 300 then
ngx.status = code
ngx.say("Failed to create superior route: ", body)
return
end

local code, body = t('/apisix/admin/stream_routes/2',
ngx.HTTP_PUT,
[[{
"protocol": {
"name": "redis",
"superior_id": 1
},
"upstream": {
"nodes": {
"127.0.0.1:1995": 1
},
"type": "roundrobin"
}
}]]
)

if code >= 300 then
ngx.print(body)
return
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body_like eval
qr/protocol name mismatch/



=== TEST 21: set valid subordinate route with matching protocol
--- extra_yaml_config
xrpc:
protocols:
- name: pingpong
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/stream_routes/2',
ngx.HTTP_PUT,
[[{
"protocol": {
"name": "pingpong",
"superior_id": 1
},
"upstream": {
"nodes": {
"127.0.0.1:1996": 1
},
"type": "roundrobin"
}
}]]
)

if code >= 300 then
ngx.status = code
ngx.say("Failed to create subordinate route: ", body)
return
end
ngx.say("subordinate route created")
}
}
--- request
GET /t
--- response_body
subordinate route created



=== TEST 22: delete superior route with existing subordinate
--- extra_yaml_config
xrpc:
protocols:
- name: pingpong
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/stream_routes/1', ngx.HTTP_DELETE)

if code >= 300 then
ngx.status = code
ngx.print(body)
return
end
ngx.say("Unexpected success deleting superior route")
}
}
--- request
GET /t
--- error_code: 400
--- response_body
{"error_msg":"can not delete this stream route directly, subordinate route [2] is still using it now"}



=== TEST 23: delete subordinate and superior route
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, message = t('/apisix/admin/stream_routes/2', ngx.HTTP_DELETE)
ngx.say("[delete] code: ", code, " message: subordinate route [2] ", message)

local code, message = t('/apisix/admin/stream_routes/1', ngx.HTTP_DELETE)
ngx.say("[delete] code: ", code, " message: superior route [1] ", message)
}
}
--- request
GET /t
--- response_body
[delete] code: 200 message: subordinate route [2] passed
[delete] code: 200 message: superior route [1] passed
Loading