beardnick commented on code in PR #12445:
URL: https://github.com/apache/apisix/pull/12445#discussion_r2366148383
##########
docs/en/latest/discovery/nacos.md:
##########
@@ -278,3 +279,52 @@ The formatted response as below:
}
}
```
+
+#### Metadata filtering
+
+APISIX supports filtering service instances based on metadata. When a route is
configured with metadata conditions, only service instances whose metadata
contains all the key-value pairs specified in the route's `metadata`
configuration will be selected. The metadata values in the route configuration
are arrays, and a service instance matches if its metadata value equals any
value in the corresponding array.
+
+Example: If a service instance has metadata `{lane: "a", env: "prod", version:
"1.0"}`, it will match routes configured with metadata `{lane: ["a"]}` or
`{lane: ["a"], env: ["prod"]}`, but not routes configured with `{lane: ["b"]}`
or `{lane: ["a"], region: ["us"]}`.
+
+Example of routing a request with metadata filtering:
+
+```shell
+$ curl http://127.0.0.1:9180/apisix/admin/routes/5 -H "X-API-KEY: $admin_key"
-X PUT -i -d '
+{
+ "uri": "/nacosWithMetadata/*",
+ "upstream": {
+ "service_name": "APISIX-NACOS",
+ "type": "roundrobin",
+ "discovery_type": "nacos",
+ "discovery_args": {
+ "metadata": {
+ "version": ["v1"]
Review Comment:
I considered your advice. The `string:string` structure is sufficient to
handle this scenario. Consul, Nacos, and Eureka support string:string metadata,
Kubernetes supports labels, and DNS supports TXT records. I'm not familiar with
TARS.
Additionally, if you plan to support string[] in the future, you could
implement a union type like `string|string[]`.
##########
apisix/discovery/nacos/init.lua:
##########
@@ -54,6 +56,25 @@ local function get_key(namespace_id, group_name,
service_name)
return namespace_id .. '.' .. group_name .. '.' .. service_name
end
+
+local function metadata_contains(host_metadata, route_metadata)
+ if not host_metadata or not next(host_metadata) then
+ return false
+ end
+
+ for k, v in pairs(route_metadata) do
+ if type(v) ~= "string" then
Review Comment:
Yes, this check is redundant.
##########
apisix/discovery/nacos/init.lua:
##########
@@ -54,6 +55,23 @@ local function get_key(namespace_id, group_name,
service_name)
return namespace_id .. '.' .. group_name .. '.' .. service_name
end
+
+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
Review Comment:
Ok, I'll. Should I put the separate file in `apisix/utils`?
##########
apisix/discovery/nacos/init.lua:
##########
@@ -54,6 +55,23 @@ local function get_key(namespace_id, group_name,
service_name)
return namespace_id .. '.' .. group_name .. '.' .. service_name
end
+
+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
Review Comment:
@SkyeYoung Where should I put the separate file?
##########
t/core/schema_def.t:
##########
@@ -255,64 +255,173 @@ passed
=== TEST 5: validate IPv6 address format in upstream nodes
--- config
-location /t {
Review Comment:
The indent here is wrong. But I shouldn't change it in this PR. I'll revert
it.
##########
ci/pod/nacos/service/Dockerfile:
##########
@@ -25,8 +25,8 @@ ENV GROUP=${GROUP:-DEFAULT_GROUP}
ADD
https://raw.githubusercontent.com/api7/nacos-test-service/main/spring-nacos-1.0-SNAPSHOT.jar
/app.jar
Review Comment:
Not related to this PR.
##########
apisix/discovery/nacos/init.lua:
##########
@@ -329,6 +344,13 @@ local function fetch_from_host(base_uri, username,
password, services)
port = host.port,
weight = host.weight or default_weight,
}
+ if host.metadata ~= nil then
+ if type(host.metadata) == 'table' then
+ node.metadata = host.metadata
+ else
+ log.error('nacos host metadata is not a table: ',
host.metadata)
+ end
Review Comment:
I fixed this by clearing the metadata field before returning the nodes.
##########
apisix/discovery/nacos/init.lua:
##########
@@ -54,6 +55,23 @@ local function get_key(namespace_id, group_name,
service_name)
return namespace_id .. '.' .. group_name .. '.' .. service_name
end
+
+local function metadata_contains(host_metadata, route_metadata)
Review Comment:
`host_metadata` refers to the information associated with endpoints, and
`route_metadata` represents the expected data for specific routes.
##########
apisix/discovery/nacos/init.lua:
##########
@@ -54,6 +55,23 @@ local function get_key(namespace_id, group_name,
service_name)
return namespace_id .. '.' .. group_name .. '.' .. service_name
end
+
+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
Review Comment:
@SkyeYoung @moonming Where should I put the separate file?
##########
apisix/discovery/nacos/init.lua:
##########
@@ -315,10 +331,12 @@ local function fetch_full_registry(premature)
local key = get_key(namespace_id, group_name,
service_info.service_name)
service_names[key] = true
for _, host in ipairs(data.hosts) do
+ assert(host.metadata == nil or type(host.metadata) == "table")
Review Comment:
Alright, I'll skip the metadata if it's not a 'table' and leave an error
message instead.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]