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

fix: the timestamp of managed field could be empty #10

Merged
merged 1 commit into from
Nov 12, 2022
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
8 changes: 7 additions & 1 deletion cmd/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,16 @@ func (m *Marshaller) buildTree(managedFields []metav1.ManagedFieldsEntry, mgrMax
switch m.timeFormat {
case TimeFormatFull:
timeFormatter = func(t *metav1.Time) string {
if t == nil {
return ""
}
return t.Format(timeLayout)
}
case TimeFormatRelative:
timeFormatter = func(t *metav1.Time) string {
if t == nil {
return ""
}
return humanDuration(m.now.Sub(t.Time))
}
case TimeFormatNone:
Expand Down Expand Up @@ -196,7 +202,7 @@ func (m *Marshaller) marshalMetaObject(obj metav1.Object) ([]byte, error) {
opMaxLength = len(field.Operation)
}

if relativeTime {
if relativeTime && field.Time != nil {
d := humanDuration(m.now.Sub(field.Time.Time))
if len(d) > timeMaxLength {
timeMaxLength = len(d)
Expand Down
21 changes: 17 additions & 4 deletions cmd/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"
"time"

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

func TestMarshaller_MarshalMetaObject(t *testing.T) {
r := require.New(t)
now := metav1.NewTime(time.Unix(1606150365, 0).UTC())
s1 := fieldpath.NewSet(
fieldpath.MakePathOrDie("metadata", "finalizers"),
Expand Down Expand Up @@ -123,9 +124,14 @@ func TestMarshaller_MarshalMetaObject(t *testing.T) {
"protocol"),
)

s4 := fieldpath.NewSet(
fieldpath.MakePathOrDie("metadata", "labels", "s4"),
)

f1, _ := s1.ToJSON()
f2, _ := s2.ToJSON()
f3, _ := s3.ToJSON()
f4, _ := s4.ToJSON()

pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -148,10 +154,16 @@ func TestMarshaller_MarshalMetaObject(t *testing.T) {
Time: &now,
FieldsV1: &metav1.FieldsV1{Raw: f3},
},
{
Manager: "m4",
Operation: metav1.ManagedFieldsOperationUpdate,
FieldsV1: &metav1.FieldsV1{Raw: f4},
},
},
Labels: map[string]string{
"app": "bar",
"version": "v1",
"s4": "v",
},
Finalizers: []string{
"service.kubernetes.io/load-balancer-cleanup",
Expand Down Expand Up @@ -194,6 +206,7 @@ m1 (Update 2020-11-23 16:52:45 +0000) - service.kubernetes.io/load-balancer-cl
m2 (Update 2020-11-23 16:52:45 +0000) - service.kubernetes.io/foo
m1 (Update 2020-11-23 16:52:45 +0000) labels:
m1 (Update 2020-11-23 16:52:45 +0000) app: bar
m4 (Update ) s4: v
m1 (Update 2020-11-23 16:52:45 +0000) version: v1
m1 (Update 2020-11-23 16:52:45 +0000) ownerReferences:
m1 (Update 2020-11-23 16:52:45 +0000) - apiVersion: ""
Expand All @@ -215,10 +228,10 @@ m2 (Update 2020-11-23 16:52:45 +0000) resources: {}
status: {}
`
data, err := MarshalMetaObject(pod, TimeFormatFull)
if err != nil {
t.Fatal(err)
r.NoError(err)
if diff := cmp.Diff(expected, string(data)); len(diff) > 0 {
t.Errorf("unexpected diff (-want +got): %s", diff)
}
assert.Equal(t, expected, string(data))
}

func TestBuildTree(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/knight42/kubectl-blame
go 1.19

require (
github.com/google/go-cmp v0.5.8
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.8.1
k8s.io/api v0.25.3
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/gofuzz v1.1.0 h1:Hsa8mG0dQ46ij8Sl2AYJDUv1oA9/d6Vk+3LG99Oe02g=
github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
Expand Down