Skip to content

Commit

Permalink
rename runtime_prefix => socket_path
Browse files Browse the repository at this point in the history
  • Loading branch information
flrgh committed Jul 30, 2024
1 parent 1ef907c commit e4974c9
Show file tree
Hide file tree
Showing 19 changed files with 62 additions and 62 deletions.
8 changes: 4 additions & 4 deletions build/dockerfiles/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ if [[ "$1" == "kong" ]]; then

# remove all dangling sockets in $PREFIX dir before starting Kong
LOGGED_SOCKET_WARNING=0
runtime_prefix=$PREFIX/runtime
for localfile in "$runtime_prefix"/*; do
socket_path=$PREFIX/sockets
for localfile in "$socket_path"/*; do
if [ -S "$localfile" ]; then
if (( LOGGED_SOCKET_WARNING == 0 )); then
printf >&2 'WARN: found dangling unix sockets in the runtime prefix '
printf >&2 '(%q) ' "$runtime_prefix"
printf >&2 'WARN: found dangling unix sockets in the prefix directory '
printf >&2 '(%q) ' "$socket_path"
printf >&2 'while preparing to start Kong. This may be a sign that Kong '
printf >&2 'was previously shut down uncleanly or is in an unknown state '
printf >&2 'and could require further investigation.\n'
Expand Down
2 changes: 1 addition & 1 deletion changelog/unreleased/kong/move-sockets-to-subdir.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
message: Moved internal unix sockets to a subdirectory of the Kong prefix.
message: Moved internal Unix sockets to a subdirectory (`sockets`) of the Kong prefix.
type: bugfix
scope: Core
2 changes: 1 addition & 1 deletion kong/clustering/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ local _log_prefix = "[clustering] "
local KONG_VERSION = kong.version

local CLUSTER_PROXY_SSL_TERMINATOR_SOCK = fmt("unix:%s/cluster_proxy_ssl_terminator.sock",
kong.configuration.runtime_prefix)
kong.configuration.socket_path)

local _M = {}

Expand Down
12 changes: 6 additions & 6 deletions kong/cmd/start.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ local function is_socket(path)
return lfs.attributes(path, "mode") == "socket"
end

local function cleanup_dangling_unix_sockets(runtime_prefix)
local function cleanup_dangling_unix_sockets(socket_path)
local found = {}

for child in lfs.dir(runtime_prefix) do
local path = runtime_prefix .. "/" .. child
for child in lfs.dir(socket_path) do
local path = socket_path .. "/" .. child
if is_socket(path) then
table.insert(found, path)
end
Expand All @@ -27,11 +27,11 @@ local function cleanup_dangling_unix_sockets(runtime_prefix)
return
end

log.warn("Found dangling unix sockets in the runtime prefix (%q) while " ..
log.warn("Found dangling unix sockets in the prefix directory (%q) while " ..
"preparing to start Kong. This may be a sign that Kong was " ..
"previously shut down uncleanly or is in an unknown state and " ..
"could require further investigation.",
runtime_prefix)
socket_path)

log.warn("Attempting to remove dangling sockets before starting Kong...")

Expand Down Expand Up @@ -59,7 +59,7 @@ local function execute(args)
assert(prefix_handler.prepare_prefix(conf, args.nginx_conf, nil, nil,
args.nginx_conf_flags))

cleanup_dangling_unix_sockets(conf.runtime_prefix)
cleanup_dangling_unix_sockets(conf.socket_path)

_G.kong = kong_global.new()
kong_global.init_pdk(_G.kong, conf)
Expand Down
4 changes: 2 additions & 2 deletions kong/cmd/utils/prefix_handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,8 @@ local function prepare_prefix(kong_config, nginx_custom_template_path, skip_writ
return nil, kong_config.prefix .. " is not a directory"
end

if not exists(kong_config.runtime_prefix) then
local ok, err = makepath(kong_config.runtime_prefix)
if not exists(kong_config.socket_path) then
local ok, err = makepath(kong_config.socket_path)
if not ok then
return nil, err
end
Expand Down
4 changes: 2 additions & 2 deletions kong/conf_loader/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,9 @@ local function load(path, custom_conf, opts)
-- load absolute paths
conf.prefix = abspath(conf.prefix)

-- the runtime prefix is where we keep listening unix sockets for IPC and
-- the socket path is where we store listening unix sockets for IPC and
-- private APIs
conf.runtime_prefix = pl_path.join(conf.prefix, "runtime")
conf.socket_path = pl_path.join(conf.prefix, "sockets")

if conf.lua_ssl_trusted_certificate
and #conf.lua_ssl_trusted_certificate > 0 then
Expand Down
4 changes: 2 additions & 2 deletions kong/global.lua
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,12 @@ function _GLOBAL.init_worker_events(kong_config)
local worker_events
local opts

local runtime_prefix = kong_config.runtime_prefix
local socket_path = kong_config.socket_path
local sock = ngx.config.subsystem == "stream" and
"stream_worker_events.sock" or
"worker_events.sock"

local listening = "unix:" .. runtime_prefix .. "/" .. sock
local listening = "unix:" .. socket_path .. "/" .. sock

local max_payload_len = kong_config.worker_events_max_payload

Expand Down
12 changes: 6 additions & 6 deletions kong/runloop/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -508,16 +508,16 @@ do
local buffer = require "string.buffer"

-- this module may be loaded before `kong.configuration` is initialized
local runtime_prefix = kong and kong.configuration
and kong.configuration.runtime_prefix
local socket_path = kong and kong.configuration
and kong.configuration.socket_path

if not runtime_prefix then
-- `kong.configuration.runtime_prefix` is already normalized to an absolute
if not socket_path then
-- `kong.configuration.socket_path` is already normalized to an absolute
-- path, but `ngx.config.prefix()` is not
runtime_prefix = require("pl.path").abspath(ngx.config.prefix() .. "/runtime")
socket_path = require("pl.path").abspath(ngx.config.prefix() .. "/sockets")
end

local STREAM_CONFIG_SOCK = "unix:" .. runtime_prefix .. "/stream_config.sock"
local STREAM_CONFIG_SOCK = "unix:" .. socket_path .. "/stream_config.sock"
local IS_HTTP_SUBSYSTEM = ngx.config.subsystem == "http"

local function broadcast_reconfigure_event(data)
Expand Down
6 changes: 3 additions & 3 deletions kong/runloop/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -896,9 +896,9 @@ return {

init_worker = {
before = function()
local runtime_prefix = kong.configuration.runtime_prefix
STREAM_TLS_TERMINATE_SOCK = fmt("unix:%s/stream_tls_terminate.sock", runtime_prefix)
STREAM_TLS_PASSTHROUGH_SOCK = fmt("unix:%s/stream_tls_passthrough.sock", runtime_prefix)
local socket_path = kong.configuration.socket_path
STREAM_TLS_TERMINATE_SOCK = fmt("unix:%s/stream_tls_terminate.sock", socket_path)
STREAM_TLS_PASSTHROUGH_SOCK = fmt("unix:%s/stream_tls_passthrough.sock", socket_path)

log_level.init_worker()

Expand Down
2 changes: 1 addition & 1 deletion kong/templates/nginx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ stream {
> if cluster_ssl_tunnel then
server {
listen unix:${{RUNTIME_PREFIX}}/cluster_proxy_ssl_terminator.sock;
listen unix:${{SOCKET_PATH}}/cluster_proxy_ssl_terminator.sock;
proxy_pass ${{cluster_ssl_tunnel}};
proxy_ssl on;
Expand Down
2 changes: 1 addition & 1 deletion kong/templates/nginx_kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ server {
server {
charset UTF-8;
server_name kong_worker_events;
listen unix:${{RUNTIME_PREFIX}}/worker_events.sock;
listen unix:${{SOCKET_PATH}}/worker_events.sock;
access_log off;
location / {
content_by_lua_block {
Expand Down
10 changes: 5 additions & 5 deletions kong/templates/nginx_kong_stream.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ server {
> end
> if stream_proxy_ssl_enabled then
listen unix:${{RUNTIME_PREFIX}}/stream_tls_terminate.sock ssl proxy_protocol;
listen unix:${{SOCKET_PATH}}/stream_tls_terminate.sock ssl proxy_protocol;
> end
access_log ${{PROXY_STREAM_ACCESS_LOG}};
Expand Down Expand Up @@ -175,7 +175,7 @@ server {
}
server {
listen unix:${{RUNTIME_PREFIX}}/stream_tls_passthrough.sock proxy_protocol;
listen unix:${{SOCKET_PATH}}/stream_tls_passthrough.sock proxy_protocol;
access_log ${{PROXY_STREAM_ACCESS_LOG}};
error_log ${{PROXY_STREAM_ERROR_LOG}} ${{LOG_LEVEL}};
Expand Down Expand Up @@ -205,7 +205,7 @@ server {
> if database == "off" then
server {
listen unix:${{RUNTIME_PREFIX}}/stream_config.sock;
listen unix:${{SOCKET_PATH}}/stream_config.sock;
error_log ${{ADMIN_ERROR_LOG}} ${{LOG_LEVEL}};
Expand All @@ -216,7 +216,7 @@ server {
> end -- database == "off"
server { # ignore (and close }, to ignore content)
listen unix:${{RUNTIME_PREFIX}}/stream_rpc.sock;
listen unix:${{SOCKET_PATH}}/stream_rpc.sock;
error_log ${{ADMIN_ERROR_LOG}} ${{LOG_LEVEL}};
content_by_lua_block {
Kong.stream_api()
Expand All @@ -225,7 +225,7 @@ server { # ignore (and close }, to ignore content)
> end -- #stream_listeners > 0
server {
listen unix:${{RUNTIME_PREFIX}}/stream_worker_events.sock;
listen unix:${{SOCKET_PATH}}/stream_worker_events.sock;
error_log ${{ADMIN_ERROR_LOG}} ${{LOG_LEVEL}};
access_log off;
content_by_lua_block {
Expand Down
2 changes: 1 addition & 1 deletion kong/tools/stream_api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ local MAX_DATA_LEN = 2^22 - 1
local HEADER_LEN = #st_pack(PACK_F, MAX_KEY_LEN, MAX_DATA_LEN)

-- this module may be loaded before `kong.configuration` is initialized
local SOCKET_PATH = "unix:" .. ngx.config.prefix() .. "/runtime/stream_rpc.sock"
local SOCKET_PATH = "unix:" .. ngx.config.prefix() .. "/sockets/stream_rpc.sock"

local stream_api = {}

Expand Down
2 changes: 1 addition & 1 deletion spec/01-unit/03-conf_loader_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2395,7 +2395,7 @@ describe("Configuration loader", function()
local FIELDS = {
-- CONF_BASIC
prefix = true,
runtime_prefix = true,
socket_path = true,
vaults = true,
database = true,
lmdb_environment_path = true,
Expand Down
38 changes: 19 additions & 19 deletions spec/02-integration/02-cmd/02-start_stop_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ local read_file = helpers.file.read


local PREFIX = helpers.test_conf.prefix
local RUNTIME_PREFIX = helpers.test_conf.runtime_prefix
local SOCKET_PATH = helpers.test_conf.socket_path
local TEST_CONF = helpers.test_conf
local TEST_CONF_PATH = helpers.test_conf_path


local function wait_until_healthy(prefix)
prefix = prefix or PREFIX
local runtime_prefix = prefix .. "/runtime"
local socket_path = prefix .. "/sockets"

local cmd

Expand All @@ -43,11 +43,11 @@ local function wait_until_healthy(prefix)
local conf = assert(helpers.get_running_conf(prefix))

if conf.proxy_listen and conf.proxy_listen ~= "off" then
helpers.wait_for_file("socket", runtime_prefix .. "/worker_events.sock")
helpers.wait_for_file("socket", socket_path .. "/worker_events.sock")
end

if conf.stream_listen and conf.stream_listen ~= "off" then
helpers.wait_for_file("socket", runtime_prefix .. "/stream_worker_events.sock")
helpers.wait_for_file("socket", socket_path .. "/stream_worker_events.sock")
end

if conf.admin_listen and conf.admin_listen ~= "off" then
Expand Down Expand Up @@ -1036,27 +1036,27 @@ describe("kong start/stop #" .. strategy, function()
end)
end)

describe("runtime_prefix", function()
describe("socket_path", function()
it("is created on demand by `kong prepare`", function()
local dir, cleanup = helpers.make_temp_dir()
finally(cleanup)

local cmd = fmt("prepare -p %q", dir)
assert.truthy(kong_exec(cmd), "expected '" .. cmd .. "' to succeed")
assert.truthy(helpers.path.isdir(dir .. "/runtime"),
"expected '" .. dir .. "/runtime' directory to be created")
assert.truthy(helpers.path.isdir(dir .. "/sockets"),
"expected '" .. dir .. "/sockets' directory to be created")
end)

it("can be a user-created symlink", function()
local prefix, cleanup = helpers.make_temp_dir()
finally(cleanup)

local runtime_prefix
runtime_prefix, cleanup = helpers.make_temp_dir()
local socket_path
socket_path, cleanup = helpers.make_temp_dir()
finally(cleanup)

assert.truthy(helpers.execute(fmt("ln -sf %q %q/runtime", runtime_prefix, prefix)),
"failed to symlink runtime prefix")
assert.truthy(helpers.execute(fmt("ln -sf %q %q/sockets", socket_path, prefix)),
"failed to symlink socket path")

local preserve_prefix = true
assert(helpers.start_kong({
Expand All @@ -1071,16 +1071,16 @@ describe("kong start/stop #" .. strategy, function()

wait_until_healthy(prefix)

assert.truthy(helpers.path.exists(runtime_prefix .. "/worker_events.sock"),
"worker events socket was not created in the runtime_prefix dir")
assert.truthy(helpers.path.exists(socket_path .. "/worker_events.sock"),
"worker events socket was not created in the socket_path dir")
end)
end)

describe("dangling socket cleanup", function()
local pidfile = TEST_CONF.nginx_pid

-- the worker events socket is just one of many unix sockets we use
local event_sock = RUNTIME_PREFIX .. "/worker_events.sock"
local event_sock = SOCKET_PATH .. "/worker_events.sock"

local env = {
prefix = PREFIX,
Expand Down Expand Up @@ -1175,8 +1175,8 @@ describe("kong start/stop #" .. strategy, function()
it("removes unix socket files in the prefix directory", function()
local _, stderr = assert_start()

assert.matches("[warn] Found dangling unix sockets in the runtime prefix", stderr, nil, true)
assert.matches(RUNTIME_PREFIX, stderr, nil, true)
assert.matches("[warn] Found dangling unix sockets in the prefix directory", stderr, nil, true)
assert.matches(SOCKET_PATH, stderr, nil, true)

assert.matches("removing unix socket", stderr)
assert.matches(event_sock, stderr, nil, true)
Expand Down Expand Up @@ -1217,7 +1217,7 @@ describe("kong start/stop #" .. strategy, function()

it("works with resty.events when KONG_PREFIX is a relative path", function()
local prefix = "relpath"
local runtime_prefix = "relpath/runtime"
local socket_path = "relpath/sockets"

finally(function()
-- this test uses a non-default prefix, so it must manage
Expand All @@ -1244,8 +1244,8 @@ describe("kong start/stop #" .. strategy, function()
-- wait until everything is running
wait_until_healthy(prefix)

assert.truthy(helpers.path.exists(runtime_prefix .. "/worker_events.sock"))
assert.truthy(helpers.path.exists(runtime_prefix .. "/stream_worker_events.sock"))
assert.truthy(helpers.path.exists(socket_path .. "/worker_events.sock"))
assert.truthy(helpers.path.exists(socket_path .. "/stream_worker_events.sock"))

local log = prefix .. "/logs/error.log"
assert.logfile(log).has.no.line("[error]", true, 0)
Expand Down
4 changes: 2 additions & 2 deletions spec/02-integration/05-proxy/01-proxy_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ describe("#stream proxy interface listeners", function()
stream_listen = "127.0.0.1:9011, 127.0.0.1:9012",
}))

local stream_events_sock_path = "unix:" .. helpers.test_conf.runtime_prefix .. "/stream_worker_events.sock"
local stream_events_sock_path = "unix:" .. helpers.test_conf.socket_path .. "/stream_worker_events.sock"

if helpers.test_conf.database == "off" then
local stream_config_sock_path = "unix:" .. helpers.test_conf.runtime_prefix .. "/stream_config.sock"
local stream_config_sock_path = "unix:" .. helpers.test_conf.socket_path .. "/stream_config.sock"

assert.equals(3, count_server_blocks(helpers.test_conf.nginx_kong_stream_conf))
assert.same({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("Stream module API endpoint", function()
plugins = "stream-api-echo",
})

socket_path = "unix:" .. helpers.get_running_conf().runtime_prefix .. "/stream_rpc.sock"
socket_path = "unix:" .. helpers.get_running_conf().socket_path .. "/stream_rpc.sock"
end)

lazy_teardown(function()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ include '*.stream_mock';
> if cluster_ssl_tunnel then
server {
listen unix:${{RUNTIME_PREFIX}}/cluster_proxy_ssl_terminator.sock;
listen unix:${{SOCKET_PATH}}/cluster_proxy_ssl_terminator.sock;
proxy_pass ${{cluster_ssl_tunnel}};
proxy_ssl on;
Expand Down
6 changes: 3 additions & 3 deletions spec/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3844,10 +3844,10 @@ end
local function cleanup_kong(prefix, preserve_prefix, preserve_dc)
-- remove socket files to ensure `pl.dir.rmtree()` ok
prefix = prefix or conf.prefix
local runtime_prefix = pl_path.join(prefix, "runtime")
for child in lfs.dir(runtime_prefix) do
local socket_path = pl_path.join(prefix, "sockets")
for child in lfs.dir(socket_path) do
if child:sub(-5) == ".sock" then
local path = pl_path.join(runtime_prefix, child)
local path = pl_path.join(socket_path, child)
os.remove(path)
end
end
Expand Down

0 comments on commit e4974c9

Please sign in to comment.