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(traffic-split): the upstream pass_host needs to support IP mode #3870

Merged
merged 3 commits into from
Mar 29, 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
25 changes: 8 additions & 17 deletions apisix/plugins/traffic-split.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,27 +148,18 @@ end


local function set_pass_host(ctx, upstream_info, host)
-- Currently only supports a single upstream of the domain name.
-- When the upstream is `IP`, do not do any `pass_host` operation.
if not core.utils.parse_ipv4(host)
and not core.utils.parse_ipv6(host)
then
local pass_host = upstream_info.pass_host or "pass"
if pass_host == "pass" then
ctx.var.upstream_host = ctx.var.host
return
end

if pass_host == "rewrite" then
ctx.var.upstream_host = upstream_info.upstream_host
return
end
local pass_host = upstream_info.pass_host or "pass"
if pass_host == "pass" then
return
end

ctx.var.upstream_host = host
if pass_host == "rewrite" then
ctx.var.upstream_host = upstream_info.upstream_host
return
end

return
-- only support single node for `node` mode currently
ctx.var.upstream_host = host
end


Expand Down
209 changes: 209 additions & 0 deletions t/plugin/traffic-split2.t
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,212 @@ GET /server_port?name=jack&age=17
GET /server_port?name=jack&age=18
--- response_body chomp
1980



=== TEST 4: the upstream node is IP and pass_host is `pass`
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[=[{
"uri": "/uri",
"plugins": {
"traffic-split": {
"rules": [
{
"match": [
{
"vars": [["arg_name", "==", "jack"]]
}
],
"weighted_upstreams": [
{
"upstream": {
"type": "roundrobin",
"pass_host": "pass",
"nodes": {
"127.0.0.1:1981":1
}
}
}
]
}
]
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:1980": 1
}
}
}]=]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]



=== TEST 5: upstream_host is `127.0.0.1`
--- request
GET /uri?name=jack
--- more_headers
host: 127.0.0.1
--- response_body
uri: /uri
host: 127.0.0.1
x-real-ip: 127.0.0.1
--- no_error_log
[error]



=== TEST 6: the upstream node is IP and pass_host is `rewrite`
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PATCH,
[=[{
"uri": "/uri",
"plugins": {
"traffic-split": {
"rules": [
{
"match": [
{
"vars": [["arg_name", "==", "jack"]]
}
],
"weighted_upstreams": [
{
"upstream": {
"type": "roundrobin",
"pass_host": "rewrite",
"upstream_host": "test.com",
"nodes": {
"127.0.0.1:1981":1
}
}
}
]
}
]
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:1980": 1
}
}
}]=]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]



=== TEST 7: upstream_host is test.com
--- request
GET /uri?name=jack
--- response_body
uri: /uri
host: test.com
x-real-ip: 127.0.0.1
--- no_error_log
[error]



=== TEST 8: the upstream node is IP and pass_host is `node`
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PATCH,
[=[{
"uri": "/uri",
"plugins": {
"traffic-split": {
"rules": [
{
"match": [
{
"vars": [["arg_name", "==", "jack"]]
}
],
"weighted_upstreams": [
{
"upstream": {
"type": "roundrobin",
"pass_host": "node",
"nodes": {
"localhost:1981":1
}
}
}
]
}
]
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:1980": 1
}
}
}]=]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]



=== TEST 9: upstream_host is localhost
--- request
GET /uri?name=jack
--- more_headers
host: 127.0.0.1
--- response_body
uri: /uri
host: localhost
x-real-ip: 127.0.0.1
--- no_error_log
[error]