Last active
July 31, 2023 22:07
-
-
Save roguh/34e93d2568d637be93b2c72695b46abd to your computer and use it in GitHub Desktop.
Revisions
-
roguh revised this gist
Jul 31, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ function resolve_hostname(hostname) local dns_resolver_lib = require("resty.dns.resolver") local dns_resolver, dns_resolver_err = dns_resolver_lib:new({ -
roguh created this gist
Jul 31, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,51 @@ function _M.resolve_hostname(hostname) local dns_resolver_lib = require("resty.dns.resolver") local dns_resolver, dns_resolver_err = dns_resolver_lib:new({ -- Kubernetes default nameserver IP -- If your hostnames are public, try Google's DNS nameserver 8.8.8.8 nameservers = {"kube-dns.kube-system.svc.cluster.local"}, -- 250 milliseconds timeout = 250, retrans = 2 }) if not dns_resolver then ngx.log( ngx.ERR, "Failed to instantiate lua-resty-dns-resolver", dns_resolver_err ) return nil end local dns_answers, dns_err, dns_tries = dns_resolver:query(hostname, nil, {}) if not dns_answers then ngx.log( ngx.ERR, "Failed to query DNS server: ", dns_err, table.concat(dns_tries, "\n ") ) return nil end if dns_answers.errcode then ngx.log( ngx.ERR, "DNS server returned error code ", dns_answers.errcode, ": ", dns_answers.errstr ) return nil end for i, answer in ipairs(dns_answers) do local ip = answer.address or answer.cname if ip ~= nil then return ip end end return nil end