diff --git a/staging/src/k8s.io/apimachinery/pkg/types/namespacedname.go b/staging/src/k8s.io/apimachinery/pkg/types/namespacedname.go index b19750f3a0b..29fb4f950a4 100644 --- a/staging/src/k8s.io/apimachinery/pkg/types/namespacedname.go +++ b/staging/src/k8s.io/apimachinery/pkg/types/namespacedname.go @@ -37,3 +37,13 @@ const ( func (n NamespacedName) String() string { return n.Namespace + string(Separator) + n.Name } + +// MarshalLog emits a struct containing required key/value pair +func (n NamespacedName) MarshalLog() interface{} { + return struct { + Name, Namespace string + }{ + Name: n.Name, + Namespace: n.Namespace, + } +} diff --git a/staging/src/k8s.io/component-base/logs/json/json_test.go b/staging/src/k8s.io/component-base/logs/json/json_test.go index 88bba09ef7e..3de190e3cdf 100644 --- a/staging/src/k8s.io/component-base/logs/json/json_test.go +++ b/staging/src/k8s.io/component-base/logs/json/json_test.go @@ -27,6 +27,7 @@ import ( "go.uber.org/zap" "go.uber.org/zap/zapcore" + "k8s.io/apimachinery/pkg/types" logsapi "k8s.io/component-base/logs/api/v1" ) @@ -71,6 +72,11 @@ func TestZapLoggerInfo(t *testing.T) { format: "{\"ts\":%f,\"caller\":\"json/json_test.go:%d\",\"msg\":\"test for duplicate keys\",\"v\":0,\"akey\":\"avalue\",\"akey\":\"anothervalue\"}\n", keysValues: []interface{}{"akey", "avalue", "akey", "anothervalue"}, }, + { + msg: "test for NamespacedName argument", + format: "{\"ts\":%f,\"caller\":\"json/json_test.go:%d\",\"msg\":\"test for NamespacedName argument\",\"v\":0,\"obj\":{\"Name\":\"kube-proxy\",\"Namespace\":\"kube-system\"}}\n", + keysValues: []interface{}{"obj", types.NamespacedName{Name: "kube-proxy", Namespace: "kube-system"}}, + }, } for _, data := range testDataInfo {