forked from networkservicemesh/fanout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
235 lines (207 loc) · 5.1 KB
/
setup.go
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright (c) 2020 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fanout
import (
"strconv"
"strings"
"github.com/caddyserver/caddy"
"github.com/caddyserver/caddy/caddyfile"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics"
"github.com/coredns/coredns/plugin/pkg/parse"
"github.com/coredns/coredns/plugin/pkg/tls"
"github.com/coredns/coredns/plugin/pkg/transport"
"github.com/pkg/errors"
)
func init() {
caddy.RegisterPlugin("fanout", caddy.Plugin{
ServerType: "dns",
Action: setup,
})
}
func setup(c *caddy.Controller) error {
f, err := parseFanout(c)
if err != nil {
return plugin.Error("fanout", err)
}
l := len(f.clients)
if len(f.clients) > maxIPCount {
return plugin.Error("fanout", errors.Errorf("more than %d TOs configured: %d", maxIPCount, l))
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
f.Next = next
return f
})
c.OnStartup(func() error {
metrics.MustRegister(c, RequestCount, RcodeCount, RequestDuration, HealthcheckFailureCount)
return f.OnStartup()
})
c.OnShutdown(f.OnShutdown)
return nil
}
// OnStartup starts a goroutines for all clients.
func (f *Fanout) OnStartup() (err error) {
return nil
}
// OnShutdown stops all configured clients.
func (f *Fanout) OnShutdown() error {
return nil
}
func parseFanout(c *caddy.Controller) (*Fanout, error) {
var (
f *Fanout
err error
i int
)
for c.Next() {
if i > 0 {
return nil, plugin.ErrOnce
}
i++
f, err = parsefanoutStanza(&c.Dispenser)
if err != nil {
return nil, err
}
}
return f, nil
}
func parsefanoutStanza(c *caddyfile.Dispenser) (*Fanout, error) {
f := New()
if !c.Args(&f.from) {
return f, c.ArgErr()
}
f.from = plugin.Host(f.from).Normalize()
to := c.RemainingArgs()
if len(to) == 0 {
return f, c.ArgErr()
}
toHosts, err := parse.HostPortOrFile(to...)
if err != nil {
return f, err
}
transports := make([]string, len(toHosts))
for c.NextBlock() {
err = parseValue(strings.ToLower(c.Val()), f, c)
if err != nil {
return nil, err
}
}
for i, host := range toHosts {
trans, h := parse.Transport(host)
p := NewClient(h, f.net)
f.clients = append(f.clients, p)
transports[i] = trans
}
if f.tlsServerName != "" {
f.tlsConfig.ServerName = f.tlsServerName
}
for i := range f.clients {
if transports[i] == transport.TLS {
f.clients[i].SetTLSConfig(f.tlsConfig)
}
}
workerCount := f.workerCount
if workerCount > len(f.clients) || workerCount == 0 {
workerCount = len(f.clients)
}
f.workerCount = workerCount
return f, nil
}
func parseValue(v string, f *Fanout, c *caddyfile.Dispenser) error {
switch v {
case "tls":
return parseTLS(f, c)
case "network":
return parseProtocol(f, c)
case "tls-server":
return parseTLSServer(f, c)
case "worker-count":
return parseWorkerCount(f, c)
case "except":
return parseIgnored(f, c)
default:
return errors.Errorf("unknown property %v", v)
}
}
func parseIgnored(f *Fanout, c *caddyfile.Dispenser) error {
ignore := c.RemainingArgs()
if len(ignore) == 0 {
return c.ArgErr()
}
for i := 0; i < len(ignore); i++ {
ignore[i] = plugin.Host(ignore[i]).Normalize()
}
f.ignored = ignore
return nil
}
func parseWorkerCount(f *Fanout, c *caddyfile.Dispenser) error {
var err error
f.workerCount, err = parsePositiveInt(c)
if err == nil {
if f.workerCount < minWorkerCount {
return errors.New("worker count should be more or equal 2. Consider to use Forward plugin")
}
if f.workerCount > maxWorkerCount {
return errors.Errorf("worker count more then max value: %v", maxWorkerCount)
}
}
return err
}
func parsePositiveInt(c *caddyfile.Dispenser) (int, error) {
if !c.NextArg() {
return -1, c.ArgErr()
}
v := c.Val()
num, err := strconv.Atoi(v)
if err != nil {
return -1, c.ArgErr()
}
if num < 0 {
return -1, c.ArgErr()
}
return num, nil
}
func parseTLSServer(f *Fanout, c *caddyfile.Dispenser) error {
if !c.NextArg() {
return c.ArgErr()
}
f.tlsServerName = c.Val()
return nil
}
func parseProtocol(f *Fanout, c *caddyfile.Dispenser) error {
if !c.NextArg() {
return c.ArgErr()
}
net := strings.ToLower(c.Val())
if net != tcp && net != "udp" && net != tcptlc {
return errors.New("unknown network protocol")
}
f.net = net
return nil
}
func parseTLS(f *Fanout, c *caddyfile.Dispenser) error {
args := c.RemainingArgs()
if len(args) > 3 {
return c.ArgErr()
}
tlsConfig, err := tls.NewTLSConfigFromArgs(args...)
if err != nil {
return err
}
f.tlsConfig = tlsConfig
return nil
}