Skip to content

Commit

Permalink
Merge pull request #3096 from Kong/feat/healthchecks
Browse files Browse the repository at this point in the history
feat(healthchecks) add active and passive health checks
  • Loading branch information
hishamhm authored Dec 15, 2017
2 parents 5834578 + f87844e commit ceab1d9
Show file tree
Hide file tree
Showing 16 changed files with 2,068 additions and 415 deletions.
2 changes: 1 addition & 1 deletion .ci/run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
set -e

export BUSTED_ARGS="-o gtest -v --exclude-tags=flaky"
export BUSTED_ARGS="-o gtest -v --exclude-tags=flaky,ipv6"
export TEST_CMD="bin/busted $BUSTED_ARGS"

createuser --createdb kong
Expand Down
5 changes: 3 additions & 2 deletions kong-0.11.2-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ dependencies = {
"luaossl == 20171028",
"luasyslog == 1.0.0",
"lua_pack == 1.0.5",
"lua-resty-dns-client == 0.6.2",
"lua-resty-worker-events == 0.3.0",
"lua-resty-dns-client == 1.0.0",
"lua-resty-worker-events == 0.3.1",
"lua-resty-mediador == 0.1.2",
"lua-resty-healthcheck == 0.2.0",
}
build = {
type = "builtin",
Expand Down
42 changes: 42 additions & 0 deletions kong/api/routes/upstreams.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
local crud = require "kong.api.crud_helpers"
local app_helpers = require "lapis.application"
local responses = require "kong.tools.responses"
local balancer = require "kong.core.balancer"
local singletons = require "kong.singletons"
local utils = require "kong.tools.utils"
local cjson = require "cjson"
local cluster_events = singletons.cluster_events


-- clean the target history for a given upstream
Expand Down Expand Up @@ -68,6 +72,25 @@ local function clean_history(upstream_id, dao_factory)
end
end


local function post_health(is_healthy)
return function(self, _)
local addr = utils.normalize_ip(self.target.target)
local ip, port = utils.format_host(addr.host), addr.port
local _, err = balancer.post_health(self.upstream, ip, port, is_healthy)
if err then
return app_helpers.yield_error(err)
end

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

return responses.send_HTTP_NO_CONTENT()
end
end


return {
["/upstreams/"] = {
GET = function(self, dao_factory)
Expand Down Expand Up @@ -195,5 +218,24 @@ return {

return responses.send_HTTP_NO_CONTENT()
end
},

["/upstreams/:upstream_name_or_id/targets/:target_or_id/healthy"] = {
before = function(self, dao_factory, helpers)
crud.find_upstream_by_name_or_id(self, dao_factory, helpers)
crud.find_target_by_target_or_id(self, dao_factory, helpers)
end,

POST = post_health(true),
},

["/upstreams/:upstream_name_or_id/targets/:target_or_id/unhealthy"] = {
before = function(self, dao_factory, helpers)
crud.find_upstream_by_name_or_id(self, dao_factory, helpers)
crud.find_target_by_target_or_id(self, dao_factory, helpers)
end,

POST = post_health(false),
}

}
1 change: 1 addition & 0 deletions kong/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@ return {
"kong_cache",
"kong_process_events",
"kong_cluster_events",
"kong_healthchecks",
},
}
Loading

0 comments on commit ceab1d9

Please sign in to comment.