-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #3517 Signed-off-by: spacewander <spacewanderlzx@gmail.com>
- Loading branch information
1 parent
7dde426
commit 197f650
Showing
14 changed files
with
614 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
-- | ||
-- Licensed to the Apache Software Foundation (ASF) under one or more | ||
-- contributor license agreements. See the NOTICE file distributed with | ||
-- this work for additional information regarding copyright ownership. | ||
-- The ASF licenses this file to You under the Apache License, Version 2.0 | ||
-- (the "License"); you may not use this file except in compliance with | ||
-- the License. You may obtain a copy of the License at | ||
-- | ||
-- http://www.apache.org/licenses/LICENSE-2.0 | ||
-- | ||
-- Unless required by applicable law or agreed to in writing, software | ||
-- distributed under the License is distributed on an "AS IS" BASIS, | ||
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
-- See the License for the specific language governing permissions and | ||
-- limitations under the License. | ||
-- | ||
local require = require | ||
local log = require("apisix.core.log") | ||
local json = require("apisix.core.json") | ||
local table = require("apisix.core.table") | ||
local math_random = math.random | ||
local package_loaded = package.loaded | ||
local setmetatable = setmetatable | ||
|
||
|
||
local _M = { | ||
RETURN_RANDOM = 1, | ||
RETURN_ALL = 2, | ||
} | ||
|
||
|
||
function _M.resolve(self, domain, selector) | ||
local client = self.client | ||
|
||
-- this function will dereference the CNAME records | ||
local answers, err = client.resolve(domain) | ||
if not answers then | ||
return nil, "failed to query the DNS server: " .. err | ||
end | ||
|
||
if answers.errcode then | ||
return nil, "server returned error code: " .. answers.errcode | ||
.. ": " .. answers.errstr | ||
end | ||
|
||
if selector == _M.RETURN_ALL then | ||
log.info("dns resolve ", domain, ", result: ", json.delay_encode(answers)) | ||
return table.deepcopy(answers) | ||
end | ||
|
||
local idx = math_random(1, #answers) | ||
local answer = answers[idx] | ||
local dns_type = answer.type | ||
if dns_type == client.TYPE_A or dns_type == client.TYPE_AAAA then | ||
log.info("dns resolve ", domain, ", result: ", json.delay_encode(answer)) | ||
return table.deepcopy(answer) | ||
end | ||
|
||
return nil, "unsupport DNS answer" | ||
end | ||
|
||
|
||
function _M.new(opts) | ||
opts.ipv6 = true | ||
opts.timeout = 2000 -- 2 sec | ||
opts.retrans = 5 -- 5 retransmissions on receive timeout | ||
|
||
-- make sure each client has its separate room | ||
package_loaded["resty.dns.client"] = nil | ||
local dns_client_mod = require("resty.dns.client") | ||
|
||
local ok, err = dns_client_mod.init(opts) | ||
if not ok then | ||
return nil, "failed to init the dns client: " .. err | ||
end | ||
|
||
return setmetatable({client = dns_client_mod}, {__index = _M}) | ||
end | ||
|
||
|
||
return _M |
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
-- | ||
-- Licensed to the Apache Software Foundation (ASF) under one or more | ||
-- contributor license agreements. See the NOTICE file distributed with | ||
-- this work for additional information regarding copyright ownership. | ||
-- The ASF licenses this file to You under the Apache License, Version 2.0 | ||
-- (the "License"); you may not use this file except in compliance with | ||
-- the License. You may obtain a copy of the License at | ||
-- | ||
-- http://www.apache.org/licenses/LICENSE-2.0 | ||
-- | ||
-- Unless required by applicable law or agreed to in writing, software | ||
-- distributed under the License is distributed on an "AS IS" BASIS, | ||
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
-- See the License for the specific language governing permissions and | ||
-- limitations under the License. | ||
-- | ||
|
||
local core = require("apisix.core") | ||
local config_local = require("apisix.core.config_local") | ||
local ipairs = ipairs | ||
local error = error | ||
|
||
|
||
local dns_client | ||
local schema = { | ||
type = "object", | ||
properties = { | ||
servers = { | ||
type = "array", | ||
minItems = 1, | ||
items = { | ||
type = "string", | ||
}, | ||
}, | ||
}, | ||
required = {"servers"} | ||
} | ||
|
||
|
||
local _M = {} | ||
|
||
|
||
function _M.nodes(service_name) | ||
local host, port = core.utils.parse_addr(service_name) | ||
core.log.info("discovery dns with host ", host, ", port ", port) | ||
|
||
local records, err = dns_client:resolve(host, core.dns_client.RETURN_ALL) | ||
if not records then | ||
return nil, err | ||
end | ||
|
||
local nodes = core.table.new(#records, 0) | ||
for i, r in ipairs(records) do | ||
if r.address then | ||
nodes[i] = {host = r.address, weight = 1, port = port} | ||
end | ||
end | ||
|
||
return nodes | ||
end | ||
|
||
|
||
function _M.init_worker() | ||
local local_conf = config_local.local_conf() | ||
local ok, err = core.schema.check(schema, local_conf.discovery.dns) | ||
if not ok then | ||
error("invalid dns discovery configuration: " .. err) | ||
return | ||
end | ||
|
||
local servers = core.table.try_read_attr(local_conf, "discovery", "dns", "servers") | ||
|
||
local opts = { | ||
hosts = {}, | ||
resolvConf = {}, | ||
nameservers = servers, | ||
order = {"last", "A", "AAAA", "CNAME"}, -- avoid querying SRV (we don't support it yet) | ||
} | ||
|
||
local client, err = core.dns_client.new(opts) | ||
if not client then | ||
error("failed to init the dns client: ", err) | ||
return | ||
end | ||
|
||
dns_client = client | ||
end | ||
|
||
|
||
return _M |
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
Oops, something went wrong.