diff --git a/kong.conf.default b/kong.conf.default index 284fd13894d..00201c158fc 100644 --- a/kong.conf.default +++ b/kong.conf.default @@ -1542,9 +1542,7 @@ # It provides observable statistics, you can retrieve them through the Admin API # `/status/dns`. -#legacy_dns_client = off # Disable the new DNS resolver, using the - # original DNS resolver. See above `dns_xxx` - # options for the original DNS resolver. +#new_dns_client = on # Enable the new DNS resolver #resolver_address = # Comma-separated list of nameservers, each diff --git a/kong/api/routes/dns.lua b/kong/api/routes/dns.lua index 37892a74b21..cdd022f6975 100644 --- a/kong/api/routes/dns.lua +++ b/kong/api/routes/dns.lua @@ -5,7 +5,7 @@ return { ["/status/dns"] = { GET = function (self, db, helpers) - if kong.configuration.legacy_dns_client then + if not kong.configuration.new_dns_client then return kong.response.exit(501, { message = "not implemented with the legacy DNS client" }) diff --git a/kong/api/routes/kong.lua b/kong/api/routes/kong.lua index 633083a6d5f..6ade907bc19 100644 --- a/kong/api/routes/kong.lua +++ b/kong/api/routes/kong.lua @@ -272,7 +272,7 @@ return { }, ["/status/dns"] = { GET = function (self, db, helpers) - if kong.configuration.legacy_dns_client then + if not kong.configuration.new_dns_client then return kong.response.exit(501, { message = "not implemented with the legacy DNS client" }) end diff --git a/kong/conf_loader/constants.lua b/kong/conf_loader/constants.lua index 894d80df3d7..7b233c9ff94 100644 --- a/kong/conf_loader/constants.lua +++ b/kong/conf_loader/constants.lua @@ -371,7 +371,7 @@ local CONF_PARSERS = { dns_error_ttl = { typ = "number" }, dns_no_sync = { typ = "boolean" }, - legacy_dns_client = { typ = "boolean" }, + new_dns_client = { typ = "boolean" }, resolver_address = { typ = "array" }, resolver_hosts_file = { typ = "string" }, diff --git a/kong/resty/dns/client.lua b/kong/resty/dns/client.lua index a5872227ff9..e5fce8b97ff 100644 --- a/kong/resty/dns/client.lua +++ b/kong/resty/dns/client.lua @@ -1,6 +1,6 @@ -- Use the new dns client library instead. If you want to switch to the original --- one, you can set `legacy_dns_client = on` in kong.conf. -if ngx.shared.kong_dns_cache and not _G.busted_legacy_dns_client then +-- one, you can set `new_dns_client = off` in kong.conf. +if ngx.shared.kong_dns_cache and _G.busted_new_dns_client ~= false then package.loaded["kong.dns.client"] = nil return require("kong.dns.client") end diff --git a/kong/templates/kong_defaults.lua b/kong/templates/kong_defaults.lua index 153a61ad076..a5e71c627d9 100644 --- a/kong/templates/kong_defaults.lua +++ b/kong/templates/kong_defaults.lua @@ -170,7 +170,7 @@ dns_not_found_ttl = 30 dns_error_ttl = 1 dns_no_sync = off -legacy_dns_client = off +new_dns_client = on resolver_address = NONE resolver_hosts_file = /etc/hosts diff --git a/kong/templates/nginx_kong.lua b/kong/templates/nginx_kong.lua index a02336697f7..98f4bd2ba62 100644 --- a/kong/templates/nginx_kong.lua +++ b/kong/templates/nginx_kong.lua @@ -23,7 +23,7 @@ lua_shared_dict kong_db_cache ${{MEM_CACHE_SIZE}}; lua_shared_dict kong_db_cache_miss 12m; lua_shared_dict kong_secrets 5m; -> if not legacy_dns_client then +> if new_dns_client then lua_shared_dict kong_dns_cache ${{RESOLVER_MEM_CACHE_SIZE}}; > end diff --git a/kong/tools/dns.lua b/kong/tools/dns.lua index 22f32a8f9c1..811585660f9 100644 --- a/kong/tools/dns.lua +++ b/kong/tools/dns.lua @@ -38,7 +38,7 @@ local setup_client = function(conf) } -- new dns client - if ngx.shared.kong_dns_cache and not _G.busted_legacy_dns_client then + if ngx.shared.kong_dns_cache and _G.busted_new_dns_client ~= false then servers = {} diff --git a/spec/01-unit/09-balancer/01-generic_spec.lua b/spec/01-unit/09-balancer/01-generic_spec.lua index b56fb1ad8f5..78b9141d88b 100644 --- a/spec/01-unit/09-balancer/01-generic_spec.lua +++ b/spec/01-unit/09-balancer/01-generic_spec.lua @@ -148,9 +148,10 @@ local function add_target(b, name, port, weight) end +for _, enable_new_dns_client in ipairs{ false, true } do for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-robin" } do - describe("[" .. algorithm .. "]", function() + describe("[" .. algorithm .. "]" .. (enable_new_dns_client and "[new dns]" or ""), function() local snapshot @@ -162,6 +163,8 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro _G.kong = kong + _G.busted_new_dns_client = enable_new_dns_client + kong.db = {} client = require "kong.resty.dns.client" @@ -897,13 +900,15 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro describe("(SRV)", function() + local srv_name = enable_new_dns_client and "_test._tcp.srvrecord.test" + or "srvrecord.test" it("adding a host",function() dnsSRV({ - { name = "srvrecord.test", target = "1.1.1.1", port = 9000, weight = 10 }, - { name = "srvrecord.test", target = "2.2.2.2", port = 9001, weight = 10 }, + { name = srv_name, target = "1.1.1.1", port = 9000, weight = 10 }, + { name = srv_name, target = "2.2.2.2", port = 9001, weight = 10 }, }) - add_target(b, "srvrecord.test", 8001, 25) + add_target(b, srv_name, 8001, 25) assert.same({ healthy = true, weight = { @@ -932,7 +937,7 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro }, }, { - host = "srvrecord.test", + host = srv_name, port = 8001, dns = "SRV", nodeWeight = 25, @@ -962,11 +967,11 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro it("switching address availability",function() dnsSRV({ - { name = "srvrecord.test", target = "1.1.1.1", port = 9000, weight = 10 }, - { name = "srvrecord.test", target = "2.2.2.2", port = 9001, weight = 10 }, + { name = srv_name, target = "1.1.1.1", port = 9000, weight = 10 }, + { name = srv_name, target = "2.2.2.2", port = 9001, weight = 10 }, }) - add_target(b, "srvrecord.test", 8001, 25) + add_target(b, srv_name, 8001, 25) assert.same({ healthy = true, weight = { @@ -995,7 +1000,7 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro }, }, { - host = "srvrecord.test", + host = srv_name, port = 8001, dns = "SRV", nodeWeight = 25, @@ -1023,7 +1028,7 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro }, b:getStatus()) -- switch to unavailable - assert(b:setAddressStatus(b:findAddress("1.1.1.1", 9000, "srvrecord.test"), false)) + assert(b:setAddressStatus(b:findAddress("1.1.1.1", 9000, srv_name), false)) assert.same({ healthy = true, weight = { @@ -1052,7 +1057,7 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro }, }, { - host = "srvrecord.test", + host = srv_name, port = 8001, dns = "SRV", nodeWeight = 25, @@ -1080,7 +1085,7 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro }, b:getStatus()) -- switch to available - assert(b:setAddressStatus(b:findAddress("1.1.1.1", 9000, "srvrecord.test"), true)) + assert(b:setAddressStatus(b:findAddress("1.1.1.1", 9000, srv_name), true)) assert.same({ healthy = true, weight = { @@ -1109,7 +1114,7 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro }, }, { - host = "srvrecord.test", + host = srv_name, port = 8001, dns = "SRV", nodeWeight = 25, @@ -1139,11 +1144,11 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro it("changing weight of an available address (dns update)",function() local record = dnsSRV({ - { name = "srvrecord.test", target = "1.1.1.1", port = 9000, weight = 10 }, - { name = "srvrecord.test", target = "2.2.2.2", port = 9001, weight = 10 }, + { name = srv_name, target = "1.1.1.1", port = 9000, weight = 10 }, + { name = srv_name, target = "2.2.2.2", port = 9001, weight = 10 }, }) - add_target(b, "srvrecord.test", 8001, 10) + add_target(b, srv_name, 8001, 10) assert.same({ healthy = true, weight = { @@ -1172,7 +1177,7 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro }, }, { - host = "srvrecord.test", + host = srv_name, port = 8001, dns = "SRV", nodeWeight = 10, @@ -1201,11 +1206,11 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro dnsExpire(client, record) dnsSRV({ - { name = "srvrecord.test", target = "1.1.1.1", port = 9000, weight = 20 }, - { name = "srvrecord.test", target = "2.2.2.2", port = 9001, weight = 20 }, + { name = srv_name, target = "1.1.1.1", port = 9000, weight = 20 }, + { name = srv_name, target = "2.2.2.2", port = 9001, weight = 20 }, }) targets.resolve_targets(b.targets) -- touch all addresses to force dns renewal - add_target(b, "srvrecord.test", 8001, 99) -- add again to update nodeWeight + add_target(b, srv_name, 8001, 99) -- add again to update nodeWeight assert.same({ healthy = true, @@ -1235,7 +1240,7 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro }, }, { - host = "srvrecord.test", + host = srv_name, port = 8001, dns = "SRV", nodeWeight = 99, @@ -1265,11 +1270,11 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro it("changing weight of an unavailable address (dns update)",function() local record = dnsSRV({ - { name = "srvrecord.test", target = "1.1.1.1", port = 9000, weight = 10 }, - { name = "srvrecord.test", target = "2.2.2.2", port = 9001, weight = 10 }, + { name = srv_name, target = "1.1.1.1", port = 9000, weight = 10 }, + { name = srv_name, target = "2.2.2.2", port = 9001, weight = 10 }, }) - add_target(b, "srvrecord.test", 8001, 25) + add_target(b, srv_name, 8001, 25) assert.same({ healthy = true, weight = { @@ -1298,7 +1303,7 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro }, }, { - host = "srvrecord.test", + host = srv_name, port = 8001, dns = "SRV", nodeWeight = 25, @@ -1326,7 +1331,7 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro }, b:getStatus()) -- switch to unavailable - assert(b:setAddressStatus(b:findAddress("2.2.2.2", 9001, "srvrecord.test"), false)) + assert(b:setAddressStatus(b:findAddress("2.2.2.2", 9001, srv_name), false)) assert.same({ healthy = true, weight = { @@ -1355,7 +1360,7 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro }, }, { - host = "srvrecord.test", + host = srv_name, port = 8001, dns = "SRV", nodeWeight = 25, @@ -1385,11 +1390,11 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro -- update weight, through dns renewal dnsExpire(client, record) dnsSRV({ - { name = "srvrecord.test", target = "1.1.1.1", port = 9000, weight = 20 }, - { name = "srvrecord.test", target = "2.2.2.2", port = 9001, weight = 20 }, + { name = srv_name, target = "1.1.1.1", port = 9000, weight = 20 }, + { name = srv_name, target = "2.2.2.2", port = 9001, weight = 20 }, }) targets.resolve_targets(b.targets) -- touch all addresses to force dns renewal - add_target(b, "srvrecord.test", 8001, 99) -- add again to update nodeWeight + add_target(b, srv_name, 8001, 99) -- add again to update nodeWeight assert.same({ healthy = true, @@ -1419,7 +1424,7 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro }, }, { - host = "srvrecord.test", + host = srv_name, port = 8001, dns = "SRV", nodeWeight = 99, @@ -1473,14 +1478,16 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro dnsA({ { name = "getkong.test", address = "1.2.3.4" }, }) + local srv_name = enable_new_dns_client and "_test._tcp.konghq.test" + or "konghq.test" dnsSRV({ - { name = "konghq.test", target = "getkong.test", port = 2, weight = 3 }, + { name = srv_name, target = "getkong.test", port = 2, weight = 3 }, }) - add_target(b, "konghq.test", 8000, 50) + add_target(b, srv_name, 8000, 50) local ip, port, hostname, handle = b:getPeer(true, nil, "a string") assert.equal("1.2.3.4", ip) assert.equal(2, port) - assert.equal("konghq.test", hostname) + assert.equal(srv_name, hostname) assert.not_nil(handle) end) end) @@ -1506,10 +1513,12 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro dnsA({ { name = "getkong.test", address = "1.2.3.4" }, }) + local srv_name = enable_new_dns_client and "_test._tcp.konghq.test" + or "konghq.test" dnsSRV({ - { name = "konghq.test", target = "getkong.test", port = 2, weight = 3 }, + { name = srv_name, target = "getkong.test", port = 2, weight = 3 }, }) - add_target(b, "konghq.test", 8000, 50) + add_target(b, srv_name, 8000, 50) local ip, port, hostname, handle = b:getPeer(true, nil, "a string") assert.equal("1.2.3.4", ip) assert.equal(2, port) @@ -1521,6 +1530,8 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro describe("getpeer()", function() + local srv_name = enable_new_dns_client and "_test._tcp.konghq.test" + or "konghq.test" local b before_each(function() @@ -1536,13 +1547,13 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro it("returns expected results/types when using SRV with IP", function() dnsSRV({ - { name = "konghq.test", target = "1.1.1.1", port = 2, weight = 3 }, + { name = srv_name, target = "1.1.1.1", port = 2, weight = 3 }, }) - add_target(b, "konghq.test", 8000, 50) + add_target(b, srv_name, 8000, 50) local ip, port, hostname, handle = b:getPeer(true, nil, "a string") assert.equal("1.1.1.1", ip) assert.equal(2, port) - assert.equal("konghq.test", hostname) + assert.equal(srv_name, hostname) assert.not_nil(handle) end) @@ -1552,13 +1563,13 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro { name = "getkong.test", address = "1.2.3.4" }, }) dnsSRV({ - { name = "konghq.test", target = "getkong.test", port = 2, weight = 3 }, + { name = srv_name, target = "getkong.test", port = 2, weight = 3 }, }) - add_target(b, "konghq.test", 8000, 50) + add_target(b, srv_name, 8000, 50) local ip, port, hostname, handle = b:getPeer(true, nil, "a string") assert.equal("1.2.3.4", ip) assert.equal(2, port) - assert.equal("konghq.test", hostname) + assert.equal(srv_name, hostname) assert.not_nil(handle) end) @@ -1570,9 +1581,9 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro { name = "getkong.test", address = "1.2.3.4" }, }) dnsSRV({ - { name = "konghq.test", target = "getkong.test", port = 2, weight = 3 }, + { name = srv_name, target = "getkong.test", port = 2, weight = 3 }, }) - add_target(b, "konghq.test", 8000, 50) + add_target(b, srv_name, 8000, 50) local ip, port, hostname, handle = b:getPeer(true, nil, "a string") assert.equal("1.2.3.4", ip) assert.equal(2, port) @@ -1723,6 +1734,8 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro describe("status:", function() + local srv_name = enable_new_dns_client and "_test._tcp.srvrecord.test" + or "srvrecord.test" local b @@ -1741,10 +1754,10 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro add_target(b, "127.0.0.1", 8000, 100) add_target(b, "0::1", 8080, 50) dnsSRV({ - { name = "srvrecord.test", target = "1.1.1.1", port = 9000, weight = 10 }, - { name = "srvrecord.test", target = "2.2.2.2", port = 9001, weight = 10 }, + { name = srv_name, target = "1.1.1.1", port = 9000, weight = 10 }, + { name = srv_name, target = "2.2.2.2", port = 9001, weight = 10 }, }) - add_target(b, "srvrecord.test", 1234, 9999) + add_target(b, srv_name, 1234, 9999) dnsA({ { name = "getkong.test", address = "5.6.7.8", ttl = 0 }, }) @@ -1754,7 +1767,7 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro local status = b:getStatus() table.sort(status.hosts, function(hostA, hostB) return hostA.host < hostB.host end) - assert.same({ + local expect_status = { healthy = true, weight = { total = 1170, @@ -1832,7 +1845,7 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro addresses = {}, }, { - host = "srvrecord.test", + host = srv_name, port = 1234, dns = "SRV", nodeWeight = 9999, @@ -1857,9 +1870,13 @@ for _, algorithm in ipairs{ "consistent-hashing", "least-connections", "round-ro }, }, }, - }, status) + } + table.sort(expect_status.hosts, function(hostA, hostB) return hostA.host < hostB.host end) + + assert.same(expect_status, status) end) end) end) end) end +end diff --git a/spec/01-unit/09-balancer/02-least_connections_spec.lua b/spec/01-unit/09-balancer/02-least_connections_spec.lua index caae6c8bbe0..5426b3953a3 100644 --- a/spec/01-unit/09-balancer/02-least_connections_spec.lua +++ b/spec/01-unit/09-balancer/02-least_connections_spec.lua @@ -153,11 +153,17 @@ local function validate_lcb(b, debug) end -describe("[least-connections]", function() +for _, enable_new_dns_client in ipairs{ false, true } do + +describe("[least-connections]" .. (enable_new_dns_client and "[new dns]" or ""), function() + local srv_name = enable_new_dns_client and "_test._tcp.konghq.com" + or "konghq.com" local snapshot setup(function() + _G.busted_new_dns_client = enable_new_dns_client + _G.package.loaded["kong.resty.dns.client"] = nil -- make sure module is reloaded _G.package.loaded["kong.runloop.balancer.targets"] = nil -- make sure module is reloaded @@ -262,10 +268,10 @@ describe("[least-connections]", function() it("honours weights", function() dnsSRV({ - { name = "konghq.com", target = "20.20.20.20", port = 80, weight = 20 }, - { name = "konghq.com", target = "50.50.50.50", port = 80, weight = 50 }, + { name = srv_name, target = "20.20.20.20", port = 80, weight = 20 }, + { name = srv_name, target = "50.50.50.50", port = 80, weight = 50 }, }) - local b = validate_lcb(new_balancer({ "konghq.com" })) + local b = validate_lcb(new_balancer({ srv_name })) local counts = {} local handles = {} @@ -286,10 +292,10 @@ describe("[least-connections]", function() it("first returns top weights, on a 0-connection balancer", function() dnsSRV({ - { name = "konghq.com", target = "20.20.20.20", port = 80, weight = 20 }, - { name = "konghq.com", target = "50.50.50.50", port = 80, weight = 50 }, + { name = srv_name, target = "20.20.20.20", port = 80, weight = 20 }, + { name = srv_name, target = "50.50.50.50", port = 80, weight = 50 }, }) - local b = validate_lcb(new_balancer({ "konghq.com" })) + local b = validate_lcb(new_balancer({ srv_name })) local handles = {} local ip, _, handle @@ -316,13 +322,13 @@ describe("[least-connections]", function() it("doesn't use unavailable addresses", function() dnsSRV({ - { name = "konghq.com", target = "20.20.20.20", port = 80, weight = 20 }, - { name = "konghq.com", target = "50.50.50.50", port = 80, weight = 50 }, + { name = srv_name, target = "20.20.20.20", port = 80, weight = 20 }, + { name = srv_name, target = "50.50.50.50", port = 80, weight = 50 }, }) - local b = validate_lcb(new_balancer({ "konghq.com" })) + local b = validate_lcb(new_balancer({ srv_name })) -- mark one as unavailable - b:setAddressStatus(b:findAddress("50.50.50.50", 80, "konghq.com"), false) + b:setAddressStatus(b:findAddress("50.50.50.50", 80, srv_name), false) local counts = {} local handles = {} for i = 1,70 do @@ -342,13 +348,13 @@ describe("[least-connections]", function() it("uses reenabled (available) addresses again", function() dnsSRV({ - { name = "konghq.com", target = "20.20.20.20", port = 80, weight = 20 }, - { name = "konghq.com", target = "50.50.50.50", port = 80, weight = 50 }, + { name = srv_name, target = "20.20.20.20", port = 80, weight = 20 }, + { name = srv_name, target = "50.50.50.50", port = 80, weight = 50 }, }) - local b = validate_lcb(new_balancer({ "konghq.com" })) + local b = validate_lcb(new_balancer({ srv_name })) -- mark one as unavailable - b:setAddressStatus(b:findAddress("20.20.20.20", 80, "konghq.com"), false) + b:setAddressStatus(b:findAddress("20.20.20.20", 80, srv_name), false) local counts = {} local handles = {} for i = 1,70 do @@ -365,7 +371,7 @@ describe("[least-connections]", function() }, counts) -- let's do another 70, after resetting - b:setAddressStatus(b:findAddress("20.20.20.20", 80, "konghq.com"), true) + b:setAddressStatus(b:findAddress("20.20.20.20", 80, srv_name), true) for i = 1,70 do local ip, _, _, handle = b:getPeer() counts[ip] = (counts[ip] or 0) + 1 @@ -388,11 +394,11 @@ describe("[least-connections]", function() it("does not return already failed addresses", function() dnsSRV({ - { name = "konghq.com", target = "20.20.20.20", port = 80, weight = 20 }, - { name = "konghq.com", target = "50.50.50.50", port = 80, weight = 50 }, - { name = "konghq.com", target = "70.70.70.70", port = 80, weight = 70 }, + { name = srv_name, target = "20.20.20.20", port = 80, weight = 20 }, + { name = srv_name, target = "50.50.50.50", port = 80, weight = 50 }, + { name = srv_name, target = "70.70.70.70", port = 80, weight = 70 }, }) - local b = validate_lcb(new_balancer({ "konghq.com" })) + local b = validate_lcb(new_balancer({ srv_name })) local tried = {} local ip, _, handle @@ -424,11 +430,11 @@ describe("[least-connections]", function() it("retries, after all addresses failed, restarts with previously failed ones", function() dnsSRV({ - { name = "konghq.com", target = "20.20.20.20", port = 80, weight = 20 }, - { name = "konghq.com", target = "50.50.50.50", port = 80, weight = 50 }, - { name = "konghq.com", target = "70.70.70.70", port = 80, weight = 70 }, + { name = srv_name, target = "20.20.20.20", port = 80, weight = 20 }, + { name = srv_name, target = "50.50.50.50", port = 80, weight = 50 }, + { name = srv_name, target = "70.70.70.70", port = 80, weight = 70 }, }) - local b = validate_lcb(new_balancer({ "konghq.com" })) + local b = validate_lcb(new_balancer({ srv_name })) local tried = {} local ip, _, handle @@ -449,10 +455,10 @@ describe("[least-connections]", function() it("releases the previous connection", function() dnsSRV({ - { name = "konghq.com", target = "20.20.20.20", port = 80, weight = 20 }, - { name = "konghq.com", target = "50.50.50.50", port = 80, weight = 50 }, + { name = srv_name, target = "20.20.20.20", port = 80, weight = 20 }, + { name = srv_name, target = "50.50.50.50", port = 80, weight = 50 }, }) - local b = validate_lcb(new_balancer({ "konghq.com" })) + local b = validate_lcb(new_balancer({ srv_name })) local handle -- define outside loop, so it gets reused and released for i = 1,70 do @@ -478,9 +484,9 @@ describe("[least-connections]", function() it("releases a connection", function() dnsSRV({ - { name = "konghq.com", target = "20.20.20.20", port = 80, weight = 20 }, + { name = srv_name, target = "20.20.20.20", port = 80, weight = 20 }, }) - local b = validate_lcb(new_balancer({ "konghq.com" })) + local b = validate_lcb(new_balancer({ srv_name })) local ip, _, _, handle = b:getPeer() assert.equal("20.20.20.20", ip) @@ -493,9 +499,9 @@ describe("[least-connections]", function() it("releases connection of already disabled/removed address", function() dnsSRV({ - { name = "konghq.com", target = "20.20.20.20", port = 80, weight = 20 }, + { name = srv_name, target = "20.20.20.20", port = 80, weight = 20 }, }) - local b = validate_lcb(new_balancer({ "konghq.com" })) + local b = validate_lcb(new_balancer({ srv_name })) local ip, _, _, handle = b:getPeer() assert.equal("20.20.20.20", ip) @@ -513,3 +519,5 @@ describe("[least-connections]", function() end) end) + +end diff --git a/spec/01-unit/09-balancer/03-consistent_hashing_spec.lua b/spec/01-unit/09-balancer/03-consistent_hashing_spec.lua index aaecbdd4301..0cd32a708cd 100644 --- a/spec/01-unit/09-balancer/03-consistent_hashing_spec.lua +++ b/spec/01-unit/09-balancer/03-consistent_hashing_spec.lua @@ -200,11 +200,16 @@ end -- END TEST HELPERS -- ---------------------- -describe("[consistent_hashing]", function() +for _, enable_new_dns_client in ipairs{ false, true } do +describe("[consistent_hashing]" .. (enable_new_dns_client and "[new dns]" or ""), function() + local srv_name = enable_new_dns_client and "_test._tcp.gelato.io" + or "gelato.io" local snapshot setup(function() + _G.busted_new_dns_client = enable_new_dns_client + _G.package.loaded["kong.resty.dns.client"] = nil -- make sure module is reloaded _G.package.loaded["kong.runloop.balancer.targets"] = nil -- make sure module is reloaded @@ -552,10 +557,10 @@ describe("[consistent_hashing]", function() { name = "mashape2.com", address = "12.34.56.2" }, }) dnsSRV({ - { name = "mashape.com", target = "mashape1.com", port = 8001, weight = 5 }, - { name = "mashape.com", target = "mashape2.com", port = 8002, weight = 5 }, + { name = srv_name, target = "mashape1.com", port = 8001, weight = 5 }, + { name = srv_name, target = "mashape2.com", port = 8002, weight = 5 }, }) - add_target(b, "mashape.com", 123, 100) + add_target(b, srv_name, 123, 100) ngx.sleep(0) assert.equal(2, count_add) assert.equal(0, count_remove) @@ -883,15 +888,15 @@ describe("[consistent_hashing]", function() { name = "mashape.com", address = "1.2.3.5" }, }) dnsSRV({ - { name = "gelato.io", target = "1.2.3.6", port = 8001, weight = 5 }, - { name = "gelato.io", target = "1.2.3.6", port = 8002, weight = 5 }, + { name = srv_name, target = "1.2.3.6", port = 8001, weight = 5 }, + { name = srv_name, target = "1.2.3.6", port = 8002, weight = 5 }, }) local b = new_balancer({ dns = client, wheelSize = 1000, }) add_target(b, "mashape.com", 80, 10) - add_target(b, "gelato.io", 80, 10) --> port + weight will be ignored + add_target(b, srv_name, 80, 10) --> port + weight will be ignored local count = count_indices(b) local state = copyWheel(b) -- 33%: 106 points @@ -903,7 +908,7 @@ describe("[consistent_hashing]", function() ["1.2.3.6:8002"] = 53, }, count) - add_target(b, "gelato.io", 80, 20) --> port + weight will be ignored + add_target(b, srv_name, 80, 20) --> port + weight will be ignored count = count_indices(b) assert.same({ ["1.2.3.4:80"] = 106, @@ -978,16 +983,16 @@ describe("[consistent_hashing]", function() end) it("renewed DNS SRV record; no changes", function() local record = dnsSRV({ - { name = "gelato.io", target = "1.2.3.6", port = 8001, weight = 5 }, - { name = "gelato.io", target = "1.2.3.6", port = 8002, weight = 5 }, - { name = "gelato.io", target = "1.2.3.6", port = 8003, weight = 5 }, + { name = srv_name, target = "1.2.3.6", port = 8001, weight = 5 }, + { name = srv_name, target = "1.2.3.6", port = 8002, weight = 5 }, + { name = srv_name, target = "1.2.3.6", port = 8003, weight = 5 }, }) dnsA({ { name = "getkong.org", address = "9.9.9.9" }, }) local b = new_balancer({ hosts = { - { name = "gelato.io" }, + { name = srv_name }, { name = "getkong.org", port = 123, weight = 10 }, }, dns = client, @@ -996,9 +1001,9 @@ describe("[consistent_hashing]", function() local state = copyWheel(b) record.expire = gettime() -1 -- expire current dns cache record dnsSRV({ -- create a new record (identical) - { name = "gelato.io", target = "1.2.3.6", port = 8001, weight = 5 }, - { name = "gelato.io", target = "1.2.3.6", port = 8002, weight = 5 }, - { name = "gelato.io", target = "1.2.3.6", port = 8003, weight = 5 }, + { name = srv_name, target = "1.2.3.6", port = 8001, weight = 5 }, + { name = srv_name, target = "1.2.3.6", port = 8002, weight = 5 }, + { name = srv_name, target = "1.2.3.6", port = 8003, weight = 5 }, }) -- create a spy to check whether dns was queried spy.on(client, "resolve") @@ -1006,7 +1011,7 @@ describe("[consistent_hashing]", function() -- invoke balancer, to expire record and re-query dns --b:_hit_all() targets.resolve_targets(b.targets) - assert.spy(client.resolve).was_called_with("gelato.io",nil, nil) + assert.spy(client.resolve).was_called_with(srv_name,nil, nil) assert.same(state, copyWheel(b)) end) it("low weight with zero-indices assigned doesn't fail", function() @@ -1046,13 +1051,13 @@ describe("[consistent_hashing]", function() -- depending on order of insertion it is either 1 or 0 indices -- but it may never error. dnsSRV({ - { name = "gelato.io", target = "1.2.3.6", port = 8001, weight = 0 }, - { name = "gelato.io", target = "1.2.3.6", port = 8002, weight = 0 }, + { name = srv_name, target = "1.2.3.6", port = 8001, weight = 0 }, + { name = srv_name, target = "1.2.3.6", port = 8002, weight = 0 }, }) local b = new_balancer({ hosts = { -- port and weight will be overridden by the above - { name = "gelato.io", port = 80, weight = 99999 }, + { name = srv_name, port = 80, weight = 99999 }, }, dns = client, wheelSize = 100, @@ -1120,3 +1125,5 @@ describe("[consistent_hashing]", function() end) end) end) + +end diff --git a/spec/01-unit/09-balancer/04-round_robin_spec.lua b/spec/01-unit/09-balancer/04-round_robin_spec.lua index 341ec4fe459..7318c8123ae 100644 --- a/spec/01-unit/09-balancer/04-round_robin_spec.lua +++ b/spec/01-unit/09-balancer/04-round_robin_spec.lua @@ -236,12 +236,17 @@ end -- END TEST HELPERS -- ---------------------- +for _, enable_new_dns_client in ipairs{ false, true } do describe("[round robin balancer]", function() + local srv_name = enable_new_dns_client and "_test._tcp.gelato.test" + or "gelato.test" local snapshot setup(function() + _G.busted_new_dns_client = enable_new_dns_client + _G.package.loaded["kong.resty.dns.client"] = nil -- make sure module is reloaded _G.package.loaded["kong.runloop.balancer.targets"] = nil -- make sure module is reloaded @@ -327,12 +332,12 @@ describe("[round robin balancer]", function() { name = "getkong.test", address = "::1" }, }) dnsSRV({ - { name = "gelato.test", target = "1.2.3.6", port = 8001 }, - { name = "gelato.test", target = "1.2.3.6", port = 8002 }, - { name = "gelato.test", target = "1.2.3.6", port = 8003 }, + { name = srv_name, target = "1.2.3.6", port = 8001 }, + { name = srv_name, target = "1.2.3.6", port = 8002 }, + { name = srv_name, target = "1.2.3.6", port = 8003 }, }) local b = new_balancer{ - hosts = {"mashape.test", "getkong.test", "gelato.test" }, + hosts = {"mashape.test", "getkong.test", srv_name }, dns = client, wheelSize = 10, } @@ -376,10 +381,10 @@ describe("[round robin balancer]", function() { name = "getkong.test", address = "::1" }, }) dnsSRV({ - { name = "gelato.test", target = "1.2.3.4", port = 8001 }, + { name = srv_name, target = "1.2.3.4", port = 8001 }, }) local b = new_balancer{ - hosts = {"mashape.test", "getkong.test", "gelato.test" }, + hosts = {"mashape.test", "getkong.test", srv_name }, dns = client, wheelSize = 10, } @@ -503,10 +508,10 @@ describe("[round robin balancer]", function() { name = "mashape2.test", address = "12.34.56.2" }, }) dnsSRV({ - { name = "mashape.test", target = "mashape1.test", port = 8001, weight = 5 }, - { name = "mashape.test", target = "mashape2.test", port = 8002, weight = 5 }, + { name = srv_name, target = "mashape1.test", port = 8001, weight = 5 }, + { name = srv_name, target = "mashape2.test", port = 8002, weight = 5 }, }) - add_target(b, "mashape.test", 80, 10) + add_target(b, srv_name, 80, 10) local _, _, _, handle = b:getPeer() local ok, err = b:setAddressStatus(handle.address, false) @@ -530,9 +535,9 @@ describe("[round robin balancer]", function() { name = "mashape1.test", address = "12.34.56.78" }, }) dnsSRV({ - { name = "mashape.test", target = "mashape1.test", port = 0, weight = 5 }, + { name = srv_name, target = "mashape1.test", port = 0, weight = 5 }, }) - add_target(b, "mashape.test", 80, 10) + add_target(b, srv_name, 80, 10) local ip, port = b:getPeer() assert.equals("12.34.56.78", ip) assert.equals(80, port) @@ -550,18 +555,18 @@ describe("[round robin balancer]", function() { name = "mashape.test", address = "1.2.3.4" }, }) dnsSRV({ - { name = "gelato.test", target = "mashape.test", port = 8001 }, + { name = srv_name, target = "mashape.test", port = 8001 }, }) local b = check_balancer(new_balancer { hosts = { - {name = "gelato.test", port = 123, weight = 100}, + {name = srv_name, port = 123, weight = 100}, }, dns = client, }) local addr, port, host = b:getPeer() assert.equal("1.2.3.4", addr) assert.equal(8001, port) - assert.equal("gelato.test", host) + assert.equal(srv_name, host) end) it("gets an IP address and port number; round-robin", function() dnsA({ @@ -758,10 +763,10 @@ describe("[round robin balancer]", function() { name = "mashape2.test", address = "12.34.56.2" }, }) dnsSRV({ - { name = "mashape.test", target = "mashape1.test", port = 8001, weight = 5 }, - { name = "mashape.test", target = "mashape2.test", port = 8002, weight = 5 }, + { name = srv_name, target = "mashape1.test", port = 8001, weight = 5 }, + { name = srv_name, target = "mashape2.test", port = 8002, weight = 5 }, }) - add_target(b, "mashape.test", 123, 100) + add_target(b, srv_name, 123, 100) ngx.sleep(0) assert.equal(2, count_add) assert.equal(0, count_remove) @@ -1078,15 +1083,15 @@ describe("[round robin balancer]", function() { name = "mashape.test", address = "1.2.3.5" }, }) dnsSRV({ - { name = "gelato.test", target = "1.2.3.6", port = 8001, weight = 5 }, - { name = "gelato.test", target = "1.2.3.6", port = 8002, weight = 5 }, + { name = srv_name, target = "1.2.3.6", port = 8001, weight = 5 }, + { name = srv_name, target = "1.2.3.6", port = 8002, weight = 5 }, }) local b = check_balancer(new_balancer { dns = client, wheelSize = 120, }) add_target(b, "mashape.test", 80, 10) - add_target(b, "gelato.test", 80, 10) --> port + weight will be ignored + add_target(b, srv_name, 80, 10) --> port + weight will be ignored local count = count_indices(b) local state = copyWheel(b) assert.same({ @@ -1096,7 +1101,7 @@ describe("[round robin balancer]", function() ["1.2.3.6:8002"] = 1, }, count) - add_target(b, "gelato.test", 80, 20) --> port + weight will be ignored + add_target(b, srv_name, 80, 20) --> port + weight will be ignored count = count_indices(b) assert.same({ ["1.2.3.4:80"] = 2, @@ -1171,16 +1176,16 @@ describe("[round robin balancer]", function() end) it("renewed DNS SRV record; no changes", function() local record = dnsSRV({ - { name = "gelato.test", target = "1.2.3.6", port = 8001, weight = 5, ttl = 0.1 }, - { name = "gelato.test", target = "1.2.3.6", port = 8002, weight = 5, ttl = 0.1 }, - { name = "gelato.test", target = "1.2.3.6", port = 8003, weight = 5, ttl = 0.1 }, + { name = srv_name, target = "1.2.3.6", port = 8001, weight = 5, ttl = 0.1 }, + { name = srv_name, target = "1.2.3.6", port = 8002, weight = 5, ttl = 0.1 }, + { name = srv_name, target = "1.2.3.6", port = 8003, weight = 5, ttl = 0.1 }, }) dnsA({ { name = "getkong.test", address = "9.9.9.9" }, }) local b = check_balancer(new_balancer { hosts = { - { name = "gelato.test" }, + { name = srv_name }, { name = "getkong.test", port = 123, weight = 10 }, }, dns = client, @@ -1190,16 +1195,16 @@ describe("[round robin balancer]", function() record.expire = gettime() -1 -- expire current dns cache record sleep(0.2) -- wait for record expiration dnsSRV({ -- create a new record (identical) - { name = "gelato.test", target = "1.2.3.6", port = 8001, weight = 5 }, - { name = "gelato.test", target = "1.2.3.6", port = 8002, weight = 5 }, - { name = "gelato.test", target = "1.2.3.6", port = 8003, weight = 5 }, + { name = srv_name, target = "1.2.3.6", port = 8001, weight = 5 }, + { name = srv_name, target = "1.2.3.6", port = 8002, weight = 5 }, + { name = srv_name, target = "1.2.3.6", port = 8003, weight = 5 }, }) -- create a spy to check whether dns was queried spy.on(client, "resolve") for _ = 1, b.wheelSize do -- call all, to make sure we hit the expired one b:getPeer() -- invoke balancer, to expire record and re-query dns end - assert.spy(client.resolve).was_called_with("gelato.test",nil, nil) + assert.spy(client.resolve).was_called_with(srv_name,nil, nil) assert.same(state, copyWheel(b)) end) it("renewed DNS A record; address changes", function() @@ -1294,7 +1299,9 @@ describe("[round robin balancer]", function() local test_name = "really.really.really.does.not.exist.hostname.test" local ttl = 0.1 local staleTtl = 0 -- stale ttl = 0, force lookup upon expiring - client.getobj().stale_ttl = 0 + if client.getobj then + client.getobj().stale_ttl = 0 + end local record = dnsA({ { name = test_name, address = "1.2.3.4", ttl = ttl }, }, staleTtl) @@ -1317,7 +1324,9 @@ describe("[round robin balancer]", function() assert.is_nil(ip) assert.equal(port, "Balancer is unhealthy") end - client.getobj().stale_ttl = 4 + if client.getobj then + client.getobj().stale_ttl = 4 + end end) it("renewed DNS A record; unhealthy entries remain unhealthy after renewal", function() local record = dnsA({ @@ -1418,13 +1427,13 @@ describe("[round robin balancer]", function() -- depending on order of insertion it is either 1 or 0 indices -- but it may never error. dnsSRV({ - { name = "gelato.test", target = "1.2.3.6", port = 8001, weight = 0 }, - { name = "gelato.test", target = "1.2.3.6", port = 8002, weight = 0 }, + { name = srv_name, target = "1.2.3.6", port = 8001, weight = 0 }, + { name = srv_name, target = "1.2.3.6", port = 8002, weight = 0 }, }) local b = check_balancer(new_balancer { hosts = { -- port and weight will be overridden by the above - { name = "gelato.test", port = 80, weight = 99999 }, + { name = srv_name, port = 80, weight = 99999 }, }, dns = client, wheelSize = 100, @@ -1562,3 +1571,5 @@ describe("[round robin balancer]", function() end) end) end) + +end diff --git a/spec/01-unit/09-balancer/06-latency_spec.lua b/spec/01-unit/09-balancer/06-latency_spec.lua index be9a23279e7..dc90293506f 100644 --- a/spec/01-unit/09-balancer/06-latency_spec.lua +++ b/spec/01-unit/09-balancer/06-latency_spec.lua @@ -150,12 +150,18 @@ local function validate_latency(b, debug) end -describe("[latency]", function() +for _, enable_new_dns_client in ipairs{ false, true } do + +describe("[latency]" .. (enable_new_dns_client and "[new dns]" or ""), function() + local srv_name = enable_new_dns_client and "_test._tcp.konghq.com" + or "konghq.com" local snapshot local old_var = ngx.var setup(function() + _G.busted_new_dns_client = enable_new_dns_client + _G.package.loaded["kong.resty.dns.client"] = nil -- make sure module is reloaded _G.package.loaded["kong.runloop.balancer.targets"] = nil -- make sure module is reloaded @@ -262,10 +268,10 @@ describe("[latency]", function() it("select low latency target", function() dnsSRV({ - { name = "konghq.com", target = "20.20.20.20", port = 80, weight = 20 }, - { name = "konghq.com", target = "50.50.50.50", port = 80, weight = 20 }, + { name = srv_name, target = "20.20.20.20", port = 80, weight = 20 }, + { name = srv_name, target = "50.50.50.50", port = 80, weight = 20 }, }) - local b = validate_latency(new_balancer({ "konghq.com" })) + local b = validate_latency(new_balancer({ srv_name })) local counts = {} local handles = {} @@ -306,10 +312,10 @@ describe("[latency]", function() it("first returns one, after update latency return another one", function() dnsSRV({ - { name = "konghq.com", target = "20.20.20.20", port = 80, weight = 20 }, - { name = "konghq.com", target = "50.50.50.50", port = 80, weight = 20 }, + { name = srv_name, target = "20.20.20.20", port = 80, weight = 20 }, + { name = srv_name, target = "50.50.50.50", port = 80, weight = 20 }, }) - local b = validate_latency(new_balancer({ "konghq.com" })) + local b = validate_latency(new_balancer({ srv_name })) local handles = {} local ip, _, handle @@ -348,13 +354,13 @@ describe("[latency]", function() it("doesn't use unavailable addresses", function() dnsSRV({ - { name = "konghq.com", target = "20.20.20.20", port = 80, weight = 20 }, - { name = "konghq.com", target = "50.50.50.50", port = 80, weight = 20 }, + { name = srv_name, target = "20.20.20.20", port = 80, weight = 20 }, + { name = srv_name, target = "50.50.50.50", port = 80, weight = 20 }, }) - local b = validate_latency(new_balancer({ "konghq.com" })) + local b = validate_latency(new_balancer({ srv_name })) -- mark one as unavailable - b:setAddressStatus(b:findAddress("50.50.50.50", 80, "konghq.com"), false) + b:setAddressStatus(b:findAddress("50.50.50.50", 80, srv_name), false) validate_latency(b) local counts = {} local handles = {} @@ -374,10 +380,10 @@ describe("[latency]", function() it("long time update ewma address score, ewma will use the most accurate value", function() dnsSRV({ - { name = "konghq.com", target = "20.20.20.20", port = 80, weight = 20 }, - { name = "konghq.com", target = "50.50.50.50", port = 80, weight = 20 }, + { name = srv_name, target = "20.20.20.20", port = 80, weight = 20 }, + { name = srv_name, target = "50.50.50.50", port = 80, weight = 20 }, }) - local b = validate_latency(new_balancer({ "konghq.com" })) + local b = validate_latency(new_balancer({ srv_name })) for _, target in pairs(b.targets) do for _, address in pairs(target.addresses) do @@ -452,13 +458,13 @@ describe("[latency]", function() it("uses reenabled (available) addresses again", function() dnsSRV({ - { name = "konghq.com", target = "20.20.20.20", port = 80, weight = 20 }, - { name = "konghq.com", target = "50.50.50.50", port = 80, weight = 20 }, + { name = srv_name, target = "20.20.20.20", port = 80, weight = 20 }, + { name = srv_name, target = "50.50.50.50", port = 80, weight = 20 }, }) - local b = validate_latency(new_balancer({ "konghq.com" })) + local b = validate_latency(new_balancer({ srv_name })) -- mark one as unavailable - b:setAddressStatus(b:findAddress("20.20.20.20", 80, "konghq.com"), false) + b:setAddressStatus(b:findAddress("20.20.20.20", 80, srv_name), false) local counts = {} local handles = {} for i = 1,70 do @@ -481,7 +487,7 @@ describe("[latency]", function() }, counts) -- let's do another 70, after resetting - b:setAddressStatus(b:findAddress("20.20.20.20", 80, "konghq.com"), true) + b:setAddressStatus(b:findAddress("20.20.20.20", 80, srv_name), true) for _, target in pairs(b.targets) do for _, address in pairs(target.addresses) do if address.ip == "20.20.20.20" then @@ -553,11 +559,11 @@ describe("[latency]", function() it("does not return already failed addresses", function() dnsSRV({ - { name = "konghq.com", target = "20.20.20.20", port = 80, weight = 20 }, - { name = "konghq.com", target = "50.50.50.50", port = 80, weight = 50 }, - { name = "konghq.com", target = "70.70.70.70", port = 80, weight = 70 }, + { name = srv_name, target = "20.20.20.20", port = 80, weight = 20 }, + { name = srv_name, target = "50.50.50.50", port = 80, weight = 50 }, + { name = srv_name, target = "70.70.70.70", port = 80, weight = 70 }, }) - local b = validate_latency(new_balancer({ "konghq.com" })) + local b = validate_latency(new_balancer({ srv_name })) local tried = {} local ip, _, handle @@ -589,11 +595,11 @@ describe("[latency]", function() it("retries, after all addresses failed, retry end", function() dnsSRV({ - { name = "konghq.com", target = "20.20.20.20", port = 80, weight = 20 }, - { name = "konghq.com", target = "50.50.50.50", port = 80, weight = 50 }, - { name = "konghq.com", target = "70.70.70.70", port = 80, weight = 70 }, + { name = srv_name, target = "20.20.20.20", port = 80, weight = 20 }, + { name = srv_name, target = "50.50.50.50", port = 80, weight = 50 }, + { name = srv_name, target = "70.70.70.70", port = 80, weight = 70 }, }) - local b = validate_latency(new_balancer({ "konghq.com" })) + local b = validate_latency(new_balancer({ srv_name })) local tried = {} local ip, _, handle @@ -616,3 +622,5 @@ describe("[latency]", function() end) end) + +end diff --git a/spec/01-unit/21-dns-client/02-client_spec.lua b/spec/01-unit/21-dns-client/02-client_spec.lua index e5a88c8e8d9..9051580d964 100644 --- a/spec/01-unit/21-dns-client/02-client_spec.lua +++ b/spec/01-unit/21-dns-client/02-client_spec.lua @@ -39,7 +39,8 @@ describe("[DNS client]", function() local client, resolver before_each(function() - _G.busted_legacy_dns_client = true + _G.busted_new_dns_client = false + client = require("kong.resty.dns.client") resolver = require("resty.dns.resolver") @@ -72,7 +73,6 @@ describe("[DNS client]", function() end) after_each(function() - _G.busted_legacy_dns_client = nil package.loaded["kong.resty.dns.client"] = nil package.loaded["resty.dns.resolver"] = nil client = nil diff --git a/spec/01-unit/21-dns-client/03-client_cache_spec.lua b/spec/01-unit/21-dns-client/03-client_cache_spec.lua index 448bd8b8a92..2857cb7f940 100644 --- a/spec/01-unit/21-dns-client/03-client_cache_spec.lua +++ b/spec/01-unit/21-dns-client/03-client_cache_spec.lua @@ -22,7 +22,8 @@ describe("[DNS client cache]", function() local client, resolver before_each(function() - _G.busted_legacy_dns_client = true + _G.busted_new_dns_client = false + client = require("kong.resty.dns.client") resolver = require("resty.dns.resolver") @@ -56,7 +57,6 @@ describe("[DNS client cache]", function() end) after_each(function() - _G.busted_legacy_dns_client = nil package.loaded["kong.resty.dns.client"] = nil package.loaded["resty.dns.resolver"] = nil client = nil diff --git a/spec/01-unit/30-new-dns-client/04-client_ipc_spec.lua b/spec/01-unit/30-new-dns-client/04-client_ipc_spec.lua index 5ed287def1d..63f50a156ea 100644 --- a/spec/01-unit/30-new-dns-client/04-client_ipc_spec.lua +++ b/spec/01-unit/30-new-dns-client/04-client_ipc_spec.lua @@ -30,7 +30,7 @@ describe("[dns-client] inter-process communication:",function() nginx_conf = "spec/fixtures/custom_nginx.template", plugins = "bundled,dns-client-test", nginx_main_worker_processes = num_workers, - legacy_dns_client = "off", + new_dns_client = "on", })) end) diff --git a/spec/02-integration/08-status_api/05-dns_client_spec.lua b/spec/02-integration/08-status_api/05-dns_client_spec.lua index 8ebeb757e64..a3e7108a5b9 100644 --- a/spec/02-integration/08-status_api/05-dns_client_spec.lua +++ b/spec/02-integration/08-status_api/05-dns_client_spec.lua @@ -22,7 +22,7 @@ for _, strategy in helpers.each_strategy() do assert(helpers.start_kong({ database = strategy, status_listen = "127.0.0.1:" .. tcp_status_port, - legacy_dns_client = "off", + new_dns_client = "on", })) client = helpers.http_client("127.0.0.1", tcp_status_port, 20000) @@ -75,7 +75,7 @@ for _, strategy in helpers.each_strategy() do assert(helpers.start_kong({ database = strategy, status_listen = "127.0.0.1:" .. tcp_status_port, - legacy_dns_client = "on", + new_dns_client = "off", })) client = helpers.http_client("127.0.0.1", tcp_status_port, 20000) @@ -120,7 +120,7 @@ for _, strategy in helpers.each_strategy() do database = strategy, cluster_listen = "127.0.0.1:9005", nginx_conf = "spec/fixtures/custom_nginx.template", - legacy_dns_client = "off", + new_dns_client = "on", })) assert(helpers.start_kong({ @@ -133,7 +133,7 @@ for _, strategy in helpers.each_strategy() do proxy_listen = "0.0.0.0:9002", nginx_conf = "spec/fixtures/custom_nginx.template", status_listen = "127.0.0.1:" .. tcp_status_port, - legacy_dns_client = "off", + new_dns_client = "on", })) client = helpers.http_client("127.0.0.1", tcp_status_port, 20000) diff --git a/spec/helpers/dns.lua b/spec/helpers/dns.lua index 68fdbfbcf2b..ffbf9b48cba 100644 --- a/spec/helpers/dns.lua +++ b/spec/helpers/dns.lua @@ -82,10 +82,17 @@ function _M.dnsSRV(client, records, staleTtl) records.ttl = records[1].ttl -- create key, and insert it - local key = records[1].name..":"..records[1].type + + -- for orignal dns client + local key = records[1].type..":"..records[1].name dnscache:set(key, records, records[1].ttl + (staleTtl or 4)) - key = records[1].name..":-1" -- A/AAAA + -- insert last-succesful lookup type + dnscache:set(records[1].name, records[1].type) + + -- for new dns client + local key = records[1].name..":"..records[1].type dnscache:set(key, records, records[1].ttl + (staleTtl or 4)) + return records end @@ -124,10 +131,19 @@ function _M.dnsA(client, records, staleTtl) records.ttl = records[1].ttl -- create key, and insert it + + -- for original dns client + local key = records[1].type..":"..records[1].name + dnscache:set(key, records, records[1].ttl + (staleTtl or 4)) + -- insert last-succesful lookup type + dnscache:set(records[1].name, records[1].type) + + -- for new dns client local key = records[1].name..":"..records[1].type dnscache:set(key, records, records[1].ttl) key = records[1].name..":-1" -- A/AAAA dnscache:set(key, records, records[1].ttl) + return records end @@ -165,10 +181,19 @@ function _M.dnsAAAA(client, records, staleTtl) records.ttl = records[1].ttl -- create key, and insert it + + -- for orignal dns client + local key = records[1].type..":"..records[1].name + dnscache:set(key, records, records[1].ttl + (staleTtl or 4)) + -- insert last-succesful lookup type + dnscache:set(records[1].name, records[1].type) + + -- for new dns client local key = records[1].name..":"..records[1].type dnscache:set(key, records, records[1].ttl + (staleTtl or 4)) key = records[1].name..":-1" -- A/AAAA dnscache:set(key, records, records[1].ttl + (staleTtl or 4)) + return records end