-
Notifications
You must be signed in to change notification settings - Fork 1.2k
🐛 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
base: main
Are you sure you want to change the base?
Conversation
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: caozhuozi 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 |
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 Once the patch is verified, the new status will be reflected by the 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. |
/ok-to-test |
There was a problem hiding this 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{} { |
There was a problem hiding this comment.
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
:
controller-runtime/pkg/log/zap/kube_helpers.go
Lines 86 to 108 in 2a5cfa4
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 { |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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
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):
To address this,
To summary the changes I made,
logr
, which just makes a logr.Logger aware of runtime.Object.(The need for a separate package and the naming are open for discussion.)