Merge pull request #3341 from Shopify/canary_upstream

Add canary annotation and alternative backends for traffic shaping
This commit is contained in:
k8s-ci-robot 2018-11-06 12:22:16 -08:00 committed by GitHub
commit 17cad51e47
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 859 additions and 23 deletions

View file

@ -127,9 +127,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()