api: encode NamespacedName with lower case in JSON

That is how a workaround in controller-runtime has encoded
NamespacedName (a33d038b84/pkg/log/zap/kube_helpers.go (L49-L57))
and it is also more consistent with Kubernetes API standards.

The namespace can be left out.

Returning an anonymous struct in MarshalLog makes it impossible for
controller-runtime to adapt the type and thus blocks updating
controller-runtime to Kubernetes 1.27 because log content would
change.
This commit is contained in:
Patrick Ohly 2023-04-12 12:35:57 +02:00
parent 5f23f83587
commit 4438208511
2 changed files with 8 additions and 2 deletions

View File

@ -41,7 +41,8 @@ func (n NamespacedName) String() string {
// MarshalLog emits a struct containing required key/value pair
func (n NamespacedName) MarshalLog() interface{} {
return struct {
Name, Namespace string
Name string `json:"name"`
Namespace string `json:"namespace,omitempty"`
}{
Name: n.Name,
Namespace: n.Namespace,

View File

@ -74,9 +74,14 @@ func TestZapLoggerInfo(t *testing.T) {
},
{
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",
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"}},
},
{
msg: "test for NamespacedName argument with no namespace",
format: "{\"ts\":%f,\"caller\":\"json/json_test.go:%d\",\"msg\":\"test for NamespacedName argument with no namespace\",\"v\":0,\"obj\":{\"name\":\"kube-proxy\"}}\n",
keysValues: []interface{}{"obj", types.NamespacedName{Name: "kube-proxy"}},
},
}
for _, data := range testDataInfo {