Skip to content

Commit f62a0f5

Browse files
emmjohnsonMarty Spiewakalvaroaleman
authored
🐛 Check to see if obj is nil so there is no panic trying to get the gvk (#1807)
* Check to see if obj is nil so there is no panic trying to get the gvk Co-authored-by: Marty Spiewak <[email protected]> * Update pkg/log/zap/zap_test.go Co-authored-by: Alvaro Aleman <[email protected]> * Fix lint check Co-authored-by: Marty Spiewak <[email protected]> Co-authored-by: Marty Spiewak <[email protected]> Co-authored-by: Alvaro Aleman <[email protected]>
1 parent b6da9c1 commit f62a0f5

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

pkg/log/zap/kube_helpers.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package zap
1818

1919
import (
2020
"fmt"
21+
"reflect"
2122

2223
"go.uber.org/zap/buffer"
2324
"go.uber.org/zap/zapcore"
@@ -64,6 +65,11 @@ type kubeObjectWrapper struct {
6465
func (w kubeObjectWrapper) MarshalLogObject(enc zapcore.ObjectEncoder) error {
6566
// TODO(directxman12): log kind and apiversion if not set explicitly (common case)
6667
// -- needs an a scheme to convert to the GVK.
68+
69+
if reflect.ValueOf(w.obj).IsNil() {
70+
return fmt.Errorf("got nil for runtime.Object")
71+
}
72+
6773
if gvk := w.obj.GetObjectKind().GroupVersionKind(); gvk.Version != "" {
6874
enc.AddString("apiVersion", gvk.GroupVersion().String())
6975
enc.AddString("kind", gvk.Kind)

pkg/log/zap/zap_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,14 @@ var _ = Describe("Zap logger setup", func() {
255255
"namespace": name.Namespace,
256256
}))
257257
})
258+
259+
It("should not panic with nil obj", func() {
260+
var pod *corev1.Pod
261+
logger.Info("here's a kubernetes object", "thing", pod)
262+
263+
outRaw := logOut.Bytes()
264+
Expect(string(outRaw)).Should(ContainSubstring("got nil for runtime.Object"))
265+
})
258266
}
259267

260268
Context("with logger created using New", func() {

0 commit comments

Comments
 (0)