1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
package traffic
import (
"crypto/tls"
"fmt"
"math/rand"
"strings"
"time"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/plugin/pkg/parse"
pkgtls "github.com/coredns/coredns/plugin/pkg/tls"
"github.com/coredns/coredns/plugin/pkg/transport"
"github.com/coredns/coredns/plugin/traffic/xds"
"github.com/caddyserver/caddy"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
var log = clog.NewWithPlugin("traffic")
func init() { plugin.Register("traffic", setup) }
func setup(c *caddy.Controller) error {
rand.Seed(int64(time.Now().Nanosecond()))
t, err := parseTraffic(c)
if err != nil {
return plugin.Error("traffic", err)
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
t.Next = next
return t
})
c.OnStartup(func() error {
go func() {
for {
opts := []grpc.DialOption{grpc.WithInsecure()}
if t.tlsConfig != nil {
opts = []grpc.DialOption{grpc.WithTransportCredentials(credentials.NewTLS(t.tlsConfig))}
}
i := 0
redo:
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
}
if err := t.c.Run(); err != nil {
log.Warning(err)
time.Sleep(2 * time.Second) // back off foo
i++
goto redo
}
}
}()
metrics.MustRegister(c, xds.ClusterGauge)
return nil
})
c.OnShutdown(func() error { return t.c.Stop() })
return nil
}
func parseTraffic(c *caddy.Controller) (*Traffic, error) {
toHosts := []string{}
t := &Traffic{node: "coredns", mgmt: "xds"}
var (
err error
tlsConfig *tls.Config
tlsServerName string
)
t.origins = make([]string, len(c.ServerBlockKeys))
for i := range c.ServerBlockKeys {
t.origins[i] = plugin.Host(c.ServerBlockKeys[i]).Normalize()
}
for c.Next() {
args := c.RemainingArgs()
if len(args) < 1 {
return nil, c.ArgErr()
}
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 dialler needs to see normal address strings. All this
// grpc:// stuff is to enforce uniform across plugins and future proofing for other protocols.
toHosts[i] = toHosts[i][len(transport.GRPC+"://"):]
}
for c.NextBlock() {
switch c.Val() {
case "cluster":
args := c.RemainingArgs()
if len(args) != 1 {
return nil, c.ArgErr()
}
t.mgmt = args[0]
case "id":
args := c.RemainingArgs()
if len(args) != 1 {
return nil, c.ArgErr()
}
t.node = args[0]
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()
default:
return nil, c.Errf("unknown property '%s'", c.Val())
}
}
}
if tlsConfig != nil {
t.tlsConfig = tlsConfig
if tlsServerName != "" {
t.tlsConfig.ServerName = tlsServerName
}
}
t.hosts = toHosts
return t, nil
}
|