Skip to content

Conversation

@sebrandon1
Copy link
Member

I was messing around into some other repos and saw there was a logr being included here that wasn't widely used. To remove an extra dependency, I just consolidated the calls to logr with calls to the existing klog/v2.

Similar to:

@coderabbitai
Copy link

coderabbitai bot commented Sep 22, 2025

Walkthrough

Replaces direct use of github.com/go-logr/logr with k8s.io/klog/v2 logging across the Istio CSR controller, removes the Reconciler.log field, updates tests to drop testr usage, adjusts controller-runtime logger initialization, and moves github.com/go-logr/logr in go.mod to an indirect requirement.

Changes

Cohort / File(s) Summary of Changes
Dependency management
go.mod
Moved github.com/go-logr/logr v1.4.2 from a direct require to an indirect requirement; version unchanged.
Reconciler core & tests
pkg/controller/istiocsr/controller.go, pkg/controller/istiocsr/test_utils.go
Removed Reconciler.log field (previously logr.Logger); deleted testr-based test logger. Reconciler constructor no longer initializes a log field.
Logging migration — controller implementation
pkg/controller/istiocsr/certificates.go, pkg/controller/istiocsr/deployments.go, pkg/controller/istiocsr/install_istiocsr.go, pkg/controller/istiocsr/rbacs.go, pkg/controller/istiocsr/serviceaccounts.go, pkg/controller/istiocsr/services.go, pkg/controller/istiocsr/utils.go
Replaced usages of r.log.* logging calls with klog.V(...).InfoS(...) and klog.ErrorS(...); added k8s.io/klog/v2 imports. Message formatting converted to klog structured form; control flow and error handling unchanged.
Operator logger setup
pkg/operator/setup_manager.go
Switched controller-runtime logger initialization to use klogr.New() (klog/v2-backed logr) and adjusted imports accordingly (changed from previous klogr/klog initialization variant).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed The title "Consolidate around klog/v2" directly and accurately describes the main change in the pull request. The changeset systematically removes the logr dependency and replaces all logr logging calls across multiple files with klog/v2 equivalents, which is precisely what "consolidate around klog/v2" conveys. The title is clear, concise, and specific enough for a teammate to understand the primary change at a glance.
Description Check ✅ Passed The pull request description is clearly related to the changeset. It explains both the motivation (removing an extra dependency on logr) and the action taken (consolidating logr calls to use the existing klog/v2), which aligns directly with the file-by-file changes shown in the summary. The description provides helpful context by referencing similar changes in other repositories, and while informal, it effectively communicates the purpose and scope of the changes.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.5.0)

Error: can't load config: unsupported version of the configuration: "" See https://golangci-lint.run/docs/product/migration-guide for migration instructions
The command is terminated due to an error: can't load config: unsupported version of the configuration: "" See https://golangci-lint.run/docs/product/migration-guide for migration instructions


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

Cache: Disabled due to data retention organization setting

Knowledge base: Disabled due to Reviews -> Disable Knowledge Base setting

📥 Commits

Reviewing files that changed from the base of the PR and between 97ab881 and 1508734.

