diff --git a/k8s_references.go b/k8s_references.go index db58f8ba..b2277873 100644 --- a/k8s_references.go +++ b/k8s_references.go @@ -76,19 +76,34 @@ func KRef(namespace, name string) ObjectRef { } } -// KObjs returns slice of ObjectRef from an slice of ObjectMeta -func KObjs(arg interface{}) []ObjectRef { - s := reflect.ValueOf(arg) +type kobjs struct { + arg interface{} +} + +func (k kobjs) MarshalLog() interface{} { + s := reflect.ValueOf(k.arg) + var empty []ObjectRef if s.Kind() != reflect.Slice { - return nil + // This mirrors the traditional behavior, but perhaps isn't + // what we want? + return empty } objectRefs := make([]ObjectRef, 0, s.Len()) for i := 0; i < s.Len(); i++ { if v, ok := s.Index(i).Interface().(KMetadata); ok { objectRefs = append(objectRefs, KObj(v)) } else { - return nil + // This mirrors the traditional behavior, but perhaps isn't + // what we want? + return empty } } return objectRefs } + +// KObjs returns an object that will be logged like +// a slice of ObjectRefs for each entry in the parameter +// slice. If the parameter is not a slice, nil will be logged. +func KObjs(arg interface{}) logr.Marshaler { + return kobjs{arg} +} diff --git a/klog_test.go b/klog_test.go index 062fae3e..eb8d79b7 100644 --- a/klog_test.go +++ b/klog_test.go @@ -1840,8 +1840,9 @@ func TestKObjs(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if !reflect.DeepEqual(KObjs(tt.obj), tt.want) { - t.Errorf("\nwant:\t %v\n got:\t %v", tt.want, KObjs(tt.obj)) + got := KObjs(tt.obj).MarshalLog() + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("\nwant:\t %v\n got:\t %v", tt.want, got) } }) }