-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add 'drain' CLI command to set /status/ready to 503 (#13838)
* feat(cli): add 'unready' CLI command to set /status/ready to 503 This command updates the `/status/ready` endpoint to return a `503 Service Unavailable` status code. This allows external tools, such as Kubernetes, to detect when Kong is not ready to receive traffic. Based on this response, Kubernetes can gracefully remove Kong from its load balancing pool according to its configured policies, facilitating a smooth shutdown process. Fix: [FTI-6276](https://konghq.atlassian.net/browse/FTI-6276) Signed-off-by: tzssangglass <[email protected]> --------- Signed-off-by: tzssangglass <[email protected]>
- Loading branch information
1 parent
fa9bed9
commit 11bc3b6
Showing
6 changed files
with
414 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
message: Add the `kong drain` CLI command to make the `/status/ready` endpoint return `503 Service Unavailable` response. | ||
type: feature | ||
scope: CLI Command |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
local http = require "resty.luasocket.http" | ||
local print = print | ||
local fmt = string.format | ||
|
||
local conf_loader = require "kong.conf_loader" | ||
local pl_path = require "pl.path" | ||
local log = require "kong.cmd.utils.log" | ||
local json_encode = require("cjson.safe").encode | ||
|
||
local function execute(args) | ||
log.disable() | ||
|
||
-- only to retrieve the default prefix or use given one | ||
local conf = assert(conf_loader(args.conf, { | ||
prefix = args.prefix | ||
})) | ||
|
||
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 | ||
|
||
log.enable() | ||
|
||
if #conf.status_listeners == 0 then | ||
print("No status listeners found in configuration.") | ||
return | ||
end | ||
|
||
local status_listener = conf.status_listeners[1] | ||
|
||
local scheme = "http" | ||
if status_listener.ssl then | ||
scheme = "https" | ||
end | ||
|
||
local url = scheme .. "://" .. status_listener.ip .. ":" .. status_listener.port .. "/status/ready" | ||
|
||
local httpc = http.new() | ||
httpc:set_timeout(1000) | ||
|
||
|
||
local res, err = httpc:request_uri(url, { | ||
method = "POST", | ||
headers = { | ||
["Content-Type"] = "application/json" | ||
}, | ||
body = json_encode({ | ||
status = "draining" | ||
}), | ||
-- we don't need to verify the SSL certificate for this request | ||
ssl_verify = false, | ||
}) | ||
|
||
httpc:close() | ||
|
||
if not res then | ||
print(fmt("Failed to send request to %s: %s", url, err)) | ||
return | ||
end | ||
|
||
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 'draining'") | ||
end | ||
|
||
|
||
local lapp = [[ | ||
Usage: kong drain [OPTIONS] | ||
Make status listeners(`/status/ready`) return 503 Service Unavailable. | ||
Example usage: | ||
kong drain | ||
Options: | ||
-c,--conf (optional string) configuration file | ||
-p,--prefix (optional string) override prefix directory | ||
]] | ||
|
||
|
||
return { | ||
lapp = lapp, | ||
execute = execute, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
11bc3b6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bazel Build
Docker image available
kong/kong:11bc3b6c487f46f83cea1ab70b27e129d59de4b6
Artifacts available https://github.com/Kong/kong/actions/runs/12002294593