Skip to content

Commit ca1b17a

Browse files
authored
INS-1614: rbac-manager: Error log in v1.9.3 (#571)
* Migrate logs to slog * Bump libs
1 parent dcb15b6 commit ca1b17a

11 files changed

Lines changed: 215 additions & 180 deletions

File tree

cmd/manager/main.go

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ package main
1818

1919
import (
2020
"flag"
21+
"log/slog"
2122
"net/http"
2223
"os"
24+
"strings"
2325

26+
"github.com/go-logr/logr"
2427
"github.com/prometheus/client_golang/prometheus/promhttp"
25-
"github.com/sirupsen/logrus"
2628
_ "k8s.io/client-go/plugin/pkg/client/auth"
2729
"k8s.io/klog"
2830
"sigs.k8s.io/controller-runtime/pkg/client/config"
31+
ctrl "sigs.k8s.io/controller-runtime/pkg/log"
2932
"sigs.k8s.io/controller-runtime/pkg/manager"
3033
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
3134

@@ -36,78 +39,117 @@ import (
3639
"github.com/fairwindsops/rbac-manager/version"
3740
)
3841

39-
var logLevel = flag.String("log-level", logrus.InfoLevel.String(), "Logrus log level")
42+
var logLevel = flag.String("log-level", "info", "Log level (debug, info, warn, error)")
4043
var addr = flag.String("metrics-address", ":8042", "The address to serve prometheus metrics.")
4144

4245
func init() {
4346
klog.InitFlags(nil)
4447
}
4548

49+
func parseLogLevel(level string) slog.Level {
50+
switch strings.ToLower(level) {
51+
case "debug":
52+
return slog.LevelDebug
53+
case "info":
54+
return slog.LevelInfo
55+
case "warn", "warning":
56+
return slog.LevelWarn
57+
case "error":
58+
return slog.LevelError
59+
default:
60+
return slog.LevelInfo
61+
}
62+
}
63+
64+
// slogToLogrAdapter adapts slog.Logger to logr.Logger for controller-runtime
65+
type slogToLogrAdapter struct {
66+
logger *slog.Logger
67+
}
68+
69+
func (a *slogToLogrAdapter) Init(info logr.RuntimeInfo) {}
70+
func (a *slogToLogrAdapter) Enabled(level int) bool { return true }
71+
func (a *slogToLogrAdapter) Info(level int, msg string, keysAndValues ...interface{}) {
72+
a.logger.Info(msg, keysAndValues...)
73+
}
74+
func (a *slogToLogrAdapter) Error(err error, msg string, keysAndValues ...interface{}) {
75+
args := append([]interface{}{"error", err}, keysAndValues...)
76+
a.logger.Error(msg, args...)
77+
}
78+
func (a *slogToLogrAdapter) WithValues(keysAndValues ...interface{}) logr.LogSink {
79+
return &slogToLogrAdapter{logger: a.logger.With(keysAndValues...)}
80+
}
81+
func (a *slogToLogrAdapter) WithName(name string) logr.LogSink {
82+
return &slogToLogrAdapter{logger: a.logger.With("name", name)}
83+
}
84+
4685
func main() {
4786
flag.Parse()
4887

49-
parsedLevel, err := logrus.ParseLevel(*logLevel)
50-
if err != nil {
51-
// This should theoretically never happen
52-
logrus.Errorf("log-level flag has invalid value %s", *logLevel)
53-
} else {
54-
logrus.SetLevel(parsedLevel)
88+
level := parseLogLevel(*logLevel)
89+
opts := &slog.HandlerOptions{
90+
Level: level,
5591
}
92+
logger := slog.New(slog.NewTextHandler(os.Stderr, opts))
93+
slog.SetDefault(logger)
94+
95+
// Set up controller-runtime logger to use slog via adapter
96+
logrLogger := logr.New(&slogToLogrAdapter{logger: logger})
97+
ctrl.SetLogger(logrLogger)
5698

57-
logrus.Info("----------------------------------")
58-
logrus.Infof("rbac-manager %v running", version.Version)
59-
logrus.Info("----------------------------------")
99+
slog.Info("----------------------------------")
100+
slog.Info("rbac-manager running", "version", version.Version)
101+
slog.Info("----------------------------------")
60102

61103
// Get a config to talk to the apiserver
62-
logrus.Debug("Setting up client for manager")
104+
slog.Debug("Setting up client for manager")
63105
cfg, err := config.GetConfig()
64106
if err != nil {
65-
logrus.Error(err, ": unable to set up client config")
107+
slog.Error("unable to set up client config", "error", err)
66108
os.Exit(1)
67109
}
68110

69111
// Create a new Cmd to provide shared dependencies and start components
70-
logrus.Debug("Setting up manager")
112+
slog.Debug("Setting up manager")
71113
mgr, err := manager.New(cfg, manager.Options{})
72114
if err != nil {
73-
logrus.Error(err, ": unable to set up overall controller manager")
115+
slog.Error("unable to set up overall controller manager", "error", err)
74116
os.Exit(1)
75117
}
76118

77-
logrus.Info("Registering components")
119+
slog.Info("Registering components")
78120

79121
// Setup Scheme for all resources
80-
logrus.Debug("Setting up scheme")
122+
slog.Debug("Setting up scheme")
81123
if err := apis.AddToScheme(mgr.GetScheme()); err != nil {
82-
logrus.Error(err, ": unable add APIs to scheme")
124+
slog.Error("unable add APIs to scheme", "error", err)
83125
os.Exit(1)
84126
}
85127

86128
// Setup all Controllers
87-
logrus.Debug("Setting up controller")
129+
slog.Debug("Setting up controller")
88130
if err := controller.Add(mgr); err != nil {
89-
logrus.Error(err, ": unable to register controller to the manager")
131+
slog.Error("unable to register controller to the manager", "error", err)
90132
os.Exit(1)
91133
}
92134

93135
// Watch Related Resources
94-
logrus.Info("Watching resources related to RBAC Definitions")
136+
slog.Info("Watching resources related to RBAC Definitions")
95137
watcher.WatchRelatedResources()
96138

97139
// Start metrics endpoint
98140
go func() {
99141
metrics.RegisterMetrics()
100142
http.Handle("/metrics", promhttp.Handler())
101143
if err := http.ListenAndServe(*addr, nil); err != nil {
102-
logrus.Error(err, ": unable to serve the metrics endpoint")
144+
slog.Error("unable to serve the metrics endpoint", "error", err)
103145
os.Exit(1)
104146
}
105147
}()
106148

107149
// Start the Cmd
108-
logrus.Info("Watching RBAC Definitions")
150+
slog.Info("Watching RBAC Definitions")
109151
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
110-
logrus.Error(err, ": unable to run the manager")
152+
slog.Error("unable to run the manager", "error", err)
111153
os.Exit(1)
112154
}
113155
}

go.mod

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ go 1.24.0
55
toolchain go1.24.6
66

77
require (
8+
github.com/go-logr/logr v1.4.3
89
github.com/prometheus/client_golang v1.23.2
9-
github.com/sirupsen/logrus v1.9.3
1010
github.com/stretchr/testify v1.11.1
11-
k8s.io/api v0.34.1
12-
k8s.io/apimachinery v0.34.1
13-
k8s.io/client-go v0.34.1
11+
k8s.io/api v0.34.2
12+
k8s.io/apimachinery v0.34.2
13+
k8s.io/client-go v0.34.2
1414
k8s.io/klog v1.0.0
15-
sigs.k8s.io/controller-runtime v0.22.1
15+
sigs.k8s.io/controller-runtime v0.22.4
1616
)
1717

1818
require (
@@ -24,56 +24,52 @@ require (
2424
github.com/evanphx/json-patch/v5 v5.9.11 // indirect
2525
github.com/fsnotify/fsnotify v1.9.0 // indirect
2626
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
27-
github.com/go-logr/logr v1.4.3 // indirect
28-
github.com/go-openapi/jsonpointer v0.22.0 // indirect
29-
github.com/go-openapi/jsonreference v0.21.1 // indirect
30-
github.com/go-openapi/swag v0.24.1 // indirect
31-
github.com/go-openapi/swag/cmdutils v0.24.0 // indirect
32-
github.com/go-openapi/swag/conv v0.24.0 // indirect
33-
github.com/go-openapi/swag/fileutils v0.24.0 // indirect
34-
github.com/go-openapi/swag/jsonname v0.24.0 // indirect
35-
github.com/go-openapi/swag/jsonutils v0.24.0 // indirect
36-
github.com/go-openapi/swag/loading v0.24.0 // indirect
37-
github.com/go-openapi/swag/mangling v0.24.0 // indirect
38-
github.com/go-openapi/swag/netutils v0.24.0 // indirect
39-
github.com/go-openapi/swag/stringutils v0.24.0 // indirect
40-
github.com/go-openapi/swag/typeutils v0.24.0 // indirect
41-
github.com/go-openapi/swag/yamlutils v0.24.0 // indirect
27+
github.com/go-openapi/jsonpointer v0.22.3 // indirect
28+
github.com/go-openapi/jsonreference v0.21.3 // indirect
29+
github.com/go-openapi/swag v0.25.3 // indirect
30+
github.com/go-openapi/swag/cmdutils v0.25.3 // indirect
31+
github.com/go-openapi/swag/conv v0.25.3 // indirect
32+
github.com/go-openapi/swag/fileutils v0.25.3 // indirect
33+
github.com/go-openapi/swag/jsonname v0.25.3 // indirect
34+
github.com/go-openapi/swag/jsonutils v0.25.3 // indirect
35+
github.com/go-openapi/swag/loading v0.25.3 // indirect
36+
github.com/go-openapi/swag/mangling v0.25.3 // indirect
37+
github.com/go-openapi/swag/netutils v0.25.3 // indirect
38+
github.com/go-openapi/swag/stringutils v0.25.3 // indirect
39+
github.com/go-openapi/swag/typeutils v0.25.3 // indirect
40+
github.com/go-openapi/swag/yamlutils v0.25.3 // indirect
4241
github.com/gogo/protobuf v1.3.2 // indirect
4342
github.com/google/btree v1.1.3 // indirect
4443
github.com/google/gnostic-models v0.7.0 // indirect
4544
github.com/google/go-cmp v0.7.0 // indirect
4645
github.com/google/uuid v1.6.0 // indirect
47-
github.com/josharian/intern v1.0.0 // indirect
4846
github.com/json-iterator/go v1.1.12 // indirect
49-
github.com/mailru/easyjson v0.9.1 // indirect
5047
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
5148
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
5249
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
5350
github.com/pmezard/go-difflib v1.0.0 // indirect
5451
github.com/prometheus/client_model v0.6.2 // indirect
55-
github.com/prometheus/common v0.66.1 // indirect
56-
github.com/prometheus/procfs v0.17.0 // indirect
52+
github.com/prometheus/common v0.67.3 // indirect
53+
github.com/prometheus/procfs v0.19.2 // indirect
5754
github.com/spf13/pflag v1.0.10 // indirect
5855
github.com/x448/float16 v0.8.4 // indirect
5956
go.yaml.in/yaml/v2 v2.4.3 // indirect
6057
go.yaml.in/yaml/v3 v3.0.4 // indirect
61-
golang.org/x/net v0.44.0 // indirect
62-
golang.org/x/oauth2 v0.31.0 // indirect
63-
golang.org/x/sync v0.17.0 // indirect
64-
golang.org/x/sys v0.36.0 // indirect
65-
golang.org/x/term v0.35.0 // indirect
66-
golang.org/x/text v0.29.0 // indirect
67-
golang.org/x/time v0.13.0 // indirect
68-
golang.org/x/tools v0.37.0 // indirect
58+
golang.org/x/net v0.47.0 // indirect
59+
golang.org/x/oauth2 v0.33.0 // indirect
60+
golang.org/x/sync v0.18.0 // indirect
61+
golang.org/x/sys v0.38.0 // indirect
62+
golang.org/x/term v0.37.0 // indirect
63+
golang.org/x/text v0.31.0 // indirect
64+
golang.org/x/time v0.14.0 // indirect
6965
gomodules.xyz/jsonpatch/v2 v2.5.0 // indirect
70-
google.golang.org/protobuf v1.36.9 // indirect
66+
google.golang.org/protobuf v1.36.10 // indirect
7167
gopkg.in/evanphx/json-patch.v4 v4.13.0 // indirect
7268
gopkg.in/inf.v0 v0.9.1 // indirect
7369
gopkg.in/yaml.v3 v3.0.1 // indirect
7470
k8s.io/klog/v2 v2.130.1 // indirect
7571
k8s.io/kube-openapi v0.0.0-20250910181357-589584f1c912 // indirect
76-
k8s.io/utils v0.0.0-20250820121507-0af2bda4dd1d // indirect
72+
k8s.io/utils v0.0.0-20251002143259-bc988d571ff4 // indirect
7773
sigs.k8s.io/json v0.0.0-20250730193827-2d320260d730 // indirect
7874
sigs.k8s.io/randfill v1.0.0 // indirect
7975
sigs.k8s.io/structured-merge-diff/v6 v6.3.0 // indirect

0 commit comments

Comments
 (0)