You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2021/07/13 12:23:57 UTC

[apisix] branch master updated: feat: add prefix when using config_etcd:getkeys (#4561)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new cfd4045  feat: add prefix when using config_etcd:getkeys (#4561)
cfd4045 is described below

commit cfd4045357b4a2af808bf39d77dcb1f46158b31f
Author: Bill <bi...@gmail.com>
AuthorDate: Tue Jul 13 20:23:49 2021 +0800

    feat: add prefix when using config_etcd:getkeys (#4561)
    
    Co-authored-by: 黄圣彪 <hu...@bilibili.com>
---
 apisix/core/config_etcd.lua            |  5 ++++
 apisix/plugins/prometheus/exporter.lua |  5 +++-
 t/core/config_etcd.t                   | 42 ++++++++++++++++++++++++++++++++++
 3 files changed, 51 insertions(+), 1 deletion(-)

diff --git a/apisix/core/config_etcd.lua b/apisix/core/config_etcd.lua
index f35f232..94e91af 100644
--- a/apisix/core/config_etcd.lua
+++ b/apisix/core/config_etcd.lua
@@ -486,6 +486,11 @@ function _M.getkey(self, key)
         return nil, "stopped"
     end
 
+    local local_conf = config_local.local_conf()
+    if local_conf and local_conf.etcd and local_conf.etcd.prefix then
+        key = local_conf.etcd.prefix .. key
+    end
+
     return getkey(self.etcd_cli, key)
 end
 
diff --git a/apisix/plugins/prometheus/exporter.lua b/apisix/plugins/prometheus/exporter.lua
index 8bb6fce..ea1c484 100644
--- a/apisix/plugins/prometheus/exporter.lua
+++ b/apisix/plugins/prometheus/exporter.lua
@@ -308,7 +308,10 @@ function _M.collect()
                            "processing metrics endpoint: ", err)
         end
 
-        local res, _ = config:getkey("/routes")
+        -- Because request any key from etcd will return the "X-Etcd-Index".
+        -- A non-existed key is preferred because it doesn't return too much data.
+        -- So use phantom key to get etcd index.
+        local res, _ = config:getkey("/phantomkey")
         if res and res.headers then
             clear_tab(key_values)
             -- global max
diff --git a/t/core/config_etcd.t b/t/core/config_etcd.t
index edab98f..97c5481 100644
--- a/t/core/config_etcd.t
+++ b/t/core/config_etcd.t
@@ -259,3 +259,45 @@ etcd auth failed
 etcd auth failed
 etcd auth failed
 etcd auth failed
+
+
+
+=== TEST 8: ensure add prefix automatically for _M.getkey
+apisix:
+  node_listen: 1984
+  admin_key: null
+etcd:
+  host:
+    - "http://127.0.0.1:2379"
+  tls:
+    verify: false
+  prefix: "/apisix"
+--- config
+    location /t {
+        content_by_lua_block {
+            local core = require("apisix.core")
+
+            local config = core.config.new()
+            local res = config:getkey("/routes/")
+            if res and res.status == 200 and res.body
+               and res.body.node and res.body.node.key == "/apisix/routes" then
+                ngx.say("passed")
+              else
+                ngx.say("failed")
+            end
+
+            local res = config:getkey("/phantomkey")
+            if res and res.status == 404 then
+                ngx.say("passed")
+            else
+                ngx.say("failed")
+            end
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+passed
+--- no_error_log
+[error]