-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
feature: supported to use host name in upstream #522
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2e2e165
feature: supported to config resolver in yaml config file.
membphis 02ba2ba
optimize: use lrucache to cached merged result of `service` and `route`.
membphis a6d74f8
feature: parsed the host name to ip address in upstream or route.
membphis 268210a
change: name style.
membphis 1bb5400
change: local cache.
membphis b8d77c0
feature: supported to filter config data after read it from `etcd`.
membphis a62a98c
feature: supported to parse host domain in upstream.
membphis 22780b5
feature: support to parse domain host in upstream of route.
membphis bac9372
bugfix: do return directly if there was no upstream.
membphis 8ca03cb
bugfix: used wrong operator.
membphis fde5336
doc
membphis 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 |
---|---|---|
|
@@ -38,7 +38,7 @@ http { | |
|
||
lua_socket_log_errors off; | ||
|
||
resolver ipv6=off local=on; | ||
resolver 114.114.114.114 ipv6=off local=on; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
resolver_timeout 5; | ||
|
||
lua_http10_buffering off; | ||
|
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 |
---|---|---|
@@ -1,24 +1,32 @@ | ||
-- Copyright (C) Yuansheng Wang | ||
|
||
local require = require | ||
local core = require("apisix.core") | ||
local plugin = require("apisix.plugin") | ||
local require = require | ||
local core = require("apisix.core") | ||
local plugin = require("apisix.plugin") | ||
local service_fetch = require("apisix.http.service").get | ||
local admin_init = require("apisix.admin.init") | ||
local get_var = require("resty.ngxvar").fetch | ||
local router = require("apisix.router") | ||
local ngx = ngx | ||
local get_method = ngx.req.get_method | ||
local ngx_exit = ngx.exit | ||
local ngx_ERROR = ngx.ERROR | ||
local math = math | ||
local error = error | ||
local ngx_var = ngx.var | ||
local ipairs = ipairs | ||
local admin_init = require("apisix.admin.init") | ||
local get_var = require("resty.ngxvar").fetch | ||
local router = require("apisix.router") | ||
local ipmatcher = require("resty.ipmatcher") | ||
local ngx = ngx | ||
local get_method = ngx.req.get_method | ||
local ngx_exit = ngx.exit | ||
local ngx_ERROR = ngx.ERROR | ||
local math = math | ||
local error = error | ||
local ngx_var = ngx.var | ||
local ipairs = ipairs | ||
local pairs = pairs | ||
local tostring = tostring | ||
local load_balancer | ||
|
||
|
||
local _M = {version = 0.2} | ||
local parsed_domain = core.lrucache.new({ | ||
ttl = 300, count = 512 | ||
}) | ||
|
||
|
||
local _M = {version = 0.3} | ||
|
||
|
||
function _M.http_init() | ||
|
@@ -143,6 +151,70 @@ function _M.http_ssl_phase() | |
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 | ||
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 | ||
not ipmatcher.parse_ipv6(host) then | ||
local ip_info = core.utils.dns_parse(dns_resolver, host) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should not drop the error msg of |
||
core.log.info("parse addr: ", core.json.delay_encode(ip_info), | ||
" resolver: ", core.json.delay_encode(dns_resolver), | ||
" addr: ", addr) | ||
if ip_info and ip_info.address then | ||
new_nodes[ip_info.address .. ":" .. port] = weight | ||
core.log.info("dns resolver domain: ", host, " to ", | ||
ip_info.address) | ||
end | ||
else | ||
new_nodes[addr] = weight | ||
end | ||
end | ||
|
||
up.dns_value = core.table.clone(up.value) | ||
up.dns_value.nodes = new_nodes | ||
core.log.info("parse upstream which contain domain: ", | ||
core.json.delay_encode(up)) | ||
return up | ||
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 | ||
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 | ||
not ipmatcher.parse_ipv6(host) then | ||
local ip_info = core.utils.dns_parse(dns_resolver, host) | ||
core.log.info("parse addr: ", core.json.delay_encode(ip_info), | ||
" resolver: ", core.json.delay_encode(dns_resolver), | ||
" addr: ", addr) | ||
if ip_info and ip_info.address then | ||
new_nodes[ip_info.address .. ":" .. port] = weight | ||
core.log.info("dns resolver domain: ", host, " to ", | ||
ip_info.address) | ||
end | ||
else | ||
new_nodes[addr] = weight | ||
end | ||
end | ||
|
||
route.dns_value = core.table.deepcopy(route.value) | ||
route.dns_value.upstream.nodes = new_nodes | ||
core.log.info("parse route which contain domain: ", | ||
core.json.delay_encode(route)) | ||
return route | ||
end | ||
|
||
|
||
do | ||
local upstream_vars = { | ||
uri = "upstream_uri", | ||
|
@@ -226,7 +298,7 @@ function _M.http_access_phase() | |
end | ||
|
||
local changed | ||
route, changed = plugin.merge_service_route(service, route) | ||
route, changed = plugin.merge_route(service, route) | ||
api_ctx.matched_route = route | ||
|
||
if changed then | ||
|
@@ -240,13 +312,28 @@ function _M.http_access_phase() | |
api_ctx.conf_version = service.modifiedIndex | ||
api_ctx.conf_id = service.value.id | ||
end | ||
|
||
else | ||
api_ctx.conf_type = "route" | ||
api_ctx.conf_version = route.modifiedIndex | ||
api_ctx.conf_id = route.value.id | ||
end | ||
|
||
local up_id = route.value.upstream_id | ||
if up_id then | ||
local upstreams_etcd = core.config.fetch_created_obj("/upstreams") | ||
if upstreams_etcd then | ||
local upstream = upstreams_etcd:get(tostring(up_id)) | ||
if upstream.has_domain then | ||
parsed_domain(upstream, api_ctx.conf_version, | ||
parse_domain_in_up, upstream) | ||
end | ||
end | ||
|
||
elseif route.has_domain then | ||
route = parsed_domain(route, api_ctx.conf_version, | ||
parse_domain_in_route, route) | ||
end | ||
|
||
local plugins = core.tablepool.fetch("plugins", 32, 0) | ||
api_ctx.plugins = plugin.filter(route, plugins) | ||
|
||
|
@@ -288,7 +375,7 @@ function _M.grpc_access_phase() | |
end | ||
|
||
local changed | ||
route, changed = plugin.merge_service_route(service, route) | ||
route, changed = plugin.merge_route(service, route) | ||
api_ctx.matched_route = route | ||
|
||
if changed then | ||
|
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.
114.114.114.114
is only friendly for Chinese. I think8.8.8.8
or1.1.1.1
is better.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.
After the international lover feel that it is wrong, we modify it better.