Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: required changes for 3.1 compatibility #36

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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("go.sha256", sha256.Loader)
l.PreloadModule("go.hex", hex.Loader)
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 = {
omegabytes marked this conversation as resolved.
Show resolved Hide 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
9 changes: 0 additions & 9 deletions lua-tree/share/lua/5.1/kong/db/schema/entities/routes.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
local typedefs = require("kong.db.schema.typedefs")
local deprecation = require("kong.deprecation")

local CACHED_SCHEMA = require("kong.router.atc").schema

-- ATC router is incompatible with goks
local kong_router_flavor = "traditional"

Expand Down Expand Up @@ -48,13 +46,6 @@ if kong_router_flavor == "expressions" then
{ custom_entity_check = {
field_sources = { "expression", "id", },
fn = function(entity)
local r = router.new(CACHED_SCHEMA)

local res, err = r:add_matcher(0, entity.id, entity.expression)
if not res then
return nil, "Router Expression failed validation: " .. err
end

return true
end,
} },
Expand Down
16 changes: 2 additions & 14 deletions lua-tree/share/lua/5.1/kong/db/schema/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
local tablex = require "pl.tablex"
local pretty = require "pl.pretty"
local utils = require "kong.tools.utils"
local nkeys = require "table.nkeys"
local nkeys = utils.nkeys
local new_tab = utils.new_tab

local cjson = { array_mt = {} } --- TODO(hbagdi) XXX analyze the impact
local is_reference = function(value) return false end
Expand Down Expand Up @@ -43,19 +44,6 @@ Schema.__index = Schema
local _cache = {}
local _workspaceable = {}

-- `table.new()` is a LuaJIT function and is not available to the Lua VM that goks uses.
local new_tab
do
local ok
ok, new_tab = pcall(require, "table.new")
if not ok then
new_tab = function(narr, nrec)
return {}
end
end
end


local validation_errors = {
-- general message
ERROR = "Validation error: %s",
Expand Down
284 changes: 1 addition & 283 deletions lua-tree/share/lua/5.1/kong/pdk/node.lua
Original file line number Diff line number Diff line change
@@ -1,283 +1 @@
--- Node-level utilities.
--
-- @module kong.node

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


local floor = math.floor
local lower = string.lower
local match = string.match
local gsub = string.gsub
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"


local node_id
local shms = {}
local n_workers = ngx.worker.count()


for shm_name, shm in pairs(shared) do
insert(shms, {
zone = shm,
name = shm_name,
capacity = shm:capacity(),
})
end


local function convert_bytes(bytes, unit, scale)
if not unit or lower(unit) == "b" then
return floor(bytes)
end

return utils.bytes_to_str(bytes, unit, scale)
end


local function sort_pid_asc(a, b)
return a.pid < b.pid
end


local function new(self)
local _NODE = {}


---
-- Returns the ID used by this node to describe itself.
--
-- @function kong.node.get_id
-- @treturn string The v4 UUID used by this node as its ID.
-- @usage
-- local id = kong.node.get_id()
function _NODE.get_id()
if node_id then
return node_id
end

local shm = ngx.shared.kong

local ok, err = shm:safe_add(NODE_ID_KEY, utils.uuid())
if not ok and err ~= "exists" then
error("failed to set 'node_id' in shm: " .. err)
end

node_id, err = shm:get(NODE_ID_KEY)
if err then
error("failed to get 'node_id' in shm: " .. err)
end

if not node_id then
error("no 'node_id' set in shm")
end

return node_id
end


---
-- Returns memory usage statistics about this node.
--
-- @function kong.node.get_memory_stats
-- @tparam[opt] string unit The unit that memory is reported in. Can be
-- any of `b/B`, `k/K`, `m/M`, or `g/G` for bytes, kibibytes, mebibytes,
-- or gibibytes, respectively. Defaults to `b` (bytes).
-- @tparam[opt] number scale The number of digits to the right of the decimal
-- point. Defaults to 2.
-- @treturn table A table containing memory usage statistics for this node.
-- If `unit` is `b/B` (the default), reported values are Lua numbers.
-- Otherwise, reported values are strings with the unit as a suffix.
-- @usage
-- local res = kong.node.get_memory_stats()
-- -- res will have the following structure:
-- {
-- lua_shared_dicts = {
-- kong = {
-- allocated_slabs = 12288,
-- capacity = 24576
-- },
-- kong_db_cache = {
-- allocated_slabs = 12288,
-- capacity = 12288
-- }
-- },
-- workers_lua_vms = {
-- {
-- http_allocated_gc = 1102,
-- pid = 18004
-- },
-- {
-- http_allocated_gc = 1102,
-- pid = 18005
-- }
-- }
-- }
--
-- local res = kong.node.get_memory_stats("k", 1)
-- -- res will have the following structure:
-- {
-- lua_shared_dicts = {
-- kong = {
-- allocated_slabs = "12.0 KiB",
-- capacity = "24.0 KiB",
-- },
-- kong_db_cache = {
-- allocated_slabs = "12.0 KiB",
-- capacity = "12.0 KiB",
-- }
-- },
-- workers_lua_vms = {
-- {
-- http_allocated_gc = "1.1 KiB",
-- pid = 18004
-- },
-- {
-- http_allocated_gc = "1.1 KiB",
-- pid = 18005
-- }
-- }
-- }
function _NODE.get_memory_stats(unit, scale)
-- validate arguments

do
unit = unit or "b"
scale = scale or 2

local pok, perr = pcall(utils.bytes_to_str, 0, unit, scale)
if not pok then
error(perr, 2)
end
end

local res = {
workers_lua_vms = self.table.new(n_workers, 0),
lua_shared_dicts = self.table.new(0, #shms),
}

-- 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"

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)

::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()

res.lua_shared_dicts[shm.name] = {
capacity = convert_bytes(shm.capacity, unit, scale),
allocated_slabs = convert_bytes(allocated, unit, scale),
}
end

return res
end


---
-- Returns the name used by the local machine.
--
-- @function kong.node.get_hostname
-- @treturn string The local machine hostname.
-- @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()
return gsub(hostname, "\n$", "")
end


local prefix = self and self.configuration and self.configuration.prefix
if prefix and self.configuration.role == "data_plane" then
local id, err = private_node.load_node_id(prefix)
if id then
node_id = id
ngx.log(ngx.DEBUG, "restored node_id from the filesystem: ", node_id)

else
id = _NODE.get_id()
if err then
ngx.log(ngx.WARN, "failed to restore node_id from the filesystem: ",
err, ", so a new one was generated: ", id)
end
end
end

return _NODE
end


return {
new = new,
}
-- not used in koko at this time
Loading