Skip to content

Commit a7bb187

Browse files
committed
fix: avoid shadowing 'log' package by aliasing import as 'logf'
The 'log' package from controller-runtime was being shadowed by local variables in multiple Reconcile methods, which could lead to confusion and bugs. Renamed the import to 'logf' to clarify usage and prevent name collisions across the codebase and generated scaffolds.
1 parent f998af1 commit a7bb187

File tree

31 files changed

+61
-61
lines changed

31 files changed

+61
-61
lines changed

docs/book/src/cronjob-tutorial/testdata/emptycontroller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"k8s.io/apimachinery/pkg/runtime"
2929
ctrl "sigs.k8s.io/controller-runtime"
3030
"sigs.k8s.io/controller-runtime/pkg/client"
31-
"sigs.k8s.io/controller-runtime/pkg/log"
31+
logf "sigs.k8s.io/controller-runtime/pkg/log"
3232

3333
batchv1 "tutorial.kubebuilder.io/project/api/v1"
3434
)
@@ -88,7 +88,7 @@ some pairs at the top of our reconcile method to have those attached to all log
8888
lines in this reconciler.
8989
*/
9090
func (r *CronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
91-
_ = log.FromContext(ctx)
91+
_ = logf.FromContext(ctx)
9292

9393
// your logic here
9494

docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
ref "k8s.io/client-go/tools/reference"
3737
ctrl "sigs.k8s.io/controller-runtime"
3838
"sigs.k8s.io/controller-runtime/pkg/client"
39-
"sigs.k8s.io/controller-runtime/pkg/log"
39+
logf "sigs.k8s.io/controller-runtime/pkg/log"
4040

4141
batchv1 "tutorial.kubebuilder.io/project/api/v1"
4242
)
@@ -98,7 +98,7 @@ var (
9898
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.20.3/pkg/reconcile
9999
// nolint:gocyclo
100100
func (r *CronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
101-
log := log.FromContext(ctx)
101+
log := logf.FromContext(ctx)
102102

103103
/*
104104
### 1: Load the CronJob by name

docs/book/src/getting-started/testdata/project/internal/controller/memcached_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
"k8s.io/apimachinery/pkg/runtime"
3434
ctrl "sigs.k8s.io/controller-runtime"
3535
"sigs.k8s.io/controller-runtime/pkg/client"
36-
"sigs.k8s.io/controller-runtime/pkg/log"
36+
logf "sigs.k8s.io/controller-runtime/pkg/log"
3737

3838
cachev1alpha1 "example.com/memcached/api/v1alpha1"
3939
)
@@ -71,7 +71,7 @@ type MemcachedReconciler struct {
7171
// For more details, check Reconcile and its Result here:
7272
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.20.3/pkg/reconcile
7373
func (r *MemcachedReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
74-
log := log.FromContext(ctx)
74+
log := logf.FromContext(ctx)
7575

7676
// Fetch the Memcached instance
7777
// The purpose is check if the Custom Resource for the Kind Memcached

docs/book/src/multiversion-tutorial/testdata/project/internal/controller/cronjob_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
ref "k8s.io/client-go/tools/reference"
3737
ctrl "sigs.k8s.io/controller-runtime"
3838
"sigs.k8s.io/controller-runtime/pkg/client"
39-
"sigs.k8s.io/controller-runtime/pkg/log"
39+
logf "sigs.k8s.io/controller-runtime/pkg/log"
4040

4141
batchv1 "tutorial.kubebuilder.io/project/api/v1"
4242
)
@@ -98,7 +98,7 @@ var (
9898
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.20.3/pkg/reconcile
9999
// nolint:gocyclo
100100
func (r *CronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
101-
log := log.FromContext(ctx)
101+
log := logf.FromContext(ctx)
102102

103103
/*
104104
### 1: Load the CronJob by name

docs/book/src/reference/watching-resources/secondary-owned-resources.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ is scaled back to 1 replica.
8080
```go
8181
// Reconcile handles the main reconciliation loop for Busybox and the Deployment
8282
func (r *BusyboxReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
83-
log := log.FromContext(ctx)
83+
log := logf.FromContext(ctx)
8484

8585
// Fetch the Busybox instance
8686
busybox := &examplecomv1alpha1.Busybox{}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const controllerImport = `import (
3838
ref "k8s.io/client-go/tools/reference"
3939
ctrl "sigs.k8s.io/controller-runtime"
4040
"sigs.k8s.io/controller-runtime/pkg/client"
41-
"sigs.k8s.io/controller-runtime/pkg/log"
41+
logf "sigs.k8s.io/controller-runtime/pkg/log"
4242
4343
batchv1 "tutorial.kubebuilder.io/project/api/v1"
4444
)
@@ -87,7 +87,7 @@ var (
8787
const skipGoCycloLint = `
8888
// nolint:gocyclo`
8989

90-
const controllerReconcileLogic = `log := log.FromContext(ctx)
90+
const controllerReconcileLogic = `log := logf.FromContext(ctx)
9191
9292
/*
9393
### 1: Load the CronJob by name

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ func (sp *Sample) updateController() {
263263
"k8s.io/apimachinery/pkg/runtime"
264264
ctrl "sigs.k8s.io/controller-runtime"
265265
"sigs.k8s.io/controller-runtime/pkg/client"
266-
"sigs.k8s.io/controller-runtime/pkg/log"
266+
logf "sigs.k8s.io/controller-runtime/pkg/log"
267267
268268
batchv1 "tutorial.kubebuilder.io/project/api/v1"
269269
)`, controllerImport)
@@ -293,7 +293,7 @@ func (sp *Sample) updateController() {
293293

294294
err = pluginutil.ReplaceInFile(
295295
filepath.Join(sp.ctx.Dir, "internal/controller/cronjob_controller.go"),
296-
` _ = log.FromContext(ctx)
296+
` _ = logf.FromContext(ctx)
297297
298298
// TODO(user): your logic here
299299

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ func (sp *Sample) updateController() {
165165

166166
err = pluginutil.ReplaceInFile(
167167
filepath.Join(sp.ctx.Dir, pathFile),
168-
"_ = log.FromContext(ctx)",
169-
"log := log.FromContext(ctx)",
168+
"_ = logf.FromContext(ctx)",
169+
"log := logf.FromContext(ctx)",
170170
)
171171
hackutils.CheckError("add log var", err)
172172

pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/internal/templates/controllers/controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ import (
8787
"k8s.io/utils/ptr"
8888
ctrl "sigs.k8s.io/controller-runtime"
8989
"sigs.k8s.io/controller-runtime/pkg/client"
90-
"sigs.k8s.io/controller-runtime/pkg/log"
90+
logf "sigs.k8s.io/controller-runtime/pkg/log"
9191
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
9292
9393
{{ if not (isEmptyStr .Resource.Path) -}}
@@ -135,7 +135,7 @@ type {{ .Resource.Kind }}Reconciler struct {
135135
// - About Controllers: https://kubernetes.io/docs/concepts/architecture/controller/
136136
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@{{ .ControllerRuntimeVersion }}/pkg/reconcile
137137
func (r *{{ .Resource.Kind }}Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
138-
log := log.FromContext(ctx)
138+
log := logf.FromContext(ctx)
139139
140140
// Fetch the {{ .Resource.Kind }} instance
141141
// The purpose is check if the Custom Resource for the Kind {{ .Resource.Kind }}

pkg/plugins/golang/v4/scaffolds/internal/templates/controllers/controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ import (
7474
"k8s.io/apimachinery/pkg/runtime"
7575
ctrl "sigs.k8s.io/controller-runtime"
7676
"sigs.k8s.io/controller-runtime/pkg/client"
77-
"sigs.k8s.io/controller-runtime/pkg/log"
77+
logf "sigs.k8s.io/controller-runtime/pkg/log"
7878
{{ if not (isEmptyStr .Resource.Path) -}}
7979
{{ .Resource.ImportAlias }} "{{ .Resource.Path }}"
8080
{{- end }}
@@ -100,7 +100,7 @@ type {{ .Resource.Kind }}Reconciler struct {
100100
// For more details, check Reconcile and its Result here:
101101
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@{{ .ControllerRuntimeVersion }}/pkg/reconcile
102102
func (r *{{ .Resource.Kind }}Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
103-
_ = log.FromContext(ctx)
103+
_ = logf.FromContext(ctx)
104104
105105
// TODO(user): your logic here
106106

0 commit comments

Comments
 (0)