plugin/template: small tweaks (#1366)

Small README updates, and fallthrough fixes (using less code)
This commit is contained in:
Miek Gieben 2018-01-09 21:48:32 +00:00 committed by GitHub
parent 0091e1c9dc
commit a19ea63d3a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 51 deletions

View file

@ -15,8 +15,8 @@ template CLASS TYPE [ZONE...] {
[additional RR] [additional RR]
[authority RR] [authority RR]
[...] [...]
[rcode responsecode] [rcode CODE]
[fallthrough [fallthrough zone...]] [fallthrough [ZONE...]]
} }
~~~ ~~~
@ -28,7 +28,8 @@ template CLASS TYPE [ZONE...] {
build by a [Go template](https://golang.org/pkg/text/template/) that contains the reply. 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`. * `rcode` **CODE** A response code (`NXDOMAIN, SERVFAIL, ...`). The default is `SUCCESS`.
* `fallthrough` Continue with the next plugin if the zone matched but no regex did not match. * `fallthrough` Continue with the next plugin if the zone matched but no regex did not match.
* `fallthrough zone` One or more zones that may fall through to other plugins. Defaults to all zones of the template. If specific zones are listed (for example `in-addr.arpa` and `ip6.arpa`), then only queries for
those zones will be subject to fallthrough.
At least one `answer` or `rcode` directive is needed (e.g. `rcode NXDOMAIN`). At least one `answer` or `rcode` directive is needed (e.g. `rcode NXDOMAIN`).

View file

@ -6,8 +6,8 @@ import (
"github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics" "github.com/coredns/coredns/plugin/metrics"
"github.com/mholt/caddy"
"github.com/mholt/caddy"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )

View file

@ -140,12 +140,7 @@ func templateParse(c *caddy.Controller) (handler Handler, err error) {
t.rcode = rcode t.rcode = rcode
case "fallthrough": case "fallthrough":
args := c.RemainingArgs() t.fall.SetZonesFromArgs(c.RemainingArgs())
if len(args) > 0 {
t.fthrough.SetZonesFromArgs(c.RemainingArgs())
} else {
t.fthrough.SetZonesFromArgs(zones)
}
default: default:
return handler, c.ArgErr() return handler, c.ArgErr()

View file

@ -31,7 +31,7 @@ type template struct {
authority []*gotmpl.Template authority []*gotmpl.Template
qclass uint16 qclass uint16
qtype uint16 qtype uint16
fthrough fall.F fall fall.F
} }
type templateData struct { type templateData struct {
@ -177,5 +177,5 @@ func (t template) match(state request.Request, zone string) (templateData, bool,
return data, true, false return data, true, false
} }
return data, false, t.fthrough.Through(state.Name()) return data, false, t.fall.Through(state.Name())
} }

View file

@ -5,26 +5,25 @@ import (
"fmt" "fmt"
"regexp" "regexp"
"testing" "testing"
"github.com/coredns/coredns/plugin/test"
"github.com/mholt/caddy"
gotmpl "text/template" gotmpl "text/template"
"github.com/coredns/coredns/plugin/pkg/dnstest" "github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/pkg/fall" "github.com/coredns/coredns/plugin/pkg/fall"
"github.com/coredns/coredns/plugin/test"
"github.com/mholt/caddy"
"github.com/miekg/dns" "github.com/miekg/dns"
) )
func TestHandler(t *testing.T) { func TestHandler(t *testing.T) {
rcodeFallthrough := 3841 // reserved for private use, used to indicate a fallthrough rcodeFallthrough := 3841 // reserved for private use, used to indicate a fallthrough
exampleDomainATemplate := template{ exampleDomainATemplate := template{
regex: []*regexp.Regexp{regexp.MustCompile("(^|[.])ip-10-(?P<b>[0-9]*)-(?P<c>[0-9]*)-(?P<d>[0-9]*)[.]example[.]$")}, regex: []*regexp.Regexp{regexp.MustCompile("(^|[.])ip-10-(?P<b>[0-9]*)-(?P<c>[0-9]*)-(?P<d>[0-9]*)[.]example[.]$")},
answer: []*gotmpl.Template{gotmpl.Must(gotmpl.New("answer").Parse("{{ .Name }} 60 IN A 10.{{ .Group.b }}.{{ .Group.c }}.{{ .Group.d }}"))}, answer: []*gotmpl.Template{gotmpl.Must(gotmpl.New("answer").Parse("{{ .Name }} 60 IN A 10.{{ .Group.b }}.{{ .Group.c }}.{{ .Group.d }}"))},
qclass: dns.ClassANY, qclass: dns.ClassANY,
qtype: dns.TypeANY, qtype: dns.TypeANY,
fthrough: fall.F{Zones: []string{"."}}, fall: fall.F{Zones: []string{"."}},
zones: []string{"."}, zones: []string{"."},
} }
exampleDomainANSTemplate := template{ exampleDomainANSTemplate := template{
regex: []*regexp.Regexp{regexp.MustCompile("(^|[.])ip-10-(?P<b>[0-9]*)-(?P<c>[0-9]*)-(?P<d>[0-9]*)[.]example[.]$")}, regex: []*regexp.Regexp{regexp.MustCompile("(^|[.])ip-10-(?P<b>[0-9]*)-(?P<c>[0-9]*)-(?P<d>[0-9]*)[.]example[.]$")},
@ -33,7 +32,7 @@ func TestHandler(t *testing.T) {
authority: []*gotmpl.Template{gotmpl.Must(gotmpl.New("authority").Parse("example. IN NS ns0.example.com."))}, authority: []*gotmpl.Template{gotmpl.Must(gotmpl.New("authority").Parse("example. IN NS ns0.example.com."))},
qclass: dns.ClassANY, qclass: dns.ClassANY,
qtype: dns.TypeANY, qtype: dns.TypeANY,
fthrough: fall.F{Zones: []string{"."}}, fall: fall.F{Zones: []string{"."}},
zones: []string{"."}, zones: []string{"."},
} }
exampleDomainMXTemplate := template{ exampleDomainMXTemplate := template{
@ -42,48 +41,48 @@ func TestHandler(t *testing.T) {
additional: []*gotmpl.Template{gotmpl.Must(gotmpl.New("additional").Parse("{{ .Name }} 60 IN A 10.{{ .Group.b }}.{{ .Group.c }}.{{ .Group.d }}"))}, additional: []*gotmpl.Template{gotmpl.Must(gotmpl.New("additional").Parse("{{ .Name }} 60 IN A 10.{{ .Group.b }}.{{ .Group.c }}.{{ .Group.d }}"))},
qclass: dns.ClassANY, qclass: dns.ClassANY,
qtype: dns.TypeANY, qtype: dns.TypeANY,
fthrough: fall.F{Zones: []string{"."}}, fall: fall.F{Zones: []string{"."}},
zones: []string{"."}, zones: []string{"."},
} }
invalidDomainTemplate := template{ invalidDomainTemplate := template{
regex: []*regexp.Regexp{regexp.MustCompile("[.]invalid[.]$")}, regex: []*regexp.Regexp{regexp.MustCompile("[.]invalid[.]$")},
rcode: dns.RcodeNameError, rcode: dns.RcodeNameError,
answer: []*gotmpl.Template{gotmpl.Must(gotmpl.New("answer").Parse("invalid. 60 {{ .Class }} SOA a.invalid. b.invalid. (1 60 60 60 60)"))}, answer: []*gotmpl.Template{gotmpl.Must(gotmpl.New("answer").Parse("invalid. 60 {{ .Class }} SOA a.invalid. b.invalid. (1 60 60 60 60)"))},
qclass: dns.ClassANY, qclass: dns.ClassANY,
qtype: dns.TypeANY, qtype: dns.TypeANY,
fthrough: fall.F{Zones: []string{"."}}, fall: fall.F{Zones: []string{"."}},
zones: []string{"."}, zones: []string{"."},
} }
rcodeServfailTemplate := template{ rcodeServfailTemplate := template{
regex: []*regexp.Regexp{regexp.MustCompile(".*")}, regex: []*regexp.Regexp{regexp.MustCompile(".*")},
rcode: dns.RcodeServerFailure, rcode: dns.RcodeServerFailure,
qclass: dns.ClassANY, qclass: dns.ClassANY,
qtype: dns.TypeANY, qtype: dns.TypeANY,
fthrough: fall.F{Zones: []string{"."}}, fall: fall.F{Zones: []string{"."}},
zones: []string{"."}, zones: []string{"."},
} }
brokenTemplate := template{ brokenTemplate := template{
regex: []*regexp.Regexp{regexp.MustCompile("[.]example[.]$")}, regex: []*regexp.Regexp{regexp.MustCompile("[.]example[.]$")},
answer: []*gotmpl.Template{gotmpl.Must(gotmpl.New("answer").Parse("{{ .Name }} 60 IN TXT \"{{ index .Match 2 }}\""))}, answer: []*gotmpl.Template{gotmpl.Must(gotmpl.New("answer").Parse("{{ .Name }} 60 IN TXT \"{{ index .Match 2 }}\""))},
qclass: dns.ClassANY, qclass: dns.ClassANY,
qtype: dns.TypeANY, qtype: dns.TypeANY,
fthrough: fall.F{Zones: []string{"."}}, fall: fall.F{Zones: []string{"."}},
zones: []string{"."}, zones: []string{"."},
} }
nonRRTemplate := template{ nonRRTemplate := template{
regex: []*regexp.Regexp{regexp.MustCompile("[.]example[.]$")}, regex: []*regexp.Regexp{regexp.MustCompile("[.]example[.]$")},
answer: []*gotmpl.Template{gotmpl.Must(gotmpl.New("answer").Parse("{{ .Name }}"))}, answer: []*gotmpl.Template{gotmpl.Must(gotmpl.New("answer").Parse("{{ .Name }}"))},
qclass: dns.ClassANY, qclass: dns.ClassANY,
qtype: dns.TypeANY, qtype: dns.TypeANY,
fthrough: fall.F{Zones: []string{"."}}, fall: fall.F{Zones: []string{"."}},
zones: []string{"."}, zones: []string{"."},
} }
nonRRAdditionalTemplate := template{ nonRRAdditionalTemplate := template{
regex: []*regexp.Regexp{regexp.MustCompile("[.]example[.]$")}, regex: []*regexp.Regexp{regexp.MustCompile("[.]example[.]$")},
additional: []*gotmpl.Template{gotmpl.Must(gotmpl.New("answer").Parse("{{ .Name }}"))}, additional: []*gotmpl.Template{gotmpl.Must(gotmpl.New("answer").Parse("{{ .Name }}"))},
qclass: dns.ClassANY, qclass: dns.ClassANY,
qtype: dns.TypeANY, qtype: dns.TypeANY,
fthrough: fall.F{Zones: []string{"."}}, fall: fall.F{Zones: []string{"."}},
zones: []string{"."}, zones: []string{"."},
} }
nonRRAuthoritativeTemplate := template{ nonRRAuthoritativeTemplate := template{
@ -91,7 +90,7 @@ func TestHandler(t *testing.T) {
authority: []*gotmpl.Template{gotmpl.Must(gotmpl.New("authority").Parse("{{ .Name }}"))}, authority: []*gotmpl.Template{gotmpl.Must(gotmpl.New("authority").Parse("{{ .Name }}"))},
qclass: dns.ClassANY, qclass: dns.ClassANY,
qtype: dns.TypeANY, qtype: dns.TypeANY,
fthrough: fall.F{Zones: []string{"."}}, fall: fall.F{Zones: []string{"."}},
zones: []string{"."}, zones: []string{"."},
} }