Skip to content

Commit

Permalink
feat(conf_loader): lookup and add wasmx dynamic module to conf
Browse files Browse the repository at this point in the history
  • Loading branch information
locao authored and gszr committed Jul 10, 2023
1 parent 7c0f3eb commit a6ce3ad
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 20 deletions.
19 changes: 0 additions & 19 deletions build/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,6 @@ wasmx_vm_cmd = select({
"//conditions:default": "",
})

wasmx_load_module = select({
"@kong//:wasmx_dynamic_mod_flag": """
if ! grep -q "ngx_wasm_module.so" kong/templates/nginx.lua; then
WASM_LINE_TPL=$((`grep -n -m1 "error_log" kong/templates/nginx.lua | cut -d: -f1` + 1))
WASM_LINE_FXT=$((`grep -n -m1 "error_log" spec/fixtures/custom_nginx.template | cut -d: -f1` + 1))
if [[ "$OSTYPE" == "darwin"* ]]; then
gsed -i "$WASM_LINE_TPL a load_module ${BUILD_DESTDIR}/openresty/nginx/modules/ngx_wasm_module.so;" kong/templates/nginx.lua
gsed -i "$WASM_LINE_FXT a load_module ${BUILD_DESTDIR}/openresty/nginx/modules/ngx_wasm_module.so;" spec/fixtures/custom_nginx.template
else
sed -i "$WASM_LINE_TPL a load_module ${BUILD_DESTDIR}/openresty/nginx/modules/ngx_wasm_module.so;" kong/templates/nginx.lua
sed -i "$WASM_LINE_FXT a load_module ${BUILD_DESTDIR}/openresty/nginx/modules/ngx_wasm_module.so;" spec/fixtures/custom_nginx.template
fi
fi
""",
"//conditions:default": "",
})

kong_directory_genrule(
name = "kong",
srcs = [
Expand Down Expand Up @@ -167,8 +150,6 @@ kong_directory_genrule(
ATC_ROUTER=${WORKSPACE_PATH}/$(location @atc_router)
cp $ATC_ROUTER ${BUILD_DESTDIR}/openresty/lualib/.
""" + wasmx_load_module +
"""
cp -r $(locations @protoc//:all_srcs) ${BUILD_DESTDIR}/kong/.
""" + install_lib_deps_cmd + install_lualib_deps_cmd + wasm_libs_cmd + wasmx_vm_cmd +
Expand Down
37 changes: 37 additions & 0 deletions kong/conf_loader/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local require = require

local kong_default_conf = require "kong.templates.kong_defaults"
local process_secrets = require "kong.cmd.utils.process_secrets"
local nginx_signals = require "kong.cmd.utils.nginx_signals"
local openssl_pkey = require "resty.openssl.pkey"
local openssl_x509 = require "resty.openssl.x509"
local pl_stringio = require "pl.stringio"
Expand Down Expand Up @@ -621,6 +622,34 @@ local function parse_value(value, typ)
end


-- Check if module is dynamic
local function check_dynamic_module(mod_name)
local configure_line = ngx.config.nginx_configure()
local mod_re = [[^.*--add-dynamic-module=(.+\/]] .. mod_name .. [[(\s|$)).*$]]
return ngx.re.match(configure_line, mod_re, "oi") ~= nil
end


-- Lookup dynamic module object
-- this function will lookup for the `mod_name` dynamic module in the following
-- paths:
-- - path defined in KONG_DYNAMIC_MODULES_PATH environment variable
-- - <nginx build prefix>/modules
-- - /usr/local/openresty/nginx/modules
-- @param[type=string] mod_name The module name to lookup, without file extension
local function lookup_dynamic_module_so(mod_name, kong_conf)
log.debug("looking up dynamic module %s", mod_name)
local nginx_bin = nginx_signals.find_nginx_bin(kong_conf)
local mod_file = fmt("%s/../modules/%s.so", pl_path.dirname(nginx_bin), mod_name)
if exists(mod_file) then
log.debug("Module '%s' found at '%s'", mod_name, mod_file)
return mod_file
end

return nil, fmt("%s dynamic module shared object not found", mod_name)
end


-- Validate Wasm properties
local function validate_wasm(wasm_enabled, filters_path)
if wasm_enabled and filters_path then
Expand Down Expand Up @@ -1229,6 +1258,14 @@ local function check_and_parse(conf, opts)
errors[#errors + 1] = err
end

if conf.wasm and check_dynamic_module("ngx_wasm_module") then
local err
conf.wasm_dynamic_module, err = lookup_dynamic_module_so("ngx_wasm_module", conf)
if err then
errors[#errors + 1] = err
end
end

return #errors == 0, errors[1], errors
end

Expand Down
1 change: 1 addition & 0 deletions kong/templates/kong_defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,5 @@ tracing_sampling_rate = 0.01
wasm = off
wasm_filters_path = NONE
wasm_dynamic_module = NONE
]]
6 changes: 5 additions & 1 deletion kong/templates/nginx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ return [[
pid pids/nginx.pid;
error_log ${{PROXY_ERROR_LOG}} ${{LOG_LEVEL}};
> if wasm and wasm_dynamic_module then
load_module $(wasm_dynamic_module);
> end
# injected nginx_main_* directives
> for _, el in ipairs(nginx_main_directives) do
$(el.name) $(el.value);
Expand All @@ -19,7 +23,7 @@ events {
> end
}
> if wasm_modules_parsed and #wasm_modules_parsed > 0 then
> if wasm and wasm_modules_parsed and #wasm_modules_parsed > 0 then
wasm {
shm_kv kong_wasm_rate_limiting_counters 12m;
Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/custom_nginx.template
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
pid pids/nginx.pid; # mandatory even for custom config templates
error_log ${{PROXY_ERROR_LOG}} ${{LOG_LEVEL}};

> if wasm and wasm_dynamic_module then
load_module $(wasm_dynamic_module);
> end

# injected nginx_main_* directives
> for _, el in ipairs(nginx_main_directives) do
$(el.name) $(el.value);
Expand Down

0 comments on commit a6ce3ad

Please sign in to comment.