Skip to content

fix: correct handling of endpointSlices in Kubernetes service discovery#12634

Merged
Baoyuantop merged 38 commits into
apache:masterfrom
ChuanFF:endpoint-slices-feat
Jan 28, 2026
Merged

fix: correct handling of endpointSlices in Kubernetes service discovery#12634
Baoyuantop merged 38 commits into
apache:masterfrom
ChuanFF:endpoint-slices-feat

Conversation

@ChuanFF

@ChuanFF ChuanFF commented Sep 24, 2025

Copy link
Copy Markdown
Contributor

Description

Currently, the Kubernetes service discovery implementation in APISIX incorrectly handles EndpointSlices, treating each individual slice as the complete set of endpoints for a service. This contradicts the official Kubernetes documentation.

According to the Kubernetes EndpointSlice documentation:

"For a given service there may be multiple EndpointSlice objects which must be joined to produce the full set of endpoints; you can find all of the slices for a given service by listing EndpointSlices in the service's namespace whose kubernetes.io/service-name label contains the service's name."

The correct behavior should be to combine all EndpointSlices sharing the same kubernetes.io/service-name label to form the complete endpoint set for a service.

Solution Approach

1. Add EndpointSlices Cache

  • Implemented endpoint_slices_cache to store all EndpointSlices with the structure:
    endpoint_slices_cache["k8s_id/namespace/k8s_service_name:port_name"] = {slice1, slice2, ...}
  • Any changes to EndpointSlices trigger updates to this cache
  • The cache is then used to assemble the complete node list for each service, which is subsequently updated in shared memory

2. Code Refactoring

  • Renamed variables to better align with Kubernetes EndpointSlices data structure terminology
  • Improved code readability and maintainability

3. Some Bug Fixes

  • Fixed incorrect handling of addresses field: The addresses field contains a list of IP strings, not objects with .ip attributes
  • Optimized node sorting logic: Simplified the nested loop structure in endpoint_buffer processing:
    -- Before (incorrect):
    for _, ports in pairs(endpoint_buffer) do
        for _, nodes in pairs(ports) do
            core.table.sort(nodes, sort_nodes_cmp)
        end
    end
    
    -- After (correct):
    for _, nodes in pairs(endpoint_buffer) do
        core.table.sort(nodes, sort_nodes_cmp)
    end
    The endpoint_buffer uses ports as keys and node lists as values, requiring only a single iteration level.

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)

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

ChuanFF commented Jan 26, 2026

Copy link
Copy Markdown
Contributor Author

The code changes have been completed; please help review the code again.

nic-6443
nic-6443 previously approved these changes Jan 27, 2026

@nic-6443 nic-6443 left a comment

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.

good job👍

nic-chen
nic-chen previously approved these changes Jan 27, 2026
Comment thread apisix/discovery/kubernetes/init.lua Outdated
return
end

core.log.debug(core.json.delay_encode(endpoint_slice))

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 can add more text to explain this error message
make it easier to understand

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/discovery/kubernetes/init.lua Outdated
return
end

core.log.debug(core.json.delay_encode(endpoint_slice))

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 2b71a3b January 27, 2026 15:33
nic-6443
nic-6443 previously approved these changes Jan 28, 2026
nic-chen
nic-chen previously approved these changes Jan 28, 2026
teset case fix
@ChuanFF
ChuanFF dismissed stale reviews from nic-chen and nic-6443 via a63997e January 28, 2026 04:28
@Baoyuantop
Baoyuantop merged commit 46176f2 into apache:master Jan 28, 2026
19 checks passed
wistefan pushed a commit to wistefan/apisix that referenced this pull request Jun 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review bug Something isn't working size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants