Skip to content

Commit 2fbccde

Browse files
committed
fix: [bcs-bk-apisix-gateway]support cluster addresses with path suffix in bcs-dynamic-route
1 parent d7e1513 commit 2fbccde

2 files changed

Lines changed: 54 additions & 24 deletions

File tree

bcs-services/bcs-bk-apisix-gateway/plugins/bcs-dynamic-route.lua

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -374,23 +374,36 @@ local function periodly_sync_cluster_credentials_in_master()
374374
end
375375
local upstream_nodes = {}
376376
local addresses = stringx.split(cluster_credential["serverAddress"], ",")
377+
local prefix = ""
377378
for i, address in ipairs(addresses) do
378379
local splited = stringx.split(address, "://")
379380
local scheme = "https"
380-
if #splited ~= 2 then
381-
upstream_nodes[i] = {
382-
host = address,
383-
weight = 100,
384-
}
385-
else
381+
local host_and_path = address
382+
if #splited == 2 then
386383
scheme = splited[1]
387-
upstream_nodes[i] = {
388-
host = splited[2],
389-
weight = 100,
390-
}
384+
host_and_path = splited[2]
385+
end
386+
387+
-- Split host:port and path prefix
388+
local host_port, path = host_and_path:match("^([^/]+)(/.*)$")
389+
if not host_port then
390+
host_port = host_and_path
391+
path = ""
392+
end
393+
394+
-- Strip trailing slash
395+
if path:sub(-1) == "/" then
396+
path = path:sub(1, -2)
397+
end
398+
if prefix == "" then
399+
prefix = path
391400
end
401+
402+
upstream_nodes[i] = {
403+
weight = 100,
404+
}
392405
-- port is required, 443 as default port
393-
local host, port = core.utils.parse_addr(upstream_nodes[i].host)
406+
local host, port = core.utils.parse_addr(host_port)
394407
upstream_nodes[i].host = host
395408
if not port then
396409
if scheme == "http" then
@@ -404,6 +417,7 @@ local function periodly_sync_cluster_credentials_in_master()
404417
upstream_nodes[i].port = port
405418
end
406419
end
420+
cluster_info["prefix"] = prefix
407421

408422
-- 定义一个通用的节点健康检查函数
409423
local function check_nodes_health(upstream_nodes, check_func, cluster_credential)
@@ -528,7 +542,8 @@ end
528542

529543
-- proxy to apiserver directly
530544
local function traffic_to_cluster_apiserver(conf, ctx, cluster_credential, upstream_uri)
531-
ctx.var.upstream_uri = "/" .. upstream_uri
545+
local prefix = cluster_credential["prefix"] or ""
546+
ctx.var.upstream_uri = prefix .. "/" .. upstream_uri
532547
if cluster_credential["user_token"] then
533548
core.request.set_header(ctx, "Authorization", "Bearer " .. cluster_credential["user_token"])
534549
end

bcs-services/bcs-gateway-discovery/plugins/apisix/bcs-dynamic-route.lua

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -353,23 +353,36 @@ local function periodly_sync_cluster_credentials_in_master()
353353
end
354354
local upstream_nodes = {}
355355
local addresses = stringx.split(cluster_credential["serverAddress"], ",")
356+
local prefix = ""
356357
for i, address in ipairs(addresses) do
357358
local splited = stringx.split(address, "://")
358359
local scheme = "https"
359-
if #splited ~= 2 then
360-
upstream_nodes[i] = {
361-
host = address,
362-
weight = 100,
363-
}
364-
else
360+
local host_and_path = address
361+
if #splited == 2 then
365362
scheme = splited[1]
366-
upstream_nodes[i] = {
367-
host = splited[2],
368-
weight = 100,
369-
}
363+
host_and_path = splited[2]
364+
end
365+
366+
-- Split host:port and path prefix
367+
local host_port, path = host_and_path:match("^([^/]+)(/.*)$")
368+
if not host_port then
369+
host_port = host_and_path
370+
path = ""
371+
end
372+
373+
-- Strip trailing slash
374+
if path:sub(-1) == "/" then
375+
path = path:sub(1, -2)
376+
end
377+
if prefix == "" then
378+
prefix = path
370379
end
380+
381+
upstream_nodes[i] = {
382+
weight = 100,
383+
}
371384
-- port is required, 443 as default port
372-
local host, port = core.utils.parse_addr(upstream_nodes[i].host)
385+
local host, port = core.utils.parse_addr(host_port)
373386
upstream_nodes[i].host = host
374387
if not port then
375388
if scheme == "http" then
@@ -383,6 +396,7 @@ local function periodly_sync_cluster_credentials_in_master()
383396
upstream_nodes[i].port = port
384397
end
385398
end
399+
cluster_info["prefix"] = prefix
386400

387401
-- 定义一个通用的节点健康检查函数
388402
local function check_nodes_health(upstream_nodes, check_func, cluster_credential)
@@ -506,7 +520,8 @@ end
506520

507521
-- proxy to apiserver directly
508522
local function traffic_to_cluster_apiserver(conf, ctx, cluster_credential, upstream_uri)
509-
ctx.var.upstream_uri = "/" .. upstream_uri
523+
local prefix = cluster_credential["prefix"] or ""
524+
ctx.var.upstream_uri = prefix .. "/" .. upstream_uri
510525
if cluster_credential["user_token"] then
511526
core.request.set_header(ctx, "Authorization", "Bearer " .. cluster_credential["user_token"])
512527
end

0 commit comments

Comments
 (0)