Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Downgrade StatusReasonConflict errors to debug messages #603

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
varshaprasad96 marked this conversation as resolved.
Show resolved Hide resolved
klog.SetLogger(ctrl.Log)

setupLog.Info("Build info",
Expand Down
40 changes: 40 additions & 0 deletions pkg/controllers/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()})
}