implement canary annotation and alternative backends

Adds the ability to create alternative backends. Alternative backends enable
traffic shaping by sharing a single location but routing to different
backends depending on the TrafficShapingPolicy defined by AlternativeBackends.

When the list of upstreams and servers are retrieved, we then call
mergeAlternativeBackends which iterates through the paths of every ingress
and checks if the backend supporting the path is a AlternativeBackend. If
so, we then iterate through the map of servers and find the real backend
that the AlternativeBackend should fall under. Once found, the
AlternativeBackend is embedded in the list of VirtualBackends for the real
backend.

If no matching real backend for a AlternativeBackend is found, then the
AlternativeBackend is deleted as it cannot be backed by any server.
This commit is contained in:
Conor Landry 2018-09-18 14:05:32 -04:00
parent 5ceb723963
commit 412cd70d3a
18 changed files with 859 additions and 23 deletions

View file

@ -122,9 +122,57 @@ local function sync_backends()
end
end
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
local alternative_balancer = balancers[balancer.alternative_backends[1]]
local clean_target_header = util.replace_special_char(alternative_balancer.traffic_shaping_policy.header, "-", "_")
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
local clean_target_cookie = util.replace_special_char(alternative_balancer.traffic_shaping_policy.cookie, "-", "_")
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
if math.random(100) <= alternative_balancer.traffic_shaping_policy.weight then
return true
end
return false
end
local function get_balancer()
local backend_name = ngx.var.proxy_upstream_name
return balancers[backend_name]
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
end
function _M.init_worker()