Skip to content

Commit

Permalink
Enable lj-releng tool to lint lua code.
Browse files Browse the repository at this point in the history
  • Loading branch information
agile6v committed Jun 9, 2020
1 parent 36f7dd2 commit bafbd4c
Show file tree
Hide file tree
Showing 36 changed files with 213 additions and 93 deletions.
4 changes: 1 addition & 3 deletions .luacheckrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
std = 'ngx_lua'
globals = {
'_TEST'
}
max_line_length = 100
exclude_files = {'./rootfs/etc/nginx/lua/test/**/*.lua', './rootfs/etc/nginx/lua/plugins/**/test/**/*.lua'}
files["rootfs/etc/nginx/lua/lua_ingress.lua"] = {
ignore = { "122" },
Expand Down
2 changes: 2 additions & 0 deletions hack/verify-lualint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ set -o nounset
set -o pipefail

luacheck --codes -q rootfs/etc/nginx/lua/

find rootfs/etc/nginx/lua/ -name "*.lua" -not -path "*/test/*" -exec lj-releng -L -s {} + && echo "lj-releng validation is success!"
14 changes: 7 additions & 7 deletions rootfs/etc/nginx/lua/balancer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ local getmetatable = getmetatable
local tostring = tostring
local pairs = pairs
local math = math

local ngx = ngx

-- measured in seconds
-- for an Nginx worker to pick up the new list of upstream peers
Expand Down Expand Up @@ -305,11 +305,11 @@ function _M.log()
balancer:after_balance()
end

if _TEST then
_M.get_implementation = get_implementation
_M.sync_backend = sync_backend
_M.route_to_alternative_balancer = route_to_alternative_balancer
_M.get_balancer = get_balancer
end
setmetatable(_M, {__index = {
get_implementation = get_implementation,
sync_backend = sync_backend,
route_to_alternative_balancer = route_to_alternative_balancer,
get_balancer = get_balancer,
}})

return _M
4 changes: 3 additions & 1 deletion rootfs/etc/nginx/lua/balancer/chash.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ local resty_chash = require("resty.chash")
local util = require("util")
local ngx_log = ngx.log
local ngx_ERR = ngx.ERR
local setmetatable = setmetatable

local _M = balancer_resty:new({ factory = resty_chash, name = "chash" })

function _M.new(self, backend)
local nodes = util.get_nodes(backend.endpoints)
local complex_val, err = util.parse_complex_value(backend["upstreamHashByConfig"]["upstream-hash-by"])
local complex_val, err =
util.parse_complex_value(backend["upstreamHashByConfig"]["upstream-hash-by"])
if err ~= nil then
ngx_log(ngx_ERR, "could not parse the value of the upstream-hash-by: ", err)
end
Expand Down
8 changes: 7 additions & 1 deletion rootfs/etc/nginx/lua/balancer/chashsubset.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ local resty_chash = require("resty.chash")
local util = require("util")
local ngx_log = ngx.log
local ngx_ERR = ngx.ERR
local setmetatable = setmetatable
local tostring = tostring
local math = math
local table = table
local pairs = pairs

local _M = { name = "chashsubset" }

Expand Down Expand Up @@ -46,7 +51,8 @@ end

function _M.new(self, backend)
local subset_map, subsets = build_subset_map(backend)
local complex_val, err = util.parse_complex_value(backend["upstreamHashByConfig"]["upstream-hash-by"])
local complex_val, err =
util.parse_complex_value(backend["upstreamHashByConfig"]["upstream-hash-by"])
if err ~= nil then
ngx_log(ngx_ERR, "could not parse the value of the upstream-hash-by: ", err)
end
Expand Down
11 changes: 10 additions & 1 deletion rootfs/etc/nginx/lua/balancer/ewma.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ local resty_lock = require("resty.lock")
local util = require("util")
local split = require("util.split")

local ngx = ngx
local math = math
local pairs = pairs
local ipairs = ipairs
local tostring = tostring
local string = string
local tonumber = tonumber
local setmetatable = setmetatable
local string_format = string.format
local ngx_log = ngx.log
local INFO = ngx.INFO
Expand Down Expand Up @@ -185,7 +193,8 @@ function _M.after_balance(_)
end

function _M.sync(self, backend)
local normalized_endpoints_added, normalized_endpoints_removed = util.diff_endpoints(self.peers, backend.endpoints)
local normalized_endpoints_added, normalized_endpoints_removed =
util.diff_endpoints(self.peers, backend.endpoints)

if #normalized_endpoints_added == 0 and #normalized_endpoints_removed == 0 then
ngx.log(ngx.INFO, "endpoints did not change for backend " .. tostring(backend.name))
Expand Down
1 change: 1 addition & 0 deletions rootfs/etc/nginx/lua/balancer/resty.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local util = require("util")
local string_format = string.format
local ngx_log = ngx.log
local INFO = ngx.INFO
local setmetatable = setmetatable

local _M = {}

Expand Down
2 changes: 2 additions & 0 deletions rootfs/etc/nginx/lua/balancer/round_robin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ local balancer_resty = require("balancer.resty")
local resty_roundrobin = require("resty.roundrobin")
local util = require("util")

local setmetatable = setmetatable

local _M = balancer_resty:new({ factory = resty_roundrobin, name = "round_robin" })

function _M.new(self, backend)
Expand Down
14 changes: 11 additions & 3 deletions rootfs/etc/nginx/lua/balancer/sticky.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ local ngx_balancer = require("ngx.balancer")
local split = require("util.split")
local same_site = require("util.same_site")

local ngx = ngx
local pairs = pairs
local ipairs = ipairs
local string = string
local tonumber = tonumber
local setmetatable = setmetatable

local _M = balancer_resty:new()
local DEFAULT_COOKIE_NAME = "route"

Expand Down Expand Up @@ -64,7 +71,8 @@ function _M.set_cookie(self, value)
}

