From e67924f46bd6a8b6b19546c68067cb31bd8167f1 Mon Sep 17 00:00:00 2001 From: Olivier Tardieu Date: Tue, 30 Jul 2024 15:55:40 -0400 Subject: [PATCH] Downgrade StatusReasonConflict errors to debug messages --- go.mod | 2 +- main.go | 2 +- pkg/controllers/support.go | 40 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index a36f1c4b..56831aa5 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/project-codeflare/codeflare-operator go 1.22.2 require ( + github.com/go-logr/logr v1.4.2 github.com/onsi/ginkgo/v2 v2.19.0 github.com/onsi/gomega v1.33.1 github.com/open-policy-agent/cert-controller v0.10.1 @@ -51,7 +52,6 @@ require ( github.com/evanphx/json-patch v5.6.0+incompatible // indirect github.com/evanphx/json-patch/v5 v5.8.0 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect - github.com/go-logr/logr v1.4.2 // indirect github.com/go-logr/zapr v1.3.0 // indirect github.com/go-openapi/jsonpointer v0.20.0 // indirect github.com/go-openapi/jsonreference v0.20.2 // indirect diff --git a/main.go b/main.go index 77e0c90a..b0b2d0c5 100644 --- a/main.go +++ b/main.go @@ -116,7 +116,7 @@ func main() { zapOptions.BindFlags(flag.CommandLine) flag.Parse() - ctrl.SetLogger(zap.New(zap.UseFlagOptions(&zapOptions))) + ctrl.SetLogger(controllers.FilteredLogger(zap.New(zap.UseFlagOptions(&zapOptions)))) klog.SetLogger(ctrl.Log) setupLog.Info("Build info", diff --git a/pkg/controllers/support.go b/pkg/controllers/support.go index 61ba32d2..f3f5fbec 100644 --- a/pkg/controllers/support.go +++ b/pkg/controllers/support.go @@ -3,11 +3,13 @@ package controllers import ( "os" + "github.com/go-logr/logr" rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1" corev1 "k8s.io/api/core/v1" networkingv1 "k8s.io/api/networking/v1" "k8s.io/apimachinery/pkg/api/equality" + "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/util/intstr" "k8s.io/apimachinery/pkg/util/validation/field" v1 "k8s.io/client-go/applyconfigurations/meta/v1" @@ -172,3 +174,41 @@ func withEnvVarName(name string) compare[corev1.EnvVar] { return e1.Name == name } } + +// logSink implements a log sink with an error log filter +type logSink struct { + sink logr.LogSink +} + +func (l logSink) Init(info logr.RuntimeInfo) { + l.sink.Init(info) +} + +func (l logSink) Enabled(level int) bool { + return l.sink.Enabled(level) +} +func (l logSink) Info(level int, msg string, keysAndValues ...any) { + l.sink.Info(level, msg, keysAndValues...) +} + +func (l logSink) Error(err error, msg string, keysAndValues ...any) { + // downgrade StatusReasonConflict errors to debug messages + if errors.IsConflict(err) { + l.sink.Info(1, msg, append(keysAndValues, "error", err.Error())...) + } else { + l.sink.Error(err, msg, keysAndValues...) + } +} + +func (l logSink) WithValues(keysAndValues ...any) logr.LogSink { + return logSink{l.sink.WithValues(keysAndValues...)} +} + +func (l logSink) WithName(name string) logr.LogSink { + return logSink{l.sink.WithName(name)} +} + +// FilteredLogger returns a copy of the logger with an error log filter +func FilteredLogger(logger logr.Logger) logr.Logger { + return logger.WithSink(logSink{logger.GetSink()}) +}