Skip to content

Commit f828a97

Browse files
committed
Try hiding the client-go warning logs
Signed-off-by: Richard Wall <[email protected]>
1 parent f644b84 commit f828a97

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

pkg/datagatherer/k8s/dynamic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ func (g *DataGathererDynamic) Run(stopCh <-chan struct{}) error {
277277

278278
// attach WatchErrorHandler, it needs to be set before starting an informer
279279
err := g.informer.SetWatchErrorHandler(func(r *k8scache.Reflector, err error) {
280-
if strings.Contains(fmt.Sprintf("%s", err), "the server could not find the requested resource") {
280+
if strings.Contains(err.Error(), logs.FilteredMessageReflectorFailedToList) {
281281
log.V(logs.Debug).Info("Server missing resource for datagatherer", "groupVersionResource", g.groupVersionResource)
282282
} else {
283283
log.Info("datagatherer informer has failed and is backing off", "groupVersionResource", g.groupVersionResource, "reason", err)

pkg/logs/logs.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import (
77
"log/slog"
88
"strings"
99

10+
"github.com/go-logr/logr"
1011
"github.com/spf13/pflag"
1112
"k8s.io/apimachinery/pkg/util/runtime"
1213
"k8s.io/apimachinery/pkg/util/sets"
1314
"k8s.io/component-base/featuregate"
1415
"k8s.io/component-base/logs"
1516
logsapi "k8s.io/component-base/logs/api/v1"
1617
_ "k8s.io/component-base/logs/json/register"
18+
"k8s.io/klog/v2"
1719
)
1820

1921
// venafi-kubernetes-agent follows [Kubernetes Logging Conventions] and writes
@@ -55,6 +57,21 @@ const (
5557
Info = 0
5658
Debug = 1
5759
Trace = 2
60+
61+
// Suppress the following logged warning:
62+
// > W1119 13:54:32.103779 119328 reflector.go:561] pkg/mod/k8s.io/[email protected]/tools/cache/reflector.go:243: failed to list route.openshift.io/v1, Resource=routes: the server could not find the requested resource
63+
//
64+
// Why? Because venafi-kubernetes-agent is expected to run in environments
65+
// where some of the DataGatherer resources are not yet installed.
66+
// If the resource is installed after venafi-kubernetes-agent starts, it
67+
// will begin collecting that resource.
68+
// If the resource is uninstalled after venafi-kubernetes-agent starts, it
69+
// will stop collecting that resource.
70+
// But if an API resource is not installed, the client-go reflector will
71+
// return an error **and** print a warning.
72+
// The error is already handled and logged in `k8s/dynamic.go`, so the
73+
// warning is redundant and causes unnecessary distraction to the user.
74+
FilteredMessageReflectorFailedToList = "failed to list"
5875
)
5976

6077
func init() {
@@ -131,6 +148,22 @@ func Initialize() error {
131148
// to the global log logger. It can be removed when this is fixed upstream
132149
// in vcert: https://github.com/Venafi/vcert/pull/512
133150
vcertLog.SetPrefix("")
151+
152+
// Set up filtering of distracting client-go logs.
153+
l := klog.Background()
154+
ls := &filteringLogSink{
155+
LogSink: l.GetSink(),
156+
}
157+
// This CallDepth is chosen by trial and error so that the logged line
158+
// numbers match the site of the `log.Info` in the code.
159+
// TODO(wallrj): Figure out how to automatically set the right call depth.
160+
// TODO(wallrj): This CallDepth is not correct for client-go logs, e.g.
161+
// > I1119 13:54:14.395382 118425 once.go:65] "Feature gate default state" feature="WatchListClient" enabled=false
162+
// ...should be...
163+
// > I1119 13:54:32.069621 119328 envvar.go:172] "Feature gate default state" feature="WatchListClient" enabled=false
164+
ls.Init(logr.RuntimeInfo{CallDepth: 6})
165+
klog.SetLogger(l.WithSink(ls))
166+
134167
return nil
135168
}
136169

@@ -153,3 +186,16 @@ func (w LogToSlogWriter) Write(p []byte) (n int, err error) {
153186
}
154187
return len(p), nil
155188
}
189+
190+
type filteringLogSink struct {
191+
logr.LogSink
192+
}
193+
194+
var _ logr.LogSink = &filteringLogSink{}
195+
196+
func (o *filteringLogSink) Info(level int, msg string, keysAndValues ...any) {
197+
if strings.Contains(msg, FilteredMessageReflectorFailedToList) {
198+
return
199+
}
200+
o.LogSink.Info(level, msg, keysAndValues...)
201+
}

0 commit comments

Comments
 (0)