This is an automated email from the ASF dual-hosted git repository. membphis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-apisix.git
The following commit(s) were added to refs/heads/master by this push: new e1ac426 feature: auto import nameservers into APISIX from system resolver. (#1167) e1ac426 is described below commit e1ac426443293c6140ddb257e482537edd43c36a Author: qiujiayu <153163...@qq.com> AuthorDate: Mon Mar 2 16:04:15 2020 +0800 feature: auto import nameservers into APISIX from system resolver. (#1167) Fix #1164 --- .travis/apisix_cli_test.sh | 14 ++++++++++ bin/apisix | 70 ++++++++++++++++++++++++++++++++++++---------- lua/apisix.lua | 34 ++++++++++++++-------- 3 files changed, 91 insertions(+), 27 deletions(-) diff --git a/.travis/apisix_cli_test.sh b/.travis/apisix_cli_test.sh index 7ef2089..5274137 100755 --- a/.travis/apisix_cli_test.sh +++ b/.travis/apisix_cli_test.sh @@ -52,3 +52,17 @@ if [ ! $? -eq 0 ]; then fi echo "passed: change default ssl port" + +# check nameserver imported +i=`grep -E '^nameserver[[:space:]]+(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4]0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])[[:space:]]?$' /etc/resolv.conf | awk '{print $2}'` +for ip in $i +do + echo $ip + grep $ip conf/nginx.conf > /dev/null + if [ ! $? -eq 0 ]; then + echo "failed: system DNS "$ip "unimported" + exit 1 + fi +done + +echo "passed: system nameserver imported" diff --git a/bin/apisix b/bin/apisix index ff48af7..3a078f8 100755 --- a/bin/apisix +++ b/bin/apisix @@ -22,7 +22,10 @@ local function trim(s) end local function excute_cmd(cmd) - local t = io.popen(cmd) + local t, err = io.popen(cmd) + if not t then + return nil, "failed to execute command: " .. cmd .. ", error info:" .. err + end local data = t:read("*all") t:close() return data @@ -113,6 +116,9 @@ stream { .. [=[{*lua_cpath*};"; lua_socket_log_errors off; + resolver {% for _, dns_addr in ipairs(dns_resolver or {}) do %} {*dns_addr*} {% end %} valid={*dns_resolver_valid*} ipv6=off; + resolver_timeout 5; + upstream apisix_backend { server 127.0.0.1:80; balancer_by_lua_block { @@ -123,7 +129,12 @@ stream { init_by_lua_block { require "resty.core" apisix = require("apisix") - apisix.stream_init() + + local dns_resolver = { {% for _, dns_addr in ipairs(dns_resolver or {}) do %} "{*dns_addr*}", {% end %} } + local args = { + dns_resolver = dns_resolver, + } + apisix.stream_init(args) } init_worker_by_lua_block { @@ -238,7 +249,12 @@ http { init_by_lua_block { require "resty.core" apisix = require("apisix") - apisix.http_init() + + local dns_resolver = { {% for _, dns_addr in ipairs(dns_resolver or {}) do %} "{*dns_addr*}", {% end %} } + local args = { + dns_resolver = dns_resolver, + } + apisix.http_init(args) } init_worker_by_lua_block { @@ -434,9 +450,9 @@ http { ]=] local function write_file(file_path, data) - local file = io.open(file_path, "w+") + local file, err = io.open(file_path, "w+") if not file then - return false, "failed to open file: " .. file_path + return false, "failed to open file: " .. file_path .. ", error info:" .. err end file:write(data) @@ -445,9 +461,9 @@ local function write_file(file_path, data) end local function read_file(file_path) - local file = io.open(file_path, "rb") + local file, err = io.open(file_path, "rb") if not file then - return false, "failed to open file: " .. file_path + return false, "failed to open file: " .. file_path .. ", error info:" .. err end local data = file:read("*all") @@ -455,12 +471,6 @@ local function read_file(file_path) return data end -local function exec(command) - local t= io.popen(command) - local res = t:read("*all") - t:close() - return trim(res) -end local function read_yaml_conf() local profile = require("apisix.core.profile") @@ -530,6 +540,24 @@ local function check_or_version(cur_ver_s, need_ver_s) return true end + +local function system_dns_resolver() + local file, err = io.open("/etc/resolv.conf") + if err then + return nil, err + else + local dns_addrs = {} + for line in file:lines() do + local addr, n = line:gsub("^nameserver%s+(%d+%.%d+%.%d+%.%d+)%s*$", "%1") + if n == 1 then + table.insert(dns_addrs, addr) + end + end + return dns_addrs + end +end + + local _M = {version = 0.1} function _M.help() @@ -568,7 +596,7 @@ local function init() local sys_conf = { lua_path = pkg_path_org, lua_cpath = pkg_cpath_org, - os_name = exec("uname"), + os_name = excute_cmd("uname"), apisix_lua_home = apisix_home, with_module_status = with_module_status, error_log = {level = "warn"}, @@ -608,6 +636,18 @@ local function init() sys_conf["worker_processes"] = "auto" end + local dns_addrs = system_dns_resolver() + if dns_addrs then + local dns_resolver = sys_conf["dns_resolver"] + if dns_resolver then + for _, addr in ipairs(dns_addrs) do + table.insert(dns_resolver, 1, addr) + end + else + sys_conf["dns_resolver"] = dns_addrs + end + end + local conf_render = template.compile(ngx_tpl) local ngxconf = conf_render(sys_conf) @@ -663,7 +703,7 @@ local function init_etcd(show_output) .. "--connect-timeout " .. timeout .. " --max-time " .. timeout * 2 .. " --retry 1 2>&1" - local res = exec(cmd) + local res = excute_cmd(cmd) if not res:find("index", 1, true) and not res:find("createdIndex", 1, true) then error(cmd .. "\n" .. res) diff --git a/lua/apisix.lua b/lua/apisix.lua index 7517c46..896b0cf 100644 --- a/lua/apisix.lua +++ b/lua/apisix.lua @@ -32,14 +32,21 @@ local pairs = pairs local tostring = tostring local load_balancer - +local dns_resolver local parsed_domain +local function parse_args(args) + if args and args["dns_resolver"] then + dns_resolver = args["dns_resolver"] + end +end + + local _M = {version = 0.3} -function _M.http_init() +function _M.http_init(args) require("resty.core") if require("ffi").os == "Linux" then @@ -57,7 +64,7 @@ function _M.http_init() seed = ngx.now() * 1000 + ngx.worker.pid() end math.randomseed(seed) - + parse_args(args) core.id.init() end @@ -171,11 +178,12 @@ end local function parse_domain_in_up(up, ver) - local local_conf = core.config.local_conf() - local dns_resolver = local_conf and local_conf.apisix and - local_conf.apisix.dns_resolver + if not dns_resolver then + local local_conf = core.config.local_conf() + dns_resolver = local_conf and local_conf.apisix and + local_conf.apisix.dns_resolver + end local new_nodes = core.table.new(0, 8) - for addr, weight in pairs(up.value.nodes) do local host, port = core.utils.parse_addr(addr) if not ipmatcher.parse_ipv4(host) and @@ -209,11 +217,12 @@ end local function parse_domain_in_route(route, ver) - local local_conf = core.config.local_conf() - local dns_resolver = local_conf and local_conf.apisix and - local_conf.apisix.dns_resolver + if not dns_resolver then + local local_conf = core.config.local_conf() + dns_resolver = local_conf and local_conf.apisix and + local_conf.apisix.dns_resolver + end local new_nodes = core.table.new(0, 8) - for addr, weight in pairs(route.value.upstream.nodes) do local host, port = core.utils.parse_addr(addr) if not ipmatcher.parse_ipv4(host) and @@ -532,8 +541,9 @@ end end -- do -function _M.stream_init() +function _M.stream_init(args) core.log.info("enter stream_init") + parse_args(args) end