2019-10-05 11:45:45 +01:00
|
|
|
package traffic
|
|
|
|
|
|
|
|
|
|
import (
|
2020-01-16 16:39:28 +01:00
|
|
|
"crypto/tls"
|
2020-01-16 09:16:05 +01:00
|
|
|
"fmt"
|
2019-10-05 11:45:45 +01:00
|
|
|
"math/rand"
|
2020-01-16 09:16:05 +01:00
|
|
|
"strings"
|
2019-10-05 11:45:45 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/coredns/coredns/core/dnsserver"
|
|
|
|
|
"github.com/coredns/coredns/plugin"
|
2020-01-18 08:39:02 +01:00
|
|
|
"github.com/coredns/coredns/plugin/metrics"
|
2019-10-05 11:45:45 +01:00
|
|
|
clog "github.com/coredns/coredns/plugin/pkg/log"
|
2020-01-16 09:16:05 +01:00
|
|
|
"github.com/coredns/coredns/plugin/pkg/parse"
|
2020-01-16 16:39:28 +01:00
|
|
|
pkgtls "github.com/coredns/coredns/plugin/pkg/tls"
|
2020-01-16 09:16:05 +01:00
|
|
|
"github.com/coredns/coredns/plugin/pkg/transport"
|
2020-01-16 08:47:17 +01:00
|
|
|
"github.com/coredns/coredns/plugin/traffic/xds"
|
2019-10-05 11:45:45 +01:00
|
|
|
|
|
|
|
|
"github.com/caddyserver/caddy"
|
2020-01-16 16:39:28 +01:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
"google.golang.org/grpc/credentials"
|
2019-10-05 11:45:45 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var log = clog.NewWithPlugin("traffic")
|
|
|
|
|
|
|
|
|
|
func init() { plugin.Register("traffic", setup) }
|
|
|
|
|
|
|
|
|
|
func setup(c *caddy.Controller) error {
|
|
|
|
|
rand.Seed(int64(time.Now().Nanosecond()))
|
2020-01-16 09:16:05 +01:00
|
|
|
t, err := parseTraffic(c)
|
2020-01-12 16:06:06 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return plugin.Error("traffic", err)
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-05 11:45:45 +01:00
|
|
|
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
|
2020-01-12 16:06:06 +01:00
|
|
|
t.Next = next
|
|
|
|
|
return t
|
2019-10-05 11:45:45 +01:00
|
|
|
})
|
|
|
|
|
|
2020-01-16 16:39:28 +01:00
|
|
|
c.OnStartup(func() error {
|
2020-02-05 16:10:58 +01:00
|
|
|
go func() {
|
|
|
|
|
for {
|
|
|
|
|
opts := []grpc.DialOption{grpc.WithInsecure()}
|
|
|
|
|
if t.tlsConfig != nil {
|
|
|
|
|
opts = []grpc.DialOption{grpc.WithTransportCredentials(credentials.NewTLS(t.tlsConfig))}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
redo:
|
|
|
|
|
|
|
|
|
|
t.c, err = xds.New(t.hosts[0], t.node, opts...)
|
|
|
|
|
err := t.c.Run()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Warning(err)
|
|
|
|
|
time.Sleep(2 * time.Second) // back off foo
|
|
|
|
|
goto redo
|
|
|
|
|
}
|
|
|
|
|
// err == nil
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}()
|
2020-01-18 08:39:02 +01:00
|
|
|
metrics.MustRegister(c, xds.ClusterGauge)
|
2020-01-16 16:39:28 +01:00
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
c.OnShutdown(func() error { return t.c.Stop() })
|
2019-10-05 11:45:45 +01:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-16 09:16:05 +01:00
|
|
|
func parseTraffic(c *caddy.Controller) (*Traffic, error) {
|
|
|
|
|
toHosts := []string{}
|
2020-03-04 15:38:29 +01:00
|
|
|
t := &Traffic{node: "coredns", mgmt: "xds"}
|
2020-01-16 16:39:28 +01:00
|
|
|
var (
|
|
|
|
|
err error
|
|
|
|
|
tlsConfig *tls.Config
|
|
|
|
|
tlsServerName string
|
|
|
|
|
)
|
2020-01-16 08:47:17 +01:00
|
|
|
|
2020-01-16 11:09:34 +01:00
|
|
|
t.origins = make([]string, len(c.ServerBlockKeys))
|
|
|
|
|
for i := range c.ServerBlockKeys {
|
|
|
|
|
t.origins[i] = plugin.Host(c.ServerBlockKeys[i]).Normalize()
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-05 11:45:45 +01:00
|
|
|
for c.Next() {
|
|
|
|
|
args := c.RemainingArgs()
|
2020-01-16 09:16:05 +01:00
|
|
|
if len(args) < 1 {
|
2020-01-16 07:15:09 +01:00
|
|
|
return nil, c.ArgErr()
|
2020-01-16 09:16:05 +01:00
|
|
|
}
|
|
|
|
|
toHosts, err = parse.HostPortOrFile(args...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
for i := range toHosts {
|
|
|
|
|
if !strings.HasPrefix(toHosts[i], transport.GRPC+"://") {
|
|
|
|
|
return nil, fmt.Errorf("not a %s scheme: %s", transport.GRPC, toHosts[i])
|
|
|
|
|
}
|
2020-01-16 11:09:34 +01:00
|
|
|
// now cut the prefix off again, because the dialler needs to see normal address strings. All this
|
|
|
|
|
// grpc:// stuff is to enforce uniform across plugins and future proofing for other protocols.
|
2020-01-16 09:16:05 +01:00
|
|
|
toHosts[i] = toHosts[i][len(transport.GRPC+"://"):]
|
2019-10-05 11:45:45 +01:00
|
|
|
}
|
2020-01-16 07:15:09 +01:00
|
|
|
for c.NextBlock() {
|
|
|
|
|
switch c.Val() {
|
2020-03-04 15:38:29 +01:00
|
|
|
case "cluster":
|
|
|
|
|
args := c.RemainingArgs()
|
|
|
|
|
if len(args) != 1 {
|
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
|
}
|
|
|
|
|
t.mgmt = args[0]
|
2020-01-16 07:15:09 +01:00
|
|
|
case "id":
|
2020-01-16 08:47:17 +01:00
|
|
|
args := c.RemainingArgs()
|
|
|
|
|
if len(args) != 1 {
|
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
|
}
|
2020-02-05 16:10:58 +01:00
|
|
|
t.node = args[0]
|
2020-01-16 16:39:28 +01:00
|
|
|
case "tls":
|
|
|
|
|
args := c.RemainingArgs()
|
|
|
|
|
if len(args) > 3 {
|
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tlsConfig, err = pkgtls.NewTLSConfigFromArgs(args...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
case "tls_servername":
|
|
|
|
|
if !c.NextArg() {
|
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
|
}
|
|
|
|
|
tlsServerName = c.Val()
|
2020-01-19 09:14:09 +01:00
|
|
|
case "ignore_health":
|
|
|
|
|
t.health = true
|
2020-01-16 08:47:17 +01:00
|
|
|
default:
|
|
|
|
|
return nil, c.Errf("unknown property '%s'", c.Val())
|
2020-01-16 07:15:09 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-10-05 11:45:45 +01:00
|
|
|
}
|
2020-01-16 08:47:17 +01:00
|
|
|
|
2020-01-16 16:39:28 +01:00
|
|
|
if tlsConfig != nil {
|
2020-02-05 16:10:58 +01:00
|
|
|
t.tlsConfig = tlsConfig
|
2020-01-16 16:39:28 +01:00
|
|
|
if tlsServerName != "" {
|
2020-02-05 16:10:58 +01:00
|
|
|
t.tlsConfig.ServerName = tlsServerName
|
2020-01-16 16:39:28 +01:00
|
|
|
}
|
2020-01-16 08:47:17 +01:00
|
|
|
}
|
2020-02-05 16:10:58 +01:00
|
|
|
t.hosts = toHosts
|
2020-01-16 08:47:17 +01:00
|
|
|
return t, nil
|
2019-10-05 11:45:45 +01:00
|
|
|
}
|
2020-01-24 12:00:07 +01:00
|
|
|
|
2020-01-24 13:34:59 +01:00
|
|
|
// parseLocality parses string s into loc's. Each loc must be space separated from the other, inside
|
2020-01-24 12:00:07 +01:00
|
|
|
// a loc we have region,zone,subzone, where subzone or subzone and zone maybe empty. If specified
|
|
|
|
|
// they must be comma separate (not spaces or anything).
|
2020-01-24 13:34:59 +01:00
|
|
|
func parseLocality(s string) ([]xds.Locality, error) {
|
|
|
|
|
sets := strings.Fields(s)
|
|
|
|
|
if len(sets) == 0 {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
locs := []xds.Locality{}
|
|
|
|
|
for _, s := range sets {
|
|
|
|
|
l := strings.Split(s, ",")
|
|
|
|
|
switch len(l) {
|
|
|
|
|
default:
|
|
|
|
|
return nil, fmt.Errorf("too many location specifiers: %q", s)
|
|
|
|
|
case 1:
|
2020-01-25 17:04:16 +01:00
|
|
|
l0 := strings.TrimSpace(l[0])
|
|
|
|
|
if l0 == "" {
|
|
|
|
|
return nil, fmt.Errorf("empty location specifer: %q", l[0])
|
|
|
|
|
}
|
|
|
|
|
locs = append(locs, xds.Locality{Region: l0})
|
2020-01-24 13:34:59 +01:00
|
|
|
continue
|
|
|
|
|
case 2:
|
2020-01-25 17:04:16 +01:00
|
|
|
l0 := strings.TrimSpace(l[0])
|
|
|
|
|
if l0 == "" {
|
|
|
|
|
return nil, fmt.Errorf("empty location specifer: %q", l[0])
|
|
|
|
|
}
|
|
|
|
|
l1 := strings.TrimSpace(l[1])
|
|
|
|
|
if l1 == "" {
|
|
|
|
|
return nil, fmt.Errorf("empty location specifer: %q", l[1])
|
|
|
|
|
}
|
|
|
|
|
locs = append(locs, xds.Locality{Region: l0, Zone: l1})
|
2020-01-24 13:34:59 +01:00
|
|
|
continue
|
|
|
|
|
case 3:
|
2020-01-25 17:04:16 +01:00
|
|
|
l0 := strings.TrimSpace(l[0])
|
|
|
|
|
if l0 == "" {
|
|
|
|
|
return nil, fmt.Errorf("empty location specifer: %q", l[0])
|
|
|
|
|
}
|
|
|
|
|
l1 := strings.TrimSpace(l[1])
|
|
|
|
|
if l1 == "" {
|
|
|
|
|
return nil, fmt.Errorf("empty location specifer: %q", l[1])
|
|
|
|
|
}
|
|
|
|
|
l2 := strings.TrimSpace(l[2])
|
|
|
|
|
if l2 == "" {
|
|
|
|
|
return nil, fmt.Errorf("empty location specifer: %q", l[2])
|
|
|
|
|
}
|
|
|
|
|
locs = append(locs, xds.Locality{Region: l0, Zone: l1, SubZone: l2})
|
2020-01-24 13:34:59 +01:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return locs, nil
|
2020-01-24 12:00:07 +01:00
|
|
|
}
|