mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 16:24:19 -04:00
14
plugin/traffic/lb.go
Normal file
14
plugin/traffic/lb.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package traffic
|
||||
|
||||
import "github.com/miekg/dns"
|
||||
|
||||
// See https://github.com/grpc/grpc/blob/master/doc/service_config.md for the fields in this proto.
|
||||
// We encode it as json and return it in a TXT field.
|
||||
var lbTXT = `grpc_config=[{"serviceConfig":{"loadBalancingConfig":[{"xds_experimental":{"lrs_load_reporting_server_name":""}}]}}]`
|
||||
|
||||
func txt(z string) []dns.RR {
|
||||
return []dns.RR{&dns.TXT{
|
||||
Hdr: dns.RR_Header{Name: z, Rrtype: dns.TypeTXT, Class: dns.ClassINET, Ttl: 5},
|
||||
Txt: []string{lbTXT},
|
||||
}}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package traffic
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strings"
|
||||
@@ -31,6 +32,9 @@ func setup(c *caddy.Controller) error {
|
||||
if err != nil {
|
||||
return plugin.Error("traffic", err)
|
||||
}
|
||||
if _, err := json.Marshal(lbTXT); err != nil {
|
||||
return fmt.Errorf("failed to marshal grpc serverConfig: %s", err)
|
||||
}
|
||||
|
||||
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
|
||||
t.Next = next
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package traffic
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/caddyserver/caddy"
|
||||
@@ -13,6 +14,16 @@ func TestSetup(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLBTxt(t *testing.T) {
|
||||
_, err := json.Marshal(lbTXT)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to marshal grpc serverConfig: %s", err)
|
||||
}
|
||||
if len(lbTXT) > 255 {
|
||||
t.Fatalf("Too long grpc serverConfig (>255): %d", len(lbTXT))
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseTraffic(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
|
||||
@@ -69,10 +69,9 @@ func (t *Traffic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg
|
||||
return 0, nil
|
||||
}
|
||||
if strings.HasPrefix(strings.ToLower(labels[0]), "_grpc_config") {
|
||||
// this is the grpc config blob encoded in a TXT record, we just return a NXDOMAIN
|
||||
// make this a separate so we can insert some logic later.
|
||||
m.Ns = soa(state.Zone)
|
||||
m.Rcode = dns.RcodeNameError
|
||||
// this is the grpc config blob encoded in a TXT record, see documentation for lbTXT.
|
||||
m.Answer = txt(state.Zone)
|
||||
m.Rcode = dns.RcodeSuccess
|
||||
w.WriteMsg(m)
|
||||
return 0, nil
|
||||
|
||||
|
||||
@@ -125,7 +125,7 @@ func TestTraffic(t *testing.T) {
|
||||
{"127.0.0.2", 18008, corepb.HealthStatus_HEALTHY},
|
||||
}),
|
||||
},
|
||||
cluster: "_grpc_config.web", qtype: dns.TypeTXT, rcode: dns.RcodeNameError, ns: true,
|
||||
cluster: "_grpc_config.web", qtype: dns.TypeTXT, rcode: dns.RcodeSuccess,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user