Skip to content

Commit

Permalink
feat(wasm): add support for wasmtime cache
Browse files Browse the repository at this point in the history
  • Loading branch information
flrgh committed May 2, 2024
1 parent 66e9b88 commit 35d459c
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions kong-3.7.0-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ build = {
["kong.templates.nginx_kong_inject"] = "kong/templates/nginx_kong_inject.lua",
["kong.templates.nginx_kong_stream_inject"] = "kong/templates/nginx_kong_stream_inject.lua",
["kong.templates.kong_yml"] = "kong/templates/kong_yml.lua",
["kong.templates.wasmtime_cache_config"] = "kong/templates/wasmtime_cache_config.lua",

["kong.resty.dns.client"] = "kong/resty/dns/client.lua",
["kong.resty.dns.utils"] = "kong/resty/dns/utils.lua",
Expand Down
22 changes: 22 additions & 0 deletions kong/cmd/utils/prefix_handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local kong_nginx_stream_template = require "kong.templates.nginx_kong_stream"
local nginx_main_inject_template = require "kong.templates.nginx_inject"
local nginx_http_inject_template = require "kong.templates.nginx_kong_inject"
local nginx_stream_inject_template = require "kong.templates.nginx_kong_stream_inject"
local wasmtime_cache_template = require "kong.templates.wasmtime_cache_config"
local system_constants = require "lua_system_constants"
local process_secrets = require "kong.cmd.utils.process_secrets"
local openssl_bignum = require "resty.openssl.bn"
Expand Down Expand Up @@ -419,6 +420,10 @@ local function compile_nginx_conf(kong_config, template)
return compile_conf(kong_config, template)
end

local function compile_wasmtime_cache_conf(kong_config)
return compile_conf(kong_config, wasmtime_cache_template)
end

local function prepare_prefixed_interface_dir(usr_path, interface_dir, kong_config)
local usr_interface_path = usr_path .. "/" .. interface_dir
local interface_path = kong_config.prefix .. "/" .. interface_dir
Expand Down Expand Up @@ -673,6 +678,23 @@ local function prepare_prefix(kong_config, nginx_custom_template_path, skip_writ
return true
end

if kong_config.wasm then
if kong_config.wasmtime_cache_directory then
local ok, err = makepath(kong_config.wasmtime_cache_directory)
if not ok then
return nil, err
end
end

if kong_config.wasmtime_cache_config_file then
local wasmtime_conf, err = compile_wasmtime_cache_conf(kong_config)
if not wasmtime_conf then
return nil, err
end
pl_file.write(kong_config.wasmtime_cache_config_file, wasmtime_conf)
end
end

-- compile Nginx configurations
local nginx_template
if nginx_custom_template_path then
Expand Down
4 changes: 4 additions & 0 deletions kong/conf_loader/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,10 @@ local function load(path, custom_conf, opts)
-- TODO: as a temporary compatibility fix, we are forcing it to 'off'.
add_wasm_directive("nginx_http_proxy_wasm_lua_resolver", "off")

-- configure wasmtime module cache
conf.wasmtime_cache_directory = pl_path.join(conf.prefix, ".wasmtime_cache")
conf.wasmtime_cache_config_file = pl_path.join(conf.prefix, ".wasmtime_config.toml")

-- wasm vm properties are inherited from previously set directives
if conf.lua_ssl_trusted_certificate and #conf.lua_ssl_trusted_certificate >= 1 then
add_wasm_directive("tls_trusted_certificate", conf.lua_ssl_trusted_certificate[1], wasm_main_prefix)
Expand Down
3 changes: 1 addition & 2 deletions kong/templates/nginx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ wasm {
> end
> end
> if #nginx_wasm_wasmtime_directives > 0 then
wasmtime {
cache_config ${{WASMTIME_CACHE_CONFIG_FILE}};
> for _, el in ipairs(nginx_wasm_wasmtime_directives) do
flag $(el.name) $(el.value);
> end
}
> end -- wasmtime
> if #nginx_wasm_v8_directives > 0 then
v8 {
Expand Down
5 changes: 5 additions & 0 deletions kong/templates/wasmtime_cache_config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
return [[
[cache]
enabled = true
directory = "${{WASMTIME_CACHE_DIRECTORY}}"
]]
3 changes: 1 addition & 2 deletions spec/fixtures/custom_nginx.template
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ wasm {
> end
> end

> if #nginx_wasm_wasmtime_directives > 0 then
wasmtime {
cache_config ${{WASMTIME_CACHE_CONFIG_FILE}};
> for _, el in ipairs(nginx_wasm_wasmtime_directives) do
flag $(el.name) $(el.value);
> end
}
> end -- wasmtime

> if #nginx_wasm_v8_directives > 0 then
v8 {
Expand Down

0 comments on commit 35d459c

Please sign in to comment.