Skip to content

Use structured logging for cluster taint policy controller #6539

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

Closed

Conversation

zhangsquared
Copy link
Contributor

@zhangsquared zhangsquared commented Jul 15, 2025

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

JSON logging support. More details are highlighted in #6230.

Which issue(s) this PR fixes:

Fixes #

Part of #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 15, 2025 21:50
@karmada-bot
Copy link
Collaborator

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

@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 seanlaii 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 karmada-bot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Jul 15, 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 @zhangsquared, 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 transitions the cluster taint policy controller to use structured logging. By adopting klog.InfoS and klog.ErrorS, the logs generated by this controller will now include key-value pairs, significantly improving their machine readability and enabling more robust log aggregation and analysis. This change aligns with best practices for observability in cloud-native environments.

Highlights

  • Structured Logging Adoption: The ClusterTaintPolicyController has been updated to utilize klog.InfoS and klog.ErrorS for all its logging statements. This change replaces the older klog.Info, klog.Infof, and klog.Errorf calls, thereby introducing structured logging which enhances log parsing and analysis efficiency.
  • Error Handling Refinement: The errors package has been imported and is now used to explicitly create an error object for structured logging in cases such as encountering an unsupported MatchCondition operator, providing more context in log entries.
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 migrates the ClusterTaintPolicyController to use structured JSON logging via klog.InfoS and klog.ErrorS.

  • Replaces unstructured klog.Infof, klog.Errorf, and klog.Info with InfoS, ErrorS, and V().InfoS.
  • Adds structured key/value pairs to error and debug logs.
  • Introduces an errors import for creating structured error messages.
Comments suppressed due to low confidence (2)

pkg/controllers/taint/clustertaintpolicy_controller.go:73

  • [nitpick] The structured log key "clusterTaintPolicyList" is misleading because it logs the ListOptions rather than the policy list. Consider renaming the key to "listOptions" to accurately describe the logged data.
		klog.ErrorS(err, "Failed to list ClusterTaintPolicy", "clusterTaintPolicyList", listOption)

pkg/controllers/taint/clustertaintpolicy_controller.go:239

  • [nitpick] The log key "clusterList" refers to the ListOptions being passed, not the retrieved list. Rename the key to "listOptions" for clarity.
				klog.ErrorS(err, "Failed to list Cluster", "clusterList", listOption)

@@ -147,7 +148,7 @@ func conditionMatches(conditions []metav1.Condition, matchConditions []policyv1a
}
}
default:
klog.Errorf("Unsupported MatchCondition operator: %s", matchCondition.Operator)
klog.ErrorS(errors.New("Unsupported MatchCondition operator"), "operator", matchCondition.Operator)
Copy link
Preview

Copilot AI Jul 15, 2025

Choose a reason for hiding this comment

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

The ErrorS call sets "operator" as the log message instead of a descriptive message. It should be: ErrorS(err, "Unsupported MatchCondition operator", "operator", matchCondition.Operator) so the message and key/value are correctly ordered.

Suggested change
klog.ErrorS(errors.New("Unsupported MatchCondition operator"), "operator", matchCondition.Operator)
klog.ErrorS(errors.New("Unsupported MatchCondition operator"), "Unsupported MatchCondition operator", "operator", matchCondition.Operator)

Copilot uses AI. Check for mistakes.

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

The pull request transitions the clusterTaintPolicyController to use structured logging. Key areas for improvement include clarifying key names in structured logs, correctly using klog.InfoS with structured logging, and fixing a bug in one klog.ErrorS call that suppresses the intended error log.

return controllerruntime.Result{}, client.IgnoreNotFound(err)
}

clusterTaintPolicyList := &policyv1alpha1.ClusterTaintPolicyList{}
listOption := &client.ListOptions{UnsafeDisableDeepCopy: ptr.To(true)}
if err := c.Client.List(ctx, clusterTaintPolicyList, listOption); err != nil {
klog.Errorf("Failed to list ClusterTaintPolicy, error: %v", err)
klog.ErrorS(err, "Failed to list ClusterTaintPolicy", "clusterTaintPolicyList", listOption)

Choose a reason for hiding this comment

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

medium

The key "clusterTaintPolicyList" is the variable name, but the value being logged is listOption. Consider using a more descriptive key like "listOptions" to avoid confusion.

Suggested change
klog.ErrorS(err, "Failed to list ClusterTaintPolicy", "clusterTaintPolicyList", listOption)
klog.ErrorS(err, "Failed to list ClusterTaintPolicy", "listOptions", listOption)

@@ -235,7 +236,7 @@ func (c *ClusterTaintPolicyController) SetupWithManager(mgr controllerruntime.Ma
clusterList := &clusterv1alpha1.ClusterList{}
listOption := &client.ListOptions{UnsafeDisableDeepCopy: ptr.To(true)}
if err := c.Client.List(ctx, clusterList, listOption); err != nil {
klog.Errorf("Failed to list Cluster, error: %v", err)
klog.ErrorS(err, "Failed to list Cluster", "clusterList", listOption)

Choose a reason for hiding this comment

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

medium

The key "clusterList" is the variable name, but the value being logged is listOption. Consider using a more descriptive key like "listOptions" to avoid confusion.

Suggested change
klog.ErrorS(err, "Failed to list Cluster", "clusterList", listOption)
klog.ErrorS(err, "Failed to list Cluster", "listOptions", listOption)

@zhangsquared zhangsquared force-pushed the log-taint-controller branch from 3abf3f4 to 0849492 Compare July 15, 2025 22:05
@karmada-bot karmada-bot added the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label Jul 15, 2025
zzhang881 and others added 2 commits July 15, 2025 18:13
Signed-off-by: zzhang881 <[email protected]>
Signed-off-by: zhangsquared <[email protected]>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: zhangsquared <[email protected]>
@zhangsquared zhangsquared force-pushed the log-taint-controller branch from 80ffaab to d77dedc Compare July 15, 2025 22:14
@zhangsquared zhangsquared marked this pull request as draft July 15, 2025 22:15
@karmada-bot karmada-bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jul 15, 2025
RainbowMango and others added 13 commits July 15, 2025 18:18
Signed-off-by: Bhaumik Patel <[email protected]>

namespace-sync-ctrl:Address comments

Signed-off-by: Bhaumik Patel <[email protected]>
Signed-off-by: sdutta133 <[email protected]>

linter issue

Signed-off-by: sdutta133 <[email protected]>

fix logging pattern

Signed-off-by: sdutta133 <[email protected]>
Signed-off-by: wei-chenglai <[email protected]>
Signed-off-by: lzhou286 <[email protected]>

Fix test image

Signed-off-by: lzhou286 <[email protected]>

Fix linter

Signed-off-by: lzhou286 <[email protected]>

Fix linter

Signed-off-by: lzhou286 <[email protected]>
… handle with clusterResourceBinding

Signed-off-by: changzhen <[email protected]>
Bumps [sigstore/cosign-installer](https://github.com/sigstore/cosign-installer) from 3.9.0 to 3.9.1.
- [Release notes](https://github.com/sigstore/cosign-installer/releases)
- [Commits](sigstore/cosign-installer@v3.9.0...v3.9.1)

---
updated-dependencies:
- dependency-name: sigstore/cosign-installer
  dependency-version: 3.9.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
Eugene Ryazanov and others added 20 commits July 15, 2025 18:18
Signed-off-by: Rituraj Singh <[email protected]>

Add persistentprerune

Signed-off-by: Rituraj Singh <[email protected]>

Remove whitespace

Signed-off-by: Rituraj Singh <[email protected]>
Bump sigs.k8s.io/custom-metrics-apiserver to v1.33.0
Bump controller-runtime to v0.21.0
Bump sigs.k8s.io/metrics-server to v0.7.1-0.20250620093659-55b4961bc1ec

Signed-off-by: RainbowMango <[email protected]>
…R129416

Adopt Kubernetes breaking change: change storage interface in PR130400
Adopt Kubernetes breaking change: use CacheDelegator to replace Cacher in PR129443
Adopt Kubernetes breaking change: Refactor explain cmd in PR129784
Adopt controller-runtime changes: Deprecates result.requeue in PR3107
Adopt Kubernetes breaking change: disable watchlist in PR131359

Signed-off-by: changzhen <[email protected]>
Signed-off-by: RainbowMango <[email protected]>
Bumps [aquasecurity/trivy-action](https://github.com/aquasecurity/trivy-action) from 0.31.0 to 0.32.0.
- [Release notes](https://github.com/aquasecurity/trivy-action/releases)
- [Commits](aquasecurity/trivy-action@0.31.0...0.32.0)

---
updated-dependencies:
- dependency-name: aquasecurity/trivy-action
  dependency-version: 0.32.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
Signed-off-by: zzhang881 <[email protected]>
Signed-off-by: zhangsquared <[email protected]>
@codecov-commenter
Copy link

codecov-commenter commented Jul 15, 2025

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

Codecov Report

Attention: Patch coverage is 0% with 8 lines in your changes missing coverage. Please review.

Project coverage is 45.45%. Comparing base (c60a2e0) to head (86ec56a).

Files with missing lines Patch % Lines
...controllers/taint/clustertaintpolicy_controller.go 0.00% 8 Missing ⚠️

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

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6539      +/-   ##
==========================================
+ Coverage   45.43%   45.45%   +0.02%     
==========================================
  Files         687      687              
  Lines       56334    56334              
==========================================
+ Hits        25594    25608      +14     
+ Misses      29140    29129      -11     
+ Partials     1600     1597       -3     
Flag Coverage Δ
unittests 45.45% <0.00%> (+0.02%) ⬆️

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.

@karmada-bot
Copy link
Collaborator

Keywords which can automatically close issues and at(@) or hashtag(#) mentions are not allowed in commit messages.

The list of commits with invalid commit messages:

  • d77dedc Update pkg/controllers/taint/clustertaintpolicy_controller.go

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.

@karmada-bot karmada-bot added the do-not-merge/contains-merge-commits Indicates a PR which contains merge commits. label Jul 15, 2025
@karmada-bot
Copy link
Collaborator

Adding label do-not-merge/contains-merge-commits because PR contains merge commits, which are not allowed in this repository.
Use git rebase to reapply your commits on top of the target branch. Detailed instructions for doing so can be found here.

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.

@zhangsquared zhangsquared deleted the log-taint-controller branch July 16, 2025 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do-not-merge/contains-merge-commits Indicates a PR which contains merge commits. do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. 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.