2018-03-18 13:13:41 +00:00
|
|
|
local ngx_balancer = require("ngx.balancer")
|
|
|
|
local json = require("cjson")
|
2018-07-18 02:27:10 +00:00
|
|
|
local util = require("util")
|
|
|
|
local dns_util = require("util.dns")
|
2018-03-18 13:13:41 +00:00
|
|
|
local configuration = require("configuration")
|
2018-05-18 21:36:43 +00:00
|
|
|
local round_robin = require("balancer.round_robin")
|
|
|
|
local chash = require("balancer.chash")
|
|
|
|
local sticky = require("balancer.sticky")
|
2018-03-23 15:06:21 +00:00
|
|
|
local ewma = require("balancer.ewma")
|
2018-03-18 13:13:41 +00:00
|
|
|
|
|
|
|
-- measured in seconds
|
2018-03-18 16:31:49 +00:00
|
|
|
-- for an Nginx worker to pick up the new list of upstream peers
|
2018-03-18 13:13:41 +00:00
|
|
|
-- it will take <the delay until controller POSTed the backend object to the Nginx endpoint> + BACKENDS_SYNC_INTERVAL
|
|
|
|
local BACKENDS_SYNC_INTERVAL = 1
|
|
|
|
|
2018-03-23 15:06:21 +00:00
|
|
|
local DEFAULT_LB_ALG = "round_robin"
|
2018-05-18 21:36:43 +00:00
|
|
|
local IMPLEMENTATIONS = {
|
|
|
|
round_robin = round_robin,
|
|
|
|
chash = chash,
|
|
|
|
sticky = sticky,
|
|
|
|
ewma = ewma,
|
|
|
|
}
|
2018-03-18 13:13:41 +00:00
|
|
|
|
|
|
|
local _M = {}
|
2018-05-18 21:36:43 +00:00
|
|
|
local balancers = {}
|
2018-03-18 13:13:41 +00:00
|
|
|
|
2018-05-18 21:36:43 +00:00
|
|
|
local function get_implementation(backend)
|
|
|
|
local name = backend["load-balance"] or DEFAULT_LB_ALG
|
2018-03-30 17:19:33 +00:00
|
|
|
|
2018-05-18 21:36:43 +00:00
|
|
|
if backend["sessionAffinityConfig"] and backend["sessionAffinityConfig"]["name"] == "cookie" then
|
|
|
|
name = "sticky"
|
|
|
|
elseif backend["upstream-hash-by"] then
|
|
|
|
name = "chash"
|
2018-04-08 20:37:13 +00:00
|
|
|
end
|
|
|
|
|
2018-05-18 21:36:43 +00:00
|
|
|
local implementation = IMPLEMENTATIONS[name]
|
|
|
|
if not implementation then
|
|
|
|
ngx.log(ngx.WARN, string.format("%s is not supported, falling back to %s", backend["load-balance"], DEFAULT_LB_ALG))
|
|
|
|
implementation = IMPLEMENTATIONS[DEFAULT_LB_ALG]
|
2018-05-09 15:26:04 +00:00
|
|
|
end
|
|
|
|
|
2018-05-18 21:36:43 +00:00
|
|
|
return implementation
|
2018-03-18 13:13:41 +00:00
|
|
|
end
|
|
|
|
|
2018-07-18 02:27:10 +00:00
|
|
|
local function resolve_external_names(original_backend)
|
|
|
|
local backend = util.deepcopy(original_backend)
|
|
|
|
local endpoints = {}
|
|
|
|
for _, endpoint in ipairs(backend.endpoints) do
|
|
|
|
local ips = dns_util.resolve(endpoint.address)
|
|
|
|
for _, ip in ipairs(ips) do
|
|
|
|
table.insert(endpoints, { address = ip, port = endpoint.port })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
backend.endpoints = endpoints
|
|
|
|
return backend
|
|
|
|
end
|
|
|
|
|
2018-08-16 18:12:10 +00:00
|
|
|
local function format_ipv6_endpoints(endpoints)
|
|
|
|
local formatted_endpoints = {}
|
|
|
|
for _, endpoint in ipairs(endpoints) do
|
|
|
|
local formatted_endpoint = endpoint
|
|
|
|
if not endpoint.address:match("^%d+.%d+.%d+.%d+$") then
|
|
|
|
formatted_endpoint.address = string.format("[%s]", endpoint.address)
|
|
|
|
end
|
|
|
|
table.insert(formatted_endpoints, formatted_endpoint)
|
|
|
|
end
|
|
|
|
return formatted_endpoints
|
|
|
|
end
|
|
|
|
|
2018-03-18 13:13:41 +00:00
|
|
|
local function sync_backend(backend)
|
2018-11-05 18:48:33 +00:00
|
|
|
if not backend.endpoints or #backend.endpoints == 0 then
|
|
|
|
ngx.log(ngx.INFO, string.format("there is no endpoint for backend %s. Skipping...", backend.name))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-05-18 21:36:43 +00:00
|
|
|
local implementation = get_implementation(backend)
|
|
|
|
local balancer = balancers[backend.name]
|
2018-03-18 13:13:41 +00:00
|
|
|
|
2018-05-09 15:26:04 +00:00
|
|
|
if not balancer then
|
2018-05-18 21:36:43 +00:00
|
|
|
balancers[backend.name] = implementation:new(backend)
|
2018-05-09 15:26:04 +00:00
|
|
|
return
|
2018-03-23 15:06:21 +00:00
|
|
|
end
|
2018-05-18 21:36:43 +00:00
|
|
|
|
2018-05-28 20:08:53 +00:00
|
|
|
-- every implementation is the metatable of its instances (see .new(...) functions)
|
|
|
|
-- here we check if `balancer` is the instance of `implementation`
|
|
|
|
-- if it is not then we deduce LB algorithm has changed for the backend
|
2018-05-18 21:36:43 +00:00
|
|
|
if getmetatable(balancer) ~= implementation then
|
|
|
|
ngx.log(ngx.INFO,
|
|
|
|
string.format("LB algorithm changed from %s to %s, resetting the instance", balancer.name, implementation.name))
|
|
|
|
balancers[backend.name] = implementation:new(backend)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-07-18 02:27:10 +00:00
|
|
|
local service_type = backend.service and backend.service.spec and backend.service.spec["type"]
|
|
|
|
if service_type == "ExternalName" then
|
|
|
|
backend = resolve_external_names(backend)
|
|
|
|
end
|
|
|
|
|
2018-08-16 18:12:10 +00:00
|
|
|
backend.endpoints = format_ipv6_endpoints(backend.endpoints)
|
|
|
|
|
2018-05-18 21:36:43 +00:00
|
|
|
balancer:sync(backend)
|
2018-03-18 13:13:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function sync_backends()
|
|
|
|
local backends_data = configuration.get_backends_data()
|
|
|
|
if not backends_data then
|
2018-05-26 01:25:41 +00:00
|
|
|
balancers = {}
|
2018-03-18 13:13:41 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local ok, new_backends = pcall(json.decode, backends_data)
|
|
|
|
if not ok then
|
|
|
|
ngx.log(ngx.ERR, "could not parse backends data: " .. tostring(new_backends))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-05-26 01:25:41 +00:00
|
|
|
local balancers_to_keep = {}
|
|
|
|
for _, new_backend in ipairs(new_backends) do
|
2018-05-18 21:36:43 +00:00
|
|
|
sync_backend(new_backend)
|
2018-05-26 01:25:41 +00:00
|
|
|
balancers_to_keep[new_backend.name] = balancers[new_backend.name]
|
2018-03-23 15:06:21 +00:00
|
|
|
end
|
2018-05-26 01:25:41 +00:00
|
|
|
|
|
|
|
for backend_name, _ in pairs(balancers) do
|
|
|
|
if not balancers_to_keep[backend_name] then
|
|
|
|
balancers[backend_name] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-18 18:05:32 +00:00
|
|
|
local function route_to_alternative_balancer(balancer)
|
|
|
|
if not balancer.alternative_backends then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
-- TODO: support traffic shaping for n > 1 alternative backends
|
2018-11-13 12:03:26 +00:00
|
|
|
local backend_name = balancer.alternative_backends[1]
|
|
|
|
if not backend_name then
|
|
|
|
ngx.log(ngx.ERR, "empty alternative backend")
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local alternative_balancer = balancers[backend_name]
|
|
|
|
if not alternative_balancer then
|
|
|
|
ngx.log(ngx.ERR, "no alternative balancer for backend: " .. tostring(backend_name))
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local traffic_shaping_policy = alternative_balancer.traffic_shaping_policy
|
|
|
|
if not traffic_shaping_policy then
|
|
|
|
ngx.log(ngx.ERR, "traffic shaping policy is not set for balanacer of backend: " .. tostring(backend_name))
|
|
|
|
return false
|
|
|
|
end
|
2018-09-18 18:05:32 +00:00
|
|
|
|
2018-11-13 12:03:26 +00:00
|
|
|
local clean_target_header = util.replace_special_char(traffic_shaping_policy.header, "-", "_")
|
2018-09-18 18:05:32 +00:00
|
|
|
|
|
|
|
local header = ngx.var["http_" .. clean_target_header]
|
|
|
|
if header then
|
|
|
|
if header == "always" then
|
|
|
|
return true
|
|
|
|
elseif header == "never" then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-13 12:03:26 +00:00
|
|
|
local clean_target_cookie = util.replace_special_char(traffic_shaping_policy.cookie, "-", "_")
|
2018-09-18 18:05:32 +00:00
|
|
|
|
|
|
|
local cookie = ngx.var["cookie_" .. clean_target_cookie]
|
|
|
|
if cookie then
|
|
|
|
if cookie == "always" then
|
|
|
|
return true
|
|
|
|
elseif cookie == "never" then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-13 12:03:26 +00:00
|
|
|
if math.random(100) <= traffic_shaping_policy.weight then
|
2018-09-18 18:05:32 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2018-05-26 01:25:41 +00:00
|
|
|
local function get_balancer()
|
|
|
|
local backend_name = ngx.var.proxy_upstream_name
|
2018-09-18 18:05:32 +00:00
|
|
|
|
|
|
|
local balancer = balancers[backend_name]
|
|
|
|
if not balancer then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if route_to_alternative_balancer(balancer) then
|
|
|
|
local alternative_balancer = balancers[balancer.alternative_backends[1]]
|
|
|
|
return alternative_balancer
|
|
|
|
end
|
|
|
|
|
|
|
|
return balancer
|
2018-03-23 15:06:21 +00:00
|
|
|
end
|
|
|
|
|
2018-03-18 13:13:41 +00:00
|
|
|
function _M.init_worker()
|
2018-05-14 21:02:09 +00:00
|
|
|
sync_backends() -- when worker starts, sync backends without delay
|
2018-05-09 15:26:04 +00:00
|
|
|
local _, err = ngx.timer.every(BACKENDS_SYNC_INTERVAL, sync_backends)
|
2018-03-18 13:13:41 +00:00
|
|
|
if err then
|
2018-05-04 21:39:57 +00:00
|
|
|
ngx.log(ngx.ERR, string.format("error when setting up timer.every for sync_backends: %s", tostring(err)))
|
2018-03-18 13:13:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-26 01:25:41 +00:00
|
|
|
function _M.rewrite()
|
|
|
|
local balancer = get_balancer()
|
2018-05-18 21:36:43 +00:00
|
|
|
if not balancer then
|
|
|
|
ngx.status = ngx.HTTP_SERVICE_UNAVAILABLE
|
|
|
|
return ngx.exit(ngx.status)
|
2018-03-23 15:06:21 +00:00
|
|
|
end
|
2018-05-26 01:25:41 +00:00
|
|
|
end
|
2018-03-23 15:06:21 +00:00
|
|
|
|
2018-05-26 01:25:41 +00:00
|
|
|
function _M.balance()
|
|
|
|
local balancer = get_balancer()
|
|
|
|
if not balancer then
|
2018-05-18 21:36:43 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-05-30 21:14:28 +00:00
|
|
|
local peer = balancer:balance()
|
|
|
|
if not peer then
|
|
|
|
ngx.log(ngx.WARN, "no peer was returned, balancer: " .. balancer.name)
|
2018-05-26 01:25:41 +00:00
|
|
|
return
|
2018-04-24 16:10:57 +00:00
|
|
|
end
|
2018-03-18 13:13:41 +00:00
|
|
|
|
2018-05-14 21:02:09 +00:00
|
|
|
ngx_balancer.set_more_tries(1)
|
|
|
|
|
2018-05-30 21:14:28 +00:00
|
|
|
local ok, err = ngx_balancer.set_current_peer(peer)
|
2018-05-09 15:26:04 +00:00
|
|
|
if not ok then
|
2018-08-16 18:12:10 +00:00
|
|
|
ngx.log(ngx.ERR, string.format("error while setting current upstream peer %s: %s", peer, err))
|
2018-03-18 13:13:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-26 01:25:41 +00:00
|
|
|
function _M.log()
|
|
|
|
local balancer = get_balancer()
|
|
|
|
if not balancer then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-05-28 20:08:53 +00:00
|
|
|
if not balancer.after_balance then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-05-26 01:25:41 +00:00
|
|
|
balancer:after_balance()
|
|
|
|
end
|
|
|
|
|
2018-05-18 21:36:43 +00:00
|
|
|
if _TEST then
|
|
|
|
_M.get_implementation = get_implementation
|
|
|
|
_M.sync_backend = sync_backend
|
|
|
|
end
|
|
|
|
|
2018-03-18 13:13:41 +00:00
|
|
|
return _M
|