Skip to content

Commit 808fb25

Browse files
committed
If firstTimestamp is not set use eventTime when printing event
1 parent 7b20442 commit 808fb25

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

pkg/printers/internalversion/printers.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,16 @@ func listWithMoreString(list []string, more bool, count, max int) string {
655655
return ret
656656
}
657657

658+
// translateMicroTimestampSince returns the elapsed time since timestamp in
659+
// human-readable approximation.
660+
func translateMicroTimestampSince(timestamp metav1.MicroTime) string {
661+
if timestamp.IsZero() {
662+
return "<unknown>"
663+
}
664+
665+
return duration.HumanDuration(time.Since(timestamp.Time))
666+
}
667+
658668
// translateTimestampSince returns the elapsed time since timestamp in
659669
// human-readable approximation.
660670
func translateTimestampSince(timestamp metav1.Time) string {
@@ -1660,6 +1670,9 @@ func printEvent(obj *api.Event, options printers.GenerateOptions) ([]metav1.Tabl
16601670
}
16611671

16621672
firstTimestamp := translateTimestampSince(obj.FirstTimestamp)
1673+
if obj.FirstTimestamp.IsZero() {
1674+
firstTimestamp = translateMicroTimestampSince(obj.EventTime)
1675+
}
16631676
lastTimestamp := translateTimestampSince(obj.LastTimestamp)
16641677
if obj.LastTimestamp.IsZero() {
16651678
lastTimestamp = firstTimestamp

pkg/printers/internalversion/printers_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,30 @@ func TestPrintEvent(t *testing.T) {
136136
// Columns: Last Seen, Type, Reason, Object, Subobject, Message, First Seen, Count, Name
137137
expected: []metav1.TableRow{{Cells: []interface{}{"2d", "Warning", "Event Reason", "deployment/Deployment Name", "spec.containers{foo}", "kubelet, Node1", "Message Data", "3d", int64(6), "event2"}}},
138138
},
139+
// Basic event, w/o FirstTimestamp set
140+
{
141+
event: api.Event{
142+
Source: api.EventSource{
143+
Component: "kubelet",
144+
Host: "Node1",
145+
},
146+
InvolvedObject: api.ObjectReference{
147+
Kind: "Deployment",
148+
Name: "Deployment Name",
149+
FieldPath: "spec.containers{foo}",
150+
},
151+
Reason: "Event Reason",
152+
Message: "Message Data",
153+
EventTime: metav1.MicroTime{Time: time.Now().UTC().AddDate(0, 0, -3)},
154+
LastTimestamp: metav1.Time{Time: time.Now().UTC().AddDate(0, 0, -3)},
155+
Count: 1,
156+
Type: api.EventTypeWarning,
157+
ObjectMeta: metav1.ObjectMeta{Name: "event3"},
158+
},
159+
options: printers.GenerateOptions{Wide: true},
160+
// Columns: Last Seen, Type, Reason, Object, Subobject, Message, First Seen, Count, Name
161+
expected: []metav1.TableRow{{Cells: []interface{}{"3d", "Warning", "Event Reason", "deployment/Deployment Name", "spec.containers{foo}", "kubelet, Node1", "Message Data", "3d", int64(1), "event3"}}},
162+
},
139163
// Basic event, w/o LastTimestamp set
140164
{
141165
event: api.Event{
@@ -150,6 +174,7 @@ func TestPrintEvent(t *testing.T) {
150174
},
151175
Reason: "Event Reason",
152176
Message: "Message Data",
177+
EventTime: metav1.MicroTime{Time: time.Now().UTC().AddDate(0, 0, -3)},
153178
FirstTimestamp: metav1.Time{Time: time.Now().UTC().AddDate(0, 0, -3)},
154179
Count: 1,
155180
Type: api.EventTypeWarning,
@@ -159,6 +184,29 @@ func TestPrintEvent(t *testing.T) {
159184
// Columns: Last Seen, Type, Reason, Object, Subobject, Message, First Seen, Count, Name
160185
expected: []metav1.TableRow{{Cells: []interface{}{"3d", "Warning", "Event Reason", "deployment/Deployment Name", "spec.containers{foo}", "kubelet, Node1", "Message Data", "3d", int64(1), "event3"}}},
161186
},
187+
// Basic event, w/o FirstTimestamp and LastTimestamp set
188+
{
189+
event: api.Event{
190+
Source: api.EventSource{
191+
Component: "kubelet",
192+
Host: "Node1",
193+
},
194+
InvolvedObject: api.ObjectReference{
195+
Kind: "Deployment",
196+
Name: "Deployment Name",
197+
FieldPath: "spec.containers{foo}",
198+
},
199+
Reason: "Event Reason",
200+
Message: "Message Data",
201+
EventTime: metav1.MicroTime{Time: time.Now().UTC().AddDate(0, 0, -3)},
202+
Count: 1,
203+
Type: api.EventTypeWarning,
204+
ObjectMeta: metav1.ObjectMeta{Name: "event3"},
205+
},
206+
options: printers.GenerateOptions{Wide: true},
207+
// Columns: Last Seen, Type, Reason, Object, Subobject, Message, First Seen, Count, Name
208+
expected: []metav1.TableRow{{Cells: []interface{}{"3d", "Warning", "Event Reason", "deployment/Deployment Name", "spec.containers{foo}", "kubelet, Node1", "Message Data", "3d", int64(1), "event3"}}},
209+
},
162210
}
163211

164212
for i, test := range tests {

0 commit comments

Comments
 (0)