From 8632835b90bac53dee0074d771d931cd7205d046 Mon Sep 17 00:00:00 2001 From: spacewander Date: Tue, 2 Feb 2021 05:41:15 +0800 Subject: [PATCH 1/3] feat: support DNS AAAA record Fix #2495 Signed-off-by: spacewander --- apisix/core/utils.lua | 7 +-- t/APISIX.pm | 4 ++ t/coredns/Corefile | 4 ++ t/coredns/db.test.local | 13 +++++ t/node/upstream-domain-with-special-dns.t | 71 +++++++++++++++++++++++ utils/set-dns.sh | 14 +++++ 6 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 t/coredns/Corefile create mode 100644 t/coredns/db.test.local create mode 100644 t/node/upstream-domain-with-special-dns.t diff --git a/apisix/core/utils.lua b/apisix/core/utils.lua index 60e30684e579..21a40b107e60 100644 --- a/apisix/core/utils.lua +++ b/apisix/core/utils.lua @@ -16,6 +16,7 @@ -- local core_str = require("apisix.core.string") local table = require("apisix.core.table") +local json = require("apisix.core.json") local log = require("apisix.core.log") local string = require("apisix.core.string") local ngx_re = require("ngx.re") @@ -98,8 +99,6 @@ local function dns_parse(domain) current_inited_resolvers = dns_resolvers end - log.info("dns resolve ", domain) - -- this function will dereference the CNAME records local answers, err = dns_client.resolve(domain) if not answers then @@ -114,8 +113,8 @@ local function dns_parse(domain) local idx = math.random(1, #answers) local answer = answers[idx] local dns_type = answer.type - -- TODO: support AAAA & SRV - if dns_type == dns_client.TYPE_A then + if dns_type == dns_client.TYPE_A or dns_type == dns_client.TYPE_AAAA then + log.info("dns resolve ", domain, ", result: ", json.delay_encode(answer)) return table.deepcopy(answer) end diff --git a/t/APISIX.pm b/t/APISIX.pm index 90417e1eda59..7ea6384366f2 100644 --- a/t/APISIX.pm +++ b/t/APISIX.pm @@ -69,6 +69,10 @@ if ($enable_local_dns) { $dns_addrs_str = "8.8.8.8 114.114.114.114"; $dns_addrs_tbl_str = "{\"8.8.8.8\", \"114.114.114.114\"}"; } +my $custom_dns_server = $ENV{"CUSTOM_DNS_SERVER"}; +if ($custom_dns_server) { + $dns_addrs_tbl_str = "{\"$custom_dns_server\"}"; +} my $default_yaml_config = read_file("conf/config-default.yaml"); diff --git a/t/coredns/Corefile b/t/coredns/Corefile new file mode 100644 index 000000000000..53dad79e08eb --- /dev/null +++ b/t/coredns/Corefile @@ -0,0 +1,4 @@ +test.local { + file db.test.local + log +} diff --git a/t/coredns/db.test.local b/t/coredns/db.test.local new file mode 100644 index 000000000000..1c406bff72d5 --- /dev/null +++ b/t/coredns/db.test.local @@ -0,0 +1,13 @@ +$ORIGIN test.local. +@ 3600 IN SOA sns.dns.icann.org. noc.dns.icann.org. ( + 2017042745 ; serial + 7200 ; refresh (2 hours) + 3600 ; retry (1 hour) + 1209600 ; expire (2 weeks) + 3600 ; minimum (1 hour) + ) + + 3600 IN NS a.iana-servers.net. + 3600 IN NS b.iana-servers.net. + +ipv6 IN AAAA ::1 diff --git a/t/node/upstream-domain-with-special-dns.t b/t/node/upstream-domain-with-special-dns.t new file mode 100644 index 000000000000..e968ec85ff5b --- /dev/null +++ b/t/node/upstream-domain-with-special-dns.t @@ -0,0 +1,71 @@ +# +# 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. +# +BEGIN { + $ENV{CUSTOM_DNS_SERVER} = "127.0.0.1:1053"; +} + +use t::APISIX 'no_plan'; + +repeat_each(1); +log_level('info'); +no_root_location(); +no_shuffle(); + +add_block_preprocessor(sub { + my ($block) = @_; + + my $yaml_config = $block->yaml_config // <<_EOC_; +apisix: + node_listen: 1984 + config_center: yaml + enable_admin: false +_EOC_ + + $block->set_value("yaml_config", $yaml_config); + + my $routes = <<_EOC_; +routes: + - + uri: /hello + upstream_id: 1 +#END +_EOC_ + + $block->set_value("apisix_yaml", $block->apisix_yaml . $routes); + + if (!$block->error_log && !$block->no_error_log) { + $block->set_value("no_error_log", "[error]"); + } +}); + +run_tests(); + +__DATA__ + +=== TEST 1: AAAA +--- listen_ipv6 +--- apisix_yaml +upstreams: + - + id: 1 + nodes: + ipv6.test.local:1980: 1 + type: roundrobin +--- request +GET /hello +--- response_body +hello world diff --git a/utils/set-dns.sh b/utils/set-dns.sh index 618b03300732..10744a5152db 100755 --- a/utils/set-dns.sh +++ b/utils/set-dns.sh @@ -24,3 +24,17 @@ cat /etc/hosts # check GitHub Action's configuration echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf echo "search apache.org" | sudo tee -a /etc/resolv.conf + +mkdir -p build-cache + +if [ ! -f "build-cache/coredns_1_8_1" ]; then + wget https://github.com/coredns/coredns/releases/download/v1.8.1/coredns_1.8.1_linux_amd64.tgz + tar -xvf coredns_1.8.1_linux_amd64.tgz + mv coredns build-cache/ + + touch build-cache/coredns_1_8_1 +fi + +pushd t/coredns || exit 1 +../../build-cache/coredns -dns.port=1053 & +popd || exit 1 From 9c9460cbdb95cbc4a96153997b3fbb8e3db05725 Mon Sep 17 00:00:00 2001 From: spacewander Date: Wed, 3 Feb 2021 23:10:40 +0800 Subject: [PATCH 2/3] indent Signed-off-by: spacewander --- utils/set-dns.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/set-dns.sh b/utils/set-dns.sh index 10744a5152db..1d6ae4059653 100755 --- a/utils/set-dns.sh +++ b/utils/set-dns.sh @@ -28,7 +28,7 @@ echo "search apache.org" | sudo tee -a /etc/resolv.conf mkdir -p build-cache if [ ! -f "build-cache/coredns_1_8_1" ]; then - wget https://github.com/coredns/coredns/releases/download/v1.8.1/coredns_1.8.1_linux_amd64.tgz + wget https://github.com/coredns/coredns/releases/download/v1.8.1/coredns_1.8.1_linux_amd64.tgz tar -xvf coredns_1.8.1_linux_amd64.tgz mv coredns build-cache/ From a459946a3d6a4310bae7d4d2b513fbb7a2c3d558 Mon Sep 17 00:00:00 2001 From: spacewander Date: Thu, 4 Feb 2021 18:23:14 +0800 Subject: [PATCH 3/3] test Signed-off-by: spacewander --- .travis/linux_openresty_common_runner.sh | 2 +- t/APISIX.pm | 17 +++++++++++++++++ t/core/etcd-auth-fail.t | 3 ++- t/core/etcd-auth.t | 3 ++- utils/centos7-ci.sh | 2 +- 5 files changed, 23 insertions(+), 4 deletions(-) diff --git a/.travis/linux_openresty_common_runner.sh b/.travis/linux_openresty_common_runner.sh index fe50864ee34b..55d42dad15fd 100755 --- a/.travis/linux_openresty_common_runner.sh +++ b/.travis/linux_openresty_common_runner.sh @@ -142,7 +142,7 @@ script() { make lint && make license-check || exit 1 # APISIX_ENABLE_LUACOV=1 PERL5LIB=.:$PERL5LIB prove -Itest-nginx/lib -r t - PERL5LIB=.:$PERL5LIB prove -Itest-nginx/lib -r t + FLUSH_ETCD=1 PERL5LIB=.:$PERL5LIB prove -Itest-nginx/lib -r t } after_success() { diff --git a/t/APISIX.pm b/t/APISIX.pm index 7ea6384366f2..618443b4c30c 100644 --- a/t/APISIX.pm +++ b/t/APISIX.pm @@ -638,4 +638,21 @@ _EOC_ $block; }); +sub run_or_exit ($) { + my ($cmd) = @_; + my $output = `$cmd`; + if ($?) { + warn "$output"; + exit 1; + } +} + +add_cleanup_handler(sub { + if ($ENV{FLUSH_ETCD}) { + delete $ENV{APISIX_PROFILE}; + run_or_exit "etcdctl del --prefix /apisix"; + run_or_exit "./bin/apisix init_etcd"; + } +}); + 1; diff --git a/t/core/etcd-auth-fail.t b/t/core/etcd-auth-fail.t index 5b3d70482c51..e04eca8fb6c6 100644 --- a/t/core/etcd-auth-fail.t +++ b/t/core/etcd-auth-fail.t @@ -15,7 +15,8 @@ # limitations under the License. # BEGIN { - $ENV{"ETCD_ENABLE_AUTH"} = "false" + $ENV{"ETCD_ENABLE_AUTH"} = "false"; + delete $ENV{"FLUSH_ETCD"}; } use t::APISIX 'no_plan'; diff --git a/t/core/etcd-auth.t b/t/core/etcd-auth.t index 794013aaa864..e83fb9cff3b8 100644 --- a/t/core/etcd-auth.t +++ b/t/core/etcd-auth.t @@ -15,7 +15,8 @@ # limitations under the License. # BEGIN { - $ENV{"ETCD_ENABLE_AUTH"} = "true" + $ENV{"ETCD_ENABLE_AUTH"} = "true"; + delete $ENV{"FLUSH_ETCD"}; } use t::APISIX 'no_plan'; diff --git a/utils/centos7-ci.sh b/utils/centos7-ci.sh index 9ef6f858f99a..d0065ac00c97 100755 --- a/utils/centos7-ci.sh +++ b/utils/centos7-ci.sh @@ -64,7 +64,7 @@ run_case() { export_or_prefix ./utils/set-dns.sh # run test cases - prove -I./test-nginx/lib -I./ -r -s t/ + FLUSH_ETCD=1 prove -I./test-nginx/lib -I./ -r t/ } case_opt=$1