Skip to content

Commit

Permalink
refactor(tools): remove reference of module from utils (#12113)
Browse files Browse the repository at this point in the history
  • Loading branch information
chronolaw authored Dec 11, 2023
1 parent ac5b634 commit 9a1b557
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 42 deletions.
8 changes: 5 additions & 3 deletions kong/api/init.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
local lapis = require "lapis"
local utils = require "kong.tools.utils"
local api_helpers = require "kong.api.api_helpers"
local Endpoints = require "kong.api.endpoints"
local hooks = require "kong.hooks"


local load_module_if_exists = require "kong.tools.module".load_module_if_exists


local ngx = ngx
local type = type
local pairs = pairs
Expand Down Expand Up @@ -95,7 +97,7 @@ do
-- Custom Routes
for _, dao in pairs(kong.db.daos) do
local schema = dao.schema
local ok, custom_endpoints = utils.load_module_if_exists("kong.api.routes." .. schema.name)
local ok, custom_endpoints = load_module_if_exists("kong.api.routes." .. schema.name)
if ok then
customize_routes(routes, custom_endpoints, schema)
end
Expand All @@ -104,7 +106,7 @@ do
-- Plugin Routes
if kong.configuration and kong.configuration.loaded_plugins then
for k in pairs(kong.configuration.loaded_plugins) do
local loaded, custom_endpoints = utils.load_module_if_exists("kong.plugins." .. k .. ".api")
local loaded, custom_endpoints = load_module_if_exists("kong.plugins." .. k .. ".api")
if loaded then
ngx.log(ngx.DEBUG, "Loading API endpoints for plugin: ", k)
if api_helpers.is_new_db_routes(custom_endpoints) then
Expand Down
5 changes: 4 additions & 1 deletion kong/cache/warmup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ local utils = require "kong.tools.utils"
local constants = require "kong.constants"
local buffer = require "string.buffer"
local acl_groups
if utils.load_module_if_exists("kong.plugins.acl.groups") then


local load_module_if_exists = require "kong.tools.module".load_module_if_exists
if load_module_if_exists("kong.plugins.acl.groups") then
acl_groups = require "kong.plugins.acl.groups"
end

Expand Down
7 changes: 4 additions & 3 deletions kong/db/dao/plugins.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
local constants = require "kong.constants"
local utils = require "kong.tools.utils"
local DAO = require "kong.db.dao"
local plugin_loader = require "kong.db.schema.plugin_loader"
local reports = require "kong.reports"
local plugin_servers = require "kong.runloop.plugin_servers"
local version = require "version"
local load_module_if_exists = require "kong.tools.module".load_module_if_exists


local Plugins = {}

Expand Down Expand Up @@ -150,7 +151,7 @@ local load_plugin_handler do
-- NOTE: no version _G.kong (nor PDK) in plugins main chunk

local plugin_handler = "kong.plugins." .. plugin .. ".handler"
local ok, handler = utils.load_module_if_exists(plugin_handler)
local ok, handler = load_module_if_exists(plugin_handler)
if not ok then
ok, handler = plugin_servers.load_plugin(plugin)
if type(handler) == "table" then
Expand Down Expand Up @@ -202,7 +203,7 @@ local function load_plugin_entity_strategy(schema, db, plugin)

local custom_strat = fmt("kong.plugins.%s.strategies.%s.%s",
plugin, db.strategy, schema.name)
local exists, mod = utils.load_module_if_exists(custom_strat)
local exists, mod = load_module_if_exists(custom_strat)
if exists and mod then
local parent_mt = getmetatable(strategy)
local mt = {
Expand Down
4 changes: 2 additions & 2 deletions kong/db/dao/vaults.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local constants = require "kong.constants"
local utils = require "kong.tools.utils"
local vault_loader = require "kong.db.schema.vault_loader"
local load_module_if_exists = require "kong.tools.module".load_module_if_exists


local Vaults = {}
Expand All @@ -19,7 +19,7 @@ local DEBUG = ngx.DEBUG


local function load_vault_strategy(vault)
local ok, strategy = utils.load_module_if_exists("kong.vaults." .. vault)
local ok, strategy = load_module_if_exists("kong.vaults." .. vault)
if not ok then
return nil, vault .. " vault is enabled but not installed;\n" .. strategy
end
Expand Down
19 changes: 10 additions & 9 deletions kong/db/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ local MetaSchema = require "kong.db.schema.metaschema"
local constants = require "kong.constants"
local log = require "kong.cmd.utils.log"
local workspaces = require "kong.workspaces"
local utils = require "kong.tools.utils"
local knode = kong and kong.node
or require "kong.pdk.node".new()


local load_module_if_exists = require "kong.tools.module".load_module_if_exists


local fmt = string.format
local type = type
local pairs = pairs
Expand Down Expand Up @@ -71,7 +73,7 @@ function DB.new(kong_config, strategy)

-- load core entities subschemas
local subschemas
ok, subschemas = utils.load_module_if_exists("kong.db.schema.entities." .. entity_name .. "_subschemas")
ok, subschemas = load_module_if_exists("kong.db.schema.entities." .. entity_name .. "_subschemas")
if ok then
for name, subschema in pairs(subschemas) do
local ok, err = entity:new_subschema(name, subschema)
Expand Down Expand Up @@ -418,7 +420,6 @@ end

do
-- migrations
local utils = require "kong.tools.utils"
local MigrationsState = require "kong.db.migrations.state"


Expand Down Expand Up @@ -490,8 +491,8 @@ do
if run_teardown and options.skip_teardown_migrations then
for _, t in ipairs(options.skip_teardown_migrations) do
for _, mig in ipairs(t.migrations) do
local ok, mod = utils.load_module_if_exists(t.namespace .. "." ..
mig.name)
local ok, mod = load_module_if_exists(t.namespace .. "." ..
mig.name)
if ok then
local strategy_migration = mod[self.strategy]
if strategy_migration and strategy_migration.teardown then
Expand Down Expand Up @@ -523,8 +524,8 @@ do
self.infos.db_name)

for _, mig in ipairs(t.migrations) do
local ok, mod = utils.load_module_if_exists(t.namespace .. "." ..
mig.name)
local ok, mod = load_module_if_exists(t.namespace .. "." ..
mig.name)
if not ok then
self.connector:close()
return nil, fmt_err(self, "failed to load migration '%s': %s",
Expand Down Expand Up @@ -638,8 +639,8 @@ do
for _, t in ipairs(migrations) do
for _, mig in ipairs(t.migrations) do
local ok, mod = utils.load_module_if_exists(t.namespace .. "." ..
mig.name)
local ok, mod = load_module_if_exists(t.namespace .. "." ..
mig.name)
if not ok then
return nil, fmt("failed to load migration '%s': %s", mig.name,
mod)
Expand Down
10 changes: 6 additions & 4 deletions kong/db/migrations/state.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
local utils = require "kong.tools.utils"
local log = require "kong.cmd.utils.log"
local Schema = require "kong.db.schema"
local Migration = require "kong.db.schema.others.migrations"
local Errors = require "kong.db.errors"


local load_module_if_exists = require "kong.tools.module".load_module_if_exists


local MigrationSchema = Schema.new(Migration)


Expand Down Expand Up @@ -67,12 +69,12 @@ local function load_subsystems(db, plugin_names)
for _, plugin_name in ipairs(sorted_plugin_names) do
local namespace = ss.namespace:gsub("%*", plugin_name)

local ok, mig_idx = utils.load_module_if_exists(namespace)
local ok, mig_idx = load_module_if_exists(namespace)

if not ok then
-- fallback to using ".init" since "/?/init.lua" isn't always in a
-- Lua-path by default, see https://github.com/Kong/kong/issues/6867
ok, mig_idx = utils.load_module_if_exists(namespace .. ".init")
ok, mig_idx = load_module_if_exists(namespace .. ".init")
end

if ok then
Expand Down Expand Up @@ -104,7 +106,7 @@ local function load_subsystems(db, plugin_names)
for _, mig_name in ipairs(subsys.migrations_index) do
local mig_module = fmt("%s.%s", subsys.namespace, mig_name)

local ok, migration = utils.load_module_if_exists(mig_module)
local ok, migration = load_module_if_exists(mig_module)
if not ok then
return nil, fmt_err(db, "failed to load migration '%s' of '%s' subsystem",
mig_module, subsys.name)
Expand Down
3 changes: 2 additions & 1 deletion kong/db/schema/others/declarative_config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ local insert = table.insert
local concat = table.concat
local tostring = tostring
local cjson_encode = require("cjson.safe").encode
local load_module_if_exists = require("kong.tools.module").load_module_if_exists


local DeclarativeConfig = {}
Expand Down Expand Up @@ -847,7 +848,7 @@ end


local function load_entity_subschemas(entity_name, entity)
local ok, subschemas = utils.load_module_if_exists("kong.db.schema.entities." .. entity_name .. "_subschemas")
local ok, subschemas = load_module_if_exists("kong.db.schema.entities." .. entity_name .. "_subschemas")
if ok then
for name, subschema in pairs(subschemas) do
local ok, err = entity:new_subschema(name, subschema)
Expand Down
9 changes: 5 additions & 4 deletions kong/db/schema/plugin_loader.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
local MetaSchema = require "kong.db.schema.metaschema"
local Entity = require "kong.db.schema.entity"
local utils = require "kong.tools.utils"
local plugin_servers = require "kong.runloop.plugin_servers"
local is_array = require "kong.tools.table".is_array
local load_module_if_exists = require "kong.tools.module".load_module_if_exists


local fmt = string.format
Expand All @@ -13,7 +14,7 @@ local plugin_loader = {}

function plugin_loader.load_subschema(parent_schema, plugin, errors)
local plugin_schema = "kong.plugins." .. plugin .. ".schema"
local ok, schema = utils.load_module_if_exists(plugin_schema)
local ok, schema = load_module_if_exists(plugin_schema)
if not ok then
ok, schema = plugin_servers.load_schema(plugin)
end
Expand Down Expand Up @@ -56,11 +57,11 @@ end


function plugin_loader.load_entities(plugin, errors, loader_fn)
local has_daos, daos_schemas = utils.load_module_if_exists("kong.plugins." .. plugin .. ".daos")
local has_daos, daos_schemas = load_module_if_exists("kong.plugins." .. plugin .. ".daos")
if not has_daos then
return {}
end
if not utils.is_array(daos_schemas, "strict") then
if not is_array(daos_schemas, "strict") then
return nil, fmt("custom plugin '%s' returned non-array daos definition table", plugin)
end

Expand Down
4 changes: 2 additions & 2 deletions kong/db/schema/vault_loader.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local MetaSchema = require "kong.db.schema.metaschema"
local Entity = require "kong.db.schema.entity"
local utils = require "kong.tools.utils"
local load_module_if_exists = require "kong.tools.module".load_module_if_exists


local tostring = tostring
Expand All @@ -11,7 +11,7 @@ local vault_loader = {}

function vault_loader.load_subschema(parent_schema, vault, errors)
local vault_schema = "kong.vaults." .. vault .. ".schema"
local ok, schema = utils.load_module_if_exists(vault_schema)
local ok, schema = load_module_if_exists(vault_schema)
if not ok then
return nil, "no configuration schema found for vault: " .. vault
end
Expand Down
4 changes: 2 additions & 2 deletions kong/db/strategies/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local utils = require("kong.tools.utils")
local load_module_if_exists = require "kong.tools.module".load_module_if_exists


local fmt = string.format
Expand Down Expand Up @@ -55,7 +55,7 @@ function _M.new(kong_config, database, schemas, errors)
end

local custom_strat = fmt("kong.db.strategies.%s.%s", database, schema.name)
local exists, mod = utils.load_module_if_exists(custom_strat)
local exists, mod = load_module_if_exists(custom_strat)
if exists and mod then
local parent_mt = getmetatable(strategy)
local mt = {
Expand Down
5 changes: 3 additions & 2 deletions kong/runloop/certificate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ local pl_utils = require "pl.utils"
local mlcache = require "kong.resty.mlcache"
local new_tab = require "table.new"
local constants = require "kong.constants"
local utils = require "kong.tools.utils"
local plugin_servers = require "kong.runloop.plugin_servers"
local openssl_x509_store = require "resty.openssl.x509.store"
local openssl_x509 = require "resty.openssl.x509"
Expand Down Expand Up @@ -418,9 +417,11 @@ end
-- here we assume the field name is always `ca_certificates`
local get_ca_certificate_reference_plugins
do
local load_module_if_exists = require "kong.tools.module".load_module_if_exists

local function is_plugin_referencing_ca_certificates(name)
local plugin_schema = "kong.plugins." .. name .. ".schema"
local ok, schema = utils.load_module_if_exists(plugin_schema)
local ok, schema = load_module_if_exists(plugin_schema)
if not ok then
ok, schema = plugin_servers.load_schema(name)
end
Expand Down
6 changes: 3 additions & 3 deletions kong/status/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local lapis = require "lapis"
local utils = require "kong.tools.utils"
local api_helpers = require "kong.api.api_helpers"
local hooks = require "kong.hooks"
local load_module_if_exists = require "kong.tools.module".load_module_if_exists


local ngx = ngx
Expand Down Expand Up @@ -58,8 +58,8 @@ end
-- Load plugins status routes
if kong.configuration and kong.configuration.loaded_plugins then
for k in pairs(kong.configuration.loaded_plugins) do
local loaded, mod = utils.load_module_if_exists("kong.plugins." ..
k .. ".status_api")
local loaded, mod = load_module_if_exists("kong.plugins." ..
k .. ".status_api")

if loaded then
ngx.log(ngx.DEBUG, "Loading Status API endpoints for plugin: ", k)
Expand Down
4 changes: 2 additions & 2 deletions kong/tools/stream_api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,10 @@ end


function stream_api.load_handlers()
local utils = require "kong.tools.utils"
local load_module_if_exists = require "kong.tools.module".load_module_if_exists

for plugin_name in pairs(kong.configuration.loaded_plugins) do
local loaded, custom_endpoints = utils.load_module_if_exists("kong.plugins." .. plugin_name .. ".api")
local loaded, custom_endpoints = load_module_if_exists("kong.plugins." .. plugin_name .. ".api")
if loaded and custom_endpoints._stream then
log(DEBUG, "Register stream api for plugin: ", plugin_name)
_handlers[plugin_name] = custom_endpoints._stream
Expand Down
1 change: 0 additions & 1 deletion kong/tools/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ do
"kong.tools.rand",
"kong.tools.system",
"kong.tools.time",
"kong.tools.module",
"kong.tools.ip",
"kong.tools.http",
}
Expand Down
8 changes: 5 additions & 3 deletions spec/01-unit/05-utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -487,24 +487,26 @@ describe("Utils", function()
end)

describe("load_module_if_exists()", function()
local load_module_if_exists = require "kong.tools.module".load_module_if_exists

it("should return false if the module does not exist", function()
local loaded, mod
assert.has_no.errors(function()
loaded, mod = utils.load_module_if_exists("kong.does.not.exist")
loaded, mod = load_module_if_exists("kong.does.not.exist")
end)
assert.False(loaded)
assert.is.string(mod)
end)
it("should throw an error with a traceback if the module is invalid", function()
local pok, perr = pcall(utils.load_module_if_exists, "spec.fixtures.invalid-module")
local pok, perr = pcall(load_module_if_exists, "spec.fixtures.invalid-module")
assert.falsy(pok)
assert.match("error loading module 'spec.fixtures.invalid-module'", perr, 1, true)
assert.match("./spec/fixtures/invalid-module.lua:", perr, 1, true)
end)
it("should load a module if it was found and valid", function()
local loaded, mod
assert.has_no.errors(function()
loaded, mod = utils.load_module_if_exists("spec.fixtures.valid-module")
loaded, mod = load_module_if_exists("spec.fixtures.valid-module")
end)
assert.True(loaded)
assert.truthy(mod)
Expand Down

0 comments on commit 9a1b557

Please sign in to comment.