Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove etcd.use_grpc #10015

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 2 additions & 113 deletions apisix/cli/etcd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -280,103 +280,7 @@ local function prepare_dirs_via_http(yaml_conf, args, index, host, host_count)
end


local function grpc_request(url, yaml_conf, key)
local cmd

local auth = ""
if yaml_conf.etcd.user then
local user = yaml_conf.etcd.user
local password = yaml_conf.etcd.password
auth = str_format("--user=%s:%s", user, password)
end

if str_sub(url, 1, 8) == "https://" then
local host = url:sub(9)

local verify = true
local certificate, pkey, cafile
if yaml_conf.etcd.tls then
local cfg = yaml_conf.etcd.tls

if cfg.verify == false then
verify = false
end

certificate = cfg.cert
pkey = cfg.key

local apisix_ssl = yaml_conf.apisix.ssl
if apisix_ssl and apisix_ssl.ssl_trusted_certificate then
cafile = apisix_ssl.ssl_trusted_certificate
end
end

cmd = str_format(
"etcdctl --insecure-transport=false %s %s %s %s " ..
"%s --endpoints=%s put %s init_dir",
verify and "" or "--insecure-skip-tls-verify",
certificate and "--cert " .. certificate or "",
pkey and "--key " .. pkey or "",
cafile and "--cacert " .. cafile or "",
auth, host, key)
else
local host = url:sub(#("http://") + 1)

cmd = str_format(
"etcdctl %s --endpoints=%s put %s init_dir",
auth, host, key)
end

local res, err = util.execute_cmd(cmd)
return res, err
end


local function prepare_dirs_via_grpc(yaml_conf, args, index, host)
local is_success = true

local errmsg
local dirs = {}
for name in pairs(constants.HTTP_ETCD_DIRECTORY) do
dirs[name] = true
end
for name in pairs(constants.STREAM_ETCD_DIRECTORY) do
dirs[name] = true
end

for dir_name in pairs(dirs) do
local key = (yaml_conf.etcd.prefix or "") .. dir_name .. "/"
local res, err
local retry_time = 0
while retry_time < 2 do
res, err = grpc_request(host, yaml_conf, key)
retry_time = retry_time + 1
if res then
break
end
print(str_format("Warning! Request etcd endpoint \'%s\' error, %s, retry time=%s",
host, err, retry_time))
end

if not res then
errmsg = str_format("request etcd endpoint \"%s\" error, %s\n", host, err)
util.die(errmsg)
end

if args and args["verbose"] then
print(res)
end
end

return is_success
end


local function prepare_dirs(use_grpc, yaml_conf, args, index, host, host_count)
if use_grpc then
return prepare_dirs_via_grpc(yaml_conf, args, index, host)
end

local function prepare_dirs(yaml_conf, args, index, host, host_count)
return prepare_dirs_via_http(yaml_conf, args, index, host, host_count)
end

Expand All @@ -400,8 +304,6 @@ function _M.init(env, args)
util.die("failed to read `etcd` field from yaml file when init etcd")
end

local etcd_conf = yaml_conf.etcd

-- 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}
Expand Down Expand Up @@ -477,22 +379,9 @@ function _M.init(env, args)
util.die("the etcd cluster needs at least 50% and above healthy nodes\n")
end

if etcd_conf.use_grpc and not env.use_apisix_base then
io_stderr:write("'use_grpc: true' in the etcd configuration " ..
"is not supported by vanilla OpenResty\n")
end

local use_grpc = etcd_conf.use_grpc and env.use_apisix_base
if use_grpc then
local ok, err = util.execute_cmd("command -v etcdctl")
if not ok then
util.die("can't find etcdctl: ", err, "\n")
end
end

local etcd_ok = false
for index, host in ipairs(etcd_healthy_hosts) do
if prepare_dirs(use_grpc, yaml_conf, args, index, host, host_count) then
if prepare_dirs(yaml_conf, args, index, host, host_count) then
etcd_ok = true
break
end
Expand Down
5 changes: 0 additions & 5 deletions apisix/cli/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ local etcd_schema = {
minimum = 1,
description = "etcd connection timeout in seconds",
},
use_grpc = {
type = "boolean",
-- TODO: set true by default in v3.2
default = false,
},
},
required = {"prefix", "host"}
}
Expand Down
11 changes: 2 additions & 9 deletions apisix/cli/snippet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,6 @@ server {
]]


local function is_grpc_used(env, etcd)
local is_grpc_available = env.use_apisix_base
return is_grpc_available and etcd.use_grpc
end


function _M.generate_conf_server(env, conf)
if not (conf.deployment and (
conf.deployment.role == "traditional" or
Expand Down Expand Up @@ -171,7 +165,6 @@ function _M.generate_conf_server(env, conf)
local client_cert_key
local ssl_trusted_certificate
local etcd_tls_verify
local use_grpc = is_grpc_used(env, etcd)
if tls then
if tls.cert then
client_cert = pl_path.abspath(tls.cert)
Expand All @@ -197,8 +190,8 @@ function _M.generate_conf_server(env, conf)
trusted_ca_cert = trusted_ca_cert,
etcd_tls_verify = etcd_tls_verify,
ssl_trusted_certificate = ssl_trusted_certificate,
scheme_name = use_grpc and "grpc" or "http",
directive_prefix = use_grpc and "grpc" or "proxy",
scheme_name = "http",
directive_prefix = "proxy",
})
end

Expand Down
67 changes: 2 additions & 65 deletions apisix/core/config_etcd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -362,40 +362,6 @@ local function readdir(etcd_cli, key, formatter)
end


