Skip to content

Commit

Permalink
Replace Logrus with slog (#18)
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com>
Co-authored-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com>
  • Loading branch information
ldmonster and Pavel Okhlopkov authored Nov 2, 2024
1 parent 7d2d3d5 commit 5d47ab4
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 48 deletions.
17 changes: 9 additions & 8 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"strings"
"time"

"github.com/deckhouse/deckhouse/pkg/log"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
apixv1client "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1"
apiErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -27,7 +27,7 @@ import (
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/metrics"

_ "github.com/flant/kube-client/klogtologrus" // route klog messages from client-go to logrus
_ "github.com/flant/kube-client/klogtolog" // route klog messages from client-go to log
)

const (
Expand Down Expand Up @@ -125,7 +125,8 @@ func (c *Client) RestConfig() *rest.Config {
}

func (c *Client) Init() error {
logEntry := log.WithField("operator.component", "KubernetesAPIClient")
logger := log.NewLogger(log.Options{})
logger = logger.With("operator.component", "KubernetesAPIClient")

var err error
var config *rest.Config
Expand All @@ -145,18 +146,18 @@ func (c *Client) Init() error {
if c.configPath != "" || c.contextName != "" {
if outOfClusterErr != nil {
err = fmt.Errorf("out-of-cluster config error: %v, in-cluster config error: %v", outOfClusterErr, err)
logEntry.Errorf("configuration problems: %s", err)
logger.Errorf("configuration problems: %s", err)
return err
}
return fmt.Errorf("in-cluster config is not found")
}
logEntry.Errorf("in-cluster problem: %s", err)
logger.Errorf("in-cluster problem: %s", err)
return err
}
} else {
// if not in cluster return outOfCluster error
if outOfClusterErr != nil {
logEntry.Errorf("out-of-cluster problem: %s", outOfClusterErr)
logger.Errorf("out-of-cluster problem: %s", outOfClusterErr)
return outOfClusterErr
}
return fmt.Errorf("no kubernetes client config found")
Expand All @@ -182,7 +183,7 @@ func (c *Client) Init() error {

c.Interface, err = kubernetes.NewForConfig(config)
if err != nil {
logEntry.Errorf("configuration problem: %s", err)
logger.Errorf("configuration problem: %s", err)
return err
}

Expand Down Expand Up @@ -226,7 +227,7 @@ func (c *Client) Init() error {
}

c.restConfig = config
logEntry.Infof("Kubernetes client is configured successfully with '%s' config", configType)
logger.Infof("Kubernetes client is configured successfully with '%s' config", configType)

return nil
}
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module github.com/flant/kube-client

go 1.21
go 1.22.8

toolchain go1.23.1

require (
github.com/deckhouse/deckhouse/pkg/log v0.0.0-20241102110904-83c5e473e0ca
github.com/onsi/gomega v1.29.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.16.0
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
k8s.io/api v0.29.8
k8s.io/apiextensions-apiserver v0.28.4
k8s.io/apimachinery v0.29.8
Expand All @@ -19,6 +19,7 @@ require (
)

require (
github.com/DataDog/gostackparse v0.7.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
11 changes: 6 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/DataDog/gostackparse v0.7.0 h1:i7dLkXHvYzHV308hnkvVGDL3BR4FWl7IsXNPz/IGQh4=
github.com/DataDog/gostackparse v0.7.0/go.mod h1:lTfqcJKqS9KnXQGnyQMCugq3u1FP6UZMfWR0aitKFMM=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
Expand All @@ -6,6 +8,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckhouse/deckhouse/pkg/log v0.0.0-20241102110904-83c5e473e0ca h1:qbVHWHa4sDRW55Xbui4vpL1XgJbrrdVr6iQARARiFWQ=
github.com/deckhouse/deckhouse/pkg/log v0.0.0-20241102110904-83c5e473e0ca/go.mod h1:Mk5HRzkc5pIcDIZ2JJ6DPuuqnwhXVkb3you8M8Mg+4w=
github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g=
github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH5pOlLGNtQ5lPWQu84=
Expand Down Expand Up @@ -88,8 +92,6 @@ github.com/prometheus/procfs v0.10.1 h1:kYK1Va/YMlutzCGazswoHKo//tZVlFpKYh+Pymzi
github.com/prometheus/procfs v0.10.1/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -100,8 +102,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand All @@ -125,7 +127,6 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=
Expand Down
14 changes: 7 additions & 7 deletions klogtologrus/adapter.go → klogtolog/adapter.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
// Package klogtologrus overrides output writer for klog to log messages with logrus.
// Package klogtolog overrides output writer for klog to log messages with log.
//
// Usage:
//
// import (
// _ "github.com/flant/kube-client/klogtologrus"
// _ "github.com/flant/kube-client/klogtolog"
// )
package klogtologrus
package klogtolog

import (
"flag"

log "github.com/sirupsen/logrus"
log "github.com/deckhouse/deckhouse/pkg/log"
"k8s.io/klog/v2"
)

func InitAdapter(enableDebug bool) {
func InitAdapter(enableDebug bool, logger *log.Logger) {
// - turn off logging to stderr
// - default stderr threshold is ERROR and it outputs errors to stderr, set it to FATAL
// - set writer for INFO severity to catch all messages
Expand All @@ -30,11 +30,11 @@ func InitAdapter(enableDebug bool) {
}

_ = klogFlagSet.Parse(args)
klog.SetOutputBySeverity("INFO", &writer{logger: log.WithField("source", "klog")})
klog.SetOutputBySeverity("INFO", &writer{logger: logger.With("source", "klog")})
}

type writer struct {
logger *log.Entry
logger *log.Logger
}

func (w *writer) Write(msg []byte) (n int, err error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"sync"
"testing"

log "github.com/deckhouse/deckhouse/pkg/log"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
log "github.com/sirupsen/logrus"

"github.com/flant/kube-client/klogtologrus"
"github.com/flant/kube-client/klogtologrus/test/service"
klogtolog "github.com/flant/kube-client/klogtolog"
"github.com/flant/kube-client/klogtolog/test/service"
)

// Test that adapter is working through default import in another package
Expand All @@ -24,16 +23,16 @@ func Test_adapter_catches_klog_WarnInfoError(t *testing.T) {

buf := gbytes.NewBuffer()

log.SetOutput(buf)
log.SetFormatter(&log.JSONFormatter{DisableTimestamp: true})
klogtologrus.InitAdapter(false)
logger := log.NewLogger(log.Options{})
logger.SetOutput(buf)
klogtolog.InitAdapter(false, logger)

tests := []struct {
level string
msg string
}{
{
"warning",
"warn",
"Warning from klog powered lib",
},
{
Expand Down Expand Up @@ -75,7 +74,8 @@ func Test_adapter_catches_klog_WarnInfoError(t *testing.T) {
func Test_klog_should_not_output_to_Stderr(t *testing.T) {
g := NewWithT(t)

log.SetOutput(io.Discard)
logger := log.NewNop()
log.SetDefault(logger)

stderr := captureStderr(func() {
fmt.Fprintf(os.Stderr, "asdasdasd")
Expand Down
16 changes: 16 additions & 0 deletions klogtolog/test/service/some_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package service

import (
log "github.com/deckhouse/deckhouse/pkg/log"

_ "github.com/flant/kube-client/klogtolog" // plug in JSON logs adapter
klog_powered_lib "github.com/flant/kube-client/klogtolog/test/klog-powered-lib"
)

func DoWithCallToKlogPoweredLib() {
log.Debug("service action")

klog_powered_lib.ActionWithKlogWarn()
klog_powered_lib.ActionWithKlogInfo()
klog_powered_lib.ActionWithKlogError()
}
16 changes: 0 additions & 16 deletions klogtologrus/test/service/some_service.go

This file was deleted.

0 comments on commit 5d47ab4

Please sign in to comment.