Files
coredns/middleware/etcd/etcd.go
Miek Gieben c30671f4c0 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

160 lines
4.2 KiB
Go

// Package etcd provides the etcd backend.
package etcd
import (
"encoding/json"
"strings"
"time"
"github.com/miekg/coredns/middleware"
"github.com/miekg/coredns/middleware/etcd/msg"
"github.com/miekg/coredns/middleware/proxy"
"github.com/miekg/coredns/middleware/singleflight"
etcdc "github.com/coreos/etcd/client"
"golang.org/x/net/context"
)
type Etcd struct {
Next middleware.Handler
Zones []string
PathPrefix string
Proxy proxy.Proxy // Proxy for looking up names during the resolution process
Client etcdc.KeysAPI
Ctx context.Context
Inflight *singleflight.Group
Stubmap *map[string]proxy.Proxy // List of proxies for stub resolving.
Debug bool // Do we allow debug queries.
debug string // Should we return debugging information, if so, contains original qname.
}
// Records looks up records in etcd. If exact is true, it will lookup just
// this name. This is used when find matches when completing SRV lookups
// for instance.
func (g Etcd) Records(name string, exact bool) ([]msg.Service, error) {
path, star := g.PathWithWildcard(name)
r, err := g.Get(path, true)
if err != nil {
return nil, err
}
segments := strings.Split(g.Path(name), "/")
switch {
case exact && r.Node.Dir:
return nil, nil
case r.Node.Dir:
return g.loopNodes(r.Node.Nodes, segments, star, nil)
default:
return g.loopNodes([]*etcdc.Node{r.Node}, segments, false, nil)
}
}
// Get is a wrapper for client.Get that uses SingleInflight to suppress multiple outstanding queries.
func (g Etcd) Get(path string, recursive bool) (*etcdc.Response, error) {
resp, err := g.Inflight.Do(path, func() (interface{}, error) {
ctx, cancel := context.WithTimeout(g.Ctx, etcdTimeout)
defer cancel()
r, e := g.Client.Get(ctx, path, &etcdc.GetOptions{Sort: false, Recursive: recursive})
if e != nil {
return nil, e
}
return r, e
})
if err != nil {
return nil, err
}
return resp.(*etcdc.Response), err
}
// skydns/local/skydns/east/staging/web
// skydns/local/skydns/west/production/web
//
// skydns/local/skydns/*/*/web
// skydns/local/skydns/*/web
// loopNodes recursively loops through the nodes and returns all the values. The nodes' keyname
// will be match against any wildcards when star is true.
func (g Etcd) loopNodes(ns []*etcdc.Node, nameParts []string, star bool, bx map[msg.Service]bool) (sx []msg.Service, err error) {
if bx == nil {
bx = make(map[msg.Service]bool)
}
Nodes:
for _, n := range ns {
if n.Dir {
nodes, err := g.loopNodes(n.Nodes, nameParts, star, bx)
if err != nil {
return nil, err
}
sx = append(sx, nodes...)
continue
}
if star {
keyParts := strings.Split(n.Key, "/")
for i, n := range nameParts {
if i > len(keyParts)-1 {
// name is longer than key
continue Nodes
}
if n == "*" || n == "any" {
continue
}
if keyParts[i] != n {
continue Nodes
}
}
}
serv := new(msg.Service)
if err := json.Unmarshal([]byte(n.Value), serv); err != nil {
return nil, err
}
b := msg.Service{Host: serv.Host, Port: serv.Port, Priority: serv.Priority, Weight: serv.Weight, Text: serv.Text, Key: n.Key}
if _, ok := bx[b]; ok {
continue
}
bx[b] = true
serv.Key = n.Key
serv.Ttl = g.Ttl(n, serv)
if serv.Priority == 0 {
serv.Priority = priority
}
sx = append(sx, *serv)
}
return sx, nil
}
// Ttl returns the smaller of the etcd TTL and the service's
// TTL. If neither of these are set (have a zero value), a default is used.
func (g Etcd) Ttl(node *etcdc.Node, serv *msg.Service) uint32 {
etcdTtl := uint32(node.TTL)
if etcdTtl == 0 && serv.Ttl == 0 {
return ttl
}
if etcdTtl == 0 {
return serv.Ttl
}
if serv.Ttl == 0 {
return etcdTtl
}
if etcdTtl < serv.Ttl {
return etcdTtl
}
return serv.Ttl
}
// etcNameError checks if the error is ErrorCodeKeyNotFound from etcd.
func isEtcdNameError(err error) bool {
if e, ok := err.(etcdc.Error); ok && e.Code == etcdc.ErrorCodeKeyNotFound {
return true
}
return false
}
const (
priority = 10 // default priority when nothing is set
ttl = 300 // default ttl when nothing is set
minTtl = 60
hostmaster = "hostmaster"
etcdTimeout = 5 * time.Second
)