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 configuring the order of the upstream dns service discovery resolve #7935

6 changes: 5 additions & 1 deletion apisix/core/config_local.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

local file = require("apisix.cli.file")
local schema = require("apisix.cli.schema")
local util = require("apisix.cli.util")


local _M = {}
Expand Down Expand Up @@ -65,7 +66,10 @@ function _M.local_conf(force)
end

-- fill the default value by the schema
schema.validate(default_conf)
local ok, err = schema.validate(default_conf)
if not ok then
error(err)
end

config_data = default_conf
return config_data
Expand Down
6 changes: 5 additions & 1 deletion apisix/discovery/dns/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,15 @@ function _M.init_worker()
local local_conf = config_local.local_conf()
local servers = local_conf.discovery.dns.servers

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 = {},
nameservers = servers,
order = {"last", "SRV", "A", "AAAA", "CNAME"},
order = order,
}

local client, err = core.dns_client.new(opts)
Expand Down
9 changes: 9 additions & 0 deletions apisix/discovery/dns/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ return {
type = "string",
},
},
order = {
type = "array",
minItems = 1,
maxItems = 5,
uniqueItems = true,
items = {
enum = {"last", "SRV", "A", "AAAA", "CNAME"}
},
},
},
required = {"servers"}
}
6 changes: 6 additions & 0 deletions conf/config-default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ nginx_config: # config for render the template to generate n
# dns:
# servers:
# - "127.0.0.1:8600" # use the real address of your dns server
# order: # order in which to try different dns record types when resolving
# - last # "last" will try the last previously successful type for a hostname.
# - SRV
# - A
# - AAAA
# - CNAME
# eureka:
# host: # it's possible to define multiple eureka hosts addresses of the same eureka cluster.
# - "http://127.0.0.1:8761"
Expand Down
120 changes: 120 additions & 0 deletions t/discovery/dns/sanity.t
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ upstreams:
id: 1
--- response_body
hello world
--- grep_error_log eval
qr/proxy request to \S+/
--- grep_error_log_out
proxy request to [0:0:0:0:0:0:0:1]:1980



Expand All @@ -133,6 +137,10 @@ upstreams:
id: 1
--- response_body
hello world
--- grep_error_log eval
qr/proxy request to \S+/
--- grep_error_log_out
proxy request to 127.0.0.1:1980



Expand Down Expand Up @@ -318,3 +326,115 @@ qr/upstream nodes: \{[^}]+\}/
qr/upstream nodes: \{("127.0.0.1:1980":60,"127.0.0.2:1980":20|"127.0.0.2:1980":20,"127.0.0.1:1980":60)\}/
--- response_body
hello world



=== TEST 16: prefer A than SRV when A is ahead of SRV in config.yaml
--- yaml_config
apisix:
node_listen: 1984
enable_admin: false
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
dns:
servers:
- "127.0.0.1:1053"
order:
- A
- SRV
--- apisix_yaml
upstreams:
- service_name: "srv-a.test.local"
discovery_type: dns
type: roundrobin
id: 1
--- error_code: 502
--- error_log
proxy request to 127.0.0.1:80



=== TEST 17: Invalid order type in config.yaml
--- yaml_config
apisix:
node_listen: 1984
enable_admin: false
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
dns:
servers:
- "127.0.0.1:1053"
order:
- B
- SRV
--- apisix_yaml
upstreams:
- service_name: "srv-a.test.local"
discovery_type: dns
type: roundrobin
id: 1
--- must_die
--- error_log
matches none of the enum values



=== TEST 18: Multiple order type in config.yaml
--- yaml_config
apisix:
node_listen: 1984
enable_admin: false
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
dns:
servers:
- "127.0.0.1:1053"
order:
- SRV
- SRV
--- apisix_yaml
upstreams:
- service_name: "srv-a.test.local"
discovery_type: dns
type: roundrobin
id: 1
--- must_die
--- error_log
expected unique items but items 1 and 2 are equal



=== TEST 19: invalid order type in config.yaml
--- yaml_config
apisix:
node_listen: 1984
enable_admin: false
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
discovery:
dns:
servers:
- "127.0.0.1:1053"
order:
- a
- SRV
--- apisix_yaml
upstreams:
- service_name: "srv-a.test.local"
discovery_type: dns
type: roundrobin
id: 1
--- must_die
--- error_log
matches none of the enum values
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test case: DNS Server returns SRV records first for the same domain name, and the next query returns A records?

4 changes: 2 additions & 2 deletions t/plugin/redirect.t
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,8 @@ apisix:
ssl:
enable: true
listen:
- 6443
- 7443
- port: 6443
- port: 7443
- port: 8443
- port: 9443
--- request
Expand Down