Skip to content

Commit

Permalink
fix(hostname) use IP if hostname is not provided Kong#48
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Silverman authored and AlinsRan committed Jun 1, 2023
1 parent 0aa2cbd commit 973cffe
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/resty/healthcheck.lua
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ function checker:run_single_check(ip, port, hostname, hostheader)
self.checks.active.https_verify_certificate)
if not session then
sock:close()
self:log(ERR, "failed SSL handshake with '", hostname, " (", ip, ":", port, ")': ", err)
self:log(ERR, "failed SSL handshake with '", hostname or "", " (", ip, ":", port, ")': ", err)
return self:report_tcp_failure(ip, port, hostname, "connect", "active")
end
end
Expand All @@ -894,7 +894,7 @@ function checker:run_single_check(ip, port, hostname, hostheader)
local bytes
bytes, err = sock:send(request)
if not bytes then
self:log(ERR, "failed to send http request to '", hostname, " (", ip, ":", port, ")': ", err)
self:log(ERR, "failed to send http request to '", hostname or "", " (", ip, ":", port, ")': ", err)
if err == "timeout" then
sock:close() -- timeout errors do not close the socket.
return self:report_timeout(ip, port, hostname, "active")
Expand All @@ -905,7 +905,7 @@ function checker:run_single_check(ip, port, hostname, hostheader)
local status_line
status_line, err = sock:receive()
if not status_line then
self:log(ERR, "failed to receive status line from '", hostname, " (",ip, ":", port, ")': ", err)
self:log(ERR, "failed to receive status line from '", hostname or "", " (",ip, ":", port, ")': ", err)
if err == "timeout" then
sock:close() -- timeout errors do not close the socket.
return self:report_timeout(ip, port, hostname, "active")
Expand All @@ -920,21 +920,21 @@ function checker:run_single_check(ip, port, hostname, hostheader)
if from then
status = tonumber(status_line:sub(from, to))
else
self:log(ERR, "bad status line from '", hostname, " (", ip, ":", port, ")': ", status_line)
self:log(ERR, "bad status line from '", hostname or "", " (", ip, ":", port, ")': ", status_line)
-- note: 'status' will be reported as 'nil'
end

sock:close()

self:log(DEBUG, "Reporting '", hostname, " (", ip, ":", port, ")' (got HTTP ", status, ")")
self:log(DEBUG, "Reporting '", hostname or "", " (", ip, ":", port, ")' (got HTTP ", status, ")")

return self:report_http_status(ip, port, hostname, status, "active")
end

-- executes a work package (a list of checks) sequentially
function checker:run_work_package(work_package)
for _, work_item in ipairs(work_package) do
self:log(DEBUG, "Checking ", work_item.hostname, " ",
self:log(DEBUG, "Checking ", work_item.hostname or "", " ",
work_item.hostheader and "(host header: ".. work_item.hostheader .. ")"
or "", work_item.ip, ":", work_item.port,
" (currently ", work_item.debug_health, ")")
Expand Down Expand Up @@ -1074,7 +1074,7 @@ function checker:event_handler(event_name, ip, port, hostname)
if event_name == self.events.remove then
if target_found then
-- remove hash part
self.targets[target_found.ip][target_found.port][target_found.hostname] = nil
self.targets[target_found.ip][target_found.port][target_found.hostname or target_found.ip] = nil
if not next(self.targets[target_found.ip][target_found.port]) then
-- no more hostnames on this port, so delete it
self.targets[target_found.ip][target_found.port] = nil
Expand Down
24 changes: 24 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,30 @@ for the complete API.

Versioning is strictly based on [Semantic Versioning](https://semver.org/)

### Releasing new versions:

* update changelog below (PR's should be merged including a changelog entry)
* based on changelog determine new SemVer version
* create a new rockspec
* render the docs using `ldoc` (don't do this within PR's)
* commit as "release x.x.x" (do not include rockspec revision)
* tag the commit with "x.x.x" (do not include rockspec revision)
* push commit and tag
* upload rock to luarocks: `luarocks upload rockspecs/[name] --api-key=abc`

### Unreleased

* fix: properly log line numbers by using tail calls [#29](https://github.com/Kong/lua-resty-healthcheck/pull/29)
* fix: when not providing a hostname, use IP [#48](https://github.com/Kong/lua-resty-healthcheck/pull/48)
* fix: makefile; make install

### 1.3.0 (17-Jun-2020)

* Adds support to mTLS to active healthchecks. This feature can be used adding
the fields `ssl_cert` and `ssl_key`, with certificate and key respectively,
when creating a new healthcheck object.
[#41](https://github.com/Kong/lua-resty-healthcheck/pull/41)

### 1.2.0 (13-Feb-2020)

* Adds `set_all_target_statuses_for_hostname`, which sets the targets for
Expand Down
111 changes: 111 additions & 0 deletions t/18-event_handler.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use Test::Nginx::Socket::Lua;
use Cwd qw(cwd);

workers(1);

plan tests => repeat_each() * 4;

my $pwd = cwd();

our $HttpConfig = qq{
lua_package_path "$pwd/lib/?.lua;;";
lua_shared_dict test_shm 8m;
lua_shared_dict my_worker_events 8m;
};

run_tests();

__DATA__

=== TEST 1: add_target() without hostname, remove_target() with same ip:port
--- http_config eval
qq{
$::HttpConfig

server {
listen 2112;
location = /status {
return 200;
}
}
}
--- config
location = /t {
content_by_lua_block {
local we = require "resty.worker.events"
assert(we.configure{ shm = "my_worker_events", interval = 0.1 })
local healthcheck = require("resty.healthcheck")
local checker = healthcheck.new({
name = "testing",
shm_name = "test_shm",
checks = {
active = {
http_path = "/status",
healthy = {
interval = 0.1
},
unhealthy = {
interval = 0.1
}
}
}
})
ngx.sleep(0.2) -- wait twice the interval
local ok, err = checker:add_target("127.0.0.1", 2112)
ngx.say(ok)
ngx.sleep(0.2) -- wait twice the interval
ok, err = checker:remove_target("127.0.0.1", 2112)
ngx.sleep(0.2) -- wait twice the interval
}
}
--- request
GET /t
--- response_body
true

=== TEST 2: add_target() with hostname, remove_target() on same target
--- http_config eval
qq{
$::HttpConfig

server {
listen 2112;
location = /status {
return 200;
}
}
}
--- config
location = /t {
content_by_lua_block {
local we = require "resty.worker.events"
assert(we.configure{ shm = "my_worker_events", interval = 0.1 })
local healthcheck = require("resty.healthcheck")
local checker = healthcheck.new({
name = "testing",
shm_name = "test_shm",
checks = {
active = {
http_path = "/status",
healthy = {
interval = 0.1
},
unhealthy = {
interval = 0.1
}
}
}
})
ngx.sleep(0.2) -- wait twice the interval
local ok, err = checker:add_target("127.0.0.1", 2112, "localhost")
ngx.say(ok)
ngx.sleep(0.2) -- wait twice the interval
ok, err = checker:remove_target("127.0.0.1", 2112, "localhost")
ngx.sleep(0.2) -- wait twice the interval
}
}
--- request
GET /t
--- response_body
true

0 comments on commit 973cffe

Please sign in to comment.