fix: add missing labels after merging route and service (#6177)

This commit is contained in:
tzssangglass 2022-01-21 17:21:20 +08:00 committed by GitHub
parent 8114ca5688
commit 800b890f8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 127 additions and 0 deletions

View File

@ -466,6 +466,10 @@ local function merge_service_route(service_conf, route_conf)
new_conf.value.host = route_conf.value.host
end
if route_conf.value.labels then
new_conf.value.labels = route_conf.value.labels
end
-- core.log.info("merged conf : ", core.json.delay_encode(new_conf))
return new_conf
end

123
t/node/merge-route.t vendored
View File

@ -424,3 +424,126 @@ GET /t
passed
--- no_error_log
[error]
=== TEST 18: labels exist if only route
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"plugins": {
"serverless-pre-function": {
"phase": "rewrite",
"functions" : ["return function(conf, ctx)
local core = require(\"apisix.core\");
ngx.say(core.json.encode(ctx.matched_route.value.labels));
end"]
}
},
"labels": {
"version": "v2"
},
"uri": "/hello"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]
=== TEST 19: hit routes
--- request
GET /hello
--- response_body
{"version":"v2"}
--- no_error_log
[error]
=== TEST 20: labels exist if merge route and service
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/services/1',
ngx.HTTP_PUT,
[[{
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
}
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.sleep(0.6) -- wait for sync
code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"serverless-pre-function": {
"phase": "rewrite",
"functions" : ["return function(conf, ctx)
local core = require(\"apisix.core\");
ngx.say(core.json.encode(ctx.matched_route.value.labels));
end"]
}
},
"labels": {
"version": "v2"
},
"uri": "/hello",
"service_id": 1
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]
=== TEST 21: hit routes
--- request
GET /hello
--- response_body
{"version":"v2"}
--- no_error_log
[error]