if self.cookie_session_affinity.expires and self.cookie_session_affinity.expires ~= "" then
cookie_data.expires = ngx.cookie_time(ngx.time() + tonumber(self.cookie_session_affinity.expires))
cookie_data.expires = ngx.cookie_time(ngx.time() +
tonumber(self.cookie_session_affinity.expires))
end

if self.cookie_session_affinity.maxage and self.cookie_session_affinity.maxage ~= "" then
Expand Down Expand Up @@ -132,8 +140,8 @@ function _M.balance(self)
end

local last_failure = self.get_last_failure()
local should_pick_new_upstream = last_failure ~= nil and self.cookie_session_affinity.change_on_failure or
upstream_from_cookie == nil
local should_pick_new_upstream = last_failure ~= nil and
self.cookie_session_affinity.change_on_failure or upstream_from_cookie == nil

if not should_pick_new_upstream then
return upstream_from_cookie
Expand Down
4 changes: 4 additions & 0 deletions rootfs/etc/nginx/lua/balancer/sticky_balanced.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ local math_random = require("math").random
local resty_chash = require("resty.chash")
local util_get_nodes = require("util").get_nodes

local ngx = ngx
local string = string
local setmetatable = setmetatable

local _M = balancer_sticky:new()

-- Consider the situation of N upstreams one of which is failing.
Expand Down
1 change: 1 addition & 0 deletions rootfs/etc/nginx/lua/balancer/sticky_persistent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
local balancer_sticky = require("balancer.sticky")
local util_get_nodes = require("util").get_nodes
local util_nodemap = require("util.nodemap")
local setmetatable = setmetatable

local _M = balancer_sticky:new()

Expand Down
11 changes: 8 additions & 3 deletions rootfs/etc/nginx/lua/certificate.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
local http = require("resty.http")
local ssl = require("ngx.ssl")
local ocsp = require("ngx.ocsp")
local ngx = ngx
local string = string
local tostring = tostring
local re_sub = ngx.re.sub
local unpack = unpack

local dns_lookup = require("util.dns").lookup

Expand Down Expand Up @@ -215,8 +219,8 @@ function _M.call()
ngx.log(ngx.ERR, "error while obtaining hostname: " .. hostname_err)
end
if not hostname then
ngx.log(ngx.INFO,
"obtained hostname is nil (the client does not support SNI?), falling back to default certificate")
ngx.log(ngx.INFO, "obtained hostname is nil (the client does "
.. "not support SNI?), falling back to default certificate")
hostname = DEFAULT_CERT_HOSTNAME
end

Expand All @@ -229,7 +233,8 @@ function _M.call()
pem_cert = certificate_data:get(pem_cert_uid)
end
if not pem_cert then
ngx.log(ngx.ERR, "certificate not found, falling back to fake certificate for hostname: " .. tostring(hostname))
ngx.log(ngx.ERR, "certificate not found, falling back to fake certificate for hostname: "
.. tostring(hostname))
return
end

Expand Down
24 changes: 16 additions & 8 deletions rootfs/etc/nginx/lua/configuration.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
local cjson = require("cjson.safe")

local io = io
local ngx = ngx
local tostring = tostring
local string = string
local table = table
local pairs = pairs