⛔ Files ignored due to path filters (2)
  • vendor/github.com/go-logr/logr/testr/testr.go is excluded by !vendor/**, !**/vendor/**
  • vendor/modules.txt is excluded by !vendor/**, !**/vendor/**
📒 Files selected for processing (4)
  • go.mod (1 hunks)
  • pkg/controller/istiocsr/controller.go (2 hunks)
  • pkg/controller/istiocsr/test_utils.go (2 hunks)
  • pkg/operator/setup_manager.go (2 hunks)
🔇 Additional comments (2)
go.mod (1)

93-93: Incorrect — github.com/go-logr/logr does not need to be a direct dependency.

No source file imports github.com/go-logr/logr; ctrllog.SetLogger(klog.Background()) is used in pkg/operator/setup_manager.go:36 and a klog.Logger field is used in pkg/controller/istiocsr/controller.go:51, so logr is pulled in transitively and go mod tidy correctly leaves it marked // indirect.

Likely an incorrect or invalid review comment.

pkg/operator/setup_manager.go (1)

34-37: ctrllog.SetLogger(klog.Background()) won’t compile. Use klogr.New().

SetLogger expects a logr.Logger. klog.Background() returns klog.Logger. Use the klog-backed logr.

-	// Use klog/v2 as the global logr.Logger for controller-runtime.
-	// Prefer klog.Background() over deprecated klogr.
-	ctrllog.SetLogger(klog.Background())
+	// Use klogr (klog-backed logr.Logger) for controller-runtime.
+	ctrllog.SetLogger(klogr.New())

Likely an incorrect or invalid review comment.

"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/go-logr/logr"
klog "k8s.io/klog/v2"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Type mismatch: Reconciler.log must be logr.Logger (not klog.Logger).

ctrl.Log.WithName(...) returns a logr.Logger. Storing it in a klog.Logger field won’t compile. Keep the field as logr.Logger and drop the direct klog import here.

-	klog "k8s.io/klog/v2"
+	"github.com/go-logr/logr"
@@
-	log           klog.Logger
+	log           logr.Logger

Also applies to: 51-51

🤖 Prompt for AI Agents
In pkg/controller/istiocsr/controller.go around lines 28 and 51, the code
imports klog and stores ctrl.Log.WithName(...) into a klog.Logger field causing
a type mismatch; change the field type to logr.Logger, remove the direct klog
import, and assign ctrl.Log.WithName(...) (a logr.Logger) to that field. Also
update any references/usages of the field to use logr.Logger methods (or convert
to klog only where needed), and ensure imports and variable declarations compile
after replacing klog.Logger with logr.Logger.

"k8s.io/client-go/tools/record"

"github.com/go-logr/logr/testr"
klog "k8s.io/klog/v2"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Use klogr adapter; klog.Background() returns klog.Logger, not logr.Logger.

Tests should provide a logr.Logger (backed by klog) to match controller-runtime. Switch to klogr.

Apply:

-	klog "k8s.io/klog/v2"
+	klogr "k8s.io/klog/v2/klogr"
@@
-		log:           klog.Background(),
+		log:           klogr.New(),

Also applies to: 41-41

🤖 Prompt for AI Agents
In pkg/controller/istiocsr/test_utils.go around lines 15 and 41, the code
imports klog and uses klog.Background() which returns a klog.Logger rather than
a logr.Logger expected by controller-runtime tests; replace the usage with the
klogr adapter: add import "github.com/go-logr/klogr", create a logr.Logger via
klogr.New() (optionally .WithName(...) if needed) and pass that into tests/code
paths instead of klog.Background(); remove or stop using klog.Background() where
a logr.Logger is required.

@sebrandon1 sebrandon1 force-pushed the remove_extra_loggers branch from 1508734 to c3d8740 Compare October 24, 2025 18:11
@openshift-ci
Copy link
Contributor

openshift-ci bot commented Oct 24, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: sebrandon1
Once this PR has been reviewed and has the lgtm label, please assign mytreya-rh for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
pkg/controller/istiocsr/install_istiocsr.go (1)

28-30: Incomplete logging migration on line 28.

This line still uses r.log.Error while all other logging calls in this function (and across the PR) have been migrated to klog. This inconsistency should be addressed.

Apply this diff to complete the migration:

-	r.log.Error(err, "failed to reconcile network policy resources")
+	klog.ErrorS(err, "failed to reconcile network policy resources")
♻️ Duplicate comments (1)
pkg/controller/istiocsr/controller.go (1)

21-21: Past review comment no longer applicable - approach has changed.

The previous review flagged a type mismatch with storing ctrl.Log.WithName(...) in a klog.Logger field. The current implementation resolves this by removing the log field entirely and using direct klog calls throughout the controller. This approach aligns with the PR objective to consolidate around klog/v2 and remove the logr dependency.

Note: This means controller-scoped logging context (previously provided by WithName) is no longer captured, but this appears intentional for the consolidation effort.

Also applies to: 47-53

🧹 Nitpick comments (1)
pkg/controller/istiocsr/controller.go (1)

146-149: Avoid creating synthetic errors solely for logging.

Creating fmt.Errorf("invalid label format") just to satisfy klog.ErrorS's signature is awkward. Since this is a validation failure rather than an operational error, consider using klog.V(2).InfoS with a warning message instead, or pass nil as the error parameter if you prefer ErrorS.

Apply this diff to use structured info logging:

-				klog.ErrorS(fmt.Errorf("invalid label format"), "label value not in expected format",
+				klog.V(2).InfoS("label value not in expected format",
 					"labelName", IstiocsrResourceWatchLabelName,
 					"labelValue", value,
 					"resource", obj.GetName())
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

Cache: Disabled due to data retention organization setting

Knowledge base: Disabled due to Reviews -> Disable Knowledge Base setting

📥 Commits

Reviewing files that changed from the base of the PR and between 1508734 and c3d8740.

⛔ Files ignored due to path filters (1)
  • vendor/github.com/go-logr/logr/testr/testr.go is excluded by !vendor/**, !**/vendor/**
📒 Files selected for processing (11)
  • go.mod (1 hunks)
  • pkg/controller/istiocsr/certificates.go (3 hunks)
  • pkg/controller/istiocsr/controller.go (11 hunks)
  • pkg/controller/istiocsr/deployments.go (4 hunks)
  • pkg/controller/istiocsr/install_istiocsr.go (3 hunks)
  • pkg/controller/istiocsr/rbacs.go (16 hunks)
  • pkg/controller/istiocsr/serviceaccounts.go (3 hunks)
  • pkg/controller/istiocsr/services.go (3 hunks)
  • pkg/controller/istiocsr/test_utils.go (0 hunks)
  • pkg/controller/istiocsr/utils.go (3 hunks)
  • pkg/operator/setup_manager.go (2 hunks)
💤 Files with no reviewable changes (1)
  • pkg/controller/istiocsr/test_utils.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • go.mod
🔇 Additional comments (11)
pkg/operator/setup_manager.go (1)

14-14: LGTM! Past review comment addressed correctly.

The import has been updated to use the klogr adapter, and the logger initialization correctly supplies a klog/v2-backed logr.Logger to controller-runtime via klogr.New(). The explanatory comment is also helpful.

Also applies to: 33-34

pkg/controller/istiocsr/rbacs.go (1)

8-8: LGTM! Consistent migration to klog/v2 structured logging.

The logging calls have been properly migrated from r.log to klog's structured logging API (ErrorS/InfoS with key-value pairs), with appropriate verbosity levels.

Also applies to: 24-24, 66-66

pkg/controller/istiocsr/serviceaccounts.go (1)

7-7: LGTM! Logging migration aligns with the PR objective.

The file properly imports klog/v2 and uses structured logging with appropriate verbosity levels.

Also applies to: 18-18, 29-29

pkg/controller/istiocsr/deployments.go (1)

18-18: LGTM! Logging migration is complete and consistent.

The deployment reconciliation logging has been properly migrated to klog/v2 structured logging with appropriate verbosity levels.

Also applies to: 42-42, 53-53, 559-559

pkg/controller/istiocsr/certificates.go (1)

8-8: LGTM! Certificate reconciliation logging properly migrated.

The logging calls correctly use klog/v2 structured logging with appropriate verbosity levels.

Also applies to: 25-25, 36-36

pkg/controller/istiocsr/install_istiocsr.go (1)

33-33: LGTM! These logging calls have been properly migrated.

The migration to klog.ErrorS and klog.V().InfoS is consistent with the PR objective.

Also applies to: 38-38, 43-43, 48-48, 53-53, 63-63

pkg/controller/istiocsr/services.go (1)

7-7: LGTM! Service reconciliation logging properly migrated.

The logging migration to klog/v2 is consistent and uses appropriate structured logging patterns.

Also applies to: 37-37, 48-48

pkg/controller/istiocsr/utils.go (1)

17-17: LGTM! Utility function logging properly migrated.

The logging calls in status updates and instance validation have been correctly migrated to klog/v2 structured logging.

Also applies to: 53-53, 410-410

pkg/controller/istiocsr/controller.go (3)

108-119: LGTM!

The constructor correctly reflects the removal of the log field and properly initializes the remaining Reconciler fields.


217-245: LGTM!

The structured logging calls in the Reconcile function are well-formed, use appropriate verbosity levels, and provide clear context for reconciliation events.


260-322: LGTM!

The logging throughout processReconcileRequest properly uses structured logging, appropriate verbosity levels, and provides comprehensive observability for reconciliation state changes and error conditions.

@openshift-ci
Copy link
Contributor

openshift-ci bot commented Oct 24, 2025

@sebrandon1: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
ci/prow/e2e-operator c3d8740 link true /test e2e-operator
ci/prow/e2e-operator-tech-preview c3d8740 link false /test e2e-operator-tech-preview
ci/prow/fips-image-scan-operator c3d8740 link true /test fips-image-scan-operator
ci/prow/unit c3d8740 link true /test unit
ci/prow/images c3d8740 link true /test images
ci/prow/ci-bundle-cert-manager-operator-bundle c3d8740 link true /test ci-bundle-cert-manager-operator-bundle

Full PR test history. Your PR dashboard.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant