2016-03-20 21:36:55 +00:00
|
|
|
package etcd
|
|
|
|
|
|
|
|
|
|
import (
|
2018-04-22 08:34:35 +01:00
|
|
|
"context"
|
|
|
|
|
|
2017-09-14 09:36:06 +01:00
|
|
|
"github.com/coredns/coredns/plugin"
|
2017-02-21 22:51:47 -08:00
|
|
|
"github.com/coredns/coredns/request"
|
2016-03-22 22:44:50 +00:00
|
|
|
|
2016-03-20 21:36:55 +00:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
|
)
|
|
|
|
|
|
2017-09-14 09:36:06 +01:00
|
|
|
// ServeDNS implements the plugin.Handler interface.
|
2016-08-08 19:18:55 -07:00
|
|
|
func (e *Etcd) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
2017-09-14 09:36:06 +01:00
|
|
|
opt := plugin.Options{}
|
2019-03-26 14:37:30 +00:00
|
|
|
state := request.Request{W: w, Req: r}
|
2017-08-25 08:55:53 +01:00
|
|
|
|
2017-09-14 09:36:06 +01:00
|
|
|
zone := plugin.Zones(e.Zones).Matches(state.Name())
|
2016-03-22 11:04:56 +00:00
|
|
|
if zone == "" {
|
2017-09-14 09:36:06 +01:00
|
|
|
return plugin.NextOrFailure(e.Name(), e.Next, ctx, w, r)
|
2016-03-22 11:04:56 +00:00
|
|
|
}
|
2016-03-20 21:36:55 +00:00
|
|
|
|
2016-03-22 11:04:56 +00:00
|
|
|
var (
|
|
|
|
|
records, extra []dns.RR
|
2022-02-22 09:38:57 -05:00
|
|
|
truncated bool
|
2016-03-22 11:04:56 +00:00
|
|
|
err error
|
|
|
|
|
)
|
2017-12-07 22:31:13 -06:00
|
|
|
|
|
|
|
|
switch state.QType() {
|
|
|
|
|
case dns.TypeA:
|
2022-02-22 09:38:57 -05:00
|
|
|
records, truncated, err = plugin.A(ctx, e, zone, state, nil, opt)
|
2017-12-07 22:31:13 -06:00
|
|
|
case dns.TypeAAAA:
|
2022-02-22 09:38:57 -05:00
|
|
|
records, truncated, err = plugin.AAAA(ctx, e, zone, state, nil, opt)
|
2017-12-07 22:31:13 -06:00
|
|
|
case dns.TypeTXT:
|
2022-02-22 09:38:57 -05:00
|
|
|
records, truncated, err = plugin.TXT(ctx, e, zone, state, nil, opt)
|
2017-12-07 22:31:13 -06:00
|
|
|
case dns.TypeCNAME:
|
2019-03-26 14:37:30 +00:00
|
|
|
records, err = plugin.CNAME(ctx, e, zone, state, opt)
|
2017-12-07 22:31:13 -06:00
|
|
|
case dns.TypePTR:
|
2019-03-26 14:37:30 +00:00
|
|
|
records, err = plugin.PTR(ctx, e, zone, state, opt)
|
2017-12-07 22:31:13 -06:00
|
|
|
case dns.TypeMX:
|
2019-03-26 14:37:30 +00:00
|
|
|
records, extra, err = plugin.MX(ctx, e, zone, state, opt)
|
2017-12-07 22:31:13 -06:00
|
|
|
case dns.TypeSRV:
|
2019-03-26 14:37:30 +00:00
|
|
|
records, extra, err = plugin.SRV(ctx, e, zone, state, opt)
|
2017-12-07 22:31:13 -06:00
|
|
|
case dns.TypeSOA:
|
2019-03-26 14:37:30 +00:00
|
|
|
records, err = plugin.SOA(ctx, e, zone, state, opt)
|
2017-12-07 22:31:13 -06:00
|
|
|
case dns.TypeNS:
|
2016-04-13 08:03:56 +01:00
|
|
|
if state.Name() == zone {
|
2019-03-26 14:37:30 +00:00
|
|
|
records, extra, err = plugin.NS(ctx, e, zone, state, opt)
|
2016-04-13 08:03:56 +01:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
fallthrough
|
2016-03-22 10:29:48 +00:00
|
|
|
default:
|
Allow debug queries to etcd middleware (#150)
With this you can retreive the raw data that the etcd middleware
used to create the reply. The debug data is put in TXT records
that are stuffed in the CH classs. This is only enabled if you
specify `debug` in the etcd stanza.
You can retrieve it by prefixing your query with 'o-o.debug.'
For instance:
; <<>> DiG 9.10.3-P4-Ubuntu <<>> @localhost -p 1053 SRV o-o.debug.production.*.skydns.local
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 47798
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 3
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;o-o.debug.production.*.skydns.local. IN SRV
;; ANSWER SECTION:
production.*.skydns.local. 154 IN SRV 10 50 8080 service1.example.com.
production.*.skydns.local. 154 IN SRV 10 50 8080 service2.example.com.
;; ADDITIONAL SECTION:
skydns.local.skydns.east.production.rails.1. 154 CH TXT "service1.example.com:8080(10,0,,false)[0,]"
skydns.local.skydns.west.production.rails.2. 154 CH TXT "service2.example.com:8080(10,0,,false)[0,]"
2016-05-22 21:16:26 +01:00
|
|
|
// Do a fake A lookup, so we can distinguish between NODATA and NXDOMAIN
|
2022-02-22 09:38:57 -05:00
|
|
|
_, _, err = plugin.A(ctx, e, zone, state, nil, opt)
|
Allow debug queries to etcd middleware (#150)
With this you can retreive the raw data that the etcd middleware
used to create the reply. The debug data is put in TXT records
that are stuffed in the CH classs. This is only enabled if you
specify `debug` in the etcd stanza.
You can retrieve it by prefixing your query with 'o-o.debug.'
For instance:
; <<>> DiG 9.10.3-P4-Ubuntu <<>> @localhost -p 1053 SRV o-o.debug.production.*.skydns.local
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 47798
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 3
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;o-o.debug.production.*.skydns.local. IN SRV
;; ANSWER SECTION:
production.*.skydns.local. 154 IN SRV 10 50 8080 service1.example.com.
production.*.skydns.local. 154 IN SRV 10 50 8080 service2.example.com.
;; ADDITIONAL SECTION:
skydns.local.skydns.east.production.rails.1. 154 CH TXT "service1.example.com:8080(10,0,,false)[0,]"
skydns.local.skydns.west.production.rails.2. 154 CH TXT "service2.example.com:8080(10,0,,false)[0,]"
2016-05-22 21:16:26 +01:00
|
|
|
}
|
2018-06-30 20:49:13 +05:30
|
|
|
if err != nil && e.IsNameError(err) {
|
2018-01-07 16:32:59 +00:00
|
|
|
if e.Fall.Through(state.Name()) {
|
2017-09-14 09:36:06 +01:00
|
|
|
return plugin.NextOrFailure(e.Name(), e.Next, ctx, w, r)
|
2017-09-07 18:21:37 +01:00
|
|
|
}
|
2017-03-06 11:43:23 +00:00
|
|
|
// Make err nil when returning here, so we don't log spam for NXDOMAIN.
|
2019-03-26 14:37:30 +00:00
|
|
|
return plugin.BackendError(ctx, e, zone, dns.RcodeNameError, state, nil /* err */, opt)
|
2016-03-22 11:04:56 +00:00
|
|
|
}
|
|
|
|
|
if err != nil {
|
2019-03-26 14:37:30 +00:00
|
|
|
return plugin.BackendError(ctx, e, zone, dns.RcodeServerFailure, state, err, opt)
|
2016-03-20 21:36:55 +00:00
|
|
|
}
|
2016-03-23 22:59:05 +00:00
|
|
|
|
|
|
|
|
if len(records) == 0 {
|
2019-03-26 14:37:30 +00:00
|
|
|
return plugin.BackendError(ctx, e, zone, dns.RcodeSuccess, state, err, opt)
|
2016-03-22 11:04:56 +00:00
|
|
|
}
|
2016-03-25 10:32:12 +00:00
|
|
|
|
Allow debug queries to etcd middleware (#150)
With this you can retreive the raw data that the etcd middleware
used to create the reply. The debug data is put in TXT records
that are stuffed in the CH classs. This is only enabled if you
specify `debug` in the etcd stanza.
You can retrieve it by prefixing your query with 'o-o.debug.'
For instance:
; <<>> DiG 9.10.3-P4-Ubuntu <<>> @localhost -p 1053 SRV o-o.debug.production.*.skydns.local
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 47798
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 3
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;o-o.debug.production.*.skydns.local. IN SRV
;; ANSWER SECTION:
production.*.skydns.local. 154 IN SRV 10 50 8080 service1.example.com.
production.*.skydns.local. 154 IN SRV 10 50 8080 service2.example.com.
;; ADDITIONAL SECTION:
skydns.local.skydns.east.production.rails.1. 154 CH TXT "service1.example.com:8080(10,0,,false)[0,]"
skydns.local.skydns.west.production.rails.2. 154 CH TXT "service2.example.com:8080(10,0,,false)[0,]"
2016-05-22 21:16:26 +01:00
|
|
|
m := new(dns.Msg)
|
|
|
|
|
m.SetReply(r)
|
2022-02-22 09:38:57 -05:00
|
|
|
m.Truncated = truncated
|
2018-12-30 17:05:08 +01:00
|
|
|
m.Authoritative = true
|
2024-07-02 20:08:56 +08:00
|
|
|
m.Answer = records
|
|
|
|
|
m.Extra = extra
|
2016-04-09 16:17:53 +01:00
|
|
|
|
|
|
|
|
w.WriteMsg(m)
|
|
|
|
|
return dns.RcodeSuccess, nil
|
2016-03-22 10:29:48 +00:00
|
|
|
}
|
2016-03-20 21:36:55 +00:00
|
|
|
|
2016-10-27 11:48:37 +00:00
|
|
|
// Name implements the Handler interface.
|
2016-10-26 10:01:52 +01:00
|
|
|
func (e *Etcd) Name() string { return "etcd" }
|