Skip to content

Commit

Permalink
fix(dns): expose /status/dns to the status API too (13466)
Browse files Browse the repository at this point in the history
The dns statistics API should be added into status_listen port, otherwise we'll not get the DNS statistics from admin_listen port in DP.

https://konghq.atlassian.net/browse/KAG-5115
  • Loading branch information
chobits authored Aug 9, 2024
1 parent 9bf3d1f commit 9d8933d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 8 deletions.
2 changes: 1 addition & 1 deletion changelog/unreleased/kong/refactor_dns_client.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
message: >
Starting from this version, a new DNS client library has been implemented and added into Kong. The new DNS client library has the following changes
- Introduced global caching for DNS records across workers, significantly reducing the query load on DNS servers.
- Introduced observable statistics for the new DNS client, and a new Admin API `/status/dns` to retrieve them.
- Introduced observable statistics for the new DNS client, and a new Status API `/status/dns` to retrieve them.
- Simplified the logic and make it more standardized
type: feature
scope: Core
1 change: 1 addition & 0 deletions kong-3.8.0-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ build = {
["kong.api.routes.tags"] = "kong/api/routes/tags.lua",
["kong.api.routes.targets"] = "kong/api/routes/targets.lua",
["kong.api.routes.upstreams"] = "kong/api/routes/upstreams.lua",
["kong.api.routes.dns"] = "kong/api/routes/dns.lua",

["kong.admin_gui"] = "kong/admin_gui/init.lua",
["kong.admin_gui.utils"] = "kong/admin_gui/utils.lua",
Expand Down
23 changes: 23 additions & 0 deletions kong/api/routes/dns.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
local kong = kong


return {
["/status/dns"] = {
GET = function (self, db, helpers)

if kong.configuration.legacy_dns_client then
return kong.response.exit(501, {
message = "not implemented with the legacy DNS client"
})
end

return kong.response.exit(200, {
worker = {
id = ngx.worker.id() or -1,
count = ngx.worker.count(),
},
stats = kong.dns.stats(),
})
end
},
}
1 change: 1 addition & 0 deletions kong/status/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ ngx.log(ngx.DEBUG, "Loading Status API endpoints")
-- Load core health route
api_helpers.attach_routes(app, require "kong.api.routes.health")
api_helpers.attach_routes(app, require "kong.status.ready")
api_helpers.attach_routes(app, require "kong.api.routes.dns")


if kong.configuration.database == "off" then
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
local helpers = require "spec.helpers"
local cjson = require "cjson"

local tcp_status_port = helpers.get_available_port()

for _, strategy in helpers.each_strategy() do
describe("Admin API - DNS client route with [#" .. strategy .. "]" , function()
describe("[#traditional] Status API - DNS client route with [#" .. strategy .. "]" , function()
local client

lazy_setup(function()
Expand All @@ -20,11 +21,11 @@ for _, strategy in helpers.each_strategy() do

assert(helpers.start_kong({
database = strategy,
nginx_conf = "spec/fixtures/custom_nginx.template",
status_listen = "127.0.0.1:" .. tcp_status_port,
legacy_dns_client = "off",
}))

client = helpers.admin_client()
client = helpers.http_client("127.0.0.1", tcp_status_port, 20000)
end)

teardown(function()
Expand Down Expand Up @@ -65,19 +66,19 @@ for _, strategy in helpers.each_strategy() do
end)
end)

describe("Admin API - DNS client route with [#" .. strategy .. "]" , function()
describe("[#traditional] Status API - DNS client route with [#" .. strategy .. "]" , function()
local client

lazy_setup(function()
helpers.get_db_utils(strategy)

assert(helpers.start_kong({
database = strategy,
nginx_conf = "spec/fixtures/custom_nginx.template",
legacy_dns_client = true,
status_listen = "127.0.0.1:" .. tcp_status_port,
legacy_dns_client = "on",
}))

client = helpers.admin_client()
client = helpers.http_client("127.0.0.1", tcp_status_port, 20000)
end)

teardown(function()
Expand All @@ -100,3 +101,63 @@ for _, strategy in helpers.each_strategy() do
end)
end)
end


-- hybrid mode

for _, strategy in helpers.each_strategy() do

describe("[#hybrid] Status API - DNS client route with [#" .. strategy .. "]" , function()
local client

lazy_setup(function()
helpers.get_db_utils(strategy) -- runs migrations

assert(helpers.start_kong({
role = "control_plane",
cluster_cert = "spec/fixtures/kong_clustering.crt",
cluster_cert_key = "spec/fixtures/kong_clustering.key",
database = strategy,
cluster_listen = "127.0.0.1:9005",
nginx_conf = "spec/fixtures/custom_nginx.template",
legacy_dns_client = "off",
}))

assert(helpers.start_kong({
role = "data_plane",
database = "off",
prefix = "servroot2",
cluster_cert = "spec/fixtures/kong_clustering.crt",
cluster_cert_key = "spec/fixtures/kong_clustering.key",
cluster_control_plane = "127.0.0.1:9005",
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",
}))

client = helpers.http_client("127.0.0.1", tcp_status_port, 20000)
end)

teardown(function()
if client then
client:close()
end
helpers.stop_kong("servroot2")
helpers.stop_kong()
end)

it("/status/dns - status code 200", function ()
local res = assert(client:send {
method = "GET",
path = "/status/dns",
headers = { ["Content-Type"] = "application/json" }
})

local body = assert.res_status(200 , res)
local json = assert(cjson.decode(body))
assert(type(json.stats) == "table")
end)

end)
end

1 comment on commit 9d8933d

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

Bazel Build

Docker image available kong/kong:9d8933d849e24d92037c623d07a0793dbaac3c5d
Artifacts available https://github.com/Kong/kong/actions/runs/10312939147

Please sign in to comment.