Skip to content
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: support DNS AAAA record #3484

Merged
merged 3 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis/linux_openresty_common_runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
7 changes: 3 additions & 4 deletions apisix/core/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
21 changes: 21 additions & 0 deletions t/APISIX.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -634,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;
3 changes: 2 additions & 1 deletion t/core/etcd-auth-fail.t
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
3 changes: 2 additions & 1 deletion t/core/etcd-auth.t
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
4 changes: 4 additions & 0 deletions t/coredns/Corefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test.local {
file db.test.local
log
}
13 changes: 13 additions & 0 deletions t/coredns/db.test.local
Original file line number Diff line number Diff line change
@@ -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
71 changes: 71 additions & 0 deletions t/node/upstream-domain-with-special-dns.t
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion utils/centos7-ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions utils/set-dns.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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