Skip to content

Commit

Permalink
feat(wasm): support directive for general shm_kv entry (#12663)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Martin <[email protected]>
Co-authored-by: Vinicius Mignot <[email protected]>
  • Loading branch information
3 people authored Mar 22, 2024
1 parent 48cd3a0 commit e0b825c
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 4 deletions.
6 changes: 6 additions & 0 deletions changelog/unreleased/kong/feat-wasm-general-shm-kv.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
message: |
Introduce `nginx_wasm_main_shm_kv` configuration entry, which enables
Wasm filters to use the Proxy-Wasm operations `get_shared_data` and
`set_shared_data` without namespaced keys.
type: feature
scope: Configuration
11 changes: 9 additions & 2 deletions kong.conf.default
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@

#mem_cache_size = 128m # Size of each of the two shared memory caches
# for traditional mode database entities
# and runtime data, `kong_core_cache` and
# and runtime data, `kong_core_cache` and
# `kong_cache`.
#
# The accepted units are `k` and `m`, with a minimum
Expand Down Expand Up @@ -2065,9 +2065,16 @@
# The following namespaces are supported:
#
# - `nginx_wasm_<directive>`: Injects `<directive>` into the `wasm {}` block.
# - `nginx_wasm_shm_kv`: Injects `shm_kv *` into the `wasm {}` block,
# allowing operators to define a general memory zone which is usable by
# the `get_shared_data`/`set_shared_data` Proxy-Wasm SDK functions as
# an in-memory key-value store of data shareable across filters.
# - `nginx_wasm_shm_kv_<name>`: Injects `shm_kv <name>` into the `wasm {}` block,
# allowing operators to define custom shared memory zones which are usable by
# the `get_shared_data`/`set_shared_data` Proxy-Wasm SDK functions.
# the `get_shared_data`/`set_shared_data` Proxy-Wasm SDK functions as
# separate namespaces in the `"<name>/<key>"` format.
# For using these functions with non-namespaced keys, the Nginx template needs
# a `shm_kv *` entry, which can be defined using `nginx_wasm_shm_kv`.
# - `nginx_wasm_wasmtime_<flag>`: Injects `flag <flag>` into the `wasmtime {}`
# block, allowing various Wasmtime-specific flags to be set.
# - `nginx_<http|proxy>_<directive>`: Injects `<directive>` into the
Expand Down
2 changes: 2 additions & 0 deletions kong/conf_loader/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ local CONF_PARSERS = {
},
},

nginx_wasm_main_shm_kv = { typ = "string" },

worker_events_max_payload = { typ = "number" },

upstream_keepalive_pool_size = { typ = "number" },
Expand Down
4 changes: 4 additions & 0 deletions kong/templates/nginx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ wasm {
> end
> for _, el in ipairs(nginx_wasm_main_directives) do
> if el.name == "shm_kv" then
shm_kv * $(el.value);
> else
$(el.name) $(el.value);
> end
> end
> if #nginx_wasm_wasmtime_directives > 0 then
wasmtime {
Expand Down
12 changes: 10 additions & 2 deletions spec/01-unit/04-prefix_handler_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -911,10 +911,18 @@ describe("NGINX conf compiler", function()
it("injects a shm_kv", function()
assert.matches("wasm {.+shm_kv counters 10m;.+}", ngx_cfg({ wasm = true, nginx_wasm_shm_kv_counters="10m" }, debug))
end)
it("injects a general shm_kv", function()
assert.matches("wasm {.+shm_kv %* 10m;.+}", ngx_cfg({ wasm = true, nginx_wasm_shm_kv = "10m" }, debug))
end)
it("injects multiple shm_kvs", function()
assert.matches(
"wasm {.+shm_kv cache 10m.+shm_kv counters 10m;.+}",
ngx_cfg({ wasm = true, nginx_wasm_shm_kv_cache="10m", nginx_wasm_shm_kv_counters="10m"}, debug)
"wasm {.+shm_kv cache 10m.+shm_kv counters 10m;.+shm_kv %* 5m;.+}",
ngx_cfg({
wasm = true,
nginx_wasm_shm_kv_cache = "10m",
nginx_wasm_shm_kv_counters = "10m",
nginx_wasm_shm_kv = "5m",
}, debug)
)
end)
it("injects default configurations if wasm=on", function()
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 @@ -41,8 +41,12 @@ wasm {
> end

> for _, el in ipairs(nginx_wasm_main_directives) do
> if el.name == "shm_kv" then
shm_kv * $(el.value);
> else
$(el.name) $(el.value);
> end
> end

> if #nginx_wasm_wasmtime_directives > 0 then
wasmtime {
Expand Down

1 comment on commit e0b825c

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:e0b825cc49ae7233376ed12929aa52c0c7a72d51
Artifacts available https://github.com/Kong/kong/actions/runs/8394362549

Please sign in to comment.