add support for ExternalName service type in dynamic mode
This commit is contained in:
parent
bdb5ddc473
commit
d4faf68416
13 changed files with 316 additions and 18 deletions
70
rootfs/etc/nginx/lua/util/dns.lua
Normal file
70
rootfs/etc/nginx/lua/util/dns.lua
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
local resolver = require("resty.dns.resolver")
|
||||
local lrucache = require("resty.lrucache")
|
||||
local configuration = require("configuration")
|
||||
local util = require("util")
|
||||
|
||||
local _M = {}
|
||||
local CACHE_SIZE = 10000
|
||||
local MAXIMUM_TTL_VALUE = 2147483647 -- maximum value according to https://tools.ietf.org/html/rfc2181
|
||||
|
||||
local cache, err = lrucache.new(CACHE_SIZE)
|
||||
if not cache then
|
||||
return error("failed to create the cache: " .. (err or "unknown"))
|
||||
end
|
||||
|
||||
local function a_records_and_max_ttl(answers)
|
||||
local addresses = {}
|
||||
local ttl = MAXIMUM_TTL_VALUE -- maximum value according to https://tools.ietf.org/html/rfc2181
|
||||
|
||||
for _, ans in ipairs(answers) do
|
||||
if ans.address then
|
||||
table.insert(addresses, ans.address)
|
||||
if ttl > ans.ttl then
|
||||
ttl = ans.ttl
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return addresses, ttl
|
||||
end
|
||||
|
||||
function _M.resolve(host)
|
||||
local cached_addresses = cache:get(host)
|
||||
if cached_addresses then
|
||||
ngx.log(ngx.INFO, string.format("addresses %s for host %s was resolved from cache", table.concat(cached_addresses, ", "), host))
|
||||
return cached_addresses
|
||||
end
|
||||
|
||||
local r, err = resolver:new{
|
||||
nameservers = util.deepcopy(configuration.nameservers),
|
||||
retrans = 5,
|
||||
timeout = 2000, -- 2 sec
|
||||
}
|
||||
|
||||
if not r then
|
||||
ngx.log(ngx.ERR, "failed to instantiate the resolver: " .. tostring(err))
|
||||
return { host }
|
||||
end
|
||||
|
||||
local answers, err, _tries = r:query(host, { qtype = r.TYPE_A }, {})
|
||||
if not answers then
|
||||
ngx.log(ngx.ERR, "failed to query the DNS server: " .. tostring(err))
|
||||
return { host }
|
||||
end
|
||||
|
||||
if answers.errcode then
|
||||
ngx.log(ngx.ERR, string.format("server returned error code: %s: %s", answers.errcode, answers.errstr))
|
||||
return { host }
|
||||
end
|
||||
|
||||
local addresses, ttl = a_records_and_max_ttl(answers)
|
||||
if #addresses == 0 then
|
||||
ngx.log(ngx.ERR, "no A record resolved")
|
||||
return { host }
|
||||
end
|
||||
|
||||
cache:set(host, addresses, ttl)
|
||||
return addresses
|
||||
end
|
||||
|
||||
return _M
|
||||
Loading…
Add table
Add a link
Reference in a new issue