From de4e0bf8ed38660eccfff7ff7169bd8465555c2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E6=B3=BD=E8=BD=A9?= Date: Thu, 31 Mar 2022 09:37:02 +0800 Subject: [PATCH] fix(DNS): support SRV with port 0 (#6739) --- apisix/discovery/dns/init.lua | 8 +++++++- t/coredns/db.test.local | 2 ++ t/discovery/dns/sanity.t | 17 +++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/apisix/discovery/dns/init.lua b/apisix/discovery/dns/init.lua index 837f1e8e8ca0..449ea924c8a0 100644 --- a/apisix/discovery/dns/init.lua +++ b/apisix/discovery/dns/init.lua @@ -37,7 +37,13 @@ function _M.nodes(service_name) local nodes = core.table.new(#records, 0) for i, r in ipairs(records) do if r.address then - nodes[i] = {host = r.address, weight = r.weight or 1, port = r.port or port} + local node_port = r.port + if not node_port or node_port == 0 then + -- if the port is zero, fallback to use the default + node_port = port + end + + nodes[i] = {host = r.address, weight = r.weight or 1, port = node_port} if r.priority then -- for SRV record, nodes with lower priority are chosen first nodes[i].priority = -r.priority diff --git a/t/coredns/db.test.local b/t/coredns/db.test.local index 094b73a87574..857083511bb1 100644 --- a/t/coredns/db.test.local +++ b/t/coredns/db.test.local @@ -46,6 +46,8 @@ split-weight.srv 86400 IN SRV 10 0 1980 C priority.srv 86400 IN SRV 10 60 1979 A priority.srv 86400 IN SRV 20 60 1980 B +zero.srv 86400 IN SRV 10 60 0 A + ; a domain has both SRV & A records srv-a 86400 IN SRV 10 60 1980 A srv-a IN A 127.0.0.1 diff --git a/t/discovery/dns/sanity.t b/t/discovery/dns/sanity.t index 89c864317ceb..29c9273ab5f5 100644 --- a/t/discovery/dns/sanity.t +++ b/t/discovery/dns/sanity.t @@ -276,3 +276,20 @@ upstreams: proxy request to 127.0.0.1:1980 --- response_body hello world + + + +=== TEST 14: SRV (port is 0) +--- apisix_yaml +upstreams: + - service_name: "zero.srv.test.local" + discovery_type: dns + type: roundrobin + id: 1 +--- error_log +connect() failed +--- error_code: 502 +--- grep_error_log eval +qr/proxy request to \S+/ +--- grep_error_log_out +proxy request to 127.0.0.1:80