Add _grpc_config replies

Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
Miek Gieben
2020-03-30 09:09:34 +02:00
parent 36dec4ca85
commit e2238cfad8
5 changed files with 33 additions and 5 deletions

14
plugin/traffic/lb.go Normal file
View 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},
}}
}

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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,
},
}