Skip to content

Commit

Permalink
Merge branch 'master' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamhm committed May 30, 2018
2 parents 850cc1d + c8e4942 commit d437828
Show file tree
Hide file tree
Showing 14 changed files with 250 additions and 86 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ or the maintainers of this project:
asynchronous and lengthy chatter and staying up-to-date with the latest
announcements or usage tips
- Gitter, for faster, but more ephemeral conversations. The room is
hosted at https://gitter.im/Mashape/kong
hosted at https://gitter.im/Kong/kong
- The IRC channel, registered on freenode as [#kong
](https://webchat.freenode.net/?channels=kong)

Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ Reference](https://getkong.org/docs/latest/lua-reference/).
#### Vagrant

You can use a Vagrant box running Kong and Postgres that you can find at
[Mashape/kong-vagrant](https://github.com/Kong/kong-vagrant).
[Kong/kong-vagrant](https://github.com/Kong/kong-vagrant).

#### Source Install

Expand Down Expand Up @@ -246,9 +246,8 @@ limitations under the License.
```

[kong-url]: https://konghq.com/
[kong-logo]: https://d2ffutrenqvap3.cloudfront.net/items/1946191x3s1H0M2u3J18/slack-imgs.png
[kong-benefits]: https://cl.ly/002i2Z432A1s/Image%202017-10-16%20at%2012.30.08%20AM.png
[google-groups-url]: https://groups.google.com/forum/#!forum/konglayer
[kong-logo]: https://konghq.com/wp-content/uploads/2018/05/kong-logo-github-readme.png
[kong-benefits]: https://konghq.com/wp-content/uploads/2018/05/kong-benefits-github-readme.png
[badge-travis-url]: https://travis-ci.org/Kong/kong/branches
[badge-travis-image]: https://travis-ci.org/Kong/kong.svg?branch=master

Expand Down
4 changes: 2 additions & 2 deletions kong-0.13.1-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ dependencies = {
"luaossl == 20180530",
"luasyslog == 1.0.0",
"lua_pack == 1.0.5",
"lua-resty-dns-client == 2.0.0",
"lua-resty-dns-client == 2.1.0",
"lua-resty-worker-events == 0.3.3",
"lua-resty-mediador == 0.1.2",
"lua-resty-healthcheck == 0.4.0",
"lua-resty-healthcheck == 0.4.2",
"lua-resty-cookie == 0.1.0",
"lua-resty-mlcache == 2.0.2",
-- external Kong plugins
Expand Down
8 changes: 4 additions & 4 deletions kong.conf.default
Original file line number Diff line number Diff line change
Expand Up @@ -495,10 +495,10 @@
# This includes the certificates configured
# for Kong's database connections.

#lua_package_path = # Sets the Lua module search path (LUA_PATH).
# Useful when developing or using custom
# plugins not stored in the default search
# path.
#lua_package_path = ./?.lua;./?/init.lua; # Sets the Lua module search path
# (LUA_PATH). Useful when developing
# or using custom plugins not stored
# in the default search path.

#lua_package_cpath = # Sets the Lua C module search path
# (LUA_CPATH).
Expand Down
4 changes: 3 additions & 1 deletion kong/api/routes/upstreams.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ local function post_health(is_healthy)
end

local health = is_healthy and 1 or 0
local packet = ("%s|%d|%d|%s"):format(ip, port, health, self.upstream.name)
local packet = ("%s|%d|%d|%s|%s"):format(ip, port, health,
self.upstream.id,
self.upstream.name)
cluster_events:broadcast("balancer:post_health", packet)

return responses.send_HTTP_NO_CONTENT()
Expand Down
1 change: 1 addition & 0 deletions kong/dao/migrations/cassandra.lua
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ return {
{
name = "2017-05-19-173100_remove_nodes_table",
up = [[
DROP INDEX IF EXISTS nodes_cluster_listening_address_idx;
DROP TABLE nodes;
]],
},
Expand Down
18 changes: 7 additions & 11 deletions kong/plugins/hmac-auth/access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -206,24 +206,20 @@ local function validate_clock_skew(headers, date_header_name, allowed_clock_skew
return true
end

local function validate_body(digest_recieved)
-- client doesnt want body validation
if not digest_recieved then
return true
end

local function validate_body(digest_received)
req_read_body()
local body = req_get_body_data()
-- request must have body as client sent a digest header
if not body then
return false

if not digest_received then
-- if there is no digest and no body, it is ok
return not body
end

local sha256 = resty_sha256:new()
sha256:update(body)
sha256:update(body or '')
local digest_created = "SHA-256=" .. ngx_encode_base64(sha256:final())

return digest_created == digest_recieved
return digest_created == digest_received
end

local function load_consumer_into_memory(consumer_id, anonymous)
Expand Down
7 changes: 4 additions & 3 deletions kong/runloop/balancer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,9 @@ local function check_target_history(upstream, balancer)
-- compare balancer history with db-loaded history
local last_equal_index = 0 -- last index where history is the same
for i, entry in ipairs(old_history) do
if entry.order ~= (new_history[i] or EMPTY_T).order then
last_equal_index = i - 1
if new_history[i] and entry.order == new_history[i].order then
last_equal_index = i
else
break
end
end
Expand Down Expand Up @@ -819,7 +820,7 @@ end

--------------------------------------------------------------------------------
-- Update health status and broadcast to workers
-- @param upstream a table with upstream data
-- @param upstream a table with upstream data: must have `name` and `id`
-- @param hostname target hostname
-- @param port target port
-- @param is_healthy boolean: true if healthy, false if unhealthy
Expand Down
5 changes: 3 additions & 2 deletions kong/runloop/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,10 @@ return {

-- manual health updates
cluster_events:subscribe("balancer:post_health", function(data)
local ip, port, health, name = data:match("([^|]+)|([^|]+)|([^|]+)|(.*)")
local pattern = "([^|]+)|([^|]+)|([^|]+)|([^|]+)|(.*)"
local ip, port, health, id, name = data:match(pattern)
port = tonumber(port)
local upstream = { name = name }
local upstream = { id = id, name = name }
local ok, err = balancer.post_health(upstream, ip, port, health == "1")
if not ok then
log(ERR, "failed posting health of ", name, " to workers: ", err)
Expand Down
65 changes: 41 additions & 24 deletions spec-old-api/02-integration/05-proxy/09-balancer_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ end
local add_upstream
local patch_upstream
local get_upstream_health
local get_router_version
local add_target
local add_api
local patch_api
Expand All @@ -318,8 +319,8 @@ do
end
end

local function api_send(method, path, body)
local api_client = helpers.admin_client()
local function api_send(method, path, body, forced_port)
local api_client = helpers.admin_client(nil, forced_port)
local res, err = api_client:send({
method = method,
path = path,
Expand Down Expand Up @@ -353,8 +354,17 @@ do
get_upstream_health = function(upstream_name)
local path = "/upstreams/" .. upstream_name .."/health"
local status, body = api_send("GET", path)
assert.same(200, status)
return body
if status == 200 then
return body
end
end

get_router_version = function(forced_port)
local path = "/cache/api_router:version"
local status, body = api_send("GET", path, nil, forced_port)
if status == 200 then
return body.message
end
end

do
Expand Down Expand Up @@ -419,26 +429,32 @@ local function poll_wait_health(upstream_name, localhost, port, value)
local expire = ngx.now() + hard_timeout
while ngx.now() < expire do
local health = get_upstream_health(upstream_name)
for _, d in ipairs(health.data) do
if d.target == localhost .. ":" .. port and d.health == value then
return
if health then
for _, d in ipairs(health.data) do
if d.target == localhost .. ":" .. port and d.health == value then
return
end
end
end
ngx.sleep(0.01) -- poll-wait
end
end


local function file_contains(filename, searched)
local fd = assert(io.open(filename, "r"))
for line in fd:lines() do
if line:find(searched, 1, true) then
fd:close()
return true
end
end
fd:close()
return false
local function wait_for_router_update(old_rv, localhost, proxy_port, admin_port)
-- add dummy upstream just to rebuild router
local dummy_upstream_name = add_upstream()
local dummy_port = add_target(dummy_upstream_name, localhost)
local dummy_api_host = add_api(dummy_upstream_name)
local dummy_server = http_server(localhost, dummy_port, { math.huge })

helpers.wait_until(function()
client_requests(1, dummy_api_host, "127.0.0.1", proxy_port)
local rv = get_router_version(admin_port)
return rv ~= old_rv
end, 10)

dummy_server:done()
end


Expand All @@ -448,6 +464,7 @@ local localhosts = {
hostname = "localhost",
}


for _, strategy in helpers.each_strategy() do

describe("Ring-balancer #" .. strategy, function()
Expand Down Expand Up @@ -493,24 +510,24 @@ for _, strategy in helpers.each_strategy() do

it("does not perform health checks when disabled (#3304)", function()

local upstream_name = add_upstream({})
local old_rv = get_router_version(9011)

local upstream_name = add_upstream()
local port = add_target(upstream_name, localhost)
local api_host = add_api(upstream_name)

helpers.wait_until(function()
return file_contains("servroot2/logs/error.log", "balancer:targets")
end, 10)
wait_for_router_update(old_rv, localhost, 9010, 9011)

-- server responds, then fails, then responds again
local server = http_server(localhost, port, { 20, 20, 20 })

local seq = {
{ port = 9000, oks = 10, fails = 0, last_status = 200 },
{ port = 9010, oks = 10, fails = 0, last_status = 200 },
{ port = 9000, oks = 0, fails = 10, last_status = 500 },
{ port = 9010, oks = 0, fails = 10, last_status = 500 },
{ port = 9000, oks = 10, fails = 0, last_status = 200 },
{ port = 9010, oks = 0, fails = 10, last_status = 500 },
{ port = 9000, oks = 0, fails = 10, last_status = 500 },
{ port = 9010, oks = 10, fails = 0, last_status = 200 },
{ port = 9000, oks = 10, fails = 0, last_status = 200 },
}
for i, test in ipairs(seq) do
local oks, fails, last_status = client_requests(10, api_host, "127.0.0.1", test.port)
Expand Down
36 changes: 36 additions & 0 deletions spec/01-unit/011-balancer_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe("Balancer", function()
local uuid = require("kong.tools.utils").uuid
local upstream_hc
local upstream_ph
local upstream_ote

teardown(function()
ngx.log:revert()
Expand Down Expand Up @@ -78,9 +79,11 @@ describe("Balancer", function()
[6] = { id = "f", name = "upstream_f", slots = 10, healthchecks = hc_defaults },
[7] = { id = "hc", name = "upstream_hc", slots = 10, healthchecks = passive_hc },
[8] = { id = "ph", name = "upstream_ph", slots = 10, healthchecks = passive_hc },
[9] = { id = "ote", name = "upstream_ote", slots = 10, healthchecks = hc_defaults },
}
upstream_hc = UPSTREAMS_FIXTURES[7]
upstream_ph = UPSTREAMS_FIXTURES[8]
upstream_ote = UPSTREAMS_FIXTURES[9]

TARGETS_FIXTURES = {
-- 1st upstream; a
Expand Down Expand Up @@ -187,6 +190,14 @@ describe("Balancer", function()
target = "127.0.0.1:2222",
weight = 10,
},
-- upstream_ote
{
id = "ote1",
created_at = "001",
upstream_id = "ote",
target = "localhost:1111",
weight = 10,
},
}

local function find_all_in_fixture_fn(fixture)
Expand Down Expand Up @@ -374,6 +385,31 @@ describe("Balancer", function()
end)
end)

describe("on_target_event()", function()
setup(function()
balancer._load_targets_into_memory("ote")
end)

it("adding a target does not recreate a balancer", function()
local b1 = balancer._create_balancer(upstream_ote)
assert.same(1, #(balancer._get_target_history(b1)))

table.insert(TARGETS_FIXTURES, {
id = "ote2",
created_at = "002",
upstream_id = "ote",
target = "localhost:1112",
weight = 10,
})
balancer.on_target_event("create", { upstream_id = "ote" })

local b2 = balancer._create_balancer(upstream_ote)
assert.same(2, #(balancer._get_target_history(b2)))

assert(b1 == b2)
end)
end)

describe("post_health()", function()
local hc, my_balancer

Expand Down
Loading

0 comments on commit d437828

Please sign in to comment.