Skip to content

Commit e0dead4

Browse files
author
Ondřej Benkovský
authored
plugin/kubernetes : make kubernetes client log in CoreDNS format (#5461)
Signed-off-by: Ondřej Benkovský <ondrej.benkovsky@jamf.com>
1 parent 4a40e9e commit e0dead4

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

plugin/kubernetes/logger.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package kubernetes
2+
3+
import (
4+
clog "github.com/coredns/coredns/plugin/pkg/log"
5+
6+
"github.com/go-logr/logr"
7+
)
8+
9+
// loggerAdapter is a simple wrapper around CoreDNS plugin logger made to implement logr.LogSink interface, which is used
10+
// as part of klog library for logging in Kubernetes client. By using this adapter CoreDNS is able to log messages/errors from
11+
// kubernetes client in a CoreDNS logging format
12+
type loggerAdapter struct {
13+
clog.P
14+
}
15+
16+
func (l *loggerAdapter) Init(_ logr.RuntimeInfo) {
17+
}
18+
19+
func (l *loggerAdapter) Enabled(_ int) bool {
20+
// verbosity is controlled inside klog library, we do not need to do anything here
21+
return true
22+
}
23+
24+
func (l *loggerAdapter) Info(_ int, msg string, _ ...interface{}) {
25+
l.P.Info(msg)
26+
}
27+
28+
func (l *loggerAdapter) Error(_ error, msg string, _ ...interface{}) {
29+
l.P.Error(msg)
30+
}
31+
32+
func (l *loggerAdapter) WithValues(_ ...interface{}) logr.LogSink {
33+
return l
34+
}
35+
36+
func (l *loggerAdapter) WithName(_ string) logr.LogSink {
37+
return l
38+
}

plugin/kubernetes/setup.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"os"
87
"strconv"
98
"strings"
109

@@ -15,6 +14,7 @@ import (
1514
clog "github.com/coredns/coredns/plugin/pkg/log"
1615
"github.com/coredns/coredns/plugin/pkg/upstream"
1716

17+
"github.com/go-logr/logr"
1818
"github.com/miekg/dns"
1919
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
2020
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // pull this in here, because we want it excluded if plugin.cfg doesn't have k8s
@@ -32,7 +32,7 @@ func init() { plugin.Register(pluginName, setup) }
3232

3333
func setup(c *caddy.Controller) error {
3434
// Do not call klog.InitFlags(nil) here. It will cause reload to panic.
35-
klog.SetOutput(os.Stdout)
35+
klog.SetLogger(logr.New(&loggerAdapter{log}))
3636

3737
k, err := kubernetesParse(c)
3838
if err != nil {

0 commit comments

Comments
 (0)