From 0d0e297b95117da0bf0bf45d465a6aeed631a0c5 Mon Sep 17 00:00:00 2001 From: Alex Zhang Date: Sat, 24 Apr 2021 14:10:53 +0800 Subject: [PATCH 1/4] feat: support to use upstream_id in stream_route --- apisix/init.lua | 61 ++++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/apisix/init.lua b/apisix/init.lua index ccae9b5b7898..4b8dbcdd23be 100644 --- a/apisix/init.lua +++ b/apisix/init.lua @@ -279,6 +279,30 @@ local function set_upstream_host(api_ctx) end +local function get_upstream_by_id(up_id) + local upstreams = core.config.fetch_created_obj("/upstreams") + if upstreams then + local upstream = upstreams:get(tostring(up_id)) + if not upstream then + core.log.error("failed to find upstream by id: " .. up_id) + return ngx_exit(1) + end + + if upstream.has_domain then + local err + upstream, err = parse_domain_in_up(upstream) + if err then + core.log.error("failed to get resolved upstream: ", err) + return ngx_exit(1) + end + end + + core.log.info("parsed upstream: ", core.json.delay_encode(upstream)) + return upstream.dns_value or upstream.value + end +end + + function _M.http_access_phase() local ngx_ctx = ngx.ctx @@ -410,30 +434,12 @@ function _M.http_access_phase() end if up_id then - local upstreams = core.config.fetch_created_obj("/upstreams") - if upstreams then - local upstream = upstreams:get(tostring(up_id)) - if not upstream then - core.log.error("failed to find upstream by id: " .. up_id) - return core.response.exit(502) - end - - if upstream.has_domain then - local err - upstream, err = parse_domain_in_up(upstream) - if err then - core.log.error("failed to get resolved upstream: ", err) - return core.response.exit(500) - end - end - - if upstream.value.pass_host then - api_ctx.pass_host = upstream.value.pass_host - api_ctx.upstream_host = upstream.value.upstream_host - end + local upstream = get_upstream_by_id(up_id) + api_ctx.matched_upstream = upstream - core.log.info("parsed upstream: ", core.json.delay_encode(upstream)) - api_ctx.matched_upstream = upstream.dns_value or upstream.value + if upstream and upstream.value.pass_host then + api_ctx.pass_host = upstream.value.pass_host + api_ctx.upstream_host = upstream.value.upstream_host end else @@ -783,11 +789,18 @@ function _M.stream_preread_phase() return ngx_exit(1) end + + local up_id = matched_route.value.upstream_id + if up_id then + api_ctx.matched_upstream = get_upstream_by_id(up_id) + else + api_ctx.matched_upstream = matched_route.value.upstream + end + local plugins = core.tablepool.fetch("plugins", 32, 0) api_ctx.plugins = plugin.stream_filter(matched_route, plugins) -- core.log.info("valid plugins: ", core.json.delay_encode(plugins, true)) - api_ctx.matched_upstream = matched_route.value.upstream api_ctx.conf_type = "stream/route" api_ctx.conf_version = matched_route.modifiedIndex api_ctx.conf_id = matched_route.value.id From 396fcb9f6494529bc257481100a5d92969eb4afc Mon Sep 17 00:00:00 2001 From: Alex Zhang Date: Tue, 27 Apr 2021 14:03:58 +0800 Subject: [PATCH 2/4] fix --- apisix/init.lua | 9 +++--- t/stream-node/sanity.t | 67 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/apisix/init.lua b/apisix/init.lua index 4b8dbcdd23be..142ca6d14547 100644 --- a/apisix/init.lua +++ b/apisix/init.lua @@ -126,7 +126,7 @@ function _M.http_init_worker() end require("apisix.debug").init_worker() - require("apisix.upstream").init_worker() + apisix_upstream.init_worker() local_conf = core.config.local_conf() @@ -437,9 +437,9 @@ function _M.http_access_phase() local upstream = get_upstream_by_id(up_id) api_ctx.matched_upstream = upstream - if upstream and upstream.value.pass_host then - api_ctx.pass_host = upstream.value.pass_host - api_ctx.upstream_host = upstream.value.upstream_host + if upstream and upstream.pass_host then + api_ctx.pass_host = upstream.pass_host + api_ctx.upstream_host = upstream.upstream_host end else @@ -755,6 +755,7 @@ function _M.stream_init_worker() plugin.init_worker() router.stream_init_worker() + apisix_upstream.init_worker() if core.config == require("apisix.core.config_yaml") then core.config.init_worker() diff --git a/t/stream-node/sanity.t b/t/stream-node/sanity.t index 7eaf06e0bb50..cd4e0197de74 100644 --- a/t/stream-node/sanity.t +++ b/t/stream-node/sanity.t @@ -163,3 +163,70 @@ GET /t passed --- no_error_log [error] + + + +=== TEST 7: set upstream (id: 1) +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/upstreams/1', + ngx.HTTP_PUT, + [[{ + "nodes": { + "127.0.0.1:1995": 1 + }, + "type": "roundrobin" + }]] + ) + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 8: set stream route (id: 1) which uses upstream_id +--- 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, + [[{ + "remote_addr": "127.0.0.1", + "upstream_id": "1" + }]] + ) + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 9: hit route +--- stream_enable +--- stream_request eval +mmm +--- stream_response +hello world +--- no_error_log +[error] From 8c5ec20c99b7fa1cd1968773ed4757ea99b1c46a Mon Sep 17 00:00:00 2001 From: Alex Zhang Date: Wed, 28 Apr 2021 09:39:47 +0800 Subject: [PATCH 3/4] fix --- apisix/init.lua | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/apisix/init.lua b/apisix/init.lua index 142ca6d14547..2ee002154f04 100644 --- a/apisix/init.lua +++ b/apisix/init.lua @@ -48,7 +48,8 @@ end local load_balancer local local_conf local dns_resolver -local ver_header = "APISIX/" .. core.version.VERSION +local ver_header = "APISIX/" .. core.version.VERSION +local is_http = ngx.config.subsystem == "http" local function parse_args(args) @@ -285,6 +286,10 @@ local function get_upstream_by_id(up_id) local upstream = upstreams:get(tostring(up_id)) if not upstream then core.log.error("failed to find upstream by id: " .. up_id) + if is_http then + return core.response.exit(502) + end + return ngx_exit(1) end @@ -293,6 +298,10 @@ local function get_upstream_by_id(up_id) upstream, err = parse_domain_in_up(upstream) if err then core.log.error("failed to get resolved upstream: ", err) + if is_http then + return core.response.exit(502) + end + return ngx_exit(1) end end From 1a90a6df031ed9b7abef38e282baff33ecd872aa Mon Sep 17 00:00:00 2001 From: Alex Zhang Date: Fri, 30 Apr 2021 09:45:03 +0800 Subject: [PATCH 4/4] fix --- apisix/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apisix/init.lua b/apisix/init.lua index 2ee002154f04..cae3ad4af7a2 100644 --- a/apisix/init.lua +++ b/apisix/init.lua @@ -299,7 +299,7 @@ local function get_upstream_by_id(up_id) if err then core.log.error("failed to get resolved upstream: ", err) if is_http then - return core.response.exit(502) + return core.response.exit(500) end return ngx_exit(1)