-- this is the Lua representation of Configuration struct in internal/ingress/types.go
local configuration_data = ngx.shared.configuration_data
local certificate_data = ngx.shared.certificate_data
Expand Down Expand Up @@ -72,12 +79,13 @@ local function handle_servers()
else
local success, set_err, forcible = certificate_servers:set(server, uid)
if not success then
local err_msg = string.format("error setting certificate for %s: %s\n", server, tostring(set_err))
local err_msg = string.format("error setting certificate for %s: %s\n",
server, tostring(set_err))
table.insert(err_buf, err_msg)
end
if forcible then
local msg = string.format("certificate_servers dictionary is full, LRU entry has been removed to store %s",
server)
local msg = string.format("certificate_servers dictionary is full, "
.. "LRU entry has been removed to store %s", server)
ngx.log(ngx.WARN, msg)
end
end
Expand All @@ -86,11 +94,13 @@ local function handle_servers()
for uid, cert in pairs(configuration.certificates) do
local success, set_err, forcible = certificate_data:set(uid, cert)
if not success then
local err_msg = string.format("error setting certificate for %s: %s\n", uid, tostring(set_err))
local err_msg = string.format("error setting certificate for %s: %s\n",
uid, tostring(set_err))
table.insert(err_buf, err_msg)
end
if forcible then
local msg = string.format("certificate_data dictionary is full, LRU entry has been removed to store %s", uid)
local msg = string.format("certificate_data dictionary is full, "
.. "LRU entry has been removed to store %s", uid)
ngx.log(ngx.WARN, msg)
end
end
Expand Down Expand Up @@ -211,8 +221,6 @@ function _M.call()
ngx.print("Not found!")
end

if _TEST then
_M.handle_servers = handle_servers
end
setmetatable(_M, {__index = { handle_servers = handle_servers }})

return _M
14 changes: 10 additions & 4 deletions rootfs/etc/nginx/lua/lua_ingress.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
local ngx_re_split = require("ngx.re").split

local certificate_configured_for_current_request = require("certificate").configured_for_current_request
local certificate_configured_for_current_request =
require("certificate").configured_for_current_request

local ngx = ngx
local io = io
local math = math
local string = string
local original_randomseed = math.randomseed
local string_format = string.format
local ngx_redirect = ngx.redirect
Expand Down Expand Up @@ -38,8 +43,8 @@ end
math.randomseed = function(seed)
local pid = ngx.worker.pid()
if seeds[pid] then
ngx.log(ngx.WARN,
string.format("ignoring math.randomseed(%d) since PRNG is already seeded for worker %d", seed, pid))
ngx.log(ngx.WARN, string.format("ignoring math.randomseed(%d) since PRNG "
.. "is already seeded for worker %d", seed, pid))
return
end

Expand Down Expand Up @@ -143,7 +148,8 @@ function _M.rewrite(location_config)
local uri = string_format("https://%s%s", redirect_host(), ngx.var.request_uri)

if location_config.use_port_in_redirects then
uri = string_format("https://%s:%s%s", redirect_host(), config.listen_ports.https, ngx.var.request_uri)
uri = string_format("https://%s:%s%s", redirect_host(),
config.listen_ports.https, ngx.var.request_uri)
end

ngx_redirect(uri, config.http_redirect_code)
Expand Down
16 changes: 11 additions & 5 deletions rootfs/etc/nginx/lua/monitor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ local clear_tab = require "table.clear"
local clone_tab = require "table.clone"
local nkeys = require "table.nkeys"

-- if an Nginx worker processes more than (MAX_BATCH_SIZE/FLUSH_INTERVAL) RPS then it will start dropping metrics
local ngx = ngx
local tonumber = tonumber
local string = string
local tostring = tostring

-- if an Nginx worker processes more than (MAX_BATCH_SIZE/FLUSH_INTERVAL) RPS
-- then it will start dropping metrics
local MAX_BATCH_SIZE = 10000
local FLUSH_INTERVAL = 1 -- second

Expand Down Expand Up @@ -80,9 +86,9 @@ function _M.call()
metrics_batch[metrics_size + 1] = metrics()
end

if _TEST then
_M.flush = flush
_M.get_metrics_batch = function() return metrics_batch end
end
setmetatable(_M, {__index = {
flush = flush,
get_metrics_batch = function() return metrics_batch end,
}})

return _M
11 changes: 9 additions & 2 deletions rootfs/etc/nginx/lua/plugins.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
local require = require
local ngx = ngx
local pairs = pairs
local ipairs = ipairs
local string_format = string.format
local new_tab = require "table.new"
local ngx_log = ngx.log
local INFO = ngx.INFO
local ERR = ngx.ERR
local pcall = pcall

local _M = {}
local MAX_NUMBER_OF_PLUGINS = 10000
Expand Down Expand Up @@ -36,10 +41,12 @@ function _M.run()

-- TODO: consider sandboxing this, should we?
-- probably yes, at least prohibit plugin from accessing env vars etc
-- but since the plugins are going to be installed by ingress-nginx operator they can be assumed to be safe also
-- but since the plugins are going to be installed by ingress-nginx
-- operator they can be assumed to be safe also
local ok, err = pcall(plugin[phase])
if not ok then
ngx_log(ERR, string_format("error while running plugin \"%s\" in phase \"%s\": %s", name, phase, err))
ngx_log(ERR, string_format("error while running plugin \"%s\" in phase \"%s\": %s",
name, phase, err))
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions rootfs/etc/nginx/lua/plugins/hello_world/main.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local ngx = ngx

local _M = {}

function _M.rewrite()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
_G._TEST = true

local main = require("plugins.hello_world.main")

Expand Down
Loading

0 comments on commit bafbd4c

Please sign in to comment.