Stop hardcoding things

Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
Miek Gieben
2020-01-16 11:09:34 +01:00
parent 0736ccb18e
commit 8582830b17
3 changed files with 75 additions and 21 deletions

View File

@@ -2,6 +2,8 @@ package traffic
import (
"context"
"strings"
"time"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/dnsutil"
@@ -13,9 +15,10 @@ import (
// Traffic is a plugin that load balances according to assignments.
type Traffic struct {
c *xds.Client
id string
Next plugin.Handler
c *xds.Client
id string
origins []string
Next plugin.Handler
}
// shutdown closes the connection to the managment endpoints and stops any running goroutines.
@@ -25,26 +28,71 @@ func (t *Traffic) shutdown() { t.c.Close() }
func (t *Traffic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := request.Request{Req: r, W: w}
cluster, _ := dnsutil.TrimZone(state.Name(), "example.org")
addr := t.c.Select(cluster)
if addr == nil {
cluster := ""
for _, o := range t.origins {
println(o, state.Name())
if strings.HasSuffix(state.Name(), o) {
cluster, _ = dnsutil.TrimZone(state.Name(), o)
state.Zone = o
break
}
}
if cluster == "" {
// TODO(miek): can this actually happen?
return plugin.NextOrFailure(t.Name(), t.Next, ctx, w, r)
}
log.Debugf("Found address %q for %q", addr, cluster)
addr := t.c.Select(cluster)
if addr != nil {
log.Debugf("Found endpoint %q for %q", addr, cluster)
} else {
log.Debugf("No healthy endpoints found for %q", cluster)
}
// assemble reply
m := new(dns.Msg)
m.SetReply(r)
m.Answer = []dns.RR{&dns.A{
Hdr: dns.RR_Header{Name: state.QName(), Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 5},
A: addr,
}}
if addr == nil {
m.Ns = soa(state.Zone)
w.WriteMsg(m)
return 0, nil
}
switch state.QType() {
case dns.TypeA:
if addr.To4() == nil { // it's an IPv6 address, return nodata in that case.
m.Ns = soa(state.Zone)
break
}
m.Answer = []dns.RR{&dns.A{Hdr: dns.RR_Header{Name: state.QName(), Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 5}, A: addr}}
case dns.TypeAAAA:
if addr.To4() != nil { // it's an IPv4 address, return nodata in that case.
m.Ns = soa(state.Zone)
break
}
m.Answer = []dns.RR{&dns.AAAA{Hdr: dns.RR_Header{Name: state.QName(), Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 5}, AAAA: addr}}
default:
m.Ns = soa(state.Zone)
}
w.WriteMsg(m)
return 0, nil
}
// soa returns a synthetic so for this zone.
func soa(z string) []dns.RR {
return []dns.RR{&dns.SOA{
Hdr: dns.RR_Header{Name: z, Rrtype: dns.TypeSOA, Class: dns.ClassINET, Ttl: 5},
Ns: dnsutil.Join("ns", z),
Mbox: dnsutil.Join("coredns", z),
Serial: uint32(time.Now().UTC().Unix()),
Refresh: 14400,
Retry: 3600,
Expire: 604800,
Minttl: 5,
}}
}
// Name implements the plugin.Handler interface.
func (t *Traffic) Name() string { return "traffic" }