Skip to content
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

🐛 KubeAwareEncoder should handle unstructured object correctly #881

Merged
merged 1 commit into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/log/zap/kube_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ func (k *KubeAwareEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Fie
// intercept stringer fields that happen to be Kubernetes runtime.Object or
// types.NamespacedName values (Kubernetes runtime.Objects commonly
// implement String, apparently).
if field.Type == zapcore.StringerType {
// *unstructured.Unstructured does NOT implement fmt.Striger interface.
// We have handle it specially.
if field.Type == zapcore.StringerType || field.Type == zapcore.ReflectType {
switch val := field.Interface.(type) {
case runtime.Object:
fields[i] = zapcore.Field{
Expand Down
22 changes: 22 additions & 0 deletions pkg/log/zap/zap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
kapi "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
)

Expand Down Expand Up @@ -238,6 +239,27 @@ var _ = Describe("Zap logger setup", func() {
}))
})

It("should log an unstructured Kubernetes object", func() {
pod := &unstructured.Unstructured{
Object: map[string]interface{}{
"metadata": map[string]interface{}{
"name": "some-pod",
"namespace": "some-ns",
},
},
}
logger.Info("here's a kubernetes object", "thing", pod)

outRaw := logOut.Bytes()
res := map[string]interface{}{}
Expect(json.Unmarshal(outRaw, &res)).To(Succeed())

Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{
"name": "some-pod",
"namespace": "some-ns",
}))
})

It("should log a standard namespaced NamespacedName name and namespace", func() {
name := types.NamespacedName{Name: "some-pod", Namespace: "some-ns"}
logger.Info("here's a kubernetes object", "thing", name)
Expand Down