-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup.go
166 lines (145 loc) · 3.23 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
package k8s_event
import (
"fmt"
"strconv"
"strings"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
)
const (
pluginName = "k8s_event"
defaultRateCacheSize = 4096
minRateCacheSize = 1
maxRateCacheSize = 65535
defaultRateBurst = 25
minRateBurst = 1
maxRateBurst = 256
defaultRateQPS = 1. / 300.
minRateQPS = 1. / 3600.
maxRateQPS = 1
defaultLevel = 1<<Error | 1<<Warning
)
var log = clog.NewWithPlugin(pluginName)
func init() { plugin.Register(pluginName, setup) }
func setup(c *caddy.Controller) error {
k, err := parse(c)
if err != nil {
return plugin.Error(pluginName, err)
}
err = k.Init()
if err != nil {
return plugin.Error(pluginName, err)
}
c.OnStartup(k.Startup(dnsserver.GetConfig(c)))
c.OnShutdown(k.Shutdown())
return nil
}
func parse(c *caddy.Controller) (*k8sEvent, error) {
var (
ke *k8sEvent
err error
)
i := 0
for c.Next() {
if i > 0 {
return nil, plugin.ErrOnce
}
i++
ke, err = parseStanza(c)
if err != nil {
return ke, err
}
}
return ke, nil
}
// parseStanza parses a k8sEvent stanza
func parseStanza(c *caddy.Controller) (*k8sEvent, error) {
ke := &k8sEvent{
levels: defaultLevel,
qps: defaultRateQPS,
burst: defaultRateBurst,
cacheSize: defaultRateCacheSize,
}
for c.NextBlock() {
switch c.Val() {
case "level":
levelsArgs := c.RemainingArgs()
if len(levelsArgs) == 0 {
return nil, c.ArgErr()
}
levels := 0
for _, l := range levelsArgs {
level, err := levelFromString(strings.ToLower(l))
if err != nil {
return nil, err
}
levels |= 1 << level
}
ke.levels = levels
case "rate":
args := c.RemainingArgs()
argsLen := len(args)
if argsLen == 0 || argsLen > 3 {
return nil, c.ArgErr()
}
qps, err := strconv.ParseFloat(args[0], 32)
if err != nil {
return nil, err
}
if qps < minRateQPS || qps > maxRateQPS {
return nil, c.Errf("qps must be in range [%f, %f]: %d", minRateQPS, maxRateQPS, qps)
}
ke.qps = float32(qps)
if argsLen--; argsLen == 0 {
break
}
burst, err := strconv.Atoi(args[1])
if err != nil {
return nil, err
}
if burst < minRateBurst || burst > maxRateBurst {
return nil, c.Errf("burst must be in range [%d, %d]: %d", minRateBurst, maxRateBurst, burst)
}
ke.burst = burst
if argsLen--; argsLen == 0 {
break
}
cacheSize, err := strconv.Atoi(args[2])
if err != nil {
return nil, err
}
if cacheSize < minRateCacheSize || cacheSize > maxRateCacheSize {
return nil, c.Errf("cacheSize must be in range [%d, %d]: %d", minRateCacheSize, maxRateCacheSize, cacheSize)
}
ke.cacheSize = cacheSize
default:
return nil, c.Errf("unknown property '%s'", c.Val())
}
}
return ke, nil
}
type Level int
const (
Debug Level = iota
Error
Fatal
Info
Warning
)
func levelFromString(s string) (Level, error) {
switch s {
case "debug":
return Debug, nil
case "error":
return Error, nil
case "fatal":
return Fatal, nil
case "info":
return Info, nil
case "warning":
return Warning, nil
}
return Debug, fmt.Errorf("invalid Level: %s", s)
}