mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 16:24:19 -04:00
@@ -42,7 +42,7 @@ traffic TO...
|
|||||||
The extended syntax is available is you want more control.
|
The extended syntax is available is you want more control.
|
||||||
|
|
||||||
~~~
|
~~~
|
||||||
traffic {
|
traffic TO... {
|
||||||
server SERVER [SERVER]...
|
server SERVER [SERVER]...
|
||||||
node ID
|
node ID
|
||||||
}
|
}
|
||||||
@@ -60,9 +60,11 @@ names. For example if the Server Block specifies `lb.example.org` as one of the
|
|||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
~~~ corefile
|
~~~
|
||||||
lb.example.org {
|
lb.example.org {
|
||||||
traffic grpc://127.0.0.1:18000
|
traffic grpc://127.0.0.1:18000 {
|
||||||
|
node test-id
|
||||||
|
}
|
||||||
debug
|
debug
|
||||||
log
|
log
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,8 +55,14 @@ func setup(c *caddy.Controller) error {
|
|||||||
func parseTraffic(c *caddy.Controller) (*Traffic, error) {
|
func parseTraffic(c *caddy.Controller) (*Traffic, error) {
|
||||||
node := "coredns"
|
node := "coredns"
|
||||||
toHosts := []string{}
|
toHosts := []string{}
|
||||||
|
t := &Traffic{}
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
t.origins = make([]string, len(c.ServerBlockKeys))
|
||||||
|
for i := range c.ServerBlockKeys {
|
||||||
|
t.origins[i] = plugin.Host(c.ServerBlockKeys[i]).Normalize()
|
||||||
|
}
|
||||||
|
|
||||||
for c.Next() {
|
for c.Next() {
|
||||||
args := c.RemainingArgs()
|
args := c.RemainingArgs()
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
@@ -70,8 +76,8 @@ func parseTraffic(c *caddy.Controller) (*Traffic, error) {
|
|||||||
if !strings.HasPrefix(toHosts[i], transport.GRPC+"://") {
|
if !strings.HasPrefix(toHosts[i], transport.GRPC+"://") {
|
||||||
return nil, fmt.Errorf("not a %s scheme: %s", transport.GRPC, toHosts[i])
|
return nil, fmt.Errorf("not a %s scheme: %s", transport.GRPC, toHosts[i])
|
||||||
}
|
}
|
||||||
// now cut the prefix off again, because the dialer needs to see normal address strings. All this
|
// now cut the prefix off again, because the dialler needs to see normal address strings. All this
|
||||||
// grpc:// stuff is to enfore uniformaty accross plugins and future proofing for other protocols.
|
// grpc:// stuff is to enforce uniform across plugins and future proofing for other protocols.
|
||||||
toHosts[i] = toHosts[i][len(transport.GRPC+"://"):]
|
toHosts[i] = toHosts[i][len(transport.GRPC+"://"):]
|
||||||
}
|
}
|
||||||
for c.NextBlock() {
|
for c.NextBlock() {
|
||||||
@@ -88,12 +94,10 @@ func parseTraffic(c *caddy.Controller) (*Traffic, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: only the first host is used.
|
// TODO: only the first host is used, need to figure out how to reconcile multiple upstream providers.
|
||||||
x, err := xds.New(toHosts[0], node)
|
if t.c, err = xds.New(toHosts[0], node); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
t := &Traffic{c: x}
|
|
||||||
return t, nil
|
return t, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package traffic
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/coredns/coredns/plugin"
|
"github.com/coredns/coredns/plugin"
|
||||||
"github.com/coredns/coredns/plugin/pkg/dnsutil"
|
"github.com/coredns/coredns/plugin/pkg/dnsutil"
|
||||||
@@ -13,9 +15,10 @@ import (
|
|||||||
|
|
||||||
// Traffic is a plugin that load balances according to assignments.
|
// Traffic is a plugin that load balances according to assignments.
|
||||||
type Traffic struct {
|
type Traffic struct {
|
||||||
c *xds.Client
|
c *xds.Client
|
||||||
id string
|
id string
|
||||||
Next plugin.Handler
|
origins []string
|
||||||
|
Next plugin.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
// shutdown closes the connection to the managment endpoints and stops any running goroutines.
|
// 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) {
|
func (t *Traffic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
||||||
state := request.Request{Req: r, W: w}
|
state := request.Request{Req: r, W: w}
|
||||||
|
|
||||||
cluster, _ := dnsutil.TrimZone(state.Name(), "example.org")
|
cluster := ""
|
||||||
addr := t.c.Select(cluster)
|
for _, o := range t.origins {
|
||||||
if addr == nil {
|
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)
|
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 := new(dns.Msg)
|
||||||
m.SetReply(r)
|
m.SetReply(r)
|
||||||
|
|
||||||
m.Answer = []dns.RR{&dns.A{
|
if addr == nil {
|
||||||
Hdr: dns.RR_Header{Name: state.QName(), Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 5},
|
m.Ns = soa(state.Zone)
|
||||||
A: addr,
|
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)
|
w.WriteMsg(m)
|
||||||
return 0, nil
|
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.
|
// Name implements the plugin.Handler interface.
|
||||||
func (t *Traffic) Name() string { return "traffic" }
|
func (t *Traffic) Name() string { return "traffic" }
|
||||||
|
|||||||
Reference in New Issue
Block a user