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
RFC: ktesting: add object formatter #116952
Conversation
This is useful for logging types which implement String incorrectly, for example by inheriting it from metav1.TypeMeta. If implementing fmt.Stringer and logr.Marshaler for the type is not possible or not desirable, then log calls can wrap an instance of the type or a pointer to it with Format. That then returns a wrapper which implements both. YAML formatting is used for pretty-printing to a multi-line string.
Please note that we're already in Test Freeze for the Fast forwards are scheduled to happen every 6 hours, whereas the most recent run was: Mon Mar 27 16:26:31 UTC 2023. |
This issue is currently awaiting triage. If a SIG or subproject determines this is a relevant issue, they will accept it by applying the The 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/test-infra repository. |
"sigs.k8s.io/yaml" | ||
) | ||
|
||
func Format[T any](obj T) formatAny[T] { |
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.
TODO: add documentation...
limitations under the License. | ||
*/ | ||
|
||
package ktesting |
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.
would we want the example #115950 was trying to fix (in non-test kubelet server.go code) to reference the test/utils/ktesting package?
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 is up for debate. I picked ktesting
because I intend to use YAML here for pretty-printing, if I can convince myself that better formatting during unit testing is really desirable. It could hide that "normal" formatting isn't as good (klog cannot (or should not?) depend on "sigs.k8s.io/yaml").
If "sigs.k8s.io/yaml" was an acceptable dependency for klog, then klog would be a good place for this code.
"staging/src/k8s.io/component-base/logs" might also be okay.
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.
Referencing the test package in normal code might not seem like a good idea.
+1 for klog
or component-base/logs
we can also consider using gopkg.in/yaml.v2
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: pohly The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
@pohly: The following tests failed, say
Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR. 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/test-infra repository. I understand the commands that are listed here. |
kubernetes/klog#374 is a solution with JSON in klog. |
i've merged kubernetes/klog#374 .. do we need a klog release and then revisit this? |
I have one more thing that I want to propose for klog, then we can do a release of it, update Kubernetes, and close this PR. |
The klog update with /close |
@pohly: Closed this PR. In response to this:
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/test-infra repository. |
What type of PR is this?
/kind feature
What this PR does / why we need it:
This is useful for logging types which implement String incorrectly, for example by inheriting it from metav1.TypeMeta. If implementing fmt.Stringer and logr.Marshaler for the type is not possible or not desirable, then log calls can wrap an instance of the type or a pointer to it with Format. That then returns a wrapper which implements both.
YAML formatting is used for pretty-printing to a multi-line string.
Which issue(s) this PR fixes:
Related-to: #115950
Special notes for your reviewer:
Does this PR introduce a user-facing change?
/cc @SataQiu @liggitt