-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.go
67 lines (53 loc) · 1.82 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
package kubeforward
import (
"context"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"log"
"sync"
)
func init() { plugin.Register("kubeforward", setup) }
func setup(c *caddy.Controller) error {
version := "0.3.5"
log.Printf("\033[34m[kubeforward] version: %s\033[0m\n", version)
// parse config
config, err := ParseConfig(c)
if err != nil {
return err
}
kubeForwardPlugin := &KubeForward{
Namespace: config.Namespace,
ServiceName: config.ServiceName, //kubernetes.io/service-name=d8-kube-dns
forwarder: nil,
options: config.opts,
cond: sync.NewCond(&sync.Mutex{}),
}
// Add the Plugin to CoreDNS, so Servers can use it in their plugin chain.
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
kubeForwardPlugin.Next = next
return kubeForwardPlugin
})
// Context for properly shutdown goroutine
ctx, cancel := context.WithCancel(context.Background())
c.OnStartup(func() error {
log.Printf("[kubeforward] Starting with namespace=%s, service_name=%s\n", config.Namespace, config.ServiceName)
// Start go routine for watch EndpointSlice
go func() {
err := startEndpointSliceWatcher(ctx, config.Namespace, config.ServiceName, config.PortName, func(newServers []string) {
kubeForwardPlugin.UpdateForwardServers(newServers, *config)
log.Printf("[kubeforward] Updated servers namespace%s, service_name=%s\n: %v", config.Namespace, config.ServiceName, newServers)
})
if err != nil {
log.Printf("[kubeforward] Error starting EndpointSlice watcher with label kubernetes.io/service-name=%s: %v", config.ServiceName, err)
}
}()
return nil
})
c.OnShutdown(func() error {
log.Printf("[kubeforward] Shutting down with namespace=%s\n", config.Namespace)
cancel()
return nil
})
return nil
}