Skip to content

Commit 5b9ff22

Browse files
committed
Migrate from logrus to log/slog
rebase with master
1 parent 3517a5b commit 5b9ff22

File tree

50 files changed

+341
-258
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+341
-258
lines changed

cmd/cmd.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20-
"github.com/sirupsen/logrus"
20+
"log"
21+
"log/slog"
22+
"os"
23+
2124
"github.com/spf13/afero"
2225

2326
"sigs.k8s.io/kubebuilder/v4/pkg/cli"
2427
cfgv3 "sigs.k8s.io/kubebuilder/v4/pkg/config/v3"
28+
"sigs.k8s.io/kubebuilder/v4/pkg/logging"
2529
"sigs.k8s.io/kubebuilder/v4/pkg/machinery"
2630
"sigs.k8s.io/kubebuilder/v4/pkg/plugin"
2731
kustomizecommonv2 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/common/kustomize/v2"
@@ -33,8 +37,14 @@ import (
3337
)
3438

3539
func init() {
36-
// Disable timestamps on the default TextFormatter
37-
logrus.SetFormatter(&logrus.TextFormatter{DisableTimestamp: true})
40+
opts := logging.HandlerOptions{
41+
SlogOpts: slog.HandlerOptions{
42+
Level: slog.LevelInfo,
43+
},
44+
}
45+
handler := logging.NewHandler(os.Stdout, opts)
46+
logger := slog.New(handler)
47+
slog.SetDefault(logger)
3848
}
3949

4050
// Run bootstraps & runs the CLI
@@ -50,7 +60,7 @@ func Run() {
5060
}
5161
externalPlugins, err := cli.DiscoverExternalPlugins(fs.FS)
5262
if err != nil {
53-
logrus.Error(err)
63+
slog.Error("error discovering external plugins", "error", err)
5464
}
5565

5666
c, err := cli.New(
@@ -71,9 +81,9 @@ func Run() {
7181
cli.WithCompletion(),
7282
)
7383
if err != nil {
74-
logrus.Fatal(err)
84+
log.Fatal(err)
7585
}
7686
if err := c.Run(); err != nil {
77-
logrus.Fatal(err)
87+
log.Fatal(err)
7888
}
7989
}

docs/book/src/plugins/extending/extending_cli_features_and_plugins.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ program in `kubebuilder init`. Following an example:
233233
package cli
234234

