Skip to content

refactor(app): improve logging in graceful eviction controllers #6555

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mohit-nagaraj
Copy link

@mohit-nagaraj mohit-nagaraj commented Jul 19, 2025

What type of PR is this?

Add one of the following kinds:
/kind cleanup

What this PR does / why we need it:
Structured logging support

Which issue(s) this PR fixes:

Partially Fix #6230

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

NONE

@Copilot Copilot AI review requested due to automatic review settings July 19, 2025 14:49
@karmada-bot karmada-bot added the kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. label Jul 19, 2025
@karmada-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign garrybest for approval. For more information see the Kubernetes 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

@karmada-bot
Copy link
Collaborator

Welcome @mohit-nagaraj! It looks like this is your first PR to karmada-io/karmada 🎉

@karmada-bot karmada-bot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Jul 19, 2025
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @mohit-nagaraj, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the logging implementation within the graceful eviction controllers (CRBGracefulEvictionController and RBGracefulEvictionController) to adopt structured logging. The change aims to enhance the observability and debuggability of these components by making log messages more consistent and easier to parse programmatically.

Highlights

  • Structured Logging Adoption: Converted klog.Infof calls to klog.InfoS for structured logging within the CRBGracefulEvictionController.
  • Structured Logging Adoption: Converted klog.Infof calls to klog.InfoS for structured logging within the RBGracefulEvictionController.
  • Code Cleanup: Improved log message clarity and machine readability by replacing printf-style formatting with key-value pairs in log statements.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors logging statements in the graceful eviction controllers to use structured logging instead of formatted string logging. The changes improve log consistency and machine readability by replacing klog.Infof() calls with klog.InfoS() and converting formatted strings to key-value pairs.

  • Converts formatted logging to structured logging using klog.InfoS()
  • Replaces string interpolation with structured key-value pairs
  • Updates log messages for better clarity and consistency

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
pkg/controllers/gracefuleviction/rb_graceful_eviction_controller.go Updates ResourceBinding controller logging to use structured format
pkg/controllers/gracefuleviction/crb_graceful_eviction_controller.go Updates ClusterResourceBinding controller logging to use structured format

