You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "AlinsRan (via GitHub)" <gi...@apache.org> on 2026/09/29 06:56:24 UTC
[PR] feat(kubernetes): select clusters by discovery_args.cluster_ids [apisix]
AlinsRan opened a new pull request, #13991:
URL: https://github.com/apache/apisix/pull/13991
### Description
In multi-cluster mode, Kubernetes service discovery can only resolve a `service_name` against one cluster (`id/namespace/name:port_name`). This PR lets an upstream pick several clusters explicitly and get the union of their endpoints:
```json
{
"discovery_type": "kubernetes",
"service_name": "default/plat-dev:port",
"discovery_args": {
"cluster_ids": ["release", "staging"]
}
}
```
Behaviour:
- The nodes are the union of the matching endpoints in the listed clusters (an `id` from the `discovery.kubernetes` array), cached per cluster endpoint version, so endpoint changes in those clusters keep refreshing the upstream. The same `host:port` found in more than one cluster is used once.
- Clusters that are not listed, including clusters added to the configuration later, never contribute nodes.
- If none of the listed clusters has matching endpoints, the upstream goes through the existing "no valid upstream node" path. There is no fallback to other clusters.
- An unknown cluster id is logged as an error and skipped at runtime. The Admin API has no hook that validates `discovery_args` against the loaded discovery configuration (`check_upstream_conf` only runs the schema and field-level checks, and the discovery modules only expose `nodes` / `init_worker` / `dump_data`), so it is not checked when the configuration is submitted.
- With `cluster_ids`, `service_name` must not carry a cluster id prefix. The Admin API rejects `cluster_id/namespace/name:port_name` in that case, and the discovery module logs an error and returns no nodes if such a configuration arrives another way.
- In single-cluster mode, `cluster_ids` has no cluster to match, so the upstream gets no nodes and an error is logged.
- Without `cluster_ids`, the existing behaviour is unchanged (`namespace/name:port_name` in single-cluster mode, `id/namespace/name:port_name` in multi-cluster mode).
`cluster_ids` is added to `discovery_args` in the upstream schema next to the existing `namespace_id` / `group_name`, as a non-empty array of unique strings.
Tests: `t/kubernetes/discovery/kubernetes5.t` covers selection against the kind cluster (listed vs unlisted clusters, unknown ids, endpoint updates), the union and dedup with fixed endpoint data, a proxied request through a route with `cluster_ids`, single-cluster mode, and the Admin API validation.
#### Which issue(s) this PR fixes:
Fixes #
### Checklist
- [x] I have explained the need for this PR and the problem it solves
- [x] I have explained the changes or the new features added to this PR
- [x] I have added tests corresponding to this change
- [x] I have updated the documentation to reflect this change
- [x] I have verified that this change is backward compatible (If not, please discuss on the [APISIX mailing list](https://github.com/apache/apisix/tree/master#community) first)
--
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: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(kubernetes): select clusters by discovery_args.cluster_ids [apisix]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #13991:
URL: https://github.com/apache/apisix/pull/13991#discussion_r4131657017
##########
apisix/discovery/kubernetes/init.lua:
##########
@@ -154,7 +161,86 @@ local function multiple_mode_init(confs)
end
-local function multiple_mode_nodes(service_name)
+local function merge_cluster_nodes(endpoint_dicts, endpoint_key, endpoint_port)
+ local nodes = {}
+ local seen = {}
+ for _, endpoint_dict in ipairs(endpoint_dicts) do
+ local cluster_nodes = k8s_core.create_endpoint_lrucache(endpoint_dict, endpoint_key,
+ endpoint_port)
+ for _, node in ipairs(cluster_nodes or {}) do
+ local addr = node.host .. ":" .. tostring(node.port)
+ if not seen[addr] then
+ seen[addr] = true
+ core.table.insert(nodes, node)
+ end
+ end
+ end
+
+ return nodes
+end
+
+
+-- with cluster_ids, service_name is "namespace/name:port_name"
+local cluster_ids_service_name_pattern = [[^([^/]+/[^/:]+):(.+)$]]
Review Comment:
This pattern still accepts malformed values such as `ns:extra/svc:p1` and `ns/svc:p1:extra`, so the Admin API accepts them even though they do not match the documented `namespace/name:port_name` shape; they then fail later as missing endpoints. Exclude both delimiters from every component so these configurations are rejected during validation.
--
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: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(kubernetes): select clusters by discovery_args.cluster_ids [apisix]
Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan merged PR #13991:
URL: https://github.com/apache/apisix/pull/13991
--
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: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(kubernetes): select clusters by discovery_args.cluster_ids [apisix]
Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on code in PR #13991:
URL: https://github.com/apache/apisix/pull/13991#discussion_r4131674364
##########
apisix/discovery/kubernetes/init.lua:
##########
@@ -154,7 +161,86 @@ local function multiple_mode_init(confs)
end
-local function multiple_mode_nodes(service_name)
+local function merge_cluster_nodes(endpoint_dicts, endpoint_key, endpoint_port)
+ local nodes = {}
+ local seen = {}
+ for _, endpoint_dict in ipairs(endpoint_dicts) do
+ local cluster_nodes = k8s_core.create_endpoint_lrucache(endpoint_dict, endpoint_key,
+ endpoint_port)
+ for _, node in ipairs(cluster_nodes or {}) do
+ local addr = node.host .. ":" .. tostring(node.port)
+ if not seen[addr] then
+ seen[addr] = true
+ core.table.insert(nodes, node)
+ end
+ end
+ end
+
+ return nodes
+end
+
+
+-- with cluster_ids, service_name is "namespace/name:port_name"
+local cluster_ids_service_name_pattern = [[^([^/]+/[^/:]+):(.+)$]]
Review Comment:
Fixed in b2d7ddbc87a02afebf8cf69c5149b16a7cf36385. The shared `cluster_ids` pattern is now `^([^/:]+/[^/:]+):([^/:]+)$`, so `:` and `/` are rejected in the namespace, name and port name segments. `ns:extra/svc:p1` and `ns/svc:p1:extra` were added as test cases.
--
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: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org