Skip to content

Commit

Permalink
fix unready in hybrid mode
Browse files Browse the repository at this point in the history
Signed-off-by: tzssangglass <[email protected]>
  • Loading branch information
tzssangglass committed Nov 7, 2024
1 parent eeb6e00 commit d03b592
Show file tree
Hide file tree
Showing 3 changed files with 234 additions and 102 deletions.
1 change: 1 addition & 0 deletions bin/kong
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ local skip_inject_cmds = {
quit = true,
health = true,
hybrid = true,
unready = true,
}

for k in pairs(cmds) do
Expand Down
106 changes: 62 additions & 44 deletions kong/cmd/unready.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,79 @@ local conf_loader = require "kong.conf_loader"
local pl_path = require "pl.path"
local log = require "kong.cmd.utils.log"

return {
execute = function(args)
log.disable()

-- only to retrieve the default prefix or use given one
local conf = assert(conf_loader(args.conf, {
prefix = args.prefix
}))
local function execute(args)
log.disable()

if pl_path.exists(conf.kong_env) then
-- load <PREFIX>/kong.conf containing running node's config
conf = assert(conf_loader(conf.kong_env))
end
-- only to retrieve the default prefix or use given one
local conf = assert(conf_loader(args.conf, {
prefix = args.prefix
}))

log.enable()
if pl_path.exists(conf.kong_env) then
-- load <PREFIX>/kong.conf containing running node's config
conf = assert(conf_loader(conf.kong_env))
end

if #conf.status_listeners == 0 then
print("No status listeners found in configuration.")
return
end
log.enable()

local status_listener = conf.status_listeners[1]
if #conf.status_listeners == 0 then
print("No status listeners found in configuration.")
return
end

local scheme = "http"
if status_listener.ssl then
scheme = "https"
end
local status_listener = conf.status_listeners[1]

local url = scheme .. "://" .. status_listener.ip .. ":" .. status_listener.port .. "/status/unready"
local scheme = "http"
if status_listener.ssl then
scheme = "https"
end

local httpc = http.new()
httpc:set_timeout(1000)
local url = scheme .. "://" .. status_listener.ip .. ":" .. status_listener.port .. "/status/unready"

local res, err = httpc:request_uri(url, {
method = "POST",
headers = {
["Content-Type"] = "application/json"
},
body = "{}",
-- we don't need to verify the SSL certificate for this request
ssl_verify = false,
})
local httpc = http.new()
httpc:set_timeout(1000)

httpc:close()
local res, err = httpc:request_uri(url, {
method = "POST",
headers = {
["Content-Type"] = "application/json"
},
body = "{}",
-- we don't need to verify the SSL certificate for this request
ssl_verify = false,
})

if not res then
print(fmt("Failed to send request to %s: %s", url, err))
return
end
httpc:close()

if res.status ~= 204 then
print(fmt("Unexpected response status from %s: %d", url, res.status))
return
end
if not res then
print(fmt("Failed to send request to %s: %s", url, err))
return
end

print("Kong's status successfully changed to 'unready'")
if res.status ~= 204 then
print(fmt("Unexpected response status from %s: %d", url, res.status))
return
end

print("Kong's status successfully changed to 'unready'")
end


local lapp = [[
Usage: kong unready [OPTIONS]
Make status listeners(`/status/ready`) return 503 Service Unavailable.
Example usage:
kong unready
Options:
-c,--conf (optional string) configuration file
-p,--prefix (optional string) override prefix directory
]]


return {
lapp = lapp,
execute = execute,
}
Loading

0 comments on commit d03b592

Please sign in to comment.