@@ -71,7 +71,7 @@ func (c *RBGracefulEvictionController) Reconcile(ctx context.Context, req contro
return controllerruntime.Result{}, err
}
if retryDuration > 0 {
klog.V(4).Infof("Retry to evict task after %v minutes.", retryDuration.Minutes())
klog.V(4).InfoS("Retry to evict task after minutes", "retryAfterMinutes", retryDuration.Minutes())
Copy link
Preview

Copilot AI Jul 19, 2025

Choose a reason for hiding this comment

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

The log message "Retry to evict task after minutes" is unclear without context. Consider changing it to "Retry to evict task after specified minutes" or "Will retry eviction task" to make it more descriptive.

Suggested change
klog.V(4).InfoS("Retry to evict task after minutes", "retryAfterMinutes", retryDuration.Minutes())
klog.V(4).InfoS("Retrying eviction task after specified duration in minutes", "retryAfterMinutes", retryDuration.Minutes())

Copilot uses AI. Check for mistakes.

Copy link
Author

Choose a reason for hiding this comment

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

Depends on the opinion of the maintainer I would prefer to wait for review once

@@ -71,7 +71,7 @@ func (c *CRBGracefulEvictionController) Reconcile(ctx context.Context, req contr
return controllerruntime.Result{}, err
}
if retryDuration > 0 {
klog.V(4).Infof("Retry to evict task after %v minutes.", retryDuration.Minutes())
klog.V(4).InfoS("Retry to evict task after minutes", "retryAfterMinutes", retryDuration.Minutes())
Copy link
Preview

Copilot AI Jul 19, 2025

Choose a reason for hiding this comment

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

The log message "Retry to evict task after minutes" is unclear without context. Consider changing it to "Retry to evict task after specified minutes" or "Will retry eviction task" to make it more descriptive.

Suggested change
klog.V(4).InfoS("Retry to evict task after minutes", "retryAfterMinutes", retryDuration.Minutes())
klog.V(4).InfoS("Will retry eviction task after specified minutes", "retryAfterMinutes", retryDuration.Minutes())

Copilot uses AI. Check for mistakes.

Copy link
Author

Choose a reason for hiding this comment

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

Same as #6555 (comment)

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors logging to use structured logging with klog.InfoS, which is a great improvement. My review focuses on enhancing the clarity and consistency of the new log statements. I've suggested changes to:

  • Improve log message phrasing.
  • Use more descriptive and less ambiguous keys for structured data.
  • Ensure consistency in logging related entities within the same file.

These changes should make the logs more useful and easier to query.

@@ -71,7 +71,7 @@ func (c *CRBGracefulEvictionController) Reconcile(ctx context.Context, req contr
return controllerruntime.Result{}, err
}
if retryDuration > 0 {
klog.V(4).Infof("Retry to evict task after %v minutes.", retryDuration.Minutes())
klog.V(4).InfoS("Retry to evict task after minutes", "retryAfterMinutes", retryDuration.Minutes())

Choose a reason for hiding this comment

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

medium

The log message "Retry to evict task after minutes" is a bit awkward. A more descriptive message would be better. Also, it's idiomatic in Go to log time.Duration values directly and let the logging library format them, rather than converting to a specific unit like minutes manually.

Suggested change
klog.V(4).InfoS("Retry to evict task after minutes", "retryAfterMinutes", retryDuration.Minutes())
klog.V(4).InfoS("Requeuing eviction task for retry", "retryAfter", retryDuration)

Comment on lines +100 to +101
klog.V(2).InfoS("Evicted cluster from ClusterResourceBinding gracefulEvictionTasks",
"cluster", cluster, "name", binding.Name)

Choose a reason for hiding this comment

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

medium

The key name is ambiguous. For clarity and consistency with the logging statement on line 55, please use clusterResourceBinding as the key for the binding's name.

Suggested change
klog.V(2).InfoS("Evicted cluster from ClusterResourceBinding gracefulEvictionTasks",
"cluster", cluster, "name", binding.Name)
klog.V(2).InfoS("Evicted cluster from ClusterResourceBinding gracefulEvictionTasks",
"cluster", cluster, "clusterResourceBinding", binding.Name)

@@ -52,7 +52,7 @@ type RBGracefulEvictionController struct {
// The Controller will requeue the Request to be processed again if an error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
func (c *RBGracefulEvictionController) Reconcile(ctx context.Context, req controllerruntime.Request) (controllerruntime.Result, error) {
klog.V(4).Infof("Reconciling ResourceBinding %s.", req.NamespacedName.String())
klog.V(4).InfoS("Reconciling ResourceBinding", "resourceBinding", req.NamespacedName.String())

Choose a reason for hiding this comment

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

medium

For consistency with other logging statements in this file (e.g., lines 100-101) and for better queryability of structured logs, it's preferable to log namespace and name as separate key-value pairs. Additionally, using a more specific key like resourceBindingName instead of just name improves clarity.

Suggested change
klog.V(4).InfoS("Reconciling ResourceBinding", "resourceBinding", req.NamespacedName.String())
klog.V(4).InfoS("Reconciling ResourceBinding", "namespace", req.Namespace, "resourceBindingName", req.Name)

@@ -71,7 +71,7 @@ func (c *RBGracefulEvictionController) Reconcile(ctx context.Context, req contro
return controllerruntime.Result{}, err
}
if retryDuration > 0 {
klog.V(4).Infof("Retry to evict task after %v minutes.", retryDuration.Minutes())
klog.V(4).InfoS("Retry to evict task after minutes", "retryAfterMinutes", retryDuration.Minutes())

Choose a reason for hiding this comment

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

medium

The log message "Retry to evict task after minutes" is a bit awkward. A more descriptive message would be better. Also, it's idiomatic in Go to log time.Duration values directly and let the logging library format them, rather than converting to a specific unit like minutes manually.

Suggested change
klog.V(4).InfoS("Retry to evict task after minutes", "retryAfterMinutes", retryDuration.Minutes())
klog.V(4).InfoS("Requeuing eviction task for retry", "retryAfter", retryDuration)

Comment on lines +100 to +101
klog.V(2).InfoS("Evicted cluster from ResourceBinding gracefulEvictionTasks", "cluster", cluster,
"namespace", binding.Namespace, "name", binding.Name)

Choose a reason for hiding this comment

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

medium

The key name is ambiguous. For better clarity and consistency, please use a more specific key like resourceBindingName.

Suggested change
klog.V(2).InfoS("Evicted cluster from ResourceBinding gracefulEvictionTasks", "cluster", cluster,
"namespace", binding.Namespace, "name", binding.Name)
klog.V(2).InfoS("Evicted cluster from ResourceBinding gracefulEvictionTasks", "cluster", cluster,
"namespace", binding.Namespace, "resourceBindingName", binding.Name)

@codecov-commenter
Copy link

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 75.00000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 45.41%. Comparing base (f53dffe) to head (68b364f).

Files with missing lines Patch % Lines
...acefuleviction/crb_graceful_eviction_controller.go 75.00% 1 Missing ⚠️
...racefuleviction/rb_graceful_eviction_controller.go 75.00% 1 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #6555   +/-   ##
=======================================
  Coverage   45.40%   45.41%           
=======================================
  Files         687      687           
  Lines       56397    56397           
=======================================
+ Hits        25605    25610    +5     
+ Misses      29195    29191    -4     
+ Partials     1597     1596    -1     
Flag Coverage Δ
unittests 45.41% <75.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Structured Logging for Karmada Components
3 participants