Skip to content

Commit 68933e9

Browse files
committed
Make reconciler errors verbose debug logs
The reconciler errors are not terribly informative, they happen on a version conflict, and as well external services, but they are the way to trigger an exponential back-off. Change them to a verbose output level, so we can still see them if requested.
1 parent 8fcf045 commit 68933e9

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

cmd/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2626
// to ensure that exec-entrypoint and run can make use of them.
27+
"go.uber.org/zap/zapcore"
2728
_ "k8s.io/client-go/plugin/pkg/client/auth"
2829

2930
"k8s.io/apimachinery/pkg/runtime"
@@ -85,11 +86,12 @@ func main() {
8586

8687
opts := zap.Options{
8788
Development: true,
89+
TimeEncoder: zapcore.ISO8601TimeEncoder,
8890
}
8991
opts.BindFlags(flag.CommandLine)
9092
flag.Parse()
9193

92-
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
94+
ctrl.SetLogger(controller.WrapLogger(zap.New(zap.UseFlagOptions(&opts))))
9395

9496
// if the enable-http2 flag is false (the default), http/2 should be disabled
9597
// due to its vulnerabilities. More specifically, disabling http/2 will

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ require (
4040
github.com/felixge/httpsnoop v1.0.4 // indirect
4141
github.com/fsnotify/fsnotify v1.9.0 // indirect
4242
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
43-
github.com/go-logr/logr v1.4.3 // indirect
43+
github.com/go-logr/logr v1.4.3
4444
github.com/go-logr/stdr v1.2.2 // indirect
4545
github.com/go-logr/zapr v1.3.0 // indirect
4646
github.com/go-openapi/jsonpointer v0.21.0 // indirect
@@ -82,7 +82,7 @@ require (
8282
go.opentelemetry.io/proto/otlp v1.5.0 // indirect
8383
go.uber.org/automaxprocs v1.6.0 // indirect
8484
go.uber.org/multierr v1.11.0 // indirect
85-
go.uber.org/zap v1.27.0 // indirect
85+
go.uber.org/zap v1.27.0
8686
golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67 // indirect
8787
golang.org/x/net v0.43.0 // indirect
8888
golang.org/x/oauth2 v0.28.0 // indirect

internal/controller/logger.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
SPDX-FileCopyrightText: Copyright 2024 SAP SE or an SAP affiliate company and cobaltcore-dev contributors
3+
SPDX-License-Identifier: Apache-2.0
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
package controller
19+
20+
import (
21+
"github.com/go-logr/logr"
22+
)
23+
24+
type wrapper struct {
25+
orig logr.LogSink
26+
}
27+
28+
// Enabled implements logr.LogSink.
29+
func (w wrapper) Enabled(level int) bool {
30+
return w.orig.Enabled(level)
31+
}
32+
33+
// Error implements logr.LogSink.
34+
func (w wrapper) Error(err error, msg string, keysAndValues ...any) {
35+
if msg == "Reconciler error" {
36+
w.orig.Info(2, msg, append(keysAndValues, "error", err.Error())...)
37+
} else {
38+
w.orig.Error(err, msg, keysAndValues...)
39+
}
40+
}
41+
42+
// Info implements logr.LogSink.
43+
func (w wrapper) Info(level int, msg string, keysAndValues ...any) {
44+
w.orig.Info(level, msg, keysAndValues...)
45+
}
46+
47+
// Init implements logr.LogSink.
48+
func (w wrapper) Init(info logr.RuntimeInfo) {
49+
w.orig.Init(info)
50+
}
51+
52+
// WithName implements logr.LogSink.
53+
func (w wrapper) WithName(name string) logr.LogSink {
54+
w.orig.WithName(name)
55+
return w
56+
}
57+
58+
// WithValues implements logr.LogSink.
59+
func (w wrapper) WithValues(keysAndValues ...any) logr.LogSink {
60+
w.orig.WithValues(keysAndValues...)
61+
return w
62+
}
63+
64+
func WrapLogger(logger logr.Logger) logr.Logger {
65+
orig := logger.GetSink()
66+
return logger.WithSink(wrapper{orig})
67+
}

0 commit comments

Comments
 (0)