-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
114 lines (95 loc) · 2.24 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
package gateway
import (
"context"
"strconv"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
)
var log = clog.NewWithPlugin(thisPlugin)
const thisPlugin = "k8s_gateway"
func init() {
plugin.Register(thisPlugin, setup)
}
func setup(c *caddy.Controller) error {
gw, err := parse(c)
if err != nil {
return plugin.Error(thisPlugin, err)
}
err = gw.RunKubeController(context.Background())
if err != nil {
return plugin.Error(thisPlugin, err)
}
gw.ExternalAddrFunc = gw.SelfAddress
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
gw.Next = next
return gw
})
return nil
}
func parse(c *caddy.Controller) (*Gateway, error) {
gw := newGateway()
for c.Next() {
zones := c.RemainingArgs()
gw.Zones = zones
if len(gw.Zones) == 0 {
gw.Zones = make([]string, len(c.ServerBlockKeys))
copy(gw.Zones, c.ServerBlockKeys)
}
for i, str := range gw.Zones {
if host := plugin.Host(str).NormalizeExact(); len(host) != 0 {
gw.Zones[i] = host[0]
}
}
for c.NextBlock() {
switch c.Val() {
case "fallthrough":
gw.Fall.SetZonesFromArgs(c.RemainingArgs())
case "secondary":
args := c.RemainingArgs()
if len(args) == 0 {
return nil, c.ArgErr()
}
gw.secondNS = args[0]
case "resources":
args := c.RemainingArgs()
gw.updateResources(args)
if len(args) == 0 {
return nil, c.Errf("Incorrectly formated 'resource' parameter")
}
case "ttl":
args := c.RemainingArgs()
if len(args) == 0 {
return nil, c.ArgErr()
}
t, err := strconv.Atoi(args[0])
if err != nil {
return nil, err
}
if t < 0 || t > 3600 {
return nil, c.Errf("ttl must be in range [0, 3600]: %d", t)
}
gw.ttlLow = uint32(t)
case "apex":
args := c.RemainingArgs()
if len(args) == 0 {
return nil, c.ArgErr()
}
gw.apex = args[0]
case "kubeconfig":
args := c.RemainingArgs()
if len(args) == 0 {
return nil, c.ArgErr()
}
gw.configFile = args[0]
if len(args) == 2 {
gw.configContext = args[1]
}
default:
return nil, c.Errf("Unknown property '%s'", c.Val())
}
}
}
return gw, nil
}