| 
									
										
										
										
											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))}
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-05 11:02:54 +01:00
										 |  |  | 				i := 0
 | 
					
						
							| 
									
										
										
										
											2020-02-05 16:10:58 +01:00
										 |  |  | 			redo:
 | 
					
						
							| 
									
										
										
										
											2020-03-05 11:02:54 +01:00
										 |  |  | 				i = i % len(t.hosts)
 | 
					
						
							|  |  |  | 				if t.c, err = xds.New(t.hosts[i], t.node, opts...); err != nil {
 | 
					
						
							|  |  |  | 					log.Warning(err)
 | 
					
						
							|  |  |  | 					time.Sleep(2 * time.Second) // back off foo
 | 
					
						
							|  |  |  | 					i++
 | 
					
						
							|  |  |  | 					goto redo
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2020-02-05 16:10:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-05 11:02:54 +01:00
										 |  |  | 				if err := t.c.Run(); err != nil {
 | 
					
						
							| 
									
										
										
										
											2020-02-05 16:10:58 +01:00
										 |  |  | 					log.Warning(err)
 | 
					
						
							|  |  |  | 					time.Sleep(2 * time.Second) // back off foo
 | 
					
						
							| 
									
										
										
										
											2020-03-05 11:02:54 +01:00
										 |  |  | 					i++
 | 
					
						
							| 
									
										
										
										
											2020-02-05 16:10:58 +01:00
										 |  |  | 					goto redo
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}()
 | 
					
						
							| 
									
										
										
										
											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-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
										 |  |  | }
 |