2019-10-05 11:45:45 +01:00
|
|
|
package traffic
|
|
|
|
|
|
|
|
|
|
import (
|
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"
|
|
|
|
|
clog "github.com/coredns/coredns/plugin/pkg/log"
|
2020-01-16 09:16:05 +01:00
|
|
|
"github.com/coredns/coredns/plugin/pkg/parse"
|
|
|
|
|
"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"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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-15 16:37:18 +01:00
|
|
|
stream, err := t.c.Run()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return plugin.Error("traffic", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := t.c.ClusterDiscovery(stream, "", "", []string{}); err != nil {
|
|
|
|
|
log.Error(err)
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-15 20:33:53 +01:00
|
|
|
go func() {
|
|
|
|
|
err = t.c.Receive(stream)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// can't do log debug in setup functions
|
|
|
|
|
log.Debug(err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
2020-01-13 11:21:20 +01:00
|
|
|
|
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) {
|
2020-01-16 08:47:17 +01:00
|
|
|
node := "coredns"
|
2020-01-16 09:16:05 +01:00
|
|
|
toHosts := []string{}
|
|
|
|
|
var err error
|
2020-01-16 08:47:17 +01:00
|
|
|
|
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])
|
|
|
|
|
}
|
|
|
|
|
// now cut the prefix off again, because the dialer needs to see normal address strings. All this
|
|
|
|
|
// grpc:// stuff is to enfore uniformaty accross plugins and future proofing for other protocols.
|
|
|
|
|
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() {
|
|
|
|
|
case "id":
|
2020-01-16 08:47:17 +01:00
|
|
|
args := c.RemainingArgs()
|
|
|
|
|
if len(args) != 1 {
|
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
|
}
|
|
|
|
|
node = args[0]
|
|
|
|
|
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 09:16:05 +01:00
|
|
|
// TODO: only the first host is used.
|
|
|
|
|
x, err := xds.New(toHosts[0], node)
|
2020-01-16 08:47:17 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t := &Traffic{c: x}
|
|
|
|
|
return t, nil
|
2019-10-05 11:45:45 +01:00
|
|
|
}
|