-
Notifications
You must be signed in to change notification settings - Fork 7
/
setup.go
83 lines (71 loc) · 2 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
package multicluster
import (
"context"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"k8s.io/client-go/tools/clientcmd"
)
const pluginName = "multicluster"
// init registers this plugin.
func init() { plugin.Register(pluginName, setup) }
func setup(c *caddy.Controller) error {
multiCluster, err := ParseStanza(c)
if err != nil {
return plugin.Error(pluginName, err)
}
onStart, onShut, err := multiCluster.InitController(context.Background())
if err != nil {
return plugin.Error(pluginName, err)
}
if onStart != nil {
c.OnStartup(onStart)
}
if onShut != nil {
c.OnShutdown(onShut)
}
// Add the Plugin to CoreDNS, so Servers can use it in their plugin chain.
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
multiCluster.Next = next
return multiCluster
})
return nil
}
// ParseStanza parses a kubernetes stanza
func ParseStanza(c *caddy.Controller) (*MultiCluster, error) {
c.Next() // Skip "multicluster" label
opts := controllerOpts{
initEndpointsCache: true, // watch endpoints by default
}
zones := plugin.OriginsFromArgsOrServerBlock(c.RemainingArgs(), c.ServerBlockKeys)
multiCluster := New(zones)
multiCluster.opts = opts
for c.NextBlock() {
switch c.Val() {
case "kubeconfig":
args := c.RemainingArgs()
if len(args) != 1 && len(args) != 2 {
return nil, c.ArgErr()
}
overrides := &clientcmd.ConfigOverrides{}
if len(args) == 2 {
overrides.CurrentContext = args[1]
}
config := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: args[0]},
overrides,
)
multiCluster.ClientConfig = config
case "fallthrough":
multiCluster.Fall.SetZonesFromArgs(c.RemainingArgs())
case "noendpoints":
if len(c.RemainingArgs()) != 0 {
return nil, c.ArgErr()
}
multiCluster.opts.initEndpointsCache = false
default:
return nil, c.Errf("unknown property '%s'", c.Val())
}
}
return multiCluster, nil
}