/
main.go
306 lines (282 loc) · 11.6 KB
/
main.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package main
import (
"fmt"
"flag"
"net"
"os"
"strconv"
"strings"
"time"
"github.com/colopl/k8s-local-dns/pkg/netif"
"github.com/coredns/coredns/coremain"
// Include all plugins.
_ "github.com/coredns/coredns/plugin/auto"
_ "github.com/coredns/coredns/plugin/autopath"
_ "github.com/coredns/coredns/plugin/bind"
_ "github.com/coredns/coredns/plugin/cache"
_ "github.com/coredns/coredns/plugin/chaos"
_ "github.com/coredns/coredns/plugin/debug"
_ "github.com/coredns/coredns/plugin/dnssec"
_ "github.com/coredns/coredns/plugin/dnstap"
_ "github.com/coredns/coredns/plugin/erratic"
_ "github.com/coredns/coredns/plugin/errors"
_ "github.com/coredns/coredns/plugin/etcd"
_ "github.com/coredns/coredns/plugin/federation"
_ "github.com/coredns/coredns/plugin/file"
_ "github.com/coredns/coredns/plugin/forward"
_ "github.com/coredns/coredns/plugin/health"
_ "github.com/coredns/coredns/plugin/hosts"
_ "github.com/coredns/coredns/plugin/kubernetes"
_ "github.com/coredns/coredns/plugin/loadbalance"
_ "github.com/coredns/coredns/plugin/log"
_ "github.com/coredns/coredns/plugin/loop"
_ "github.com/coredns/coredns/plugin/metadata"
_ "github.com/coredns/coredns/plugin/metrics"
_ "github.com/coredns/coredns/plugin/nsid"
_ "github.com/coredns/coredns/plugin/pprof"
_ "github.com/coredns/coredns/plugin/proxy"
_ "github.com/coredns/coredns/plugin/reload"
_ "github.com/coredns/coredns/plugin/rewrite"
_ "github.com/coredns/coredns/plugin/root"
_ "github.com/coredns/coredns/plugin/route53"
_ "github.com/coredns/coredns/plugin/secondary"
_ "github.com/coredns/coredns/plugin/template"
_ "github.com/coredns/coredns/plugin/tls"
_ "github.com/coredns/coredns/plugin/trace"
_ "github.com/coredns/coredns/plugin/whoami"
_ "github.com/mholt/caddy/onevent"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/mholt/caddy"
"k8s.io/kubernetes/pkg/util/dbus"
utilexec "k8s.io/kubernetes/pkg/util/exec"
utiliptables "k8s.io/kubernetes/pkg/util/iptables"
)
// configParams lists the configuration options that can be provided to dns-cache
type configParams struct {
localIPStr string // comma separated listen ips for the local cache agent
localIPs []net.IP // parsed ip addresses for the local cache agent to listen for dns requests
localPort string // port to listen for dns requests
metricsListenAddress string // address to serve metrics on
interfaceName string // Name of the interface to be created
interval time.Duration // specifies how often to run iptables rules check
exitChan chan bool // Channel to terminate background goroutines
healthPort string // port for the healthcheck
}
type iptablesRule struct {
table utiliptables.Table
chain utiliptables.Chain
args []string
}
type cacheApp struct {
setupIptables bool
iptables utiliptables.Interface
iptablesRules []iptablesRule
params configParams
netifHandle *netif.NetifManager
}
var cache = cacheApp{params: configParams{localPort: "53"}}
func isLockedErr(err error) bool {
return strings.Contains(err.Error(), "holding the xtables lock")
}
func (c *cacheApp) Init() {
err := c.parseAndValidateFlags()
if err != nil {
clog.Fatalf("Error parsing flags - %s, Exiting", err)
}
c.netifHandle = netif.NewNetifManager(c.params.localIPs)
if c.setupIptables {
c.initIptables()
}
err = c.teardownNetworking()
if err != nil {
// It is likely to hit errors here if previous shutdown cleaned up all iptables rules and interface.
// Logging error at info level
clog.Infof("Hit error during teardown - %s", err)
}
err = c.setupNetworking()
if err != nil {
cache.teardownNetworking()
clog.Fatalf("Failed to setup - %s, Exiting", err)
}
initMetrics(c.params.metricsListenAddress)
}
func init() {
cache.Init()
caddy.OnProcessExit = append(caddy.OnProcessExit, func() { cache.teardownNetworking() })
}
func (c *cacheApp) initIptables() {
// using the localIPStr param since we need ip strings here
for _, localIP := range strings.Split(c.params.localIPStr, ",") {
c.iptablesRules = append(c.iptablesRules, []iptablesRule{
// Match traffic destined for localIp:localPort and set the flows to be NOTRACKED, this skips connection tracking
{utiliptables.Table("raw"), utiliptables.ChainPrerouting, []string{"-p", "tcp", "-d", localIP,
"--dport", c.params.localPort, "-j", "NOTRACK"}},
{utiliptables.Table("raw"), utiliptables.ChainPrerouting, []string{"-p", "udp", "-d", localIP,
"--dport", c.params.localPort, "-j", "NOTRACK"}},
// There are rules in filter table to allow tracked connections to be accepted. Since we skipped connection tracking,
// need these additional filter table rules.
{utiliptables.TableFilter, utiliptables.ChainInput, []string{"-p", "tcp", "-d", localIP,
"--dport", c.params.localPort, "-j", "ACCEPT"}},
{utiliptables.TableFilter, utiliptables.ChainInput, []string{"-p", "udp", "-d", localIP,
"--dport", c.params.localPort, "-j", "ACCEPT"}},
// Match traffic from localIp:localPort and set the flows to be NOTRACKED, this skips connection tracking
{utiliptables.Table("raw"), utiliptables.ChainOutput, []string{"-p", "tcp", "-s", localIP,
"--sport", c.params.localPort, "-j", "NOTRACK"}},
{utiliptables.Table("raw"), utiliptables.ChainOutput, []string{"-p", "udp", "-s", localIP,
"--sport", c.params.localPort, "-j", "NOTRACK"}},
// Additional filter table rules for traffic frpm localIp:localPort
{utiliptables.TableFilter, utiliptables.ChainOutput, []string{"-p", "tcp", "-s", localIP,
"--sport", c.params.localPort, "-j", "ACCEPT"}},
{utiliptables.TableFilter, utiliptables.ChainOutput, []string{"-p", "udp", "-s", localIP,
"--sport", c.params.localPort, "-j", "ACCEPT"}},
// Skip connection tracking for requests to nodelocalDNS that are locally generated, example - by hostNetwork pods
{utiliptables.Table("raw"), utiliptables.ChainOutput, []string{"-p", "tcp", "-d", localIP,
"--dport", c.params.localPort, "-j", "NOTRACK"}},
{utiliptables.Table("raw"), utiliptables.ChainOutput, []string{"-p", "udp", "-d", localIP,
"--dport", c.params.localPort, "-j", "NOTRACK"}},
// skip connection tracking for healthcheck requests generated by liveness probe to health plugin
{utiliptables.Table("raw"), utiliptables.ChainOutput, []string{"-p", "tcp", "-d", localIP,
"--dport", c.params.healthPort, "-j", "NOTRACK"}},
{utiliptables.Table("raw"), utiliptables.ChainOutput, []string{"-p", "tcp", "-s", localIP,
"--sport", c.params.healthPort, "-j", "NOTRACK"}},
}...)
}
c.iptables = newIPTables()
}
func newIPTables() utiliptables.Interface {
execer := utilexec.New()
dbus := dbus.New()
return utiliptables.New(execer, dbus, utiliptables.ProtocolIpv4)
}
func (c *cacheApp) setupNetworking() error {
var err error
clog.Infof("Setting up networking for node cache")
err = c.netifHandle.AddDummyDevice(c.params.interfaceName)
if err != nil {
return err
}
if c.setupIptables {
for _, rule := range c.iptablesRules {
_, err = c.iptables.EnsureRule(utiliptables.Prepend, rule.table, rule.chain, rule.args...)
if err != nil {
return err
}
}
} else {
clog.Infof("Skipping iptables setup for node cache")
}
return err
}
func (c *cacheApp) teardownNetworking() error {
clog.Infof("Tearing down")
if c.params.exitChan != nil {
// Stop the goroutine that periodically checks for iptables rules/dummy interface
// exitChan is a buffered channel of size 1, so this will not block
c.params.exitChan <- true
}
err := c.netifHandle.RemoveDummyDevice(c.params.interfaceName)
if c.setupIptables {
for _, rule := range c.iptablesRules {
exists := true
for exists == true {
c.iptables.DeleteRule(rule.table, rule.chain, rule.args...)
exists, _ = c.iptables.EnsureRule(utiliptables.Prepend, rule.table, rule.chain, rule.args...)
}
// Delete the rule one last time since EnsureRule creates the rule if it doesn't exist
c.iptables.DeleteRule(rule.table, rule.chain, rule.args...)
}
}
return err
}
func (c *cacheApp) parseAndValidateFlags() error {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Runs coreDNS v1.2.6 as a nodelocal cache listening on the specified ip:port")
flag.PrintDefaults()
}
flag.StringVar(&c.params.localIPStr, "localip", "", "comma-separated string of ip addresses to bind dnscache to")
flag.StringVar(&c.params.interfaceName, "interfacename", "nodelocaldns", "name of the interface to be created")
flag.DurationVar(&c.params.interval, "syncinterval", 60, "interval(in seconds) to check for iptables rules")
flag.StringVar(&c.params.metricsListenAddress, "metrics-listen-address", "0.0.0.0:9353", "address to serve metrics on")
flag.BoolVar(&c.setupIptables, "setupiptables", true, "indicates whether iptables rules should be setup")
flag.StringVar(&c.params.healthPort, "health-port", "8080", "port used by health plugin")
flag.Parse()
for _, ipstr := range strings.Split(c.params.localIPStr, ",") {
newIP := net.ParseIP(ipstr)
if newIP == nil {
return fmt.Errorf("Invalid localip specified - %q", ipstr)
}
c.params.localIPs = append(c.params.localIPs, newIP)
}
// lookup specified dns port
if f := flag.Lookup("dns.port"); f == nil {
return fmt.Errorf("Failed to lookup \"dns.port\" parameter")
} else {
c.params.localPort = f.Value.String()
}
if _, err := strconv.Atoi(c.params.localPort); err != nil {
return fmt.Errorf("Invalid port specified - %q", c.params.localPort)
}
if _, err := strconv.Atoi(c.params.healthPort); err != nil {
return fmt.Errorf("Invalid healthcheck port specified - %q", c.params.healthPort)
}
return nil
}
func (c *cacheApp) runChecks() {
if c.setupIptables {
for _, rule := range c.iptablesRules {
exists, err := c.iptables.EnsureRule(utiliptables.Prepend, rule.table, rule.chain, rule.args...)
switch {
case exists:
// debug messages can be printed by including "debug" plugin in coreFile.
clog.Debugf("iptables rule %v for nodelocaldns already exists", rule)
continue
case err == nil:
clog.Infof("Added back nodelocaldns rule - %v", rule)
continue
// if we got here, either iptables check failed or adding rule back failed.
case isLockedErr(err):
clog.Infof("Error checking/adding iptables rule %v, due to xtables lock in use, retrying in %v", rule, c.params.interval)
setupErrCount.WithLabelValues("iptables_lock").Inc()
default:
clog.Errorf("Error adding iptables rule %v - %s", rule, err)
setupErrCount.WithLabelValues("iptables").Inc()
}
}
}
exists, err := c.netifHandle.EnsureDummyDevice(c.params.interfaceName)
if !exists {
if err != nil {
clog.Errorf("Failed to add non-existent interface %s: %s", c.params.interfaceName, err)
setupErrCount.WithLabelValues("interface_add").Inc()
}
clog.Infof("Added back interface - %s", c.params.interfaceName)
}
if err != nil {
clog.Errorf("Error checking dummy device %s - %s", c.params.interfaceName, err)
setupErrCount.WithLabelValues("interface_check").Inc()
}
}
func (c *cacheApp) run() {
c.params.exitChan = make(chan bool, 1)
tick := time.NewTicker(c.params.interval * time.Second)
for {
select {
case <-tick.C:
c.runChecks()
case <-c.params.exitChan:
clog.Warningf("Exiting iptables/interface check goroutine")
return
}
}
}
func main() {
// Ensure that the required setup is ready
// https://github.com/kubernetes/dns/issues/282 sometimes the interface gets the ip and then loses it, if added too soon.
cache.runChecks()
go cache.run()
coremain.Run()
// Unlikely to reach here, if we did it is because coremain exited and the signal was not trapped.
clog.Errorf("Untrapped signal, tearing down")
cache.teardownNetworking()
}