Skip to content

Commit

Permalink
tests(clustering): sync v1 should work with or without privileged age…
Browse files Browse the repository at this point in the history
…nt (#13866)

Refactored some code in #13857

KAG-5807, KAG-5811
  • Loading branch information
chronolaw authored Nov 15, 2024
1 parent 86e37ce commit da1100d
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 15 deletions.
5 changes: 5 additions & 0 deletions kong/global.lua
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ function _GLOBAL.init_worker_events(kong_config)
enable_privileged_agent = true
end

-- for debug and test
ngx.log(ngx.DEBUG,
"lua-resty-events enable_privileged_agent is ",
enable_privileged_agent)

opts = {
unique_timeout = 5, -- life time of unique event data in lrucache
broker_id = 0, -- broker server runs in nginx worker #0
Expand Down
27 changes: 12 additions & 15 deletions kong/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -881,23 +881,20 @@ function Kong.init_worker()
end

if kong.clustering then
if is_control_plane(kong.configuration) then-- CP needs to support both full and incremental sync
kong.clustering:init_worker()

-- full sync is only enabled for DP if incremental sync is disabled
elseif is_data_plane(kong.configuration) and not kong.sync then
local using_dedicated = kong.configuration.dedicated_config_processing
if using_dedicated and process.type() == "privileged agent" then
-- full sync dp agent
kong.clustering:init_worker()
return
local is_cp = is_control_plane(kong.configuration)
local is_dp_sync_v1 = is_data_plane(kong.configuration) and not kong.sync
local using_dedicated = kong.configuration.dedicated_config_processing

end
-- CP needs to support both full and incremental sync
-- full sync is only enabled for DP if incremental sync is disabled
if is_cp or is_dp_sync_v1 then
kong.clustering:init_worker()
end

if not using_dedicated then
-- full sync dp
kong.clustering:init_worker()
end
-- see is_dp_worker_process() in clustering/utils.lua
if using_dedicated and process.type() == "privileged agent" then
assert(not is_cp)
return
end
end

Expand Down
123 changes: 123 additions & 0 deletions spec/02-integration/09-hybrid_mode/14-dp_privileged_agent_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
local helpers = require "spec.helpers"
local cjson = require("cjson.safe")
local CLUSTERING_SYNC_STATUS = require("kong.constants").CLUSTERING_SYNC_STATUS

for _, dedicated in ipairs { "on", "off" } do
for _, strategy in helpers.each_strategy() do

describe("DP diabled Incremental Sync RPC #" .. strategy, function()

lazy_setup(function()
helpers.get_db_utils(strategy, {
"clustering_data_planes",
}) -- 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",

cluster_incremental_sync = "on", -- ENABLE incremental sync
}))

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",
nginx_worker_processes = 2, -- multiple workers

cluster_incremental_sync = "off", -- DISABLE incremental sync

dedicated_config_processing = dedicated, -- privileged agent
}))
end)

lazy_teardown(function()
helpers.stop_kong("servroot2")
helpers.stop_kong()
end)

after_each(function()
helpers.clean_logfile("servroot2/logs/error.log")
helpers.clean_logfile()
end)

describe("works when dedicated_config_processing = " .. dedicated, function()
it("shows DP status", function()
helpers.wait_until(function()
local admin_client = helpers.admin_client()
finally(function()
admin_client:close()
end)

local res = assert(admin_client:get("/clustering/data-planes"))
local body = assert.res_status(200, res)
local json = cjson.decode(body)

for _, v in pairs(json.data) do
if v.ip == "127.0.0.1" then
assert.near(14 * 86400, v.ttl, 3)
assert.matches("^(%d+%.%d+)%.%d+", v.version)
assert.equal(CLUSTERING_SYNC_STATUS.NORMAL, v.sync_status)
return true
end
end
end, 10)

-- cp will not run rpc
assert.logfile().has.no.line("[rpc]", true)

-- dp lua-resty-events should work well with or without privileged_agent
assert.logfile("servroot2/logs/error.log").has.line(
"lua-resty-events enable_privileged_agent is " .. tostring(dedicated == "on"), true)
end)
end)

describe("sync works when dedicated_config_processing = " .. dedicated, function()
it("proxy on DP follows CP config", function()
local admin_client = helpers.admin_client(10000)
finally(function()
admin_client:close()
end)

local res = assert(admin_client:post("/services", {
body = { name = "mockbin-service", url = "https://127.0.0.1:15556/request", },
headers = {["Content-Type"] = "application/json"}
}))
assert.res_status(201, res)

res = assert(admin_client:post("/services/mockbin-service/routes", {
body = { paths = { "/" }, },
headers = {["Content-Type"] = "application/json"}
}))

helpers.wait_until(function()
local proxy_client = helpers.http_client("127.0.0.1", 9002)

res = proxy_client:send({
method = "GET",
path = "/",
})

local status = res and res.status
proxy_client:close()
if status == 200 then
return true
end
end, 10)
end)
end)


end)

end -- for _, strategy
end -- for _, dedicated
4 changes: 4 additions & 0 deletions spec/02-integration/19-incrmental_sync/01-sync_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ describe("Incremental Sync RPC #" .. strategy, function()
assert.logfile().has.no.line("unable to update clustering data plane status", true)

assert.logfile("servroot2/logs/error.log").has.line("[kong.sync.v2] update entity", true)

-- dp lua-resty-events should work without privileged_agent
assert.logfile("servroot2/logs/error.log").has.line(
"lua-resty-events enable_privileged_agent is false", true)
end)

it("update route on CP", function()
Expand Down

1 comment on commit da1100d

@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:da1100d34b6aaf47c3bfb4fa313c699b848dd6b3
Artifacts available https://github.com/Kong/kong/actions/runs/11849951514

Please sign in to comment.