From ddac444421dd20b45a78a232f558d364b6fb3fdd Mon Sep 17 00:00:00 2001 From: Ashish Tiwari Date: Tue, 26 Sep 2023 09:28:48 +0530 Subject: [PATCH] feat: add support for passing resolv.conf in dns discovery (#9770) --- apisix/discovery/dns/init.lua | 4 ++-- apisix/discovery/dns/schema.lua | 12 +++++++++++- ci/common.sh | 3 +++ conf/config-default.yaml | 1 + t/discovery/dns/mix.t | 1 + t/discovery/dns/sanity.t | 27 +++++++++++++++++++++++++++ 6 files changed, 45 insertions(+), 3 deletions(-) diff --git a/apisix/discovery/dns/init.lua b/apisix/discovery/dns/init.lua index 609ad5ea163f..601de0ebc9ee 100644 --- a/apisix/discovery/dns/init.lua +++ b/apisix/discovery/dns/init.lua @@ -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, } diff --git a/apisix/discovery/dns/schema.lua b/apisix/discovery/dns/schema.lua index 989938ab1fa3..03c7934ae4cf 100644 --- a/apisix/discovery/dns/schema.lua +++ b/apisix/discovery/dns/schema.lua @@ -24,6 +24,9 @@ return { type = "string", }, }, + resolv_conf = { + type = "string", + }, order = { type = "array", minItems = 1, @@ -34,5 +37,12 @@ return { }, }, }, - required = {"servers"} + oneOf = { + { + required = {"servers"}, + }, + { + required = {"resolv_conf"}, + } + } } diff --git a/ci/common.sh b/ci/common.sh index e1961fc6b01e..3ad1e651cf30 100644 --- a/ci/common.sh +++ b/ci/common.sh @@ -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 diff --git a/conf/config-default.yaml b/conf/config-default.yaml index f15f6d1df604..09c459677fcc 100755 --- a/conf/config-default.yaml +++ b/conf/config-default.yaml @@ -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 diff --git a/t/discovery/dns/mix.t b/t/discovery/dns/mix.t index 6453b5c37a01..e8499429842e 100644 --- a/t/discovery/dns/mix.t +++ b/t/discovery/dns/mix.t @@ -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 diff --git a/t/discovery/dns/sanity.t b/t/discovery/dns/sanity.t index c26eceeba04b..4e0eaa7ae194 100644 --- a/t/discovery/dns/sanity.t +++ b/t/discovery/dns/sanity.t @@ -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