From a63c487b293253381a3c31ba3fab8bf74c0ff390 Mon Sep 17 00:00:00 2001 From: Akayeshmantha Date: Mon, 16 Mar 2020 19:37:38 +0100 Subject: [PATCH 1/5] add multiple etcd support. --- bin/apisix | 41 ++++++++++++++++++++++++++++------------- conf/config.yaml | 3 ++- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/bin/apisix b/bin/apisix index 2d21aa073372..3db29ec29ad8 100755 --- a/bin/apisix +++ b/bin/apisix @@ -706,28 +706,43 @@ local function init_etcd(show_output) end local etcd_conf = yaml_conf.etcd - local uri = etcd_conf.host .. "/v2/keys" .. (etcd_conf.prefix or "") local timeout = etcd_conf.timeout or 3 + local uri + local hostCount = table.getn(yaml_conf.etcd.host) - 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 + 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 = exec(cmd) - if not res:find("index", 1, true) - and not res:find("createdIndex", 1, true) then - error(cmd .. "\n" .. res) + local res = exec(cmd) + if not res:find("index", 1, true) + and not res:find("createdIndex", 1, true) then + is_success = false; + if (index == hostCount) then + error(cmd .. "\n" .. res) + end + break + end + + if show_output then + print(cmd) + print(res) + end end - if show_output then - print(cmd) - print(res) + if is_success then + break end end end diff --git a/conf/config.yaml b/conf/config.yaml index 4bb942d05f6e..baa5904279b7 100644 --- a/conf/config.yaml +++ b/conf/config.yaml @@ -113,7 +113,8 @@ nginx_config: # config for render the template to genarate n - 'unix:' etcd: - host: "http://127.0.0.1:2379" # etcd address + host: + - "http://127.0.0.1:2379" # multiple etcd address prefix: "/apisix" # apisix configurations prefix timeout: 3 # 3 seconds From 11288e0d2ffaf88627673b43996d2103ee2a51dc Mon Sep 17 00:00:00 2001 From: Akayeshmantha Date: Wed, 18 Mar 2020 09:46:49 +0100 Subject: [PATCH 2/5] fix for review comments. --- bin/apisix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/apisix b/bin/apisix index 3db29ec29ad8..697d21216f79 100755 --- a/bin/apisix +++ b/bin/apisix @@ -709,7 +709,7 @@ local function init_etcd(show_output) local timeout = etcd_conf.timeout or 3 local uri - local hostCount = table.getn(yaml_conf.etcd.host) + local host_count = #(yaml_conf.etcd.host) for index, host in ipairs(yaml_conf.etcd.host) do @@ -728,7 +728,7 @@ local function init_etcd(show_output) local res = exec(cmd) if not res:find("index", 1, true) and not res:find("createdIndex", 1, true) then - is_success = false; + is_success = false if (index == hostCount) then error(cmd .. "\n" .. res) end From 32100a225fceb3c9c1d6d4f9f9205bdd4f1b4661 Mon Sep 17 00:00:00 2001 From: Akayeshmantha Date: Wed, 18 Mar 2020 10:38:38 +0100 Subject: [PATCH 3/5] convert old single etcd config to multiple etcd config. --- bin/apisix | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bin/apisix b/bin/apisix index 697d21216f79..ee90dfa54209 100755 --- a/bin/apisix +++ b/bin/apisix @@ -711,6 +711,11 @@ local function init_etcd(show_output) local uri local host_count = #(yaml_conf.etcd.host) + --convert old single etcd config to multiple etcd config + if type(yaml_conf.etcd.host) == "string" then + yaml_conf.etcd.host = {yaml_conf.etcd.host} + end + for index, host in ipairs(yaml_conf.etcd.host) do local is_success = true From 94bf00c7a2a1c41665e7b1f12281379b3a4b5046 Mon Sep 17 00:00:00 2001 From: Akayeshmantha Date: Wed, 18 Mar 2020 10:43:45 +0100 Subject: [PATCH 4/5] fix. --- bin/apisix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/apisix b/bin/apisix index ee90dfa54209..78c2914839c4 100755 --- a/bin/apisix +++ b/bin/apisix @@ -709,13 +709,13 @@ local function init_etcd(show_output) local timeout = etcd_conf.timeout or 3 local uri - local host_count = #(yaml_conf.etcd.host) - --convert old single etcd config to multiple etcd config if type(yaml_conf.etcd.host) == "string" then yaml_conf.etcd.host = {yaml_conf.etcd.host} end + local host_count = #(yaml_conf.etcd.host) + for index, host in ipairs(yaml_conf.etcd.host) do local is_success = true From 67c4a7ccf59319e69c4da5f5449bb79b05ed4ae4 Mon Sep 17 00:00:00 2001 From: Akayeshmantha Date: Wed, 18 Mar 2020 19:42:25 +0100 Subject: [PATCH 5/5] add more comments. --- conf/config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conf/config.yaml b/conf/config.yaml index baa5904279b7..e0500927da31 100644 --- a/conf/config.yaml +++ b/conf/config.yaml @@ -113,7 +113,7 @@ nginx_config: # config for render the template to genarate n - 'unix:' etcd: - host: + host: # it's possible to define multiple etcd hosts addresses of the same etcd cluster. - "http://127.0.0.1:2379" # multiple etcd address prefix: "/apisix" # apisix configurations prefix timeout: 3 # 3 seconds