-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
feat: add dns discovery #3629
Merged
Merged
feat: add dns discovery #3629
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
http_code_upstream_unavailable
:For static variables, use capital letters
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@membphis
I have submitted another PR to use the number literal directly: #3672, which is more consistent in the style.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed my idea and followed your suggestion.