Skip to content

Commit

Permalink
PR review refinements
Browse files Browse the repository at this point in the history
  • Loading branch information
flrgh committed Jul 27, 2023
1 parent 4c3a0c3 commit 9a6b04f
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 109 deletions.
35 changes: 8 additions & 27 deletions kong/db/schema/entities/filter_chains.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local typedefs = require "kong.db.schema.typedefs"
local wasm = require "kong.runloop.wasm"

local kong = kong

---@class kong.db.schema.entities.filter_chain : table
---
Expand All @@ -27,7 +26,8 @@ local kong = kong
local filter = {
type = "record",
fields = {
{ name = { type = "string", required = true, one_of = wasm.filter_names, }, },
{ name = { type = "string", required = true, one_of = wasm.filter_names,
err = "no such filter", }, },
{ config = { type = "string", required = false, }, },
{ enabled = { type = "boolean", default = true, required = true, }, },
},
Expand Down Expand Up @@ -73,35 +73,16 @@ return {
-- validate filter chain input.
--
-- The `one_of` check on `filters[].name` already covers validation, but in
-- the case where wasm is disabled or no filters are installed, it will
-- return a cryptic error:
-- the case where wasm is disabled or no filters are installed, this check
-- adds an additional entity-level error (e.g. "wasm support is not enabled"
-- or "no wasm filters are available").
--
-- ```
-- name = "expected one of: "
-- ```
--
-- This check simply adds an entity-level error message in those cases to
-- clarify.
-- All of the wasm API routes are disabled when wasm is also disabled, so
-- this primarily serves the dbless `/config` endpoint.
{ custom_entity_check = {
field_sources = { "filters" },
run_with_invalid_fields = true,
fn = function(_)
-- kong.runloop.wasm.enabled() doesn't differentiate between
-- wasm == "off" and #wasm_modules_parsed == 0, so we inspect
-- configuration here instead
if not wasm.enabled() then
if not kong.configuration.wasm then
return nil, "wasm is disabled"

elseif not kong.configuration.wasm_modules_parsed
or #kong.configuration.wasm_modules_parsed == 0
then
return nil, "no wasm filters are available on this node"
end
end

return true
end,
fn = wasm.status,
},
},
},
Expand Down
99 changes: 71 additions & 28 deletions kong/runloop/wasm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ local TYPE_COMBINED = 2


local ENABLED = false
local STATUS = "wasm support is not enabled"


local hash_chain
do
Expand Down Expand Up @@ -536,47 +538,78 @@ local function get_filter_chain_for_request(route, service)
end


---@param filters kong.configuration.wasm_filter[]|nil
local function set_available_filters(filters)
clear_tab(_M.filters)
clear_tab(_M.filters_by_name)
clear_tab(_M.filter_names)

if filters then
for i, filter in ipairs(filters) do
_M.filters[i] = filter
_M.filters_by_name[filter.name] = filter
_M.filter_names[i] = filter.name
end
end
end


---@param reason string
local function disable(reason)
set_available_filters(nil)

_G.dns_client = nil

ENABLED = false
STATUS = reason or "wasm support is not enabled"
end


local function enable(kong_config)
set_available_filters(kong_config.wasm_modules_parsed)

-- setup a DNS client for ngx_wasm_module
_G.dns_client = _G.dns_client or dns(kong_config)

proxy_wasm = proxy_wasm or require "resty.wasmx.proxy_wasm"
ATTACH_OPTS.isolation = proxy_wasm.isolations.FILTER

ENABLED = true
STATUS = "wasm support is enabled"
end


_M.get_version = get_version

_M.update_in_place = update_in_place

_M.set_state = set_state

function _M.enable(filters)
enable({
wasm = true,
wasm_modules_parsed = filters,
})
end

_M.disable = disable


---@param kong_config table
function _M.init(kong_config)
-- we don't expect wasm.init() to be called more than once under normal
-- circumstances, but explicitly clearing these tables allows it to be
-- called multiple times in our tests
clear_tab(_M.filters)
clear_tab(_M.filters_by_name)
clear_tab(_M.filter_names)
if kong_config.wasm then
local filters = kong_config.wasm_modules_parsed

if not kong_config.wasm then
ENABLED = false
return
end
if filters and #filters > 0 then
enable(kong_config)

---@type kong.configuration.wasm_filter[]
local modules = kong_config.wasm_modules_parsed
if not modules or #modules == 0 then
ENABLED = false
return
end
else
disable("no wasm filters are available")
end

for i, filter in ipairs(modules) do
_M.filters[i] = filter
_M.filters_by_name[filter.name] = filter
_M.filter_names[i] = filter.name
else
disable("wasm support is not enabled")
end

-- setup a DNS client for ngx_wasm_module
_G.dns_client = dns(kong_config)

proxy_wasm = require "resty.wasmx.proxy_wasm"
ENABLED = true

ATTACH_OPTS.isolation = proxy_wasm.isolations.FILTER
end


Expand Down Expand Up @@ -688,4 +721,14 @@ function _M.enabled()
end


---@return boolean? ok
---@return string? error
function _M.status()
if not ENABLED then
return nil, STATUS
end

return true
end

return _M
9 changes: 3 additions & 6 deletions spec/02-integration/20-wasm/01-admin-api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ describe("wasm admin API [#" .. strategy .. "]", function()
local service, route

lazy_setup(function()
require("kong.runloop.wasm").init({
wasm = true,
wasm_modules_parsed = {
{ name = "tests" },
{ name = "response_transformer" },
}
require("kong.runloop.wasm").enable({
{ name = "tests" },
{ name = "response_transformer" },
})

bp, db = helpers.get_db_utils(strategy, {
Expand Down
17 changes: 7 additions & 10 deletions spec/02-integration/20-wasm/02-db_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ describe("wasm DB entities [#" .. strategy .. "]", function()


lazy_setup(function()
require("kong.runloop.wasm").init({
wasm = true,
wasm_modules_parsed = {
{ name = "test" },
{ name = "other" },
}
require("kong.runloop.wasm").enable({
{ name = "test" },
{ name = "other" },
})

local _
Expand Down Expand Up @@ -317,8 +314,8 @@ describe("wasm DB entities [#" .. strategy .. "]", function()
assert.is_table(err_t.fields)
assert.same({
filters = {
[2] = { name = "expected one of: test, other" },
[4] = { name = "expected one of: test, other" },
[2] = { name = "no such filter" },
[4] = { name = "no such filter" },
},
}, err_t.fields)

Expand All @@ -343,8 +340,8 @@ describe("wasm DB entities [#" .. strategy .. "]", function()
assert.is_table(err_t.fields)
assert.same({
filters = {
[2] = { name = "expected one of: test, other" },
[4] = { name = "expected one of: test, other" },
[2] = { name = "no such filter" },
[4] = { name = "no such filter" },
},
}, err_t.fields)

Expand Down
9 changes: 3 additions & 6 deletions spec/02-integration/20-wasm/03-runtime_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ for _, strategy in helpers.each_strategy({ "postgres", "off" }) do

describe("#wasm filter execution (#" .. strategy .. ")", function()
lazy_setup(function()
require("kong.runloop.wasm").init({
wasm = true,
wasm_modules_parsed = {
{ name = "tests" },
{ name = "response_transformer" },
},
require("kong.runloop.wasm").enable({
{ name = "tests" },
{ name = "response_transformer" },
})

local bp = helpers.get_db_utils("postgres", {
Expand Down
7 changes: 2 additions & 5 deletions spec/02-integration/20-wasm/04-proxy-wasm_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ describe("proxy-wasm filters (#wasm)", function()
local hosts_file

lazy_setup(function()
require("kong.runloop.wasm").init({
wasm = true,
wasm_modules_parsed = {
{ name = "tests" },
},
require("kong.runloop.wasm").enable({
{ name = "tests" },
})

local bp, db = helpers.get_db_utils(DATABASE, {
Expand Down
9 changes: 3 additions & 6 deletions spec/02-integration/20-wasm/05-cache-invalidation_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,9 @@ describe("#wasm filter chain cache " .. mode_suffix, function()


lazy_setup(function()
require("kong.runloop.wasm").init({
wasm = true,
wasm_modules_parsed = {
{ name = "tests" },
{ name = "response_transformer" },
},
require("kong.runloop.wasm").enable({
{ name = "tests" },
{ name = "response_transformer" },
})

local bp
Expand Down
24 changes: 3 additions & 21 deletions spec/02-integration/20-wasm/07-declarative_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ end

describe("#wasm declarative config", function()
local client
local filter_names

lazy_setup(function()
assert(helpers.start_kong({
Expand All @@ -59,22 +58,6 @@ describe("#wasm declarative config", function()
}))

client = helpers.admin_client()

do
local res = client:get("/")
assert.response(res).has.status(200)
local json = assert.response(res).has.jsonbody()
assert.is_table(json.configuration)
assert.is_table(json.configuration.wasm_modules_parsed)
local filters = {}

for _, item in ipairs(json.configuration.wasm_modules_parsed) do
assert.is_table(item)
assert.is_string(item.name)
table.insert(filters, item.name)
end
filter_names = table.concat(filters, ", ")
end
end)

lazy_teardown(function()
Expand Down Expand Up @@ -114,8 +97,7 @@ describe("#wasm declarative config", function()

assert.same("filters.1.name", err.field)
assert.same("field", err.type)
assert.matches("expected one of: ", err.message)
assert.matches(filter_names, err.message)
assert.same("no such filter", err.message)
end)
end)

Expand Down Expand Up @@ -159,7 +141,7 @@ describe("#wasm declarative config (no installed filters)", function()
},
})

expect_entity_error(res, "no wasm filters are available on this node")
expect_entity_error(res, "no wasm filters are available")
end)
end)

Expand Down Expand Up @@ -197,6 +179,6 @@ describe("#wasm declarative config (wasm = off)", function()
},
})

expect_entity_error(res, "wasm is disabled")
expect_entity_error(res, "wasm support is not enabled")
end)
end)

0 comments on commit 9a6b04f

Please sign in to comment.