plugin/kubernetes : make kubernetes client log in CoreDNS format (#5461)
Signed-off-by: Ondřej Benkovský <ondrej.benkovsky@jamf.com>
This commit is contained in:
parent
4a40e9e3a0
commit
e0dead4aa2
2 changed files with 40 additions and 2 deletions
38
plugin/kubernetes/logger.go
Normal file
38
plugin/kubernetes/logger.go
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
package kubernetes
|
||||||
|
|
||||||
|
import (
|
||||||
|
clog "github.com/coredns/coredns/plugin/pkg/log"
|
||||||
|
|
||||||
|
"github.com/go-logr/logr"
|
||||||
|
)
|
||||||
|
|
||||||
|
// loggerAdapter is a simple wrapper around CoreDNS plugin logger made to implement logr.LogSink interface, which is used
|
||||||
|
// as part of klog library for logging in Kubernetes client. By using this adapter CoreDNS is able to log messages/errors from
|
||||||
|
// kubernetes client in a CoreDNS logging format
|
||||||
|
type loggerAdapter struct {
|
||||||
|
clog.P
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *loggerAdapter) Init(_ logr.RuntimeInfo) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *loggerAdapter) Enabled(_ int) bool {
|
||||||
|
// verbosity is controlled inside klog library, we do not need to do anything here
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *loggerAdapter) Info(_ int, msg string, _ ...interface{}) {
|
||||||
|
l.P.Info(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *loggerAdapter) Error(_ error, msg string, _ ...interface{}) {
|
||||||
|
l.P.Error(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *loggerAdapter) WithValues(_ ...interface{}) logr.LogSink {
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *loggerAdapter) WithName(_ string) logr.LogSink {
|
||||||
|
return l
|
||||||
|
}
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -15,6 +14,7 @@ import (
|
||||||
clog "github.com/coredns/coredns/plugin/pkg/log"
|
clog "github.com/coredns/coredns/plugin/pkg/log"
|
||||||
"github.com/coredns/coredns/plugin/pkg/upstream"
|
"github.com/coredns/coredns/plugin/pkg/upstream"
|
||||||
|
|
||||||
|
"github.com/go-logr/logr"
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
|
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
_ "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
|
_ "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) }
|
||||||
|
|
||||||
func setup(c *caddy.Controller) error {
|
func setup(c *caddy.Controller) error {
|
||||||
// Do not call klog.InitFlags(nil) here. It will cause reload to panic.
|
// Do not call klog.InitFlags(nil) here. It will cause reload to panic.
|
||||||
klog.SetOutput(os.Stdout)
|
klog.SetLogger(logr.New(&loggerAdapter{log}))
|
||||||
|
|
||||||
k, err := kubernetesParse(c)
|
k, err := kubernetesParse(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Add table
Reference in a new issue