Skip to content

🐛 let logr.Logger understand Kubernetes Object #3210

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: main
Choose a base branch
from

Conversation

caozhuozi
Copy link
Contributor

@caozhuozi caozhuozi commented May 8, 2025

fixes: #1290
This addresses a long-standing bug.

An earlier attempt to fix this was made in #1883, but it hasn't received any reviews for a long time.

possible reasons include (in my opinion):

  • The PR is quite old, and outdated pull requests often become less appealing for reviewers to pick up.
  • The PR was relatively large and lacked detailed explanations of the changes, making it difficult to review.

To address this,

  • I created a new PR.
  • I simplified the original approach by minimizing the scope of changes and making the implementation easier to understand.

To summary the changes I made,

  1. Introduced a dedicated simple helper package named logr, which just makes a logr.Logger aware of runtime.Object.
    (The need for a separate package and the naming are open for discussion.)
  2. Applied this helper to wrap the logger created in zap.New().

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label May 8, 2025
@k8s-ci-robot k8s-ci-robot requested a review from FillZpp May 8, 2025 06:04
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: caozhuozi
Once this PR has been reviewed and has the lgtm label, please assign sbueringer 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

@k8s-ci-robot
Copy link
Contributor

Hi @caozhuozi. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed 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.

@k8s-ci-robot k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels May 8, 2025
@alvaroaleman
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jul 26, 2025
Copy link
Member

@alvaroaleman alvaroaleman left a comment

Choose a reason for hiding this comment

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

This change changes behavior ppl might rely on, so please mark it as breaking

}

// wrapKeyAndValues replaces Kubernetes objects with [kubeObjectWrapper].
func (k *KubeAwareSink) wrapKeyAndValues(keysAndValues []interface{}) []interface{} {
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't fully match the behavior of the KubeAwareEncoder, it only does the wrapping if its not set to Verbose:

if k.Verbose {
// Kubernetes objects implement fmt.Stringer, so if we
// want verbose output, just delegate to that.
return k.Encoder.EncodeEntry(entry, fields)
}
for i, field := range fields {
// intercept stringer fields that happen to be Kubernetes runtime.Object or
// types.NamespacedName values (Kubernetes runtime.Objects commonly
// implement String, apparently).
// *unstructured.Unstructured does NOT implement fmt.Striger interface.
// We have handle it specially.
if field.Type == zapcore.StringerType || field.Type == zapcore.ReflectType {
switch val := field.Interface.(type) {
case runtime.Object:
fields[i] = zapcore.Field{
Type: zapcore.ObjectMarshalerType,
Key: field.Key,
Interface: kubeObjectWrapper{obj: val},
}
}
}
}

var _ logr.LogSink = (*KubeAwareSink)(nil)

// KubeAwareSink is a logr.LogSink that understands Kubernetes objects.
type KubeAwareSink struct {
Copy link
Member

Choose a reason for hiding this comment

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

Why is this type exported?

sink logr.LogSink
}

// KubeAware wraps a logr.logger to make it aware of Kubernetes objects.
Copy link
Member

Choose a reason for hiding this comment

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

Please describe the actual behavior here

limitations under the License.
*/

// Package logr contains helpers for setting up a Kubernetes object aware logr.Logger.
Copy link
Member

Choose a reason for hiding this comment

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

Unless I am missing something, this fully obsoletes the need for the KubeAwareEncoder. If so, please delete it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

logger.WithValues bypasses KubeAwareEncoder
3 participants