Skip to content

Commit

Permalink
feat(pdk): kong.node.is[_not]_data_plane|control_plane|traditional|et…
Browse files Browse the repository at this point in the history
…c. helpers

### Summary

Adds following PDK functions:

- `kong.node.is_data_plane()`
- `kong.node.is_control_plane()`
- `kong.node.is_traditional()`
- `kong.node.is_dbless()`
- `kong.node.is_hybrid()`
- `kong.node.is_serving_http_traffic()`
- `kong.node.is_serving_stream_traffic()`
- `kong.node.is_serving_proxy_traffic()`
- `kong.node.is_serving_admin_apis()`
- `kong.node.is_serving_admin_gui()`
- `kong.node.is_serving_status_apis()`
- `kong.node.is_not_data_plane()`
- `kong.node.is_not_control_plane()`
- `kong.node.is_not_traditional()`
- `kong.node.is_not_hybrid()`
- `kong.node.is_not_serving_http_traffic()`
- `kong.node.is_not_serving_stream_traffic()`
- `kong.node.is_not_serving_proxy_traffic()`
- `kong.node.is_not_serving_admin_apis()`
- `kong.node.is_not_serving_admin_gui()`
- `kong.node.is_not_serving_status_apis()`

Signed-off-by: Aapo Talvensaari <[email protected]>
  • Loading branch information
