Skip to content

Commit

Permalink
feat(pdk) kong.node.get_hostname (#6613)
Browse files Browse the repository at this point in the history
  • Loading branch information
kikito committed Dec 2, 2020
1 parent e109315 commit 4fa9f96
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 37 deletions.
3 changes: 1 addition & 2 deletions kong/api/routes/kong.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local utils = require "kong.tools.utils"
local singletons = require "kong.singletons"
local conf_loader = require "kong.conf_loader"
local cjson = require "cjson"
Expand Down Expand Up @@ -76,7 +75,7 @@ return {
return kong.response.exit(200, {
tagline = tagline,
version = version,
hostname = utils.get_hostname(),
hostname = knode.get_hostname(),
node_id = node_id,
timers = {
running = ngx.timer.running_count(),
Expand Down
2 changes: 1 addition & 1 deletion kong/clustering.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ local function communicate(premature, conf)

local c = assert(ws_client:new(WS_OPTS))
local uri = "wss://" .. address .. "/v1/outlet?node_id=" ..
kong.node.get_id() .. "&node_hostname=" .. utils.get_hostname()
kong.node.get_id() .. "&node_hostname=" .. kong.node.get_hostname()

local opts = {
ssl_verify = true,
Expand Down
30 changes: 29 additions & 1 deletion kong/pdk/node.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
-- @module kong.node

local utils = require "kong.tools.utils"
local ffi = require "ffi"


local floor = math.floor
local lower = string.lower
local match = string.match
local gsub = string.gsub
local sort = table.sort
local insert = table.insert
local shared = ngx.shared

local C = ffi.C
local ffi_new = ffi.new
local ffi_str = ffi.string

local NODE_ID_KEY = "kong:node_id"

Expand Down Expand Up @@ -227,6 +231,30 @@ local function new(self)
end


---
-- Returns the name used by the local machine
--
-- @function kong.node.get_hostname
-- @treturn string The local machine hostname
-- @usage
-- local hostname = kong.node.get_hostname()
function _NODE.get_hostname()
local SIZE = 253 -- max number of chars for a hostname

local buf = ffi_new("unsigned char[?]", SIZE)
local res = C.gethostname(buf, SIZE)

if res == 0 then
local hostname = ffi_str(buf, SIZE)
return gsub(hostname, "%z+$", "")
end

local f = io.popen("/bin/hostname")
local hostname = f:read("*a") or ""
f:close()
return gsub(hostname, "\n$", "")
end

return _NODE
end

Expand Down
4 changes: 4 additions & 0 deletions kong/reports.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ local cjson = require "cjson.safe"
local utils = require "kong.tools.utils"
local constants = require "kong.constants"
local counter = require "resty.counter"
local knode = (kong and kong.node) and kong.node or
require "kong.pdk.node".new()


local kong_dict = ngx.shared.kong
Expand Down Expand Up @@ -61,6 +63,8 @@ do

local system_infos = utils.get_system_infos()

system_infos.hostname = system_infos.hostname or knode.get_hostname()

-- <14>: syslog facility code 'log alert'
_buffer[#_buffer + 1] = "<14>version=" .. meta._VERSION

Expand Down
26 changes: 1 addition & 25 deletions kong/tools/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,28 +88,6 @@ _M.pack = function(...) return {n = select("#", ...), ...} end
-- Explicitly honors the `n` field if given in the table, so it is `nil` safe
_M.unpack = function(t, i, j) return unpack(t, i or 1, j or t.n or #t) end

--- Retrieves the hostname of the local machine
-- @return string The hostname
function _M.get_hostname()
local result
local SIZE = 128

local buf = ffi_new("unsigned char[?]", SIZE)
local res = C.gethostname(buf, SIZE)

if res == 0 then
local hostname = ffi_str(buf, SIZE)
result = gsub(hostname, "%z+$", "")
else
local f = io.popen("/bin/hostname")
local hostname = f:read("*a") or ""
f:close()
result = gsub(hostname, "\n$", "")
end

return result
end

do
local _system_infos

Expand All @@ -118,9 +96,7 @@ do
return _system_infos
end

_system_infos = {
hostname = _M.get_hostname()
}
_system_infos = {}

local ok, _, stdout = pl_utils.executeex("getconf _NPROCESSORS_ONLN")
if ok then
Expand Down
8 changes: 0 additions & 8 deletions spec/01-unit/05-utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,11 @@ local pl_path = require "pl.path"

describe("Utils", function()

describe("get_hostname()", function()
it("should retrieve the hostname", function()
assert.is_string(utils.get_hostname())
end)
end)

describe("get_system_infos()", function()
it("retrieves various host infos", function()
local infos = utils.get_system_infos()
assert.is_number(infos.cores)
assert.is_string(infos.hostname)
assert.is_string(infos.uname)
assert.not_matches("\n$", infos.hostname)
assert.not_matches("\n$", infos.uname)
end)
it("caches the result", function()
Expand Down
12 changes: 12 additions & 0 deletions t/01-pdk/12-node/00-phase_checks.t
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ qq{
log = true,
admin_api = true,
},
{
method = "get_hostname",
args = {},
init_worker = true,
certificate = "pending",
rewrite = true,
access = true,
header_filter = true,
body_filter = true,
log = true,
admin_api = true,
},
}
phase_check_functions(phases.init_worker)
Expand Down
35 changes: 35 additions & 0 deletions t/01-pdk/12-node/03-get_hostname.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use strict;
use warnings FATAL => 'all';
use Test::Nginx::Socket::Lua;
use t::Util;

plan tests => repeat_each() * (blocks() * 3);

run_tests();

__DATA__
=== TEST 1: node.get_id() returns node identifier
--- http_config eval
qq{
$t::Util::HttpConfig
lua_shared_dict kong 24k;
}
--- config
location = /t {
content_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()
math.randomseed(ngx.time())
ngx.say(pdk.node.get_id())
}
}
--- request
GET /t
--- response_body_like
^([a-zA-Z0-9](?:(?:[a-zA-Z0-9-]*|(?<!-)\.(?![-.]))*[a-zA-Z0-9]+)?)$
--- no_error_log
[error]

0 comments on commit 4fa9f96

Please sign in to comment.