From 8c707c803134c36c67d6deb2f6ac4929ee83256e Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Tue, 22 Mar 2016 22:44:50 +0000 Subject: [PATCH] more --- core/setup/etcd.go | 37 ++++++++++++++++++++++++++----------- middleware/etcd/etcd.go | 34 ++++++++++------------------------ middleware/etcd/handler.go | 4 ++++ middleware/zone.go | 15 ++++++++++++++- 4 files changed, 54 insertions(+), 36 deletions(-) diff --git a/core/setup/etcd.go b/core/setup/etcd.go index 68e150392..1423e71aa 100644 --- a/core/setup/etcd.go +++ b/core/setup/etcd.go @@ -10,38 +10,53 @@ import ( "github.com/miekg/coredns/middleware" "github.com/miekg/coredns/middleware/etcd" + "github.com/miekg/coredns/middleware/etcd/singleflight" + "github.com/miekg/coredns/middleware/proxy" etcdc "github.com/coreos/etcd/client" + "golang.org/x/net/context" ) const defaultEndpoint = "http://127.0.0.1:2379" // Etcd sets up the etcd middleware. func Etcd(c *Controller) (middleware.Middleware, error) { - client, err := etcdParse(c) + etcd, err := etcdParse(c) if err != nil { return nil, err } return func(next middleware.Handler) middleware.Handler { - return etcd.NewEtcd(client, next, c.ServerBlockHosts) + etcd.Next = next + return etcd }, nil } -func etcdParse(c *Controller) (etcdc.KeysAPI, error) { +func etcdParse(c *Controller) (etcd.Etcd, error) { + etc := etcd.Etcd{ + // make stuff configurable + Proxy: proxy.New([]string{"8.8.8.8:53"}), + PathPrefix: "skydns", + Ctx: context.Background(), + Inflight: &singleflight.Group{}, + } for c.Next() { if c.Val() == "etcd" { - // etcd [address...] - if !c.NextArg() { - // TODO(certs) and friends, this is client side - client, err := newEtcdClient([]string{defaultEndpoint}, "", "", "") - return client, err + // etcd [origin...] + client, err := newEtcdClient([]string{defaultEndpoint}, "", "", "") + if err != nil { + return etcd.Etcd{}, err } - client, err := newEtcdClient(c.RemainingArgs(), "", "", "") - return client, err + etc.Client = client + etc.Zones = c.RemainingArgs() + if len(etc.Zones) == 0 { + etc.Zones = c.ServerBlockHosts + } + middleware.Zones(etc.Zones).FullyQualify() + return etc, nil } } - return nil, nil + return etcd.Etcd{}, nil } func newEtcdClient(endpoints []string, tlsCert, tlsKey, tlsCACert string) (etcdc.KeysAPI, error) { diff --git a/middleware/etcd/etcd.go b/middleware/etcd/etcd.go index 20dff2506..d1e6bfadf 100644 --- a/middleware/etcd/etcd.go +++ b/middleware/etcd/etcd.go @@ -14,28 +14,14 @@ import ( "golang.org/x/net/context" ) -type ( - Etcd struct { - Next middleware.Handler - Zones []string - Proxy proxy.Proxy - client etcdc.KeysAPI - ctx context.Context - inflight *singleflight.Group - PathPrefix string - } -) - -func NewEtcd(client etcdc.KeysAPI, next middleware.Handler, zones []string) Etcd { - return Etcd{ - Next: next, - Zones: zones, - Proxy: proxy.New([]string{"8.8.8.8:53"}), - client: client, - ctx: context.Background(), - inflight: &singleflight.Group{}, - PathPrefix: "skydns", // TODO(miek): configurable - } +type Etcd struct { + Next middleware.Handler + Zones []string + Proxy proxy.Proxy + Client etcdc.KeysAPI + Ctx context.Context + Inflight *singleflight.Group + PathPrefix string } func (g Etcd) Records(name string, exact bool) ([]msg.Service, error) { @@ -57,8 +43,8 @@ func (g Etcd) Records(name string, exact bool) ([]msg.Service, error) { // 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) { - r, e := g.client.Get(g.ctx, path, &etcdc.GetOptions{Sort: false, Recursive: recursive}) + resp, err := g.Inflight.Do(path, func() (interface{}, error) { + r, e := g.Client.Get(g.Ctx, path, &etcdc.GetOptions{Sort: false, Recursive: recursive}) if e != nil { return nil, e } diff --git a/middleware/etcd/handler.go b/middleware/etcd/handler.go index 95f60c18e..cad0519ba 100644 --- a/middleware/etcd/handler.go +++ b/middleware/etcd/handler.go @@ -2,6 +2,7 @@ package etcd import ( "github.com/miekg/coredns/middleware" + "github.com/miekg/dns" "golang.org/x/net/context" ) @@ -35,6 +36,9 @@ func (e Etcd) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (i case "SRV": records, extra, err = e.SRV(zone, state) default: + // For SOA and NS we might still want this + // and use dns. as the name to put these + // also for stub // rwrite and return // Nodata response // also catch other types, so that they return NODATA diff --git a/middleware/zone.go b/middleware/zone.go index aa1171c28..13255411c 100644 --- a/middleware/zone.go +++ b/middleware/zone.go @@ -1,6 +1,10 @@ package middleware -import "strings" +import ( + "strings" + + "github.com/miekg/dns" +) type Zones []string @@ -11,6 +15,7 @@ func (z Zones) Matches(qname string) string { zone := "" // TODO(miek): use IsSubDomain here? for _, zname := range z { + println(zname, qname) if strings.HasSuffix(qname, zname) { if len(zname) > len(zone) { zone = zname @@ -19,3 +24,11 @@ func (z Zones) Matches(qname string) string { } return zone } + +// Fully qualify all zones in z +func (z Zones) FullyQualify() { + for i, _ := range z { + z[i] = dns.Fqdn(z[i]) + } + +}