bungle committed Oct 20, 2023
1 parent 74bd113 commit 739689f
Show file tree
Hide file tree
Showing 26 changed files with 434 additions and 148 deletions.
4 changes: 2 additions & 2 deletions kong/api/routes/clustering.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ return {
schema = kong.db.clustering_data_planes.schema,
methods = {
GET = function(self, dao, helpers)
if kong.configuration.role ~= "control_plane" then
if kong.node.is_not_control_plane() then
return kong.response.exit(400, {
message = "this endpoint is only available when Kong is " ..
"configured to run as Control Plane for the cluster"
Expand All @@ -28,7 +28,7 @@ return {
schema = kong.db.clustering_data_planes.schema,
methods = {
GET = function(self, db, helpers)
if kong.configuration.role ~= "control_plane" then
if kong.node.is_not_control_plane() then
return kong.response.exit(400, {
message = "this endpoint is only available when Kong is " ..
"configured to run as Control Plane for the cluster"
Expand Down
4 changes: 2 additions & 2 deletions kong/api/routes/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ end
return {
["/config"] = {
GET = function(self, db)
if kong.db.strategy ~= "off" then
if kong.node.is_not_dbless() then
return kong.response.exit(400, {
message = "this endpoint is only available when Kong is " ..
"configured to not use a database"
Expand All @@ -109,7 +109,7 @@ return {
return kong.response.exit(200, { config = file.buf:get() })
end,
POST = function(self, db)
if kong.db.strategy ~= "off" then
if kong.node.is_not_dbless() then
return kong.response.exit(400, {
message = "this endpoint is only available when Kong is " ..
"configured to not use a database"
Expand Down
12 changes: 5 additions & 7 deletions kong/api/routes/debug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ local DEFAULT_LOG_LEVEL_TIMEOUT = 60 -- 60s


local function handle_put_log_level(self, broadcast)
if kong.configuration.database == "off" then
if kong.node.is_dbless() then
local message = "cannot change log level when not using a database"
return kong.response.exit(405, { message = message })
end
Expand Down Expand Up @@ -113,18 +113,16 @@ local routes = {
}


local cluster_name
local cluster_name = kong.node.is_control_plane()
and "/debug/cluster/control-planes-nodes/log-level/:log_level"
or "/debug/cluster/log-level/:log_level"

if kong.configuration.role == "control_plane" then
cluster_name = "/debug/cluster/control-planes-nodes/log-level/:log_level"
else
cluster_name = "/debug/cluster/log-level/:log_level"
end

routes[cluster_name] = {
PUT = function(self)
return handle_put_log_level(self, CLUSTER_LEVEL_BROADCAST)
end
}


return routes
6 changes: 1 addition & 5 deletions kong/api/routes/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ local knode = (kong and kong.node) and kong.node or
require "kong.pdk.node".new()


local dbless = kong.configuration.database == "off"
local data_plane_role = kong.configuration.role == "data_plane"


return {
["/status"] = {
GET = function(self, dao, helpers)
Expand Down Expand Up @@ -49,7 +45,7 @@ return {
-- to make decisions when something changes in the data-plane (e.g.
-- if the gateway gets unexpectedly restarted and its configuration
-- has been reset to empty).
if dbless or data_plane_role then
if kong.node.is_dbless() then
status_response.configuration_hash = declarative.get_current_hash()
-- remove the meanless database entry when in dbless mode or data plane
status_response.database = nil
Expand Down
2 changes: 1 addition & 1 deletion kong/api/routes/upstreams.lua
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ local api_routes = {
}

-- upstream targets' healthcheck management is not available in the hybrid mode
if kong.configuration.role ~= "control_plane" then
if kong.node.is_not_control_plane() then
api_routes["/upstreams/:upstreams/targets/:targets/healthy"] = {
PUT = function(self, db)
return set_target_health(self, db, true)
Expand Down
15 changes: 3 additions & 12 deletions kong/cluster_events/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,9 @@ function _M.new(opts)
local poll_delay = max(opts.poll_delay or 0, 0)

do
local db_strategy

if opts.db.strategy == "postgres" then
db_strategy = require "kong.cluster_events.strategies.postgres"

elseif opts.db.strategy == "off" then
db_strategy = require "kong.cluster_events.strategies.off"

else
return error("no cluster_events strategy for " ..
opts.db.strategy)
end
local db_strategy = kong.node.is_dbless()
and require("kong.cluster_events.strategies.off")
or require("kong.cluster_events.strategies.postgres")

local event_ttl_in_db = max(poll_offset * 10, MIN_EVENT_TTL_IN_DB)

Expand Down
6 changes: 2 additions & 4 deletions kong/clustering/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,12 @@ function _M:init_worker()
filters = filters,
}

local role = self.conf.role

if role == "control_plane" then
if kong.node.is_control_plane() then
self:init_cp_worker(basic_info)
return
end

if role == "data_plane" then
if kong.node.is_data_plane() then
self:init_dp_worker(basic_info)
end
end
Expand Down
2 changes: 1 addition & 1 deletion kong/db/dao/workspaces.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local Workspaces = {}

function Workspaces:truncate()
self.super.truncate(self)
if kong.configuration.database == "off" then
if kong.node.is_dbless() then
return true
end

Expand Down
4 changes: 2 additions & 2 deletions kong/db/declarative/export.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ end


local function begin_transaction(db)
if db.strategy == "postgres" then
if kong.node.is_not_dbless() then
local ok, err = db.connector:connect("read")
if not ok then
return nil, err
Expand All @@ -86,7 +86,7 @@ end


local function end_transaction(db)
if db.strategy == "postgres" then
if kong.node.is_not_dbless() then
-- just finish up the read-only transaction,
-- either COMMIT or ROLLBACK is fine.
db.connector:query("ROLLBACK;", "read")
Expand Down
2 changes: 1 addition & 1 deletion kong/db/schema/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1692,7 +1692,7 @@ function Schema:process_auto_fields(data, context, nulls, opts)
local resolve_references
if is_select and not nulls then
if kong and kong.configuration then
resolve_references = kong.configuration.role ~= "control_plane"
resolve_references = kong.node.is_not_control_plane()
else
resolve_references = true
end
Expand Down
2 changes: 1 addition & 1 deletion kong/db/strategies/postgres/connector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ end


function _mt:init_worker(strategies)
if ngx.worker.id() == 0 and #kong.configuration.admin_listeners > 0 then
if ngx.worker.id() == 0 and kong.node.is_serving_admin_apis() then
local table_names = get_names_of_tables_with_ttl(strategies)
local ttl_escaped = self:escape_identifier("ttl")
local expire_at_escaped = self:escape_identifier("expire_at")
Expand Down
17 changes: 8 additions & 9 deletions kong/global.lua
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,10 @@ function _GLOBAL.init_cluster_events(kong_config, db)
end


local function get_lru_size(kong_config)
if (process.type() == "privileged agent")
or (kong_config.role == "control_plane")
or (kong_config.role == "traditional" and #kong_config.proxy_listeners == 0
and #kong_config.stream_listeners == 0)
local function get_lru_size()
if process.type() == "privileged agent"
or kong.node.is_control_plane()
or (kong.node.is_traditional() and kong.node.is_not_serving_proxy_traffic())
then
return 1000
end
Expand All @@ -243,7 +242,7 @@ function _GLOBAL.init_cache(kong_config, cluster_events, worker_events)
local page = 1
local cache_pages = 1

if kong_config.database == "off" then
if kong.node.is_dbless() then
db_cache_ttl = 0
db_cache_neg_ttl = 0
end
Expand All @@ -258,7 +257,7 @@ function _GLOBAL.init_cache(kong_config, cluster_events, worker_events)
page = page,
cache_pages = cache_pages,
resty_lock_opts = LOCK_OPTS,
lru_size = get_lru_size(kong_config),
lru_size = get_lru_size(),
})
end

Expand All @@ -269,7 +268,7 @@ function _GLOBAL.init_core_cache(kong_config, cluster_events, worker_events)
local page = 1
local cache_pages = 1

if kong_config.database == "off" then
if kong.node.is_dbless() then
db_cache_ttl = 0
db_cache_neg_ttl = 0
end
Expand All @@ -284,7 +283,7 @@ function _GLOBAL.init_core_cache(kong_config, cluster_events, worker_events)
page = page,
cache_pages = cache_pages,
resty_lock_opts = LOCK_OPTS,
lru_size = get_lru_size(kong_config),
lru_size = get_lru_size(),
})
end

Expand Down
Loading

0 comments on commit 739689f

Please sign in to comment.