Merge pull request #4478 from zikhan/AddWildcardAffinity

Re-add Support for Wildcard Hosts with Sticky Sessions
This commit is contained in:
Kubernetes Prow Robot 2019-08-27 10:52:07 -07:00 committed by GitHub
commit 8740c1b661
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 1 deletions

View file

@ -103,8 +103,18 @@ local function pick_new_upstream(self)
end
local function should_set_cookie(self)
if self.cookie_session_affinity.locations then
if self.cookie_session_affinity.locations and ngx.var.host then
local locs = self.cookie_session_affinity.locations[ngx.var.host]
if locs == nil then
-- Based off of wildcard hostname in ../certificate.lua
local wildcard_host, _, err = ngx.re.sub(ngx.var.host, "^[^\\.]+\\.", "*.", "jo")
if err then
ngx.log(ngx.ERR, "error: ", err);
elseif wildcard_host then
locs = self.cookie_session_affinity.locations[wildcard_host]
end
end
if locs ~= nil then
for _, path in pairs(locs) do
if ngx.var.location_path == path then

View file

@ -141,6 +141,41 @@ describe("Sticky", function()
end)
end)
context("when client doesn't have a cookie set and cookie_locations contains a matching wildcard location", function()
before_each(function ()
ngx.var.host = "dev.test.com"
end)
after_each(function ()
ngx.var.host = "test.com"
end)
it("sets a cookie on the client", function()
local s = {}
cookie.new = function(self)
local cookie_instance = {
set = function(self, payload)
assert.equal(payload.key, test_backend.sessionAffinityConfig.cookieSessionAffinity.name)
assert.equal(payload.path, ngx.var.location_path)
assert.equal(payload.domain, nil)
assert.equal(payload.httponly, true)
assert.equal(payload.secure, false)
return true, nil
end,
get = function(k) return false end,
}
s = spy.on(cookie_instance, "set")
return cookie_instance, false
end
local b = get_test_backend()
b.sessionAffinityConfig.cookieSessionAffinity.locations = {}
b.sessionAffinityConfig.cookieSessionAffinity.locations["*.test.com"] = {"/"}
local sticky_balancer_instance = sticky:new(b)
assert.has_no.errors(function() sticky_balancer_instance:balance() end)
assert.spy(s).was_called()
end)
end)
context("when client doesn't have a cookie set and location not in cookie_locations", function()
it("picks an endpoint for the client", function()
local sticky_balancer_instance = sticky:new(test_backend)