Skip to content

feat: kubernetes discovery readiness check#12852

Merged
nic-6443 merged 17 commits intoapache:masterfrom
ChuanFF:k8s-discovery-healthcheck
Jan 28, 2026
Merged

feat: kubernetes discovery readiness check#12852
nic-6443 merged 17 commits intoapache:masterfrom
ChuanFF:k8s-discovery-healthcheck

Conversation

@ChuanFF
Copy link
Copy Markdown
Contributor

@ChuanFF ChuanFF commented Dec 31, 2025

Description

This pull request adds a Kubernetes service discovery readiness status query to the /status/ready interface. Specifically, the readiness check calls the check_discovery_ready method of the service discovery module. Other types of service discovery, such as Consul, can also adapt this method to support the service discovery module.

relate discuss:
#12635

Which issue(s) this PR fixes:

Fixes #

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement New feature or request labels Dec 31, 2025
Comment thread apisix/discovery/kubernetes/init.lua Outdated
if not endpoint_dict then
core.log.error("failed to get lua_shared_dict:", get_endpoint_dict_name(id),
", please check your APISIX version")
return false, "failed to get lua_shared_dict: ", get_endpoint_dict_name(id),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use .. for string concatenation.

Comment thread apisix/discovery/kubernetes/init.lua Outdated


local function post_list(handle)
handle.endpoint_dict:safe_set("discovery_ready",true)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

safe_set lacks error handling

Comment thread apisix/init.lua Outdated
end
end
end
return true;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not use semicolons.

ngx.exit(200)
local http = require("resty.http")
local healthcheck_uri = "http://127.0.0.1:7085" .. "/status/ready"
for i = 1, 4 do
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment explaining the rationale for selecting the number of retries.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

APISIX takes time to load data from the Kubernetes API server. In scenarios with small amounts of data, it may complete in less than 1 second. The maximum wait time I set, 5 seconds, is solely to account for network fluctuations.

@Baoyuantop Baoyuantop added the wait for update wait for the author's response in this issue/PR label Jan 4, 2026
@Baoyuantop Baoyuantop added awaiting review and removed wait for update wait for the author's response in this issue/PR labels Jan 5, 2026
nic-6443
nic-6443 previously approved these changes Jan 26, 2026
Comment thread apisix/discovery/kubernetes/init.lua Outdated


local function post_list(handle)
local _, err = handle.endpoint_dict:safe_set("discovery_ready",true)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
local _, err = handle.endpoint_dict:safe_set("discovery_ready",true)
local _, err = handle.endpoint_dict:safe_set("discovery_ready", true)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

AlinsRan
AlinsRan previously approved these changes Jan 26, 2026
nic-chen
nic-chen previously approved these changes Jan 26, 2026
@ChuanFF ChuanFF dismissed stale reviews from nic-chen, AlinsRan, and nic-6443 via 40d10e3 January 26, 2026 12:19
@ChuanFF
Copy link
Copy Markdown
Contributor Author

ChuanFF commented Jan 26, 2026

550e240
I set the discovery_ready flag after data cleanup in post_list to prevent it from being cleared during the second round of endpoint listing.

Please help review the code again.

nic-6443
nic-6443 previously approved these changes Jan 27, 2026
nic-chen
nic-chen previously approved these changes Jan 27, 2026
Comment thread apisix/discovery/kubernetes/init.lua Outdated
for _, key in ipairs(handle.existing_keys) do
if not handle.current_keys_hash[key] then
core.log.info("kubernetes discovery module found dirty data in shared dict, key: ",
key)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad indentation

Image

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Several indentation issues have been fixed.

Comment thread apisix/discovery/kubernetes/init.lua Outdated
local endpoint_dict = get_endpoint_dict(id)
if not endpoint_dict then
core.log.error("failed to get lua_shared_dict:", get_endpoint_dict_name(id),
", please check your APISIX version")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Comment thread apisix/discovery/kubernetes/init.lua
Comment thread apisix/init.lua Outdated
return
local function config_ready_check()
local role = core.table.try_read_attr(local_conf, "deployment", "role")
local provider = core.table.try_read_attr(local_conf, "deployment", "role_" ..
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local provider = core.table.try_read_attr(local_conf, "deployment", 
                                          "role_" .. role, "config_provider")

Comment thread apisix/init.lua
return false, "unknown config provider: " .. tostring(provider)
end

local status_shdict = ngx.shared["status-report"]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we may get nil, need to check it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread apisix/init.lua Outdated
local worker_count = ngx.worker.count()
if #ids ~= worker_count then
local error = "worker count: " .. worker_count .. " but status report count: " .. #ids
core.log.warn(error)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we use error log level?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread apisix/init.lua Outdated
local ready = status_shdict:get(id)
if not ready then
local error = "worker id: " .. id .. " has not received configuration"
core.log.warn(error)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

@ChuanFF ChuanFF dismissed stale reviews from nic-chen and nic-6443 via ed33511 January 27, 2026 15:11
@Baoyuantop Baoyuantop requested a review from membphis January 28, 2026 02:31
Comment thread apisix/discovery/kubernetes/init.lua
@nic-6443 nic-6443 merged commit 0b959f5 into apache:master Jan 28, 2026
19 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review enhancement New feature or request size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants