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

membphis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 669772f  bugfix: check if the service's upstream node is host. (#1402)
669772f is described below

commit 669772f5d58090122fb9b42de28957e0dc60e44e
Author: YuanSheng Wang <[email protected]>
AuthorDate: Sun Apr 12 09:31:58 2020 +0800

    bugfix: check if the service's upstream node is host. (#1402)
---
 apisix/balancer.lua     |  3 +-
 apisix/http/service.lua | 36 +++++++++++++++----
 apisix/plugin.lua       |  8 +++++
 t/lib/test_admin.lua    | 25 +++++++++++---
 t/node/merge-route.t    | 91 +++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 151 insertions(+), 12 deletions(-)

diff --git a/apisix/balancer.lua b/apisix/balancer.lua
index c2b94cd..a5134bc 100644
--- a/apisix/balancer.lua
+++ b/apisix/balancer.lua
@@ -307,7 +307,8 @@ function _M.run(route, ctx)
 
     local ok, err = balancer.set_current_peer(ip, port)
     if not ok then
-        core.log.error("failed to set server peer: ", err)
+        core.log.error("failed to set server peer [", ip, ":", port,
+                       "] err: ", err)
         return core.response.exit(502)
     end
 
diff --git a/apisix/http/service.lua b/apisix/http/service.lua
index 42b6493..42d31dd 100644
--- a/apisix/http/service.lua
+++ b/apisix/http/service.lua
@@ -17,10 +17,11 @@
 local core = require("apisix.core")
 local services
 local error = error
+local pairs = pairs
 
 
 local _M = {
-    version = 0.1,
+    version = 0.2,
 }
 
 
@@ -38,13 +39,36 @@ function _M.services()
 end
 
 
+local function filter(service)
+    service.has_domain = false
+    if not service.value then
+        return
+    end
+
+    if not service.value.upstream then
+        return
+    end
+
+    for addr, _ in pairs(service.value.upstream.nodes or {}) do
+        local host = core.utils.parse_addr(addr)
+        if not core.utils.parse_ipv4(host) and
+           not core.utils.parse_ipv6(host) then
+            service.has_domain = true
+            break
+        end
+    end
+
+    core.log.info("filter service: ", core.json.delay_encode(service))
+end
+
+
 function _M.init_worker()
     local err
-    services, err = core.config.new("/services",
-                        {
-                            automatic = true,
-                            item_schema = core.schema.service
-                        })
+    services, err = core.config.new("/services", {
+        automatic = true,
+        item_schema = core.schema.service,
+        filter = filter,
+    })
     if not services then
         error("failed to create etcd instance for fetching upstream: " .. err)
         return
diff --git a/apisix/plugin.lua b/apisix/plugin.lua
index 0a6b17a..8186d15 100644
--- a/apisix/plugin.lua
+++ b/apisix/plugin.lua
@@ -303,6 +303,10 @@ local function merge_service_route(service_conf, 
route_conf)
 
     if route_conf.value.plugins then
         for name, conf in pairs(route_conf.value.plugins) do
+            if not new_conf.value.plugins then
+                new_conf.value.plugins = {}
+            end
+
             new_conf.value.plugins[name] = conf
         end
     end
@@ -345,6 +349,10 @@ local function merge_consumer_route(route_conf, 
consumer_conf)
             if not new_route_conf then
                 new_route_conf = core.table.deepcopy(route_conf)
             end
+            if not new_route_conf.value.plugins then
+                new_route_conf.value.plugins = {}
+            end
+
             new_route_conf.value.plugins[name] = conf
         end
     end
diff --git a/t/lib/test_admin.lua b/t/lib/test_admin.lua
index 8c2fc25..8b4d6c5 100644
--- a/t/lib/test_admin.lua
+++ b/t/lib/test_admin.lua
@@ -100,6 +100,23 @@ function _M.test_ipv6(uri)
 end
 
 
+function _M.comp_tab(left_tab, right_tab)
+    if type(left_tab) == "string" then
+        left_tab = json.decode(left_tab)
+    end
+    if type(right_tab) == "string" then
+        right_tab = json.decode(right_tab)
+    end
+
+    local ok, err = com_tab(left_tab, right_tab)
+    if not ok then
+        return 500, "failed, " .. err
+    end
+
+    return true
+end
+
+
 function _M.test(uri, method, body, pattern)
     if type(body) == "table" then
         body = json.encode(body)
@@ -117,7 +134,7 @@ function _M.test(uri, method, body, pattern)
     -- https://github.com/ledgetech/lua-resty-http
     uri = ngx.var.scheme .. "://" .. ngx.var.server_addr
           .. ":" .. ngx.var.server_port .. uri
-    local res = httpc:request_uri(uri,
+    local res, err = httpc:request_uri(uri,
         {
             method = method,
             body = body,
@@ -139,11 +156,9 @@ function _M.test(uri, method, body, pattern)
     if pattern == nil then
         return res.status, "passed", res.body
     end
+
     local res_data = json.decode(res.body)
-    if type(pattern) == "string" then
-        pattern = json.decode(pattern)
-    end
-    local ok, err = com_tab(pattern, res_data)
+    local ok, err = _M.comp_tab(pattern, res_data)
     if not ok then
         return 500, "failed, " .. err, res_data
     end
diff --git a/t/node/merge-route.t b/t/node/merge-route.t
index ae52c05..772ab0d 100644
--- a/t/node/merge-route.t
+++ b/t/node/merge-route.t
@@ -233,3 +233,94 @@ GET /t
 --- error_log eval
 [qr/merge_service_route.*"time_window":60,/,
 qr/merge_service_route.*"time_window":60,/]
+
+
+
+=== TEST 10: set service(only upstream with host)
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/services/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "upstream": {
+                        "type": "roundrobin",
+                        "nodes": {
+                            "httpbin.org:443": 1
+                        }
+                    }
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 11: set route(bind service 1)
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/get",
+                    "host": "httpbin.org",
+                    "plugins": {
+                        "proxy-rewrite": {
+                                "scheme": "https"
+
+                        }
+                    },
+                    "service_id": "1"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 12: hit route
+--- request
+GET /get
+--- more_headers
+host: httpbin.org
+--- response_body eval
+qr/"Host": "httpbin.org"/
+--- no_error_log
+[error]
+--- timeout: 5
+
+
+
+=== TEST 13: not hit route
+--- request
+GET /get
+--- more_headers
+host: httpbin.orgxxx
+--- error_code: 404
+--- no_error_log
+[error]

Reply via email to