Skip to content

feat(nacos): add metadata filtering support to nacos discovery #12445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

beardnick
Copy link
Contributor

@beardnick beardnick commented Jul 19, 2025

Description

Add metadata filtering support to nacos discovery.

Which issue(s) this PR fixes:

Fixes #12392

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)

- Add metadata validation schema in schema_def.lua
- Implement metadata filtering logic in nacos discovery
- Add comprehensive test cases for metadata validation
- Update documentation for metadata filtering feature
- Fix lint issues and code formatting
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement New feature or request labels Jul 19, 2025
@jizhuozhi
Copy link

There seems to be duplicate work #12448 (coincidentally, we have similar demands). According to my understanding of apisix, all routes share the same service discovery results, so it is not suitable to filter in the discovery.

@Baoyuantop
Copy link
Contributor

There seems to be duplicate work #12448 (coincidentally, we have similar demands). According to my understanding of apisix, all routes share the same service discovery results, so it is not suitable to filter in the discovery.

I don't think it's a duplicate at the moment. Using metadata to filter nacos services is a very useful feature. For more information, you can check the related issue.

@jizhuozhi
Copy link

I don't think it's a duplicate at the moment. Using metadata to filter nacos services is a very useful feature. For more information, you can check the related issue.

Got it, thanks for you reply :)

This is used to filter when pulling the service list from service discovery to avoid pulling a large number of useless instances. We also have similar demands when using Consul. I will try to add the following.

@beardnick beardnick requested a review from Baoyuantop July 26, 2025 06:07
jizhuozhi added a commit to jizhuozhi/apisix that referenced this pull request Jul 27, 2025
docs keep same with apache#12445, but use metadata_match and array values
jizhuozhi added a commit to jizhuozhi/apisix that referenced this pull request Jul 27, 2025
docs keep same with apache#12445, but use metadata_match and array values
@Baoyuantop Baoyuantop requested a review from SkyeYoung July 28, 2025 06:52
Copy link
Member

@SkyeYoung SkyeYoung left a comment

Choose a reason for hiding this comment

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

After checking #12392 and #12464, I have a question:

I haven't seen any discussions that should be implemented as single-value matching or multi-value matching?

#12464 Propose:

Currently, someone in Nacos is also implementing the same function #12392 #12445, but only supports single value matching. However, we hope to support multi-value matching.

This is because user requests often do not have any tags, so if we want to allow multiple instances with different metadata to provide services together, we must register a special metadata to group them, but this grouping cannot be changed dynamically.

I think it makes sense.

But I am not an expert in this field, so what do you think?

Comment on lines 59 to 73
local function metadata_contains(host_metadata, route_metadata)
if not route_metadata or not next(route_metadata) then
return true
end
if not host_metadata or not next(host_metadata) then
return false
end

for k, v in pairs(route_metadata) do
if host_metadata[k] ~= v then
return false
end
end
return true
end
Copy link
Member

Choose a reason for hiding this comment

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

Can u abstract this func into a separate file for reuse?

Comment on lines +378 to +389
-- Apply metadata filtering if specified
local route_metadata = discovery_args and discovery_args.metadata
if route_metadata and next(route_metadata) then
local filtered_nodes = {}
for _, node in ipairs(nodes) do
if metadata_contains(node.metadata, route_metadata) then
core.table.insert(filtered_nodes, node)
end
end
return filtered_nodes
end

Copy link
Member

Choose a reason for hiding this comment

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

ditto

@Baoyuantop Baoyuantop added the wait for update wait for the author's response in this issue/PR label Aug 1, 2025
@SkyeYoung
Copy link
Member

@beardnick ping

@beardnick
Copy link
Contributor Author

After checking #12392 and #12464, I have a question:

I haven't seen any discussions that should be implemented as single-value matching or multi-value matching?

#12464 Propose:

Currently, someone in Nacos is also implementing the same function #12392 #12445, but only supports single value matching. However, we hope to support multi-value matching.
This is because user requests often do not have any tags, so if we want to allow multiple instances with different metadata to provide services together, we must register a special metadata to group them, but this grouping cannot be changed dynamically.

I think it makes sense.

But I am not an expert in this field, so what do you think?

Certainly, that sounds very reasonable. I’m happy to support multi-value matching.

@SkyeYoung
Copy link
Member

@beardnick Are you planning to implement this in this PR, or in another one?

If you want to implement it in other PRs, then this PR is ready for review.

@beardnick
Copy link
Contributor Author

@beardnick Are you planning to implement this in this PR, or in another one?

If you want to implement it in other PRs, then this PR is ready for review.

In this PR.

…pport multi-value arrays while preserving prior structure

This updates the previous metadata validation tests to assert array-of-string values (multi-value matching) and keeps the original table-driven layout for easier review.
@SkyeYoung
Copy link
Member

@beardnick Please fix the errors reported in the CI.

@beardnick
Copy link
Contributor Author

beardnick commented Aug 12, 2025

@beardnick Please fix the errors reported in the CI.

The failed tests seem unrelated to my PR. Could you please rerun them?

@beardnick beardnick requested a review from SkyeYoung August 14, 2025 05:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request size:L This PR changes 100-499 lines, ignoring generated files. user responded wait for update wait for the author's response in this issue/PR
Projects
Status: 👀 In review
Development

Successfully merging this pull request may close these issues.

discovery_args not work in upstream when using nacos
4 participants