plugin/rewrite: support opt-in metadata in regex name replacements (#8665)

* plugin/rewrite: support opt-in metadata in regex name replacements

Signed-off-by: houyuwushang <180804215+houyuwushang@users.noreply.github.com>

* test: separate CoreDNS and third-party imports

Signed-off-by: houyuwushang <180804215+houyuwushang@users.noreply.github.com>

---------

Signed-off-by: houyuwushang <180804215+houyuwushang@users.noreply.github.com>
This commit is contained in:
houyuwushang
2026-10-19 18:54:15 +08:00
committed by GitHub
parent 6db8bf8d80
commit 1550b867f7
4 changed files with 333 additions and 2 deletions

View File

@@ -46,6 +46,9 @@ e.g., to rewrite ANY queries to HINFO, use `rewrite type ANY HINFO`.
See below in the **Response Rewrites** section for further details.
Regex name rules also support `metadata` before the answer options to expand
request metadata in **TO**, as described in **Metadata in request names** below.
If you specify multiple rules and an incoming query matches multiple rules, the rewrite
will behave as follows:
@@ -111,6 +114,32 @@ The following example rewrites the `schmoogle.com` suffix to `google.com`.
rewrite name suffix .schmoogle.com. .google.com.
~~~
### Metadata in request names
Regex name rules can opt into request metadata substitution by adding `metadata`
immediately after **TO**, before any `answer` options:
~~~
metadata
rewrite name regex ^([^.]+)\.example\.$ {1}.{kubernetes/client-namespace}.svc.cluster.local. metadata answer auto
~~~
With the *kubernetes* plugin configured to provide client metadata (using
`pods verified`), this maps `web.example.` from namespace `team-a` to
`web.team-a.svc.cluster.local.`. `answer auto` maps response names back to
`web.example.`.
* `{0}` inserts the entire match and `{1}`, `{2}`, etc. insert capture groups.
* `{plugin/label}` inserts the value of that metadata label. Label names are case
sensitive; substituted DNS names are normalized to lower case.
* If a referenced label is absent or empty, the rule is skipped, including in
`stop` mode. Later rules or plugins can handle the original name.
* Substituted values are literal text, not further placeholders. An invalid
resulting DNS name returns SERVFAIL, as with other name rewrites.
This option applies only to the request's **TO** value. Existing rules without
`metadata` and explicit response rewrite patterns retain their current behavior.
### Response Rewrites
When rewriting incoming DNS requests' names (field `name`), CoreDNS re-writes