feat(DNS): allow specifying port to override the discovered one (#6764)

It makes the workaround in https://github.com/apache/apisix/issues/6732#issuecomment-1082597439
work.
Signed-off-by: spacewander <spacewanderlzx@gmail.com>
This commit is contained in:
罗泽轩 2022-04-01 09:29:51 +08:00 committed by GitHub
parent 0708b5838b
commit ecfd3ff3b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 3 deletions

View File

@ -37,10 +37,10 @@ function _M.nodes(service_name)
local nodes = core.table.new(#records, 0)
for i, r in ipairs(records) do
if r.address then
local node_port = r.port
if not node_port or node_port == 0 then
local node_port = port
if not node_port and r.port ~= 0 then
-- if the port is zero, fallback to use the default
node_port = port
node_port = r.port
end
nodes[i] = {host = r.address, weight = r.weight or 1, port = node_port}

View File

@ -135,3 +135,6 @@ being selected.
We treat weight 0 record has a weight of 1 so the node "have a very small chance of
being selected", which is also the common way to treat this type of record.
For SRV record which has port 0, we will fallback to use the upstream protocol's default port.
You can also specify the port in the "service_name" field directly, like "srv.blah.service:8848".

View File

@ -128,3 +128,6 @@ Upstream 配置是这样的:
> 当没有任何候选服务器时,域管理员应使用权重为 0 的,使 RR 更为易读(噪音更少)。当存在权重大于 0 的记录时,权重为 0 的记录被选中的可能性很小。
我们把权重为 0 的记录当作权重为 1因此节点“被选中的可能性很小”这也是处理此类记录的常用方法。
对于端口为 0 的 SRV 记录,我们会使用上游协议的默认端口。
你也可以在 “service_name” 字段中直接指定端口,比如 “srv.blah.service:8848”。

View File

@ -293,3 +293,19 @@ connect() failed
qr/proxy request to \S+/
--- grep_error_log_out
proxy request to 127.0.0.1:80
=== TEST 15: SRV (override port)
--- apisix_yaml
upstreams:
- service_name: "port.srv.test.local:1980"
discovery_type: dns
type: roundrobin
id: 1
--- grep_error_log eval
qr/upstream nodes: \{[^}]+\}/
--- grep_error_log_out eval
qr/upstream nodes: \{("127.0.0.1:1980":60,"127.0.0.2:1980":20|"127.0.0.2:1980":20,"127.0.0.1:1980":60)\}/
--- response_body
hello world