diff --git a/kong/clustering/services/sync/rpc.lua b/kong/clustering/services/sync/rpc.lua index 56e2a9f1d27..b05daccd221 100644 --- a/kong/clustering/services/sync/rpc.lua +++ b/kong/clustering/services/sync/rpc.lua @@ -141,6 +141,8 @@ function _M:init_dp(manager) return self:sync_once() end + ngx_log(ngx_DEBUG, "no sync runs, version is ", version) + return true end) end diff --git a/spec/02-integration/18-hybrid_rpc/09-notify_new_version_spec.lua b/spec/02-integration/18-hybrid_rpc/09-notify_new_version_spec.lua new file mode 100644 index 00000000000..b803420d476 --- /dev/null +++ b/spec/02-integration/18-hybrid_rpc/09-notify_new_version_spec.lua @@ -0,0 +1,87 @@ +local rep = string.rep +local helpers = require "spec.helpers" + + +-- register a rpc connected event in custom plugin rpc-notify-new-version-test +-- DISABLE rpc sync on cp side +-- ENABLE rpc sync on dp side +for _, strategy in helpers.each_strategy() do + describe("Hybrid Mode 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", + plugins = "bundled,rpc-notify-new-version-test", + nginx_worker_processes = 4, -- multiple workers + cluster_rpc = "on", -- enable rpc + cluster_rpc_sync = "off", -- disable rpc 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", + plugins = "bundled,rpc-notify-new-version-test", + nginx_worker_processes = 4, -- multiple workers + cluster_rpc = "on", -- enable rpc + cluster_rpc_sync = "on", -- enable rpc sync + })) + end) + + lazy_teardown(function() + helpers.stop_kong("servroot2") + helpers.stop_kong() + end) + + describe("sync.v2.notify_new_version works", function() + it("on dp side", function() + local name = "servroot2/logs/error.log" + + -- dp logs + assert.logfile(name).has.line( + "kong.test.notify_new_version ok", true, 10) + + assert.logfile(name).has.line( + "no sync runs, version is " .. rep(".", 32), true, 10) + assert.logfile(name).has.line( + "no sync runs, version is " .. rep("0", 32), true, 10) + + assert.logfile(name).has.line( + "sync_once retry count exceeded. retry_count: 6", true, 10) + assert.logfile(name).has.no.line( + "assertion failed", true, 0) + + local name = nil + + -- cp logs + for i = 0, 6 do + assert.logfile(name).has.line( + "kong.sync.v2.get_delta ok: " .. i, true, 10) + end + + assert.logfile(name).has.line( + "kong.test.notify_new_version ok", true, 10) + + assert.logfile(name).has.no.line( + "assertion failed", true, 0) + assert.logfile(name).has.no.line( + "[error]", true, 0) + + end) + end) + end) +end -- for _, strategy diff --git a/spec/fixtures/custom_plugins/kong/plugins/rpc-notify-new-version-test/handler.lua b/spec/fixtures/custom_plugins/kong/plugins/rpc-notify-new-version-test/handler.lua new file mode 100644 index 00000000000..7d6d7952804 --- /dev/null +++ b/spec/fixtures/custom_plugins/kong/plugins/rpc-notify-new-version-test/handler.lua @@ -0,0 +1,98 @@ +local fmt = string.format +local rep = string.rep + + +local RpcSyncV2NotifyNewVersioinTestHandler = { + VERSION = "1.0", + PRIORITY = 1000, +} + + +function RpcSyncV2NotifyNewVersioinTestHandler:init_worker() + local counter = 0 + + -- mock function on cp side + kong.rpc.callbacks:register("kong.sync.v2.get_delta", function(node_id, current_versions) + local latest_version = fmt("v02_%028x", 10) + + local fake_uuid = "00000000-0000-0000-0000-111111111111" + + -- a basic config data + local deltas = { + { + entity = { + id = fake_uuid, + name = "default", + }, + type = "workspaces", + version = latest_version, + ws_id = fake_uuid, + }, + } + + ngx.log(ngx.DEBUG, "kong.sync.v2.get_delta ok: ", counter) + counter = counter + 1 + + return { default = { deltas = deltas, wipe = true, }, } + end) + + -- test dp's sync.v2.notify_new_version on cp side + kong.rpc.callbacks:register("kong.test.notify_new_version", function(node_id) + local dp_node_id = next(kong.rpc.clients) + local method = "kong.sync.v2.notify_new_version" + + -- no default + local msg = {} + local res, err = kong.rpc:call(dp_node_id, method, msg) + assert(not res) + assert(err == "default namespace does not exist inside params") + + -- no default.new_version + local msg = { default = {}, } + local res, err = kong.rpc:call(dp_node_id, method, msg) + assert(not res) + assert(err == "'new_version' key does not exist") + + -- less version string + -- "....." < "00000" < "v02_xx" + local msg = { default = { new_version = rep(".", 32), }, } + local res, err = kong.rpc:call(dp_node_id, method, msg) + assert(res) + assert(not err) + + -- less or equal version string + -- "00000" < "v02_xx" + local msg = { default = { new_version = rep("0", 32), }, } + local res, err = kong.rpc:call(dp_node_id, method, msg) + assert(res) + assert(not err) + + -- greater version string + local msg = { default = { new_version = fmt("v02_%028x", 20), }, } + local res, err = kong.rpc:call(dp_node_id, method, msg) + assert(res) + assert(not err) + + ngx.log(ngx.DEBUG, "kong.test.notify_new_version ok") + + return true + end) + + local worker_events = assert(kong.worker_events) + + -- if rpc is ready we will send test calls + worker_events.register(function(capabilities_list) + local node_id = "control_plane" + + -- trigger cp's test + local res, err = kong.rpc:call(node_id, "kong.test.notify_new_version") + assert(res == true) + assert(not err) + + ngx.log(ngx.DEBUG, "kong.test.notify_new_version ok") + + end, "clustering:jsonrpc", "connected") +end + + +return RpcSyncV2NotifyNewVersioinTestHandler diff --git a/spec/fixtures/custom_plugins/kong/plugins/rpc-notify-new-version-test/schema.lua b/spec/fixtures/custom_plugins/kong/plugins/rpc-notify-new-version-test/schema.lua new file mode 100644 index 00000000000..f920dcb599c --- /dev/null +++ b/spec/fixtures/custom_plugins/kong/plugins/rpc-notify-new-version-test/schema.lua @@ -0,0 +1,12 @@ +return { + name = "rpc-notify-new-version-test", + fields = { + { + config = { + type = "record", + fields = { + }, + }, + }, + }, +}