-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.go
61 lines (52 loc) · 1.55 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
package gathersrv
import (
"fmt"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"strings"
)
const gatherSrvPluginName = "gathersrv"
// init registers this plugin.
func init() { plugin.Register(gatherSrvPluginName, setup) }
func setup(c *caddy.Controller) error {
var clusters []Cluster
c.Next() // Ignore "gathersrv" and give us the next token.
if !c.NextArg() {
return plugin.Error(gatherSrvPluginName, c.ArgErr())
}
domain := parseDomain(c.Val())
if domain == "" {
return plugin.Error(gatherSrvPluginName, fmt.Errorf("Provided incorrect domain <%s>", c.Val()))
}
for c.NextBlock() {
suffix := parseDomain(c.Val())
if suffix == "" {
return plugin.Error(gatherSrvPluginName, fmt.Errorf("Provided incorrect domain <%s>", c.Val()))
}
if !c.NextArg() {
return plugin.Error(gatherSrvPluginName, c.ArgErr())
}
prefix := c.Val()
if c.NextArg() {
return plugin.Error(gatherSrvPluginName, c.ArgErr())
}
clusters = append(clusters, Cluster{Prefix: prefix, Suffix: suffix})
}
if c.NextArg() {
return plugin.Error(gatherSrvPluginName, c.ArgErr())
}
if len(clusters) == 0 {
return plugin.Error(gatherSrvPluginName, fmt.Errorf("You have to provide at least one cluster definition."))
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
return GatherSrv{Clusters: clusters, Domain: domain, Next: next}
})
return nil
}
func parseDomain(raw string) string {
if strings.HasSuffix(raw, ".") {
return plugin.Name(raw).Normalize()
}
return ""
}