local function grpc_waitdir(self, etcd_cli, key, modified_index, timeout)
local watching_stream = self.watching_stream
if not watching_stream then
local attr = {}
attr.start_revision = modified_index
local opts = {}
opts.timeout = timeout

local st, err = etcd_cli:create_grpc_watch_stream(key, attr, opts)
if not st then
log.error("create watch stream failed: ", err)
return nil, err
end

log.info("create watch stream for key: ", key, ", modified_index: ", modified_index)

self.watching_stream = st
watching_stream = st
end

return etcd_cli:read_grpc_watch_stream(watching_stream)
end


local function flush_watching_streams(self)
local etcd_cli = self.etcd_cli
if not etcd_cli.use_grpc then
return
end

self.watching_stream = nil
end


local function http_waitdir(self, etcd_cli, key, modified_index, timeout)
if not watch_ctx.idx[key] then
watch_ctx.idx[key] = 1
Expand Down Expand Up @@ -470,12 +436,7 @@ local function waitdir(self)
return nil, "not inited"
end

local res, err
if etcd_cli.use_grpc then
res, err = grpc_waitdir(self, etcd_cli, key, modified_index, timeout)
else
res, err = http_waitdir(self, etcd_cli, key, modified_index, timeout)
end
local res, err = http_waitdir(self, etcd_cli, key, modified_index, timeout)

if not res then
-- log.error("failed to get key from etcd: ", err)
Expand Down Expand Up @@ -620,13 +581,9 @@ local function sync_data(self)
return nil, "missing 'key' arguments"
end

if not self.etcd_cli.use_grpc then
init_watch_ctx(self.key)
end
init_watch_ctx(self.key)

if self.need_reload then
flush_watching_streams(self)

local res, err = readdir(self.etcd_cli, self.key)
if not res then
return false, err
Expand Down Expand Up @@ -916,7 +873,6 @@ local function _automatic_fetch(premature, self)
end

if not exiting() and self.running then
flush_watching_streams(self)
ngx_timer_at(0, _automatic_fetch, self)
end
end
Expand Down Expand Up @@ -1118,10 +1074,6 @@ function _M.init()
return true
end

if local_conf.etcd.use_grpc then
return true
end

-- don't go through proxy during start because the proxy is not available
local etcd_cli, prefix, err = etcd_apisix.new_without_proxy()
if not etcd_cli then
Expand All @@ -1147,21 +1099,6 @@ function _M.init_worker()
return true
end

if not local_conf.etcd.use_grpc then
return true
end

-- don't go through proxy during start because the proxy is not available
local etcd_cli, prefix, err = etcd_apisix.new_without_proxy()
if not etcd_cli then
return nil, "failed to start a etcd instance: " .. err
end

local res, err = readdir(etcd_cli, prefix, create_formatter(prefix))
if not res then
return nil, err
end

return true
end

Expand Down
16 changes: 0 additions & 16 deletions apisix/core/etcd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ local clone_tab = require("table.clone")
local health_check = require("resty.etcd.health_check")
local pl_path = require("pl.path")
local ipairs = ipairs
local pcall = pcall
local setmetatable = setmetatable
local string = string
local tonumber = tonumber
Expand Down Expand Up @@ -72,17 +71,6 @@ local function _new(etcd_conf)
end
end

if etcd_conf.use_grpc then
if ngx_get_phase() == "init" then
etcd_conf.use_grpc = false
else
local ok = pcall(require, "resty.grpc")
if not ok then
etcd_conf.use_grpc = false
end
end
end

local etcd_cli, err = etcd.new(etcd_conf)
if not etcd_cli then
return nil, nil, err
Expand Down Expand Up @@ -349,10 +337,6 @@ do
return nil, nil, err
end

if tmp_etcd_cli.use_grpc then
etcd_cli_init_phase = tmp_etcd_cli
end

return tmp_etcd_cli, prefix
end

Expand Down
1 change: 0 additions & 1 deletion ci/linux_openresty_runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@


export OPENRESTY_VERSION=source
#export TEST_CI_USE_GRPC=true
. ./ci/linux_openresty_common_runner.sh
2 changes: 0 additions & 2 deletions conf/config-default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -673,9 +673,7 @@ deployment: # Deployment configurations
host: # Set etcd address(es) in the same etcd cluster.
- "http://127.0.0.1:2379" # If TLS is enabled for etcd, use https://127.0.0.1:2379.
prefix: /apisix # Set etcd prefix.
use_grpc: false # Use gRPC (experimental) for etcd configuration sync.
timeout: 30 # Set timeout in seconds.
# Set a higher timeout (e.g. an hour) if `use_grpc` is true.
# resync_delay: 5 # Set resync time in seconds after a sync failure.
# The actual resync time would be resync_delay plus 50% random jitter.
# health_check_timeout: 10 # Set timeout in seconds for etcd health check.
Expand Down
11 changes: 0 additions & 11 deletions t/APISIX.pm
Original file line number Diff line number Diff line change
Expand Up @@ -878,17 +878,6 @@ deployment:
_EOC_

if ($yaml_config !~ m/deployment:/) {
# TODO: remove this temporary option once we have using gRPC by default
if ($ENV{TEST_CI_USE_GRPC}) {
$default_deployment .= <<_EOC_;
etcd:
host:
- "http://127.0.0.1:2379"
prefix: /apisix
use_grpc: true
_EOC_
}

$yaml_config = $default_deployment . $yaml_config;
}

Expand Down
Loading
Loading