Skip to content
This repository was archived by the owner on Dec 11, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
14256e9
temp: ngx.decode_base64url
omegabytes Dec 8, 2022
2f294c1
replace lua resty.openssl.digest with go crypto/sha256
omegabytes Dec 8, 2022
4ac298b
rename pkg crypto to sha256
omegabytes Dec 8, 2022
b6e369b
replace lua resty.string.to_hex with go encoding/hex
omegabytes Dec 8, 2022
788f46c
replace table.nkeys with local fn
omegabytes Dec 9, 2022
86df807
fmt and lint
omegabytes Dec 9, 2022
ff8aeb1
remove log_level from constants
omegabytes Dec 10, 2022
ab748dd
replace table.new with local fn
omegabytes Dec 10, 2022
393daa9
remove/replace resty.* imports
omegabytes Dec 10, 2022
739a308
rework goto statements
omegabytes Dec 10, 2022
001809f
remove ffi imports from node.lua
omegabytes Dec 10, 2022
884c12f
pdk/private/node.lua remove pl imports
omegabytes Dec 10, 2022
23c7b76
remove ngx imports from node and reports
omegabytes Dec 10, 2022
0345f15
override nkeys for router/utils
omegabytes Dec 10, 2022
ea9bcf7
remove ngx.worker.id ref
omegabytes Dec 10, 2022
b47d2e6
just get rid of atc already
omegabytes Dec 10, 2022
33e4b3c
refactor: name go.hex and go.sha256 pkg
omegabytes Dec 12, 2022
9798f25
empty atc and node files
omegabytes Dec 12, 2022
8bd2cdc
remove commented code
omegabytes Dec 12, 2022
a7b66b9
remove debug traceback
omegabytes Dec 12, 2022
74a909b
move new_tab and nkeys to kong.tools.utils
omegabytes Dec 13, 2022
fc044a8
remove node, add newlines
omegabytes Dec 13, 2022
cddf098
revert reports changes
omegabytes Dec 13, 2022
821f5f7
revert router.utils changes
omegabytes Dec 13, 2022
4c25d00
update patchfile
omegabytes Dec 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import (
"github.com/kong/goks"
"github.com/kong/goks/internal/fs"
"github.com/kong/goks/lualibs/go/cjson/safe"
"github.com/kong/goks/lualibs/go/hex"
"github.com/kong/goks/lualibs/go/ipmatcher"
"github.com/kong/goks/lualibs/go/jsonschema"
"github.com/kong/goks/lualibs/go/lyaml"
"github.com/kong/goks/lualibs/go/ngx"
"github.com/kong/goks/lualibs/go/rand"
"github.com/kong/goks/lualibs/go/sha256"
"github.com/kong/goks/lualibs/go/uuid"
"github.com/kong/goks/lualibs/go/x509"
json "github.com/layeh/gopher-json"
Expand Down Expand Up @@ -63,6 +65,8 @@ func New(opts Opts) (*VM, error) {
l.PreloadModule("go.jsonschema", jsonschema.Loader)
l.PreloadModule("cjson.safe", safe.Loader)
l.PreloadModule("lyaml", lyaml.Loader)
l.PreloadModule("sha256", sha256.Loader)
l.PreloadModule("hex", hex.Loader)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We're using the pattern "go.xxx" for those replacement modules:

Suggested change
l.PreloadModule("sha256", sha256.Loader)
l.PreloadModule("hex", hex.Loader)
l.PreloadModule("go.sha256", sha256.Loader)
l.PreloadModule("go.hex", hex.Loader)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ngx.LoadNgx(l)

if err := setup(l); err != nil {
Expand Down
19 changes: 0 additions & 19 deletions lua-tree/share/lua/5.1/kong/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -218,25 +218,6 @@ local constants = {

KEY_FORMATS_MAP = key_formats_map,
KEY_FORMATS = key_formats,

LOG_LEVELS = {
Comment thread
omegabytes marked this conversation as resolved.
debug = ngx.DEBUG,
info = ngx.INFO,
notice = ngx.NOTICE,
warn = ngx.WARN,
error = ngx.ERR,
crit = ngx.CRIT,
alert = ngx.ALERT,
emerg = ngx.EMERG,
[ngx.DEBUG] = "debug",
[ngx.INFO] = "info",
[ngx.NOTICE] = "notice",
[ngx.WARN] = "warn",
[ngx.ERR] = "error",
[ngx.CRIT] = "crit",
[ngx.ALERT] = "alert",
[ngx.EMERG] = "emerg",
},
}

for _, v in ipairs(constants.CLUSTERING_SYNC_STATUS) do
Expand Down
18 changes: 17 additions & 1 deletion lua-tree/share/lua/5.1/kong/db/schema/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local tablex = require "pl.tablex"
local pretty = require "pl.pretty"
local utils = require "kong.tools.utils"
local nkeys = require "table.nkeys"

local cjson = { array_mt = {} } --- TODO(hbagdi) XXX analyze the impact
local is_reference = function(value) return false end
Expand Down Expand Up @@ -55,6 +54,23 @@ do
end
end

-- `table.nkeys()` is a LuaJIT function and is not available to the Lua VM that goks uses.
local nkeys
do
local ok
ok, nkeys = pcall(require, "table.nkeys")
if not ok then
nkeys = function (tab)
local count = 0
for _, v in pairs(tab) do
if v ~= nil then
count = count + 1
end
end
return count
end
end
end
Copy link
Copy Markdown
Contributor Author

@omegabytes omegabytes Dec 9, 2022

Choose a reason for hiding this comment

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

This is copypasta from resty/healthecheck.lua. I'm following the pattern of local_tab initialized on L46 of this file. This addresses the error:

lua-tree/share/lua/5.1/kong/db/schema/init.lua:4: module table.nkeys not found:
    no field package.preload['table.nkeys']
    open /table/nkeys.lua: file does not exist
    open lua-tree/share/lua/5.1/table/nkeys.lua: file does not exist
    open lua-tree/share/lua/5.1/table/nkeys/init.lua: file does not exist,

I can't tell if this resolves the issue, however. I get a new error:

Received unexpected error:
lua-tree/share/lua/5.1/kong/constants.lua:222: table index is nil
stack traceback:
    lua-tree/share/lua/5.1/kong/constants.lua:222: in function <lua-tree/share/lua/5.1/kong/constants.lua:0>
    [G]: in function 'require'
    lua-tree/share/lua/5.1/kong/db/schema/typedefs.lua:6: in function <lua-tree/share/lua/5.1/kong/db/schema/typedefs.lua:0>
    [G]: in function 'require'
    setup.lua:6: in main chunk
    [G]: ?

I'm able to validate the method is called. The if block beginning on line 62 is entered but nkeys = function (tab) on line 63 panics. I'm not sure if it's the method call itself or something else. A print statement on line 64 is never executed. Curiously, print statements before the method is called are also not executed?

The stacktrace is also not very illuminating: lua-tree/share/lua/5.1/kong/constants.lua:222: in function <lua-tree/share/lua/5.1/kong/constants.lua:0> is on a different line in kong-ee and points to LOG_LEVELS. This looks new to 3.1, I don't see it in this 3.0 branch.

My guess is either:

  1. This is a new error. The introduction of LOG_LEVELS is causing something to break. What and why is unclear.
  2. The update I made to nkeys somehow breaks this import, or something that processes this import.

Number 1 is the most reasonable, but I don't really understand what table index is nil means in this context and I'm struggling to trace where exactly this is breaking.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't really understand what table index is nil means in this context and I'm struggling to trace where exactly this is breaking.

the LOG_LEVELS table, starting in constants.lua:222 has this form:

  LOG_LEVELS = {
    debug = ngx.DEBUG,
    info = ngx.INFO,
    notice = ngx.NOTICE,
    warn = ngx.WARN,
    error = ngx.ERR,
    crit = ngx.CRIT,
    alert = ngx.ALERT,
    emerg = ngx.EMERG,
    [ngx.DEBUG] = "debug",
    [ngx.INFO] = "info",
    [ngx.NOTICE] = "notice",
    [ngx.WARN] = "warn",
    [ngx.ERR] = "error",
    [ngx.CRIT] = "crit",
    [ngx.ALERT] = "alert",
    [ngx.EMERG] = "emerg",
  },

lines 231-238 use the form [key-expression] = value-expression, so it evaluates the ngx.DEBUG, ngx.INFO, etc to get the actual keys (unlike key-name=value-expression, which directly sets the key as a string constant). if any of these is nil, this error will trigger. unfortunately, this aborts the whole (sub) table construction, so we don't know which of these is failing.

Now to find where are those ngx.XXXX values....

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Now to find where are those ngx.XXXX values....

Can't find them. I think the easiest would be to add to internal/vm/setup.go

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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


local validation_errors = {
-- general message
Expand Down
97 changes: 38 additions & 59 deletions lua-tree/share/lua/5.1/kong/pdk/node.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
-- @module kong.node

local utils = require "kong.tools.utils"
local ffi = require "ffi"
local private_node = require "kong.pdk.private.node"


Expand All @@ -15,9 +14,6 @@ local sort = table.sort
local insert = table.insert
local ngx = ngx
local shared = ngx.shared
local C = ffi.C
local ffi_new = ffi.new
local ffi_str = ffi.string

local NODE_ID_KEY = "kong:node_id"

Expand Down Expand Up @@ -169,57 +165,50 @@ local function new(self)
-- get workers Lua VM allocated memory

do
if not shared.kong then
goto lua_shared_dicts
end

local keys, err = shared.kong:get_keys()
if not keys then
res.workers_lua_vms.err = "could not get kong shm keys: " .. err
goto lua_shared_dicts
end

if #keys == 1024 then
-- Preventive warning log for future Kong developers, in case 'kong'
-- shm becomes mis-used or over-used.
ngx.log(ngx.WARN, "ngx.shared.kong:get_keys() returned 1024 keys, ",
"but it may have more")
end

for i = 1, #keys do
local pid = match(keys[i], "kong:mem:(%d+)")
if not pid then
goto continue
end

local w = self.table.new(0, 2)
w.pid = tonumber(pid)

local count, err = shared.kong:get("kong:mem:" .. pid)
if err then
w.err = "could not get worker's HTTP Lua VM memory (pid: " ..
pid .. "): " .. err

elseif type(count) ~= "number" then
w.err = "could not get worker's HTTP Lua VM memory (pid: " ..
pid .. "): reported value is corrupted"

if shared.kong then

local keys, err = shared.kong:get_keys()
if keys then

if #keys == 1024 then
-- Preventive warning log for future Kong developers, in case 'kong'
-- shm becomes mis-used or over-used.
ngx.log(ngx.WARN, "ngx.shared.kong:get_keys() returned 1024 keys, ",
"but it may have more")
end

for i = 1, #keys do
local pid = match(keys[i], "kong:mem:(%d+)")
if pid then
local w = self.table.new(0, 2)
w.pid = tonumber(pid)

local count, err = shared.kong:get("kong:mem:" .. pid)
if err then
w.err = "could not get worker's HTTP Lua VM memory (pid: " ..
pid .. "): " .. err

elseif type(count) ~= "number" then
w.err = "could not get worker's HTTP Lua VM memory (pid: " ..
pid .. "): reported value is corrupted"

else
count = count * 1024 -- reported value is in kb
w.http_allocated_gc = convert_bytes(count, unit, scale)
end

insert(res.workers_lua_vms, w)
end
end

sort(res.workers_lua_vms, sort_pid_asc)
else
count = count * 1024 -- reported value is in kb
w.http_allocated_gc = convert_bytes(count, unit, scale)
res.workers_lua_vms.err = "could not get kong shm keys: " .. err
end

insert(res.workers_lua_vms, w)

::continue::
end

sort(res.workers_lua_vms, sort_pid_asc)
end

-- get lua_shared_dicts allocated slabs
::lua_shared_dicts::

for _, shm in ipairs(shms) do
local allocated = shm.capacity - shm.zone:free_space()

Expand All @@ -241,16 +230,6 @@ local function new(self)
-- @usage
-- local hostname = kong.node.get_hostname()
function _NODE.get_hostname()
local SIZE = 253 -- max number of chars for a hostname

local buf = ffi_new("unsigned char[?]", SIZE)
local res = C.gethostname(buf, SIZE)

if res == 0 then
local hostname = ffi_str(buf, SIZE)
return gsub(hostname, "%z+$", "")
end

local f = io.popen("/bin/hostname")
local hostname = f:read("*a") or ""
f:close()
Expand Down
Loading