235235
import (
236-
log "github.com/sirupsen/logrus"
236+
log "log/slog"
237237
"github.com/spf13/cobra"
238238

239239
"sigs.k8s.io/kubebuilder/v4/pkg/cli"

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ require (
66
github.com/gobuffalo/flect v1.0.3
77
github.com/onsi/ginkgo/v2 v2.23.4
88
github.com/onsi/gomega v1.38.0
9-
github.com/sirupsen/logrus v1.9.3
109
github.com/spf13/afero v1.14.0
1110
github.com/spf13/cobra v1.9.1
1211
github.com/spf13/pflag v1.0.7

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/f
4848
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
4949
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
5050
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
51-
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
52-
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
5351
github.com/spf13/afero v1.14.0 h1:9tH6MapGnn/j0eb0yIXiLjERO8RB6xIVZRDCX7PtqWA=
5452
github.com/spf13/afero v1.14.0/go.mod h1:acJQ8t0ohCGuMN3O+Pv0V0hgMxNYDlvdk+VTfyZmbYo=
5553
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
@@ -60,7 +58,6 @@ github.com/spf13/pflag v1.0.7/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
6058
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6159
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
6260
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
63-
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
6461
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
6562
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
6663
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
@@ -78,7 +75,6 @@ golang.org/x/net v0.42.0 h1:jzkYrhi3YQWD6MLBJcsklgQsoAcw89EcZbJw8Z614hs=
7875
golang.org/x/net v0.42.0/go.mod h1:FF1RA5d3u7nAYA4z2TkclSCKh68eSXtiFwcWQpPXdt8=
7976
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
8077
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
81-
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
8278
golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=
8379
golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
8480
golang.org/x/text v0.27.0 h1:4fGWRpyh641NLlecmyl4LOe6yDdfaYNrGb2zdfo4JV4=

hack/docs/generate_samples.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ limitations under the License.
1717
package main
1818

1919
import (
20-
log "github.com/sirupsen/logrus"
20+
"log/slog"
21+
"os"
2122

2223
cronjob "sigs.k8s.io/kubebuilder/v4/hack/docs/internal/cronjob-tutorial"
2324
gettingstarted "sigs.k8s.io/kubebuilder/v4/hack/docs/internal/getting-started"
2425
multiversion "sigs.k8s.io/kubebuilder/v4/hack/docs/internal/multiversion-tutorial"
26+
"sigs.k8s.io/kubebuilder/v4/pkg/logging"
2527
)
2628

2729
// KubebuilderBinName make sure executing `build_kb` to generate kb executable from the source code
@@ -43,11 +45,18 @@ func main() {
4345
"multiversion": updateMultiversionTutorial,
4446
}
4547

46-
log.SetFormatter(&log.TextFormatter{DisableTimestamp: true})
47-
log.Println("Generating documents...")
48+
opts := logging.HandlerOptions{
49+
SlogOpts: slog.HandlerOptions{
50+
Level: slog.LevelInfo,
51+
},
52+
}
53+
handler := logging.NewHandler(os.Stdout, opts)
54+
logger := slog.New(handler)
55+
slog.SetDefault(logger)
56+
slog.Info("Generating documents...")
4857

4958
for tutorial, updater := range tutorials {
50-
log.Printf("Generating %s tutorial\n", tutorial)
59+
slog.Info("Generating tutorial", "name", tutorial)
5160
updater()
5261
}
5362
}

hack/docs/internal/cronjob-tutorial/generate_cronjob.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ package cronjob
1818

1919
import (
2020
"fmt"
21+
log "log/slog"
2122
"os/exec"
2223
"path/filepath"
2324

24-
log "github.com/sirupsen/logrus"
2525
"github.com/spf13/afero"
2626

2727
hackutils "sigs.k8s.io/kubebuilder/v4/hack/docs/utils"
@@ -37,25 +37,25 @@ type Sample struct {
3737

3838
// NewSample create a new instance of the cronjob sample and configure the KB CLI that will be used
3939
func NewSample(binaryPath, samplePath string) Sample {
40-
log.Infof("Generating the sample context of Cronjob...")
40+
log.Info("Generating the sample context of Cronjob...")
4141
ctx := hackutils.NewSampleContext(binaryPath, samplePath, "GO111MODULE=on")
4242
return Sample{&ctx}
4343
}
4444

4545
// Prepare the Context for the sample project
4646
func (sp *Sample) Prepare() {
47-
log.Infof("destroying directory for cronjob sample project")
47+
log.Info("destroying directory for cronjob sample project")
4848
sp.ctx.Destroy()
4949

50-
log.Infof("refreshing tools and creating directory...")
50+
log.Info("refreshing tools and creating directory...")
5151
err := sp.ctx.Prepare()
5252

5353
hackutils.CheckError("creating directory for sample project", err)
5454
}
5555

5656
// GenerateSampleProject will generate the sample
5757
func (sp *Sample) GenerateSampleProject() {
58-
log.Infof("Initializing the cronjob project")
58+
log.Info("Initializing the cronjob project")
5959

6060
err := sp.ctx.Init(
6161
"--domain", "tutorial.kubebuilder.io",
@@ -65,7 +65,7 @@ func (sp *Sample) GenerateSampleProject() {
6565
)
6666
hackutils.CheckError("Initializing the cronjob project", err)
6767

68-
log.Infof("Adding a new config type")
68+
log.Info("Adding a new config type")
6969
err = sp.ctx.CreateAPI(
7070
"--group", "batch",
7171
"--version", "v1",
@@ -74,7 +74,7 @@ func (sp *Sample) GenerateSampleProject() {
7474
)
7575
hackutils.CheckError("Creating the API", err)
7676

77-
log.Infof("Implementing admission webhook")
77+
log.Info("Implementing admission webhook")
7878
err = sp.ctx.CreateWebhook(
7979
"--group", "batch",
8080
"--version", "v1",
@@ -86,7 +86,7 @@ func (sp *Sample) GenerateSampleProject() {
8686

8787
// UpdateTutorial the cronjob tutorial with the scaffold changes
8888
func (sp *Sample) UpdateTutorial() {
89-
log.Println("Update tutorial with cronjob code")
89+
log.Info("Update tutorial with cronjob code")
9090
// 1. update specs
9191
sp.updateSpec()
9292
// 2. update webhook

hack/docs/internal/getting-started/generate_getting_started.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ limitations under the License.
1717
package gettingstarted
1818

1919
import (
20+
"log/slog"
2021
"os/exec"
2122
"path/filepath"
2223

23-
log "github.com/sirupsen/logrus"
24-
2524
hackutils "sigs.k8s.io/kubebuilder/v4/hack/docs/utils"
2625
pluginutil "sigs.k8s.io/kubebuilder/v4/pkg/plugin/util"
2726
"sigs.k8s.io/kubebuilder/v4/test/e2e/utils"
@@ -34,7 +33,7 @@ type Sample struct {
3433

3534
// NewSample create a new instance of the getting started sample and configure the KB CLI that will be used
3635
func NewSample(binaryPath, samplePath string) Sample {
37-
log.Infof("Generating the sample context of getting-started...")
36+
slog.Info("Generating the sample context of getting-started...")
3837
ctx := hackutils.NewSampleContext(binaryPath, samplePath, "GO111MODULE=on")
3938
return Sample{&ctx}
4039
}
@@ -192,18 +191,18 @@ func (sp *Sample) updateController() {
192191

193192
// Prepare the Context for the sample project
194193
func (sp *Sample) Prepare() {
195-
log.Infof("Destroying directory for getting-started sample project")
194+
slog.Info("Destroying directory for getting-started sample project")
196195
sp.ctx.Destroy()
197196

198-
log.Infof("Refreshing tools and creating directory...")
197+
slog.Info("Refreshing tools and creating directory...")
199198
err := sp.ctx.Prepare()
200199

201200
hackutils.CheckError("Creating directory for sample project", err)
202201
}
203202

204203
// GenerateSampleProject will generate the sample
205204
func (sp *Sample) GenerateSampleProject() {
206-
log.Infof("Initializing the getting started project")
205+
slog.Info("Initializing the getting started project")
207206
err := sp.ctx.Init(
208207
"--domain", "example.com",
209208
"--repo", "example.com/memcached",
@@ -212,7 +211,7 @@ func (sp *Sample) GenerateSampleProject() {
212211
)
213212
hackutils.CheckError("Initializing the getting started project", err)
214213

215-
log.Infof("Adding a new config type")
214+
slog.Info("Adding a new config type")
216215
err = sp.ctx.CreateAPI(
217216
"--group", "cache",
218217
"--version", "v1alpha1",

hack/docs/internal/multiversion-tutorial/generate_multiversion.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ limitations under the License.
1717
package multiversion
1818

1919
import (
20+
log "log/slog"
2021
"os/exec"
2122
"path/filepath"
2223

23-
log "github.com/sirupsen/logrus"
24-
2524
hackutils "sigs.k8s.io/kubebuilder/v4/hack/docs/utils"
2625
pluginutil "sigs.k8s.io/kubebuilder/v4/pkg/plugin/util"
2726
"sigs.k8s.io/kubebuilder/v4/test/e2e/utils"
@@ -34,23 +33,23 @@ type Sample struct {
3433

3534
// NewSample create a new instance of the sample and configure the KB CLI that will be used
3635
func NewSample(binaryPath, samplePath string) Sample {
37-
log.Infof("Generating the sample context of MultiVersion Cronjob...")
36+
log.Info("Generating the sample context of MultiVersion Cronjob...")
3837
ctx := hackutils.NewSampleContext(binaryPath, samplePath, "GO111MODULE=on")
3938
return Sample{&ctx}
4039
}
4140

4241
// Prepare the Context for the sample project
4342
func (sp *Sample) Prepare() {
44-
log.Infof("refreshing tools and creating directory for multiversion ...")
43+
log.Info("refreshing tools and creating directory for multiversion ...")
4544
err := sp.ctx.Prepare()
4645
hackutils.CheckError("creating directory for multiversion project", err)
4746
}
4847

4948
// GenerateSampleProject will generate the sample
5049
func (sp *Sample) GenerateSampleProject() {
51-
log.Infof("Initializing the multiversion cronjob project")
50+
log.Info("Initializing the multiversion cronjob project")
5251

53-
log.Infof("Creating v2 API")
52+
log.Info("Creating v2 API")
5453
err := sp.ctx.CreateAPI(
5554
"--group", "batch",
5655
"--version", "v2",
@@ -60,7 +59,7 @@ func (sp *Sample) GenerateSampleProject() {
6059
)
6160
hackutils.CheckError("Creating the v2 API without controller", err)
6261

63-
log.Infof("Creating conversion webhook for v1")
62+
log.Info("Creating conversion webhook for v1")
6463
err = sp.ctx.CreateWebhook(
6564
"--group", "batch",
6665
"--version", "v1",
@@ -71,7 +70,7 @@ func (sp *Sample) GenerateSampleProject() {
7170
)
7271
hackutils.CheckError("Creating conversion webhook for v1", err)
7372

74-
log.Infof("Workaround to fix the issue with the conversion webhook")
73+
log.Info("Workaround to fix the issue with the conversion webhook")
7574
// FIXME: This is a workaround to fix the issue with the conversion webhook
7675
// We should be able to inject the code when we create webhooks with different
7776
// types of webhooks. However, currently, we are not able to do that and we need to
@@ -81,7 +80,7 @@ func (sp *Sample) GenerateSampleProject() {
8180
_, err = sp.ctx.Run(cmd)
8281
hackutils.CheckError("Copying the code from cronjob tutorial", err)
8382

84-
log.Infof("Creating defaulting and validation webhook for v2")
83+
log.Info("Creating defaulting and validation webhook for v2")
8584
err = sp.ctx.CreateWebhook(
8685
"--group", "batch",
8786
"--version", "v2",
@@ -94,7 +93,7 @@ func (sp *Sample) GenerateSampleProject() {
9493

9594
// UpdateTutorial the muilt-version sample tutorial with the scaffold changes
9695
func (sp *Sample) UpdateTutorial() {
97-
log.Println("Update tutorial with multiversion code")
96+
log.Info("Update tutorial with multiversion code")
9897

9998
// Update files according to the multiversion
10099
sp.updateCronjobV1DueForce()

hack/docs/utils/utils.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,16 @@ limitations under the License.
1717
package utils
1818

1919
import (
20+
log "log/slog"
2021
"os"
2122

22-
log "github.com/sirupsen/logrus"
23-
2423
"sigs.k8s.io/kubebuilder/v4/test/e2e/utils"
2524
)
2625

2726
// CheckError will exit with exit code 1 when err is not nil.
2827
func CheckError(msg string, err error) {
2928
if err != nil {
30-
log.Errorf("error %s: %s", msg, err)
29+
log.Error("error occurred", "message", msg, "error", err)
3130
os.Exit(1)
3231
}
3332
}

pkg/cli/alpha/command.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ limitations under the License.
1414
package alpha
1515

1616
import (
17-
log "github.com/sirupsen/logrus"
17+
"log"
18+
1819
"github.com/spf13/cobra"
1920

2021
"sigs.k8s.io/kubebuilder/v4/pkg/cli/alpha/internal"

0 commit comments

Comments
 (0)