More grpc LB tweaks

Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
Miek Gieben
2020-03-30 16:10:07 +02:00
parent 494227d95f
commit fafb347966
2 changed files with 17 additions and 8 deletions

View File

@@ -1,16 +1,19 @@
package traffic
import (
"fmt"
"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":[{"eds_experimental":{"LrsLoadReportingServerName":"","Cluster": "xds"}}]}}]`
// TOOD(miek): balancer name should not be hardcoded
var lbTXT = `grpc_config=[{"serviceConfig":{"loadBalancingConfig":[{"eds_experimental":{"Cluster": "xds", "EDSServiceName":"%s", "BalancerName":"xds"}}]}}]`
func txt(z string) []dns.RR {
func txt(z, cluster 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},
Txt: []string{fmt.Sprintf(lbTXT, cluster)},
}}
}

View File

@@ -70,7 +70,7 @@ func (t *Traffic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg
}
if strings.HasPrefix(strings.ToLower(labels[0]), "_grpc_config") {
// this is the grpc config blob encoded in a TXT record, see documentation for lbTXT.
m.Answer = txt(state.Zone)
m.Answer = txt(state.Zone, labels[1]) // 1 is the cluster
m.Rcode = dns.RcodeSuccess
w.WriteMsg(m)
return 0, nil
@@ -95,10 +95,16 @@ func (t *Traffic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg
w.WriteMsg(m)
return 0, nil
}
// OK, _grcplb._tcp query; we need to return the endpoint for the mgmt cluster *NOT* the cluster
// we got the query for. This should exist, but we'll check later anyway.
cluster = t.mgmt
sockaddr, _ = t.c.Select(cluster, healthy)
// OK, _grcplb._tcp query; we need to return the endpoint for the cluster in this query
cluster = labels[2]
sockaddr, ok = t.c.Select(cluster, healthy)
if !ok {
// nodata error when this cluster doesn't exist.
m.Ns = soa(state.Zone)
m.Rcode = dns.RcodeSuccess
w.WriteMsg(m)
return 0, nil
}
break
default:
m.Ns = soa(state.Zone)