Commit 6e466d50 authored by Miek Gieben's avatar Miek Gieben Committed by Yong Tang

Remove dnsutil.Dedup (#1867)

Remove the code and remove the call in etcd and kubernetes handlers.
This does mean we should not add dups in the first place, which means
adding maps in backend_lookup to prevent dups from begin added.

This should cut down on the allocations because dnsutil.Dedup is very
expensive by converting everything to strings, we avoid doing that now.
parent 58d69913
...@@ -19,6 +19,8 @@ func A(b ServiceBackend, zone string, state request.Request, previousRecords []d ...@@ -19,6 +19,8 @@ func A(b ServiceBackend, zone string, state request.Request, previousRecords []d
return nil, err return nil, err
} }
dup := make(map[string]bool)
for _, serv := range services { for _, serv := range services {
what, ip := serv.HostType() what, ip := serv.HostType()
...@@ -67,10 +69,13 @@ func A(b ServiceBackend, zone string, state request.Request, previousRecords []d ...@@ -67,10 +69,13 @@ func A(b ServiceBackend, zone string, state request.Request, previousRecords []d
continue continue
case dns.TypeA: case dns.TypeA:
if _, ok := dup[serv.Host]; !ok {
dup[serv.Host] = true
records = append(records, serv.NewA(state.QName(), ip)) records = append(records, serv.NewA(state.QName(), ip))
}
case dns.TypeAAAA: case dns.TypeAAAA:
// nodata? // nada
} }
} }
return records, nil return records, nil
...@@ -83,6 +88,8 @@ func AAAA(b ServiceBackend, zone string, state request.Request, previousRecords ...@@ -83,6 +88,8 @@ func AAAA(b ServiceBackend, zone string, state request.Request, previousRecords
return nil, err return nil, err
} }
dup := make(map[string]bool)
for _, serv := range services { for _, serv := range services {
what, ip := serv.HostType() what, ip := serv.HostType()
...@@ -132,12 +139,15 @@ func AAAA(b ServiceBackend, zone string, state request.Request, previousRecords ...@@ -132,12 +139,15 @@ func AAAA(b ServiceBackend, zone string, state request.Request, previousRecords
// both here again // both here again
case dns.TypeA: case dns.TypeA:
// nada? // nada
case dns.TypeAAAA: case dns.TypeAAAA:
if _, ok := dup[serv.Host]; !ok {
dup[serv.Host] = true
records = append(records, serv.NewAAAA(state.QName(), ip)) records = append(records, serv.NewAAAA(state.QName(), ip))
} }
} }
}
return records, nil return records, nil
} }
...@@ -149,7 +159,18 @@ func SRV(b ServiceBackend, zone string, state request.Request, opt Options) (rec ...@@ -149,7 +159,18 @@ func SRV(b ServiceBackend, zone string, state request.Request, opt Options) (rec
return nil, nil, err return nil, nil, err
} }
// Looping twice to get the right weight vs priority type s struct {
n string
p uint16
}
dup := make(map[s]bool)
type a struct {
n string
a string
}
dupAddr := make(map[a]bool)
// Looping twice to get the right weight vs priority. This might break because we may drop duplicate SRV records latter on.
w := make(map[int]int) w := make(map[int]int)
for _, serv := range services { for _, serv := range services {
weight := 100 weight := 100
...@@ -212,13 +233,21 @@ func SRV(b ServiceBackend, zone string, state request.Request, opt Options) (rec ...@@ -212,13 +233,21 @@ func SRV(b ServiceBackend, zone string, state request.Request, opt Options) (rec
// IPv6 lookups here as well? AAAA(zone, state1, nil). // IPv6 lookups here as well? AAAA(zone, state1, nil).
case dns.TypeA, dns.TypeAAAA: case dns.TypeA, dns.TypeAAAA:
addr := serv.Host
serv.Host = msg.Domain(serv.Key) serv.Host = msg.Domain(serv.Key)
srv := serv.NewSRV(state.QName(), weight) srv := serv.NewSRV(state.QName(), weight)
if _, ok := dup[s{srv.Target, srv.Port}]; !ok {
dup[s{srv.Target, srv.Port}] = true
records = append(records, srv) records = append(records, srv)
}
if _, ok := dupAddr[a{srv.Target, addr}]; !ok {
dupAddr[a{srv.Target, addr}] = true
extra = append(extra, newAddress(serv, srv.Target, ip, what)) extra = append(extra, newAddress(serv, srv.Target, ip, what))
} }
} }
}
return records, extra, nil return records, extra, nil
} }
...@@ -229,6 +258,17 @@ func MX(b ServiceBackend, zone string, state request.Request, opt Options) (reco ...@@ -229,6 +258,17 @@ func MX(b ServiceBackend, zone string, state request.Request, opt Options) (reco
return nil, nil, err return nil, nil, err
} }
type s struct {
n string
p uint16
}
dup := make(map[s]bool)
type a struct {
n string
a string
}
dupAddr := make(map[a]bool)
lookup := make(map[string]bool) lookup := make(map[string]bool)
for _, serv := range services { for _, serv := range services {
if !serv.Mail { if !serv.Mail {
...@@ -271,11 +311,21 @@ func MX(b ServiceBackend, zone string, state request.Request, opt Options) (reco ...@@ -271,11 +311,21 @@ func MX(b ServiceBackend, zone string, state request.Request, opt Options) (reco
// e.AAAA as well // e.AAAA as well
case dns.TypeA, dns.TypeAAAA: case dns.TypeA, dns.TypeAAAA:
addr := serv.Host
serv.Host = msg.Domain(serv.Key) serv.Host = msg.Domain(serv.Key)
records = append(records, serv.NewMX(state.QName())) mx := serv.NewMX(state.QName())
if _, ok := dup[s{mx.Mx, mx.Preference}]; !ok {
dup[s{mx.Mx, mx.Preference}] = true
records = append(records, mx)
}
// Fake port to be 0 for address...
if _, ok := dupAddr[a{serv.Host, addr}]; !ok {
dupAddr[a{serv.Host, addr}] = true
extra = append(extra, newAddress(serv, serv.Host, ip, what)) extra = append(extra, newAddress(serv, serv.Host, ip, what))
} }
} }
}
return records, extra, nil return records, extra, nil
} }
...@@ -318,11 +368,16 @@ func PTR(b ServiceBackend, zone string, state request.Request, opt Options) (rec ...@@ -318,11 +368,16 @@ func PTR(b ServiceBackend, zone string, state request.Request, opt Options) (rec
return nil, err return nil, err
} }
dup := make(map[string]bool)
for _, serv := range services { for _, serv := range services {
if ip := net.ParseIP(serv.Host); ip == nil { if ip := net.ParseIP(serv.Host); ip == nil {
if _, ok := dup[serv.Host]; !ok {
dup[serv.Host] = true
records = append(records, serv.NewPTR(state.QName(), serv.Host)) records = append(records, serv.NewPTR(state.QName(), serv.Host))
} }
} }
}
return records, nil return records, nil
} }
......
...@@ -4,7 +4,6 @@ import ( ...@@ -4,7 +4,6 @@ import (
"context" "context"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/dnsutil"
"github.com/coredns/coredns/request" "github.com/coredns/coredns/request"
"github.com/miekg/dns" "github.com/miekg/dns"
...@@ -88,8 +87,6 @@ func (e *Etcd) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) ( ...@@ -88,8 +87,6 @@ func (e *Etcd) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (
m.Answer = append(m.Answer, records...) m.Answer = append(m.Answer, records...)
m.Extra = append(m.Extra, extra...) m.Extra = append(m.Extra, extra...)
// TODO(miek): get rid of this by not adding dups in the first place, dnsutil.Append()?
m = dnsutil.Dedup(m)
state.SizeAndDo(m) state.SizeAndDo(m)
m, _ = state.Scrub(m) m, _ = state.Scrub(m)
w.WriteMsg(m) w.WriteMsg(m)
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
package msg package msg
import ( import (
"fmt"
"net" "net"
"strings" "strings"
...@@ -37,36 +36,6 @@ type Service struct { ...@@ -37,36 +36,6 @@ type Service struct {
Key string `json:"-"` Key string `json:"-"`
} }
// RR returns an RR representation of s. It is in a condensed form to minimize space
// when this is returned in a DNS message.
// The RR will look like:
// 1.rails.production.east.skydns.local. 300 CH TXT "service1.example.com:8080(10,0,,false)[0,]"
// etcd Key Ttl Host:Port < see below >
// between parens: (Priority, Weight, Text (only first 200 bytes!), Mail)
// between blockquotes: [TargetStrip,Group]
// If the record is synthesised by CoreDNS (i.e. no lookup in etcd happened):
//
// TODO(miek): what to put here?
//
func (s *Service) RR() *dns.TXT {
l := len(s.Text)
if l > 200 {
l = 200
}
t := new(dns.TXT)
t.Hdr.Class = dns.ClassCHAOS
t.Hdr.Ttl = s.TTL
t.Hdr.Rrtype = dns.TypeTXT
t.Hdr.Name = Domain(s.Key)
t.Txt = make([]string, 1)
t.Txt[0] = fmt.Sprintf("%s:%d(%d,%d,%s,%t)[%d,%s]",
s.Host, s.Port,
s.Priority, s.Weight, s.Text[:l], s.Mail,
s.TargetStrip, s.Group)
return t
}
// NewSRV returns a new SRV record based on the Service. // NewSRV returns a new SRV record based on the Service.
func (s *Service) NewSRV(name string, weight uint16) *dns.SRV { func (s *Service) NewSRV(name string, weight uint16) *dns.SRV {
host := targetStrip(dns.Fqdn(s.Host), s.TargetStrip) host := targetStrip(dns.Fqdn(s.Host), s.TargetStrip)
......
...@@ -4,7 +4,6 @@ import ( ...@@ -4,7 +4,6 @@ import (
"context" "context"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/dnsutil"
"github.com/coredns/coredns/request" "github.com/coredns/coredns/request"
"github.com/miekg/dns" "github.com/miekg/dns"
...@@ -79,9 +78,6 @@ func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M ...@@ -79,9 +78,6 @@ func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M
m.Answer = append(m.Answer, records...) m.Answer = append(m.Answer, records...)
m.Extra = append(m.Extra, extra...) m.Extra = append(m.Extra, extra...)
// TODO(miek): get rid of this by not adding dups in the first place, dnsutil.Append()?
m = dnsutil.Dedup(m)
state.SizeAndDo(m) state.SizeAndDo(m)
m, _ = state.Scrub(m) m, _ = state.Scrub(m)
w.WriteMsg(m) w.WriteMsg(m)
......
...@@ -107,7 +107,7 @@ func (k *Kubernetes) Services(state request.Request, exact bool, opt plugin.Opti ...@@ -107,7 +107,7 @@ func (k *Kubernetes) Services(state request.Request, exact bool, opt plugin.Opti
return []msg.Service{svc}, nil return []msg.Service{svc}, nil
case dns.TypeNS: case dns.TypeNS:
// We can only get here if the qname equal the zone, see ServeDNS in handler.go. // We can only get here if the qname equals the zone, see ServeDNS in handler.go.
ns := k.nsAddr() ns := k.nsAddr()
svc := msg.Service{Host: ns.A.String(), Key: msg.Path(state.QName(), "coredns")} svc := msg.Service{Host: ns.A.String(), Key: msg.Path(state.QName(), "coredns")}
return []msg.Service{svc}, nil return []msg.Service{svc}, nil
......
package dnsutil
import "github.com/miekg/dns"
// Dedup de-duplicates a message.
func Dedup(m *dns.Msg) *dns.Msg {
// TODO(miek): expensive!
m.Answer = dns.Dedup(m.Answer, nil)
m.Ns = dns.Dedup(m.Ns, nil)
m.Extra = dns.Dedup(m.Extra, nil)
return m
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment