Skip to content

Commit

Permalink
feat: share etcd client
Browse files Browse the repository at this point in the history
Signed-off-by: spacewander <spacewanderlzx@gmail.com>
  • Loading branch information
spacewander committed Dec 9, 2020
1 parent c7b198f commit 1f3e4ae
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 21 deletions.
59 changes: 40 additions & 19 deletions apisix/core/config_etcd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,39 @@ function _M.getkey(self, key)
end


local get_etcd
do
local etcd_cli

function get_etcd()
if etcd_cli == nil then
local local_conf, err = config_local.local_conf()
if not local_conf then
return nil, err
end

local etcd_conf = clone_tab(local_conf.etcd)
etcd_conf.http_host = etcd_conf.host
etcd_conf.host = nil
etcd_conf.prefix = nil
etcd_conf.protocol = "v3"
etcd_conf.api_prefix = "/v3"

-- default to verify etcd cluster certificate
etcd_conf.ssl_verify = true
if etcd_conf.tls and etcd_conf.tls.verify == false then
etcd_conf.ssl_verify = false
end

etcd_cli, err = etcd.new(etcd_conf)
return etcd_cli, err
end

return etcd_cli
end
end


local function _automatic_fetch(premature, self)
if premature then
return
Expand All @@ -495,7 +528,7 @@ local function _automatic_fetch(premature, self)

local ok, err = xpcall(function()
if not self.etcd_cli then
local etcd_cli, err = etcd.new(self.etcd_conf)
local etcd_cli, err = get_etcd()
if not etcd_cli then
error("failed to create etcd instance for key ["
.. self.key .. "]: " .. (err or "unknown"))
Expand Down Expand Up @@ -548,22 +581,11 @@ function _M.new(key, opts)
return nil, err
end

local etcd_conf = clone_tab(local_conf.etcd)
local etcd_conf = local_conf.etcd
local prefix = etcd_conf.prefix
etcd_conf.http_host = etcd_conf.host
etcd_conf.host = nil
etcd_conf.prefix = nil
etcd_conf.protocol = "v3"
etcd_conf.api_prefix = "/v3"
etcd_conf.ssl_verify = true

-- default to verify etcd cluster certificate
if etcd_conf.tls and etcd_conf.tls.verify == false then
etcd_conf.ssl_verify = false
end

if not etcd_conf.resync_delay or etcd_conf.resync_delay < 0 then
etcd_conf.resync_delay = 5
local resync_delay = etcd_conf.resync_delay
if not resync_delay or resync_delay < 0 then
resync_delay = 5
end

local automatic = opts and opts.automatic
Expand All @@ -575,7 +597,6 @@ function _M.new(key, opts)

local obj = setmetatable({
etcd_cli = nil,
etcd_conf = etcd_conf,
key = key and prefix .. key,
automatic = automatic,
item_schema = item_schema,
Expand All @@ -589,7 +610,7 @@ function _M.new(key, opts)
prev_index = 0,
last_err = nil,
last_err_time = nil,
resync_delay = etcd_conf.resync_delay,
resync_delay = resync_delay,
timeout = timeout,
single_item = single_item,
filter = filter_fun,
Expand All @@ -603,7 +624,7 @@ function _M.new(key, opts)
ngx_timer_at(0, _automatic_fetch, obj)

else
local etcd_cli, err = etcd.new(etcd_conf)
local etcd_cli, err = get_etcd()
if not etcd_cli then
return nil, "failed to start a etcd instance: " .. err
end
Expand Down
2 changes: 1 addition & 1 deletion rockspec/apisix-master-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ description = {

dependencies = {
"lua-resty-template = 1.9",
"lua-resty-etcd = 1.4.2",
"lua-resty-etcd = 1.4.3",
"lua-resty-balancer = 0.02rc5",
"lua-resty-ngxvar = 0.5",
"lua-resty-jit-uuid = 0.0.7",
Expand Down
6 changes: 6 additions & 0 deletions t/APISIX.pm
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@ _EOC_
more_clear_headers Date;
}
location = /v3/auth/authenticate {
content_by_lua_block {
ngx.log(ngx.WARN, "etcd auth failed!")
}
}
location = /.well-known/openid-configuration {
content_by_lua_block {
ngx.say([[
Expand Down
26 changes: 26 additions & 0 deletions t/core/config_etcd.t
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,29 @@ GET /t
passed
--- no_error_log
[error]



=== TEST 7: ensure only one auth request per subsystem for all the etcd sync
--- yaml_config
apisix:
node_listen: 1984
etcd:
host:
- "http://127.0.0.1:1980" -- fake server port
timeout: 1
user: root # root username for etcd
password: 5tHkHhYkjr6cQY # root password for etcd
--- config
location /t {
content_by_lua_block {
ngx.sleep(0.5)
}
}
--- request
GET /t
--- grep_error_log eval
qr/etcd auth failed/
--- grep_error_log_out
etcd auth failed
etcd auth failed
2 changes: 1 addition & 1 deletion t/node/healthcheck-passive.t
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ __DATA__
"interval": 100,
"http_failures": 2
}
},
},]] .. [[
"passive": {
"healthy": {
"http_statuses": [200, 201],
Expand Down

0 comments on commit 1f3e4ae

Please sign in to comment.