From cbeaeb8c9530880c47c4ffb1471db8d71ecaac06 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Sat, 21 Jan 2023 14:20:02 +0800 Subject: [PATCH 01/11] compatible_checkers only log warn once style fix change to array style clean checkers.lua lint fix --- kong-3.2.1-0.rockspec | 1 + kong/clustering/compat/checkers.lua | 152 ++++++++++++++++++++++++++++ kong/clustering/compat/init.lua | 91 ++--------------- 3 files changed, 159 insertions(+), 85 deletions(-) create mode 100644 kong/clustering/compat/checkers.lua diff --git a/kong-3.2.1-0.rockspec b/kong-3.2.1-0.rockspec index 54b65508825..885325a412c 100644 --- a/kong-3.2.1-0.rockspec +++ b/kong-3.2.1-0.rockspec @@ -78,6 +78,7 @@ build = { ["kong.clustering.compat"] = "kong/clustering/compat/init.lua", ["kong.clustering.compat.version"] = "kong/clustering/compat/version.lua", ["kong.clustering.compat.removed_fields"] = "kong/clustering/compat/removed_fields.lua", + ["kong.clustering.compat.checkers"] = "kong/clustering/compat/checkers.lua", ["kong.clustering.config_helper"] = "kong/clustering/config_helper.lua", ["kong.clustering.tls"] = "kong/clustering/tls.lua", diff --git a/kong/clustering/compat/checkers.lua b/kong/clustering/compat/checkers.lua new file mode 100644 index 00000000000..f3cf76c05e3 --- /dev/null +++ b/kong/clustering/compat/checkers.lua @@ -0,0 +1,152 @@ +local ipairs = ipairs + + +local ngx = ngx +local ngx_log = ngx.log +local ngx_WARN = ngx.WARN + + +local _log_prefix = "[clustering] " +local KONG_VERSION = require("kong.meta").version + + +local compatible_checkers = { + { 3003000000, --[[ 3.3.0.0 ]] + function(config_table, dp_version, log_suffix) + -- remove updated_at field for core entities ca_certificates, certificates, consumers, + -- targets, upstreams, plugins, workspaces, clustering_data_planes and snis + local entity_names = { + "ca_certificates", "certificates", "consumers", "targets", "upstreams", + "plugins", "workspaces", "clustering_data_planes", "snis", + } + + local has_update + + for _, name in ipairs(entity_names) do + for _, config_entity in ipairs(config_table[name] or {}) do + ngx_log(ngx_WARN, _log_prefix, "Kong Gateway v" .. KONG_VERSION .. + " contains configuration '" .. name .. ".updated_at'", + " which is incompatible with dataplane version " .. dp_version .. " and will", + " be removed.", log_suffix) + config_entity.updated_at = nil + end + end + end + }, + + { 3002000000, --[[ 3.2.0.0 ]] + function(config_table, dp_version, log_suffix) + local config_services = config_table["services"] + if not config_services then + return nil + end + + local has_update + for _, t in ipairs(config_services) do + if t["protocol"] == "tls" then + if t["client_certificate"] or t["tls_verify"] or + t["tls_verify_depth"] or t["ca_certificates"] then + + t["client_certificate"] = nil + t["tls_verify"] = nil + t["tls_verify_depth"] = nil + t["ca_certificates"] = nil + + has_update = true + end + end + end + + if has_update then + ngx_log(ngx_WARN, _log_prefix, "Kong Gateway v" .. KONG_VERSION .. + " tls protocol service contains configuration 'service.client_certificate'", + " or 'service.tls_verify' or 'service.tls_verify_depth' or 'service.ca_certificates'", + " which is incompatible with dataplane version " .. dp_version .. " and will", + " be removed.", log_suffix) + end + + return has_update + end + }, + + { 3002000000, --[[ 3.2.0.0 ]] + function(config_table, dp_version, log_suffix) + local config_upstreams = config_table["upstreams"] + if not config_upstreams then + return nil + end + + local has_update + for _, t in ipairs(config_upstreams) do + if t["algorithm"] == "latency" then + t["algorithm"] = "round-robin" + has_update = true + end + end + + if has_update then + ngx_log(ngx_WARN, _log_prefix, "Kong Gateway v" .. KONG_VERSION .. + " configuration 'upstream.algorithm' contain 'latency' option", + " which is incompatible with dataplane version " .. dp_version .. " and will", + " be fall back to 'round-robin'.", log_suffix) + end + + return has_update + end + }, + + { 3002000000, --[[ 3.2.0.0 ]] + function(config_table, dp_version, log_suffix) + local config_plugins = config_table["plugins"] + if not config_plugins then + return nil + end + + local has_update + for _, plugin in ipairs(config_plugins) do + if plugin["instance_name"] ~= nil then + plugin["instance_name"] = nil + has_update = true + end + end + + if has_update then + ngx_log(ngx_WARN, _log_prefix, "Kong Gateway v" .. KONG_VERSION .. + " contains configuration 'plugin.instance_name'", + " which is incompatible with dataplane version " .. dp_version, + " and will be removed.", log_suffix) + end + + return has_update + end + }, + + { 3001000000, --[[ 3.1.0.0 ]] + function(config_table, dp_version, log_suffix) + local config_upstreams = config_table["upstreams"] + if not config_upstreams then + return nil + end + + local has_update + for _, t in ipairs(config_upstreams) do + if t["use_srv_name"] ~= nil then + t["use_srv_name"] = nil + has_update = true + end + end + + if has_update then + ngx_log(ngx_WARN, _log_prefix, "Kong Gateway v" .. KONG_VERSION .. + " contains configuration 'upstream.use_srv_name'", + " which is incompatible with dataplane version " .. dp_version, + " and will be removed.", log_suffix) + end + + return has_update + end + }, +} + + +return compatible_checkers diff --git a/kong/clustering/compat/init.lua b/kong/clustering/compat/init.lua index e11f5673e6d..2854bf7a280 100644 --- a/kong/clustering/compat/init.lua +++ b/kong/clustering/compat/init.lua @@ -26,6 +26,7 @@ local extract_major_minor = version.extract_major_minor local _log_prefix = "[clustering] " local REMOVED_FIELDS = require("kong.clustering.compat.removed_fields") +local COMPATIBLE_CHECKERS = require("kong.clustering.compat.checkers") local CLUSTERING_SYNC_STATUS = constants.CLUSTERING_SYNC_STATUS local KONG_VERSION = meta.version @@ -360,91 +361,11 @@ function _M.update_compatible_payload(payload, dp_version, log_suffix) end end - if dp_version_num < 3003000000 --[[ 3.3.0.0 ]] then - -- remove updated_at field for core entities ca_certificates, certificates, consumers, - -- targets, upstreams, plugins, workspaces, clustering_data_planes and snis - local entity_names = {'ca_certificates', 'certificates', 'consumers', 'targets', 'upstreams', - 'plugins', 'workspaces', 'clustering_data_planes', 'snis'} - - for _, name in ipairs(entity_names) do - for _, config_entity in ipairs(config_table[name] or {}) do - if config_entity["updated_at"] ~= nil then - ngx_log(ngx_WARN, _log_prefix, "Kong Gateway v" .. KONG_VERSION .. - " contains configuration '" .. name .. ".updated_at'", - " which is incompatible with dataplane version " .. dp_version .. " and will", - " be removed.", log_suffix) - config_entity["updated_at"] = nil - has_update = true - end - end - end - end - - if dp_version_num < 3002000000 --[[ 3.2.0.0 ]] then - local config_plugins = config_table["plugins"] - if config_plugins then - for _, plugin in ipairs(config_plugins) do - if plugin["instance_name"] ~= nil then - ngx_log(ngx_WARN, _log_prefix, "Kong Gateway v" .. KONG_VERSION .. - " contains configuration 'plugin.instance_name'", - " which is incompatible with dataplane version " .. dp_version .. " and will", - " be removed.", log_suffix) - plugin["instance_name"] = nil - has_update = true - end - end - end - - local config_services = config_table["services"] - if config_services then - for _, t in ipairs(config_services) do - if t["protocol"] == "tls" then - if t["client_certificate"] or t["tls_verify"] - or t["tls_verify_depth"] or t["ca_certificates"] then - ngx_log(ngx_WARN, _log_prefix, "Kong Gateway v" .. KONG_VERSION .. - " tls protocol service contains configuration 'service.client_certificate'", - " or 'service.tls_verify' or 'service.tls_verify_depth' or 'service.ca_certificates'", - " which is incompatible with dataplane version " .. dp_version .. " and will", - " be removed.", log_suffix) - t["client_certificate"] = nil - t["tls_verify"] = nil - t["tls_verify_depth"] = nil - t["ca_certificates"] = nil - has_update = true - end - end - end - end - - local config_upstreams = config_table["upstreams"] - if config_upstreams then - for _, t in ipairs(config_upstreams) do - if t["algorithm"] == "latency" then - ngx_log(ngx_WARN, _log_prefix, "Kong Gateway v" .. KONG_VERSION .. - " configuration 'upstream.algorithm' contain 'latency' option", - " which is incompatible with dataplane version " .. dp_version .. " and will", - " be fall back to 'round-robin'.", log_suffix) - t["algorithm"] = "round-robin" - has_update = true - end - end - end - end - - - if dp_version_num < 3001000000 --[[ 3.1.0.0 ]] then - local config_upstream = config_table["upstreams"] - if config_upstream then - for _, t in ipairs(config_upstream) do - if t["use_srv_name"] ~= nil then - ngx_log(ngx_WARN, _log_prefix, "Kong Gateway v" .. KONG_VERSION .. - " contains configuration 'upstream.use_srv_name'", - " which is incompatible with dataplane version " .. dp_version .. " and will", - " be removed.", log_suffix) - t["use_srv_name"] = nil - has_update = true - end - end + for _, checker in ipairs(COMPATIBLE_CHECKERS) do + local ver = checker[1] + local fn = checker[2] + if dp_version_num < ver and fn(config_table, dp_version, log_suffix) then + has_update = true end end From 4d1ff67ae5c62715fcc5d4e11ec7249b1c3aa899 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Wed, 8 Mar 2023 14:55:01 +0800 Subject: [PATCH 02/11] log_warn_message --- kong/clustering/compat/checkers.lua | 56 ++++++++++++++++++----------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/kong/clustering/compat/checkers.lua b/kong/clustering/compat/checkers.lua index f3cf76c05e3..a017aafbbea 100644 --- a/kong/clustering/compat/checkers.lua +++ b/kong/clustering/compat/checkers.lua @@ -1,15 +1,29 @@ local ipairs = ipairs -local ngx = ngx -local ngx_log = ngx.log -local ngx_WARN = ngx.WARN -local _log_prefix = "[clustering] " local KONG_VERSION = require("kong.meta").version +local log_warn_message +do + local ngx_log = ngx.log + local ngx_WARN = ngx.WARN + local fmt = string.format + + local _log_prefix = "[clustering] " + + log_warn_message = function(hint, action, dp_version, log_suffix) + local msg = fmt("Kong Gateway v%s %s " .. + "which is incompatible with dataplane version %s " .. + "and will %s.", + KONG_VERSION, hint, dp_version, action) + ngx_log(ngx_WARN, _log_prefix, msg, log_suffix) + end +end + + local compatible_checkers = { { 3003000000, --[[ 3.3.0.0 ]] function(config_table, dp_version, log_suffix) @@ -58,11 +72,11 @@ local compatible_checkers = { end if has_update then - ngx_log(ngx_WARN, _log_prefix, "Kong Gateway v" .. KONG_VERSION .. - " tls protocol service contains configuration 'service.client_certificate'", - " or 'service.tls_verify' or 'service.tls_verify_depth' or 'service.ca_certificates'", - " which is incompatible with dataplane version " .. dp_version .. " and will", - " be removed.", log_suffix) + log_warn_message("tls protocol service contains configuration 'service.client_certificate' " .. + "or 'service.tls_verify' or 'service.tls_verify_depth' or 'service.ca_certificates'", + "be removed", + dp_version, + log_suffix) end return has_update @@ -85,10 +99,10 @@ local compatible_checkers = { end if has_update then - ngx_log(ngx_WARN, _log_prefix, "Kong Gateway v" .. KONG_VERSION .. - " configuration 'upstream.algorithm' contain 'latency' option", - " which is incompatible with dataplane version " .. dp_version .. " and will", - " be fall back to 'round-robin'.", log_suffix) + log_warn_message("configuration 'upstream.algorithm' contains 'latency' option", + "fall back to 'round-robin'", + dp_version, + log_suffix) end return has_update @@ -111,10 +125,10 @@ local compatible_checkers = { end if has_update then - ngx_log(ngx_WARN, _log_prefix, "Kong Gateway v" .. KONG_VERSION .. - " contains configuration 'plugin.instance_name'", - " which is incompatible with dataplane version " .. dp_version, - " and will be removed.", log_suffix) + log_warn_message("contains configuration 'plugin.instance_name'", + "be removed", + dp_version, + log_suffix) end return has_update @@ -137,10 +151,10 @@ local compatible_checkers = { end if has_update then - ngx_log(ngx_WARN, _log_prefix, "Kong Gateway v" .. KONG_VERSION .. - " contains configuration 'upstream.use_srv_name'", - " which is incompatible with dataplane version " .. dp_version, - " and will be removed.", log_suffix) + log_warn_message("contains configuration 'upstream.use_srv_name'", + "be removed", + dp_version, + log_suffix) end return has_update From 63d697e7c9a9eb8d9a74977216d56cf9db8a172d Mon Sep 17 00:00:00 2001 From: chronolaw Date: Wed, 8 Mar 2023 14:57:59 +0800 Subject: [PATCH 03/11] style fix --- kong/clustering/compat/checkers.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/kong/clustering/compat/checkers.lua b/kong/clustering/compat/checkers.lua index a017aafbbea..21f3a533e00 100644 --- a/kong/clustering/compat/checkers.lua +++ b/kong/clustering/compat/checkers.lua @@ -1,8 +1,6 @@ local ipairs = ipairs - - local KONG_VERSION = require("kong.meta").version From 6e6bf6885ed6cf652a728bb0058ce903ac8d9302 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Wed, 8 Mar 2023 15:15:34 +0800 Subject: [PATCH 04/11] style lint --- kong/clustering/compat/checkers.lua | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/kong/clustering/compat/checkers.lua b/kong/clustering/compat/checkers.lua index 21f3a533e00..ea99fbd2730 100644 --- a/kong/clustering/compat/checkers.lua +++ b/kong/clustering/compat/checkers.lua @@ -1,15 +1,14 @@ local ipairs = ipairs -local KONG_VERSION = require("kong.meta").version - - local log_warn_message do local ngx_log = ngx.log local ngx_WARN = ngx.WARN local fmt = string.format + local KONG_VERSION = require("kong.meta").version + local _log_prefix = "[clustering] " log_warn_message = function(hint, action, dp_version, log_suffix) From 05ec679fcc5f9253e2fa209d15e7b441bb5a2ac9 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Tue, 14 Mar 2023 09:19:04 +0800 Subject: [PATCH 05/11] minor style clean --- kong/clustering/compat/checkers.lua | 3 ++- kong/clustering/compat/init.lua | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/kong/clustering/compat/checkers.lua b/kong/clustering/compat/checkers.lua index ea99fbd2730..b3d88b0f030 100644 --- a/kong/clustering/compat/checkers.lua +++ b/kong/clustering/compat/checkers.lua @@ -56,7 +56,8 @@ local compatible_checkers = { for _, t in ipairs(config_services) do if t["protocol"] == "tls" then if t["client_certificate"] or t["tls_verify"] or - t["tls_verify_depth"] or t["ca_certificates"] then + t["tls_verify_depth"] or t["ca_certificates"] + then t["client_certificate"] = nil t["tls_verify"] = nil diff --git a/kong/clustering/compat/init.lua b/kong/clustering/compat/init.lua index 2854bf7a280..3d9859317b0 100644 --- a/kong/clustering/compat/init.lua +++ b/kong/clustering/compat/init.lua @@ -26,7 +26,7 @@ local extract_major_minor = version.extract_major_minor local _log_prefix = "[clustering] " local REMOVED_FIELDS = require("kong.clustering.compat.removed_fields") -local COMPATIBLE_CHECKERS = require("kong.clustering.compat.checkers") +local COMPATIBILITY_CHECKERS = require("kong.clustering.compat.checkers") local CLUSTERING_SYNC_STATUS = constants.CLUSTERING_SYNC_STATUS local KONG_VERSION = meta.version @@ -361,7 +361,7 @@ function _M.update_compatible_payload(payload, dp_version, log_suffix) end end - for _, checker in ipairs(COMPATIBLE_CHECKERS) do + for _, checker in ipairs(COMPATIBILITY_CHECKERS) do local ver = checker[1] local fn = checker[2] if dp_version_num < ver and fn(config_table, dp_version, log_suffix) then From 58f88bc660fe61254e4bf9477f94c134ba1edb93 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Thu, 16 Mar 2023 13:56:15 +0800 Subject: [PATCH 06/11] clean `update_at` code --- kong/clustering/compat/checkers.lua | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/kong/clustering/compat/checkers.lua b/kong/clustering/compat/checkers.lua index b3d88b0f030..1f8dacf53ea 100644 --- a/kong/clustering/compat/checkers.lua +++ b/kong/clustering/compat/checkers.lua @@ -35,13 +35,16 @@ local compatible_checkers = { for _, name in ipairs(entity_names) do for _, config_entity in ipairs(config_table[name] or {}) do - ngx_log(ngx_WARN, _log_prefix, "Kong Gateway v" .. KONG_VERSION .. - " contains configuration '" .. name .. ".updated_at'", - " which is incompatible with dataplane version " .. dp_version .. " and will", - " be removed.", log_suffix) + log_warn_message("contains configuration '" .. name .. ".updated_at'", + "be removed", + dp_version, + log_suffix) config_entity.updated_at = nil + has_update = true end end + + return has_update end }, @@ -70,7 +73,7 @@ local compatible_checkers = { end if has_update then - log_warn_message("tls protocol service contains configuration 'service.client_certificate' " .. + log_warn_message("tls protocol service contains configuration 'service.client_certificate'" .. "or 'service.tls_verify' or 'service.tls_verify_depth' or 'service.ca_certificates'", "be removed", dp_version, From ac67ef323e2d9a91a6bf8e45ce2141276462281e Mon Sep 17 00:00:00 2001 From: chronolaw Date: Thu, 16 Mar 2023 14:06:05 +0800 Subject: [PATCH 07/11] code clean --- kong/clustering/compat/checkers.lua | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/kong/clustering/compat/checkers.lua b/kong/clustering/compat/checkers.lua index 1f8dacf53ea..376f3246d33 100644 --- a/kong/clustering/compat/checkers.lua +++ b/kong/clustering/compat/checkers.lua @@ -32,15 +32,23 @@ local compatible_checkers = { } local has_update + local updated_entities = {} for _, name in ipairs(entity_names) do for _, config_entity in ipairs(config_table[name] or {}) do + config_entity.updated_at = nil + + updated_entities[name] = true + has_update = true + end + end + + if has_update then + for name, _ in pairs(updated_entities) do log_warn_message("contains configuration '" .. name .. ".updated_at'", "be removed", dp_version, log_suffix) - config_entity.updated_at = nil - has_update = true end end From ee530947b81b2e8bd5f55af87f38186126991f52 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Thu, 16 Mar 2023 14:12:03 +0800 Subject: [PATCH 08/11] code clean --- kong/clustering/compat/checkers.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kong/clustering/compat/checkers.lua b/kong/clustering/compat/checkers.lua index 376f3246d33..96436be1702 100644 --- a/kong/clustering/compat/checkers.lua +++ b/kong/clustering/compat/checkers.lua @@ -1,3 +1,4 @@ +local pairs = pairs local ipairs = ipairs @@ -36,7 +37,7 @@ local compatible_checkers = { for _, name in ipairs(entity_names) do for _, config_entity in ipairs(config_table[name] or {}) do - config_entity.updated_at = nil + config_entity["updated_at"] = nil updated_entities[name] = true has_update = true From 083b4a86a6c5ba5f865f0f3507b4588a059ebf2e Mon Sep 17 00:00:00 2001 From: chronolaw Date: Thu, 16 Mar 2023 17:26:37 +0800 Subject: [PATCH 09/11] add logic from #10501 --- kong/clustering/compat/checkers.lua | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/kong/clustering/compat/checkers.lua b/kong/clustering/compat/checkers.lua index 96436be1702..568145de4db 100644 --- a/kong/clustering/compat/checkers.lua +++ b/kong/clustering/compat/checkers.lua @@ -37,10 +37,13 @@ local compatible_checkers = { for _, name in ipairs(entity_names) do for _, config_entity in ipairs(config_table[name] or {}) do - config_entity["updated_at"] = nil + if config_entity["updated_at"] then - updated_entities[name] = true - has_update = true + config_entity["updated_at"] = nil + + updated_entities[name] = true + has_update = true + end end end From 817c312760ef15f96004360dcf2200fe3853a7ba Mon Sep 17 00:00:00 2001 From: chronolaw Date: Thu, 6 Apr 2023 15:29:05 +0800 Subject: [PATCH 10/11] refact log message --- kong/clustering/compat/checkers.lua | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/kong/clustering/compat/checkers.lua b/kong/clustering/compat/checkers.lua index 568145de4db..15bc41f597c 100644 --- a/kong/clustering/compat/checkers.lua +++ b/kong/clustering/compat/checkers.lua @@ -41,18 +41,17 @@ local compatible_checkers = { config_entity["updated_at"] = nil - updated_entities[name] = true has_update = true - end - end - end - if has_update then - for name, _ in pairs(updated_entities) do - log_warn_message("contains configuration '" .. name .. ".updated_at'", - "be removed", - dp_version, - log_suffix) + if not updated_entities[name] then + log_warn_message("contains configuration '" .. name .. ".updated_at'", + "be removed", + dp_version, + log_suffix) + + updated_entities[name] = true + end + end end end From 84f60e4b8a7149165e76114363964072a03eae6a Mon Sep 17 00:00:00 2001 From: chronolaw Date: Thu, 6 Apr 2023 15:53:09 +0800 Subject: [PATCH 11/11] fix lint --- kong/clustering/compat/checkers.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/kong/clustering/compat/checkers.lua b/kong/clustering/compat/checkers.lua index 15bc41f597c..4baa23ff50e 100644 --- a/kong/clustering/compat/checkers.lua +++ b/kong/clustering/compat/checkers.lua @@ -1,4 +1,3 @@ -local pairs = pairs local ipairs = ipairs