-
Notifications
You must be signed in to change notification settings - Fork 1
/
kubeapi.go
49 lines (40 loc) · 1.37 KB
/
kubeapi.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
package kubeapi
import (
"context"
"errors"
"github.com/miekg/dns"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
)
// KubeAPI implements a plugin that establishes a Kubernetes API client.
type KubeAPI struct {
Next plugin.Handler
APIServer string
APICertAuth string
APIClientCert string
APIClientKey string
ClientConfig clientcmd.ClientConfig
Client kubernetes.Interface
}
// Name implements the Handler interface.
func (k KubeAPI) Name() string { return pluginName }
// ServeDNS implements the plugin.Handler interface.
func (k KubeAPI) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
return plugin.NextOrFailure(k.Name(), k.Next, ctx, w, r)
}
// Client returns the Kubernetes API client defined by kubeapi plugin in the server config.
// This should only be called after all plugins have loaded, e.g. from a caddy Controller OnStartup function.
func Client(config *dnsserver.Config) (kubernetes.Interface, error) {
for _, h := range config.Handlers() {
k, ok := h.(*KubeAPI)
if !ok {
continue
}
return k.Client, nil
}
// Either the kubeapi plugin was not defined in the serverblock, or Client() was called
// before it had a chance to register itself.
return nil, errors.New("kubeapi plugin not registered in server block")
}