moonming commented on a change in pull request #2036:
URL: https://github.com/apache/apisix/pull/2036#discussion_r487706803



##########
File path: apisix/core/etcd.lua
##########
@@ -44,24 +49,144 @@ end
 _M.new = new
 
 
+local function kvs_to_node(kvs)
+    local node = {}
+    node.key = kvs.key
+    node.value = kvs.value
+    node.createdIndex = tonumber(kvs.create_revision)
+    node.modifiedIndex = tonumber(kvs.mod_revision)
+    return node
+end
+
+local function kvs_to_nodes(res, start_index)
+    res.body.node.dir = true
+    res.body.node.nodes = {}
+    for i=start_index, #res.body.kvs do
+        if start_index == 1 then
+            res.body.node.nodes[i] = kvs_to_node(res.body.kvs[i])
+        else
+            res.body.node.nodes[i-1] = kvs_to_node(res.body.kvs[i])
+        end
+    end
+    return res
+end
+
+
+local function not_found(res)
+    res.body.message = "Key not found"
+    res.reason = "Not found"
+    res.status = 404
+    return res
+end
+
+
+function _M.get_format(res, realkey)
+    if res.body.error == "etcdserver: user name is empty" then
+        return nil, "insufficient credentials code: 401"
+    end
+
+    res.headers["X-Etcd-Index"] = res.body.header.revision
+
+    if not res.body.kvs then
+        return not_found(res)
+    end
+    res.body.action = "get"
+
+    res.body.node = kvs_to_node(res.body.kvs[1])
+    -- kvs.value = nil, so key is root
+    if type(res.body.kvs[1].value) == "userdata" or not res.body.kvs[1].value 
then
+        -- remove last "/" when necesary
+        if string.sub(res.body.node.key, -1, -1) == "/" then
+            res.body.node.key = string.sub(res.body.node.key, 1, 
#res.body.node.key-1)
+        end
+        res = kvs_to_nodes(res, 2)
+    -- key not match, so realkey is root
+    elseif res.body.kvs[1].key ~= realkey then
+        res.body.node.key = realkey
+        res = kvs_to_nodes(res, 1)
+    -- first is root (in v2, root not contains value), others are nodes
+    elseif #res.body.kvs > 1 then
+        res = kvs_to_nodes(res, 2)
+    end
+
+    res.body.kvs = nil
+    return res, nil
+end
+
+
+function _M.watch_format(v3res)

Review comment:
       Is this the format conversion function between etcd v2 and v3?

##########
File path: rockspec/apisix-master-0.rockspec
##########
@@ -52,6 +52,7 @@ dependencies = {
     "lua-resty-kafka = 0.07",
     "lua-resty-logger-socket = 2.0-0",
     "skywalking-nginx-lua-plugin = 1.0-0",
+    "base64 = 1.5-2"

Review comment:
       we don't need this dependency, you can use ngx.encode_base64 and 
ngx.decode_base64

##########
File path: .travis/linux_tengine_runner.sh
##########
@@ -259,6 +259,12 @@ do_install() {
 
     sudo luarocks install luacheck > build.log 2>&1 || (cat build.log && exit 
1)
 
+    wget 
https://github.com/etcd-io/etcd/releases/download/v3.4.0/etcd-v3.4.0-linux-amd64.tar.gz
+    tar xf etcd-v3.4.0-linux-amd64.tar.gz
+    sudo cp etcd-v3.4.0-linux-amd64/etcd /usr/local/bin/
+    sudo cp etcd-v3.4.0-linux-amd64/etcdctl /usr/local/bin/
+    rm -rf etcd-v3.4.0-linux-amd64

Review comment:
       repeat many times, can we move them to one script?

##########
File path: bin/apisix
##########
@@ -873,35 +873,26 @@ local function init_etcd(show_output)
 
     local host_count = #(yaml_conf.etcd.host)
 
-    -- check whether the user has enabled etcd v2 protocol
-    for index, host in ipairs(yaml_conf.etcd.host) do
-        uri = host .. "/v2/keys"
-        local cmd = "curl -i -m ".. timeout * 2 .. " -o /dev/null -s -w 
%{http_code} " .. uri
-        local res = excute_cmd(cmd)
-        if res == "404" then
-            io.stderr:write(string.format("failed: please make sure that you 
have enabled the v2 protocol of etcd on %s.\n", host))
-            return
-        end
-    end
-
     local etcd_ok = false
     for index, host in ipairs(yaml_conf.etcd.host) do
 
         local is_success = true
-        uri = host .. "/v2/keys" .. (etcd_conf.prefix or "")
 
         for _, dir_name in ipairs({"/routes", "/upstreams", "/services",
                                    "/plugins", "/consumers", "/node_status",
                                    "/ssl", "/global_rules", "/stream_routes",
                                    "/proto"}) do
-            local cmd = "curl " .. uri .. dir_name
-                    .. "?prev_exist=false -X PUT -d dir=true "
-                    .. "--connect-timeout " .. timeout
+            local key =  (etcd_conf.prefix or "") .. dir_name .. "/"
+
+            local base64_encode = require("base64").encode

Review comment:
       please use ngx.encode_base64




----------------------------------------------------------------
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]


Reply via email to