Skip to content

Commit

Permalink
feat: add support for passing resolv.conf in dns discovery (#9770)
Browse files Browse the repository at this point in the history
  • Loading branch information
Revolyssup committed Sep 26, 2023
1 parent 299b525 commit ddac444
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 3 deletions.
4 changes: 2 additions & 2 deletions apisix/discovery/dns/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ end
function _M.init_worker()
local local_conf = config_local.local_conf()
local servers = local_conf.discovery.dns.servers

local resolv_conf = local_conf.discovery.dns.resolv_conf
local default_order = {"last", "SRV", "A", "AAAA", "CNAME"}
local order = core.table.try_read_attr(local_conf, "discovery", "dns", "order")
order = order or default_order

local opts = {
hosts = {},
resolvConf = {},
resolvConf = resolv_conf,
nameservers = servers,
order = order,
}
Expand Down
12 changes: 11 additions & 1 deletion apisix/discovery/dns/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ return {
type = "string",
},
},
resolv_conf = {
type = "string",
},
order = {
type = "array",
minItems = 1,
Expand All @@ -34,5 +37,12 @@ return {
},
},
},
required = {"servers"}
oneOf = {
{
required = {"servers"},
},
{
required = {"resolv_conf"},
}
}
}
3 changes: 3 additions & 0 deletions ci/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ set_coredns() {
pushd t/coredns || exit 1
../../build-cache/coredns -dns.port=1053 &
popd || exit 1

touch build-cache/test_resolve.conf
echo "nameserver 127.0.0.1:1053" > build-cache/test_resolve.conf
}

GRPC_SERVER_EXAMPLE_VER=20210819
Expand Down
1 change: 1 addition & 0 deletions conf/config-default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ nginx_config: # Config for render the template to generate n
# dns:
# servers:
# - "127.0.0.1:8600" # Replace with the address of your DNS server.
# resolv_conf: /etc/resolv.conf # Replace with the path to the local DNS resolv config. Configure either "servers" or "resolv_conf".
# order: # Resolve DNS records this order.
# - last # Try the latest successful type for a hostname.
# - SRV
Expand Down
1 change: 1 addition & 0 deletions t/discovery/dns/mix.t
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,4 @@ connect to 127.0.0.1:1053
connect to 127.0.0.1:1053
connect to 127.0.0.1:1053
connect to 127.0.0.1:1053
connect to 127.0.0.1:1053
27 changes: 27 additions & 0 deletions t/discovery/dns/sanity.t
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,30 @@ upstreams:
--- must_die
--- error_log
matches none of the enum values
=== TEST 20: use resolv.conf
--- yaml_config
apisix:
node_listen: 1984
enable_admin: false
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery: # service discovery center
dns:
resolv_conf: build-cache/test_resolve.conf
--- apisix_yaml
upstreams:
- service_name: "sd.test.local:1980"
discovery_type: dns
type: roundrobin
id: 1
--- grep_error_log eval
qr/upstream nodes: \{[^}]+\}/
--- grep_error_log_out eval
qr/upstream nodes: \{("127.0.0.1:1980":1,"127.0.0.2:1980":1|"127.0.0.2:1980":1,"127.0.0.1:1980":1)\}/
--- response_body
hello world

0 comments on commit ddac444

Please sign in to comment.