Skip to content

Commit

Permalink
feat: init set controller-runtime logger
Browse files Browse the repository at this point in the history
Signed-off-by: CeerDecy <[email protected]>
  • Loading branch information
CeerDecy committed Oct 16, 2024
1 parent b46e4bf commit 3addcde
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
14 changes: 8 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ require (
)

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/Chronokeeper/anyxml v0.0.0-20160530174208-54457d8e98c6 // indirect
Expand Down Expand Up @@ -281,12 +282,14 @@ require (
github.com/evanphx/json-patch v5.9.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
github.com/expr-lang/expr v1.16.9 // indirect
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 // indirect
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect
github.com/fatih/camelcase v1.0.0 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/frankban/quicktest v1.14.6 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-faster/city v1.0.1 // indirect
Expand Down Expand Up @@ -347,6 +350,7 @@ require (
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/kedacore/keda/v2 v2.15.1
github.com/klauspost/compress v1.17.8 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/kr/pretty v0.3.1 // indirect
Expand Down Expand Up @@ -457,6 +461,7 @@ require (
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240730163845-b1a4ccb954bf // indirect
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
gopkg.in/fsnotify.v1 v1.4.7 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand All @@ -466,6 +471,7 @@ require (
k8s.io/cli-runtime v0.30.0 // indirect
k8s.io/component-base v0.30.2 // indirect
k8s.io/kube-openapi v0.0.0-20240403164606-bc84c2ddaf99 // indirect
knative.dev/pkg v0.0.0-20240805063731-c88d5dad9653 // indirect
moul.io/http2curl v1.0.0 // indirect
oras.land/oras-go v1.2.4 // indirect
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.29.0 // indirect
Expand All @@ -474,14 +480,10 @@ require (
sigs.k8s.io/kustomize/api v0.17.3 // indirect
sigs.k8s.io/kustomize/kyaml v0.17.2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/expr-lang/expr v1.16.9 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/kedacore/keda/v2 v2.15.1
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
knative.dev/pkg v0.0.0-20240805063731-c88d5dad9653 // indirect
)

require github.com/sykesm/zap-logfmt v0.0.4 // indirect

replace (
erda.cloud/rocketmq => github.com/erda-project/rocketmq-operator v0.0.0-20221222075906-f28c42d7bf23
github.com/containerd/containerd => github.com/containerd/containerd v1.4.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2626,6 +2626,8 @@ github.com/swaggo/http-swagger v0.0.0-20200308142732-58ac5e232fba/go.mod h1:O1lA
github.com/swaggo/swag v1.5.1/go.mod h1:1Bl9F/ZBpVWh22nY0zmYyASPO1lI/zIwRDrpZU+tv8Y=
github.com/swaggo/swag v1.6.3/go.mod h1:wcc83tB4Mb2aNiL/HP4MFeQdpHUrca+Rp/DRNgWAUio=
github.com/swaggo/swag v1.6.6-0.20200529100950-7c765ddd0476/go.mod h1:xDhTyuFIujYiN3DKWC/H/83xcfHp+UE/IzWWampG7Zc=
github.com/sykesm/zap-logfmt v0.0.4 h1:U2WzRvmIWG1wDLCFY3sz8UeEmsdHQjHFNlIdmroVFaI=
github.com/sykesm/zap-logfmt v0.0.4/go.mod h1:AuBd9xQjAe3URrWT1BBDk2v2onAZHkZkWRMiYZXiZWA=
github.com/syndtr/goleveldb v0.0.0-20180815032940-ae2bd5eed72d/go.mod h1:Z4AUp2Km+PwemOoO/VB5AOx9XSsIItzFjoJlOSiYmn0=
github.com/syndtr/goleveldb v1.0.1-0.20190625010220-02440ea7a285 h1:uSDYjYejelKyceA6DiCsngFof9jAyeaSyX9XC5a1a7Q=
github.com/syndtr/goleveldb v1.0.1-0.20190625010220-02440ea7a285/go.mod h1:9OrXJhf154huy1nPWmuSrkgjPUtUNhA+Zmy+6AESzuA=
Expand Down
25 changes: 25 additions & 0 deletions pkg/k8sclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,42 @@ import (
"os"
"time"

zaplogfmt "github.com/sykesm/zap-logfmt"
uzap "go.uber.org/zap"
"go.uber.org/zap/zapcore"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/bundle"
"github.com/erda-project/erda/pkg/k8sclient/config"
"github.com/erda-project/erda/pkg/k8sclient/scheme"
)

func init() {
leveler := uzap.LevelEnablerFunc(func(level zapcore.Level) bool {
// Set the level fairly high since it's so verbose
return level >= zapcore.DPanicLevel
})
stackTraceLeveler := uzap.LevelEnablerFunc(func(level zapcore.Level) bool {
// Attempt to suppress the stack traces in the logs since they are so verbose.
// The controller runtime seems to ignore this since the stack is still always printed.
return false
})

Check warning on line 46 in pkg/k8sclient/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/k8sclient/client.go#L45-L46

Added lines #L45 - L46 were not covered by tests
logfmtEncoder := zaplogfmt.NewEncoder(uzap.NewProductionEncoderConfig())
logger := zap.New(
zap.Level(leveler),
zap.StacktraceLevel(stackTraceLeveler),
zap.UseDevMode(false),
zap.WriteTo(os.Stdout),
zap.Encoder(logfmtEncoder))
log.SetLogger(logger)
}

type K8sClient struct {
// custom options
timeout *time.Duration
Expand Down

0 comments on commit 3addcde

Please sign in to comment.