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



##########
File path: bin/apisix
##########
@@ -870,35 +870,30 @@ 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
-                    .. " --max-time " .. timeout * 2 .. " --retry 1 2>&1"
+            local res = require("posix.stdlib").setenv("ETCDCTL_API", 3)
+            local key =  (etcd_conf.prefix or "") .. dir_name .. "/"
+            -- use suggested v3 ctl `etcdctl` and avoid base64 conversion
+            --local base64 = require("base64")
+            --uri = host .. "/v3/kv/put"
+            --local post_json = '{"value":"' .. base64.encode("null") ..  '", 
"key":"' .. base64.encode(key) .. '"}'
+            --cmd = "curl " .. uri

Review comment:
       fixed

##########
File path: bin/apisix
##########
@@ -870,35 +870,30 @@ 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
-                    .. " --max-time " .. timeout * 2 .. " --retry 1 2>&1"
+            local res = require("posix.stdlib").setenv("ETCDCTL_API", 3)
+            local key =  (etcd_conf.prefix or "") .. dir_name .. "/"
+            -- use suggested v3 ctl `etcdctl` and avoid base64 conversion
+            --local base64 = require("base64")
+            --uri = host .. "/v3/kv/put"
+            --local post_json = '{"value":"' .. base64.encode("null") ..  '", 
"key":"' .. base64.encode(key) .. '"}'
+            --cmd = "curl " .. uri
+            --        .. " -X POST -d '" .. post_json
+            --        .. "' --connect-timeout " .. timeout
+            --        .. " --max-time " .. timeout * 2 .. " --retry 1 2>&1"
+            local cmd = "etcdctl --endpoints=" .. host .. " put " .. key .. " 
init_dir"
 
             local res = excute_cmd(cmd)
-            if not res:find("index", 1, true)
-                    and not res:find("createdIndex", 1, true) then
+            if (etcd_version == "v3" and not res:find("OK", 1, true)) then
+                --  (etcd_version == "v3" and not res:find("revision", 1, 
true)) then

Review comment:
       fixed




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