This is an automated email from the ASF dual-hosted git repository.

spacewander pushed a commit to branch release/2.13
in repository https://gitbox.apache.org/repos/asf/apisix.git

commit b4d294daa0373fad8b31127f36adcedd3d9d4317
Author: 罗泽轩 <[email protected]>
AuthorDate: Thu Mar 31 09:37:02 2022 +0800

    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 837f1e8e8..449ea924c 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 094b73a87..857083511 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 89c864317..29c9273ab 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

Reply via email to