forked from infobloxopen/kubenodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.go
157 lines (136 loc) · 3.28 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
package kubenodes
import (
"context"
"errors"
"fmt"
"strconv"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/tools/cache"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/kubeapi"
core "k8s.io/api/core/v1"
)
const pluginName = "kubenodes"
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)
}
k.setWatch(context.Background())
c.OnStartup(startWatch(k, dnsserver.GetConfig(c)))
c.OnShutdown(stopWatch(k))
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
k.Next = next
return k
})
return nil
}
func parse(c *caddy.Controller) (*KubeNodes, error) {
var (
kns *KubeNodes
err error
)
i := 0
for c.Next() {
if i > 0 {
return nil, plugin.ErrOnce
}
i++
kns, err = parseStanza(c)
if err != nil {
return kns, err
}
}
return kns, nil
}
// parseStanza parses a kubenodes stanza
func parseStanza(c *caddy.Controller) (*KubeNodes, error) {
kns := New(plugin.OriginsFromArgsOrServerBlock(c.RemainingArgs(), c.ServerBlockKeys))
for c.NextBlock() {
switch c.Val() {
case "external":
kns.ipType = core.NodeExternalIP
kns.dnsType = core.NodeExternalDNS
case "fallthrough":
kns.Fall.SetZonesFromArgs(c.RemainingArgs())
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)
}
kns.ttl = uint32(t)
default:
return nil, c.Errf("unknown property '%s'", c.Val())
}
}
return kns, nil
}
func (k *KubeNodes) setWatch(ctx context.Context) {
// define Node controller and reverse lookup indexer
k.indexer, k.controller = cache.NewIndexerInformer(
&cache.ListWatch{
ListFunc: func(o v1.ListOptions) (runtime.Object, error) {
return k.client.CoreV1().Nodes().List(ctx, o)
},
WatchFunc: func(o v1.ListOptions) (watch.Interface, error) {
return k.client.CoreV1().Nodes().Watch(ctx, o)
},
},
&core.Node{},
0,
cache.ResourceEventHandlerFuncs{},
cache.Indexers{"reverse": func(obj interface{}) ([]string, error) {
node, ok := obj.(*core.Node)
if !ok {
return nil, errors.New("unexpected obj type")
}
var idx []string
for _, addr := range node.Status.Addresses {
if addr.Type != k.ipType {
continue
}
idx = append(idx, addr.Address)
}
return idx, nil
}},
)
}
func startWatch(k *KubeNodes, config *dnsserver.Config) func() error {
return func() error {
// retrieve client from kubeapi plugin
var err error
k.client, err = kubeapi.Client(config)
if err != nil {
return err
}
// start the informer
go k.controller.Run(k.stopCh)
return nil
}
}
func stopWatch(k *KubeNodes) func() error {
return func() error {
k.stopLock.Lock()
defer k.stopLock.Unlock()
if !k.shutdown {
close(k.stopCh)
k.shutdown = true
return nil
}
return fmt.Errorf("shutdown already in progress")
}
}