Yiyiyimu commented on a change in pull request #2036:
URL: https://github.com/apache/apisix/pull/2036#discussion_r475052806
##########
File path: apisix/core/etcd.lua
##########
@@ -15,11 +15,36 @@
-- limitations under the License.
--
local fetch_local_conf = require("apisix.core.config_local").local_conf
-local etcd = require("resty.etcd")
-local clone_tab = require("table.clone")
+local etcd = require("resty.etcd")
+local clone_tab = require("table.clone")
+local io = io
+local type = type
+local ipairs = ipairs
+local string = string
+local tonumber = tonumber
local _M = {version = 0.1}
+local prefix_v3 = {
+ ["3.5"] = "/v3",
+ ["3.4"] = "/v3",
+ ["3.3"] = "/v3beta",
+ ["3.2"] = "/v3alpha",
+}
+-- TODO: Default lua-resty-etcd version auto-detection is broken, so directly
get version from cmd
+-- we don't need to call this so many times, need to save it in some
place
+local function etcd_version_from_cmd()
+ local cmd = "export ETCDCTL_API=3 && etcdctl version"
+ local t, err = io.popen(cmd)
+ if not t then
+ return nil, "failed to execute command: " .. cmd .. ", error info:" ..
err
+ end
+ local data = t:read("*all")
+ t:close()
+ return prefix_v3[data:sub(-4,-2)]
+end
+_M.etcd_version_from_cmd = etcd_version_from_cmd
Review comment:
> we should only support `v3` and `/v3`, and we can detect and set by
Lua code instead of configure.
> _Originally posted by @moonming in
https://github.com/apache/apisix/pull/2036_
I turned to do the job by call `etcdctl version` in command line in Lua
code, since the original lua-resty-etcd version auto-detection seems not work
and return error
```
.../deps/share/lua/5.1/resty/http.lua:133: API disabled in the context of
init_worker_by_lua*
# stack traceback:
# [C]: in function 'ngx_socket_tcp'
# .../incubator-apisix/deps/share/lua/5.1/resty/http.lua:133: in
function 'new'
# .../incubator-apisix/deps/share/lua/5.1/resty/etcd/v2.lua:157: in
function 'version'
# .../incubator-apisix/deps/share/lua/5.1/resty/etcd.lua:22: in
function 'etcd_version'
# .../incubator-apisix/deps/share/lua/5.1/resty/etcd.lua:46: in
function 'new'
# ./apisix/core/config_etcd.lua:459: in function 'new'
```
But calling command line this much time might be too time-consuming. I still
think it might be a good choice to save it in the configuration or maybe
someplace else.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]