diff --git a/client/client.go b/client/client.go index c7b7581..cffa57e 100644 --- a/client/client.go +++ b/client/client.go @@ -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" @@ -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 ( @@ -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 @@ -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") @@ -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 } @@ -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 } diff --git a/go.mod b/go.mod index 072e643..a2b69bd 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index 1d98caa..e345bd6 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= @@ -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= @@ -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= diff --git a/klogtologrus/adapter.go b/klogtolog/adapter.go similarity index 68% rename from klogtologrus/adapter.go rename to klogtolog/adapter.go index cef4aa9..a619c08 100644 --- a/klogtologrus/adapter.go +++ b/klogtolog/adapter.go @@ -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 @@ -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) { diff --git a/klogtologrus/test/adapter_test.go b/klogtolog/test/adapter_test.go similarity index 87% rename from klogtologrus/test/adapter_test.go rename to klogtolog/test/adapter_test.go index 9045c6f..f570891 100644 --- a/klogtologrus/test/adapter_test.go +++ b/klogtolog/test/adapter_test.go @@ -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 @@ -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", }, { @@ -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") diff --git a/klogtologrus/test/klog-powered-lib/lib_methods.go b/klogtolog/test/klog-powered-lib/lib_methods.go similarity index 100% rename from klogtologrus/test/klog-powered-lib/lib_methods.go rename to klogtolog/test/klog-powered-lib/lib_methods.go diff --git a/klogtolog/test/service/some_service.go b/klogtolog/test/service/some_service.go new file mode 100644 index 0000000..bcde462 --- /dev/null +++ b/klogtolog/test/service/some_service.go @@ -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() +} diff --git a/klogtologrus/test/service/some_service.go b/klogtologrus/test/service/some_service.go deleted file mode 100644 index 27ff9b1..0000000 --- a/klogtologrus/test/service/some_service.go +++ /dev/null @@ -1,16 +0,0 @@ -package service - -import ( - log "github.com/sirupsen/logrus" - - _ "github.com/flant/kube-client/klogtologrus" // plug in JSON logs adapter - klog_powered_lib "github.com/flant/kube-client/klogtologrus/test/klog-powered-lib" -) - -func DoWithCallToKlogPoweredLib() { - log.Trace("service action") - - klog_powered_lib.ActionWithKlogWarn() - klog_powered_lib.ActionWithKlogInfo() - klog_powered_lib.ActionWithKlogError() -}