@@ -156,6 +156,15 @@ func KObj(obj KMetadata) ObjectRef
156
156
// >> I1025 00:15:15.525108 1 controller_utils.go:116] "Pod status updated" pod="kube-system/kubedns" status="ready"
157
157
func KRef(namespace, name string) ObjectRef
158
158
159
+ // KObjSlice takes a slice of objects that implement the KMetadata interface
160
+ // and returns an object that gets logged as a slice of ObjectRef values or a
161
+ // string containing those values, depending on whether the logger prefers text
162
+ // output or structured output.
163
+ // >> klog.InfoS("Pods status updated", "pods", klog.KObjSlice(pods), "status", "ready")
164
+ // output:
165
+ // >> I1025 00:15:15.525108 1 controller_utils.go:116] "Pods status updated" pods="[kube-system/kubedns kube-system/metrics-server]" status="ready"
166
+ func KObjSlice(arg interface{}) interface {}
167
+
159
168
// ObjectRef represents a reference to a kubernetes object used for logging purpose
160
169
// In text logs it is serialized into "{namespace}/{name}" or "{name}" if namespace is empty
161
170
type ObjectRef struct {
@@ -467,16 +476,18 @@ func ChangePodStatus(newStatus, currentStatus string) {
467
476
## Good practice for passing values in structured logging
468
477
469
478
When passing a value for a key-value pair, please use following rules:
470
- * Prefer using Kubernetes objects (for example ` *v1.Pod ` ) and log them using ` klog.KObj `
479
+ * Prefer using Kubernetes objects and log them using ` klog.KObj ` or ` klog.KObjSlice `
471
480
* When the original object is not available, use ` klog.KRef ` instead
481
+ * when only one object (for example ` *v1.Pod ` ), we use` klog.KObj `
482
+ * When type is object slice (for example ` []*v1.Pod ` ), we use ` klog.KObjSlice `
472
483
* Pass structured values directly (avoid calling ` .String() ` on them first)
473
484
* When the goal is to log a ` []byte ` array as string, explicitly convert with ` string(<byte array>) ` .
474
485
475
- ### Prefer using Kubernetes objects (for example ` *v1.Pod ` ) and log them using ` klog.KObj `
486
+ ### Prefer using Kubernetes objects (for example ` *v1.Pod ` or ` []*v1.Pod ` ) and log them using ` klog.KObj ` or ` klog.KObjSlice `
476
487
477
488
As part of the structured logging migration, we want to ensure that Kubernetes object references are
478
489
consistent within the
479
- codebase. Two new utility functions were introduced to klog: ` klog.KObj ` and ` klog.KRef ` .
490
+ codebase. Three new utility functions were introduced to klog: ` klog.KObj ` ` klog.KObjSlice ` and ` klog.KRef ` .
480
491
481
492
Any existing logging code that makes a reference (such as name, namespace) to a Kubernetes
482
493
object (for example: Pod, Node, Deployment, CustomResourceDefinition) should be rewritten to
0 commit comments