From a5108d2fe23f457d0841622f03878756d63ad3dc Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Fri, 21 Jan 2022 10:58:49 +0800 Subject: [PATCH] fix: add missing labels after merging route and service Closes #6169 Signed-off-by: tzssangglass --- apisix/plugin.lua | 4 ++ t/node/merge-route.t | 123 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) diff --git a/apisix/plugin.lua b/apisix/plugin.lua index 7864a0084ddd..67815440454e 100644 --- a/apisix/plugin.lua +++ b/apisix/plugin.lua @@ -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 diff --git a/t/node/merge-route.t b/t/node/merge-route.t index 72991f07ccb5..b6f1d467e8d5 100644 --- a/t/node/merge-route.t +++ b/t/node/merge-route.t @@ -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]