Skip to content

Commit 7ce5788

Browse files
authored
fix: the timestamp of managed field could be empty (#10)
Signed-off-by: Jian Zeng <anonymousknight96@gmail.com> Signed-off-by: Jian Zeng <anonymousknight96@gmail.com>
1 parent 4351e4f commit 7ce5788

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

cmd/marshal.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,16 @@ func (m *Marshaller) buildTree(managedFields []metav1.ManagedFieldsEntry, mgrMax
7979
switch m.timeFormat {
8080
case TimeFormatFull:
8181
timeFormatter = func(t *metav1.Time) string {
82+
if t == nil {
83+
return ""
84+
}
8285
return t.Format(timeLayout)
8386
}
8487
case TimeFormatRelative:
8588
timeFormatter = func(t *metav1.Time) string {
89+
if t == nil {
90+
return ""
91+
}
8692
return humanDuration(m.now.Sub(t.Time))
8793
}
8894
case TimeFormatNone:
@@ -196,7 +202,7 @@ func (m *Marshaller) marshalMetaObject(obj metav1.Object) ([]byte, error) {
196202
opMaxLength = len(field.Operation)
197203
}
198204

199-
if relativeTime {
205+
if relativeTime && field.Time != nil {
200206
d := humanDuration(m.now.Sub(field.Time.Time))
201207
if len(d) > timeMaxLength {
202208
timeMaxLength = len(d)

cmd/marshal_test.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"testing"
55
"time"
66

7-
"github.com/stretchr/testify/assert"
7+
"github.com/google/go-cmp/cmp"
88
"github.com/stretchr/testify/require"
99
corev1 "k8s.io/api/core/v1"
1010
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -59,6 +59,7 @@ func TestFieldListMatchObject(t *testing.T) {
5959
}
6060

6161
func TestMarshaller_MarshalMetaObject(t *testing.T) {
62+
r := require.New(t)
6263
now := metav1.NewTime(time.Unix(1606150365, 0).UTC())
6364
s1 := fieldpath.NewSet(
6465
fieldpath.MakePathOrDie("metadata", "finalizers"),
@@ -123,9 +124,14 @@ func TestMarshaller_MarshalMetaObject(t *testing.T) {
123124
"protocol"),
124125
)
125126

127+
s4 := fieldpath.NewSet(
128+
fieldpath.MakePathOrDie("metadata", "labels", "s4"),
129+
)
130+
126131
f1, _ := s1.ToJSON()
127132
f2, _ := s2.ToJSON()
128133
f3, _ := s3.ToJSON()
134+
f4, _ := s4.ToJSON()
129135

130136
pod := &corev1.Pod{
131137
ObjectMeta: metav1.ObjectMeta{
@@ -148,10 +154,16 @@ func TestMarshaller_MarshalMetaObject(t *testing.T) {
148154
Time: &now,
149155
FieldsV1: &metav1.FieldsV1{Raw: f3},
150156
},
157+
{
158+
Manager: "m4",
159+
Operation: metav1.ManagedFieldsOperationUpdate,
160+
FieldsV1: &metav1.FieldsV1{Raw: f4},
161+
},
151162
},
152163
Labels: map[string]string{
153164
"app": "bar",
154165
"version": "v1",
166+
"s4": "v",
155167
},
156168
Finalizers: []string{
157169
"service.kubernetes.io/load-balancer-cleanup",
@@ -194,6 +206,7 @@ m1 (Update 2020-11-23 16:52:45 +0000) - service.kubernetes.io/load-balancer-cl
194206
m2 (Update 2020-11-23 16:52:45 +0000) - service.kubernetes.io/foo
195207
m1 (Update 2020-11-23 16:52:45 +0000) labels:
196208
m1 (Update 2020-11-23 16:52:45 +0000) app: bar
209+
m4 (Update ) s4: v
197210
m1 (Update 2020-11-23 16:52:45 +0000) version: v1
198211
m1 (Update 2020-11-23 16:52:45 +0000) ownerReferences:
199212
m1 (Update 2020-11-23 16:52:45 +0000) - apiVersion: ""
@@ -215,10 +228,10 @@ m2 (Update 2020-11-23 16:52:45 +0000) resources: {}
215228
status: {}
216229
`
217230
data, err := MarshalMetaObject(pod, TimeFormatFull)
218-
if err != nil {
219-
t.Fatal(err)
231+
r.NoError(err)
232+
if diff := cmp.Diff(expected, string(data)); len(diff) > 0 {
233+
t.Errorf("unexpected diff (-want +got): %s", diff)
220234
}
221-
assert.Equal(t, expected, string(data))
222235
}
223236

224237
func TestBuildTree(t *testing.T) {

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/knight42/kubectl-blame
33
go 1.19
44

55
require (
6+
github.com/google/go-cmp v0.5.8
67
github.com/spf13/cobra v1.6.1
78
github.com/stretchr/testify v1.8.1
89
k8s.io/api v0.25.3

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
170170
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
171171
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
172172
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
173+
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
173174
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
174175
github.com/google/gofuzz v1.1.0 h1:Hsa8mG0dQ46ij8Sl2AYJDUv1oA9/d6Vk+3LG99Oe02g=
175176
github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=

0 commit comments

Comments
 (0)