plugin/template: README tweaks (#1361)

* plugin/template: README tweaks

* Go gen it
This commit is contained in:
Miek Gieben 2018-01-08 13:13:25 +00:00 committed by GitHub
parent a322d90f6f
commit dd37627e8e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 13 deletions

View file

@ -29,6 +29,7 @@ var Directives = []string{
"dnssec", "dnssec",
"autopath", "autopath",
"reverse", "reverse",
"template",
"hosts", "hosts",
"federation", "federation",
"kubernetes", "kubernetes",

View file

@ -30,6 +30,7 @@ import (
_ "github.com/coredns/coredns/plugin/rewrite" _ "github.com/coredns/coredns/plugin/rewrite"
_ "github.com/coredns/coredns/plugin/root" _ "github.com/coredns/coredns/plugin/root"
_ "github.com/coredns/coredns/plugin/secondary" _ "github.com/coredns/coredns/plugin/secondary"
_ "github.com/coredns/coredns/plugin/template"
_ "github.com/coredns/coredns/plugin/tls" _ "github.com/coredns/coredns/plugin/tls"
_ "github.com/coredns/coredns/plugin/trace" _ "github.com/coredns/coredns/plugin/trace"
_ "github.com/coredns/coredns/plugin/whoami" _ "github.com/coredns/coredns/plugin/whoami"

View file

@ -10,20 +10,20 @@ The *template* plugin allows you to dynamically repond to queries by just writin
~~~ ~~~
template CLASS TYPE [REGEX...] { template CLASS TYPE [REGEX...] {
[answer RR]
[answer RR] [answer RR]
[additional RR] [additional RR]
[authority RR] [authority RR]
[...] [...]
[rcode responsecode] [rcode CODE]
} }
~~~ ~~~
* **CLASS** the query class (usually IN or ANY) * **CLASS** the query class (usually IN or ANY)
* **TYPE** the query type (A, PTR, ...) * **TYPE** the query type (A, PTR, ...)
* **REGEX** [Go regexp](https://golang.org/pkg/regexp/) that are matched against the incoming question name. Specifying no regex matches everything (default: `.*`). First matching regex wins. * **REGEX** [Go regexp](https://golang.org/pkg/regexp/) that are matched against the incoming question name. Specifying no regex matches everything (default: `.*`). First matching regex wins.
* `RR` A [RFC 1035](https://tools.ietf.org/html/rfc1035#section-5) style `<rr>` fragment build by a [Go template](https://golang.org/pkg/text/template/) that contains the answer. * `answer|additional|authority` **RR** A [RFC 1035](https://tools.ietf.org/html/rfc1035#section-5) style resource record fragment
* `responsecode` A response code (`NXDOMAIN, SERVFAIL, ...`). The default is `SUCCESS`. build by a [Go template](https://golang.org/pkg/text/template/) that contains the reply.
* `rcode` **CODE** A response code (`NXDOMAIN, SERVFAIL, ...`). The default is `SUCCESS`.
At least one answer section or rcode is needed. At least one answer section or rcode is needed.
@ -32,17 +32,19 @@ At least one answer section or rcode is needed.
## Templates ## Templates
Each resource record is a full-featured [Go template](https://golang.org/pkg/text/template/) with the following predefined data Each resource record is a full-featured [Go template](https://golang.org/pkg/text/template/) with the following predefined data
* `.Name` the query name, as a string * `.Name` the query name, as a string (lowercased).
* `.Class` the query class (usually `IN`) * `.Class` the query class (usually `IN`).
* `.Type` the RR type requested (e.g. `PTR`) * `.Type` the RR type requested (e.g. `PTR`).
* `.Match` an array of all matches. `index .Match 0` refers to the whole match. * `.Match` an array of all matches. `index .Match 0` refers to the whole match.
* `.Group` a map of the named capture groups. * `.Group` a map of the named capture groups.
* `.Message` the incoming DNS query message. * `.Message` the complete incoming DNS message.
* `.Question` the matched question section. * `.Question` the matched question section.
The output of the template must be a [RFC 1035](https://tools.ietf.org/html/rfc1035) style resource record line (commonly refered to as a "zone file"). The output of the template must be a [RFC 1035](https://tools.ietf.org/html/rfc1035) style resource record line (commonly refered to as a "zone file").
**WARNING** there is a syntactical problem with Go templates and caddy config files. Expressions like `{{$var}}` will be interpreted as a reference to an environment variable by caddy/coredns while `{{ $var }}` will work. Try to avoid template variables. See [Bugs](#bugs). **WARNING** there is a syntactical problem with Go templates and CoreDNS config files. Expressions
like `{{$var}}` will be interpreted as a reference to an environment variable by CoreDNS (and
Caddy) while `{{ $var }}` will work. See [Bugs](#bugs) and corefile(5).
## Metrics ## Metrics
@ -93,7 +95,7 @@ path (`dc1.example.com`) added.
} }
~~~ ~~~
1. Using numbered matches works well if there are very few groups (1-4) Using numbered matches works well if there are a few groups (1-4).
### Resolve A/PTR for .example ### Resolve A/PTR for .example

View file

@ -91,9 +91,7 @@ func (h Handler) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
} }
// Name implements the plugin.Handler interface. // Name implements the plugin.Handler interface.
func (h Handler) Name() string { func (h Handler) Name() string { return "template" }
return "template"
}
func executeRRTemplate(section string, template *gotmpl.Template, data templateData) (dns.RR, error) { func executeRRTemplate(section string, template *gotmpl.Template, data templateData) (dns.RR, error) {
buffer := &bytes.Buffer{} buffer := &bytes.Buffer{}