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

Automated cherry pick of #89999: If firstTimestamp is not set use eventTime when printing event #94252

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions pkg/printers/internalversion/printers.go
Expand Up @@ -655,6 +655,16 @@ func listWithMoreString(list []string, more bool, count, max int) string {
return ret
}

// translateMicroTimestampSince returns the elapsed time since timestamp in
// human-readable approximation.
func translateMicroTimestampSince(timestamp metav1.MicroTime) string {
if timestamp.IsZero() {
return "<unknown>"
}

return duration.HumanDuration(time.Since(timestamp.Time))
}

// translateTimestampSince returns the elapsed time since timestamp in
// human-readable approximation.
func translateTimestampSince(timestamp metav1.Time) string {
Expand Down Expand Up @@ -1660,6 +1670,9 @@ func printEvent(obj *api.Event, options printers.GenerateOptions) ([]metav1.Tabl
}

firstTimestamp := translateTimestampSince(obj.FirstTimestamp)
if obj.FirstTimestamp.IsZero() {
firstTimestamp = translateMicroTimestampSince(obj.EventTime)
}
lastTimestamp := translateTimestampSince(obj.LastTimestamp)
if obj.LastTimestamp.IsZero() {
lastTimestamp = firstTimestamp
Expand Down
48 changes: 48 additions & 0 deletions pkg/printers/internalversion/printers_test.go
Expand Up @@ -136,6 +136,30 @@ func TestPrintEvent(t *testing.T) {
// Columns: Last Seen, Type, Reason, Object, Subobject, Message, First Seen, Count, Name
expected: []metav1.TableRow{{Cells: []interface{}{"2d", "Warning", "Event Reason", "deployment/Deployment Name", "spec.containers{foo}", "kubelet, Node1", "Message Data", "3d", int64(6), "event2"}}},
},
// Basic event, w/o FirstTimestamp set
{
event: api.Event{
Source: api.EventSource{
Component: "kubelet",
Host: "Node1",
},
InvolvedObject: api.ObjectReference{
Kind: "Deployment",
Name: "Deployment Name",
FieldPath: "spec.containers{foo}",
},
Reason: "Event Reason",
Message: "Message Data",
EventTime: metav1.MicroTime{Time: time.Now().UTC().AddDate(0, 0, -3)},
LastTimestamp: metav1.Time{Time: time.Now().UTC().AddDate(0, 0, -3)},
Count: 1,
Type: api.EventTypeWarning,
ObjectMeta: metav1.ObjectMeta{Name: "event3"},
},
options: printers.GenerateOptions{Wide: true},
// Columns: Last Seen, Type, Reason, Object, Subobject, Message, First Seen, Count, Name
expected: []metav1.TableRow{{Cells: []interface{}{"3d", "Warning", "Event Reason", "deployment/Deployment Name", "spec.containers{foo}", "kubelet, Node1", "Message Data", "3d", int64(1), "event3"}}},
},
// Basic event, w/o LastTimestamp set
{
event: api.Event{
Expand All @@ -150,6 +174,7 @@ func TestPrintEvent(t *testing.T) {
},
Reason: "Event Reason",
Message: "Message Data",
EventTime: metav1.MicroTime{Time: time.Now().UTC().AddDate(0, 0, -3)},
FirstTimestamp: metav1.Time{Time: time.Now().UTC().AddDate(0, 0, -3)},
Count: 1,
Type: api.EventTypeWarning,
Expand All @@ -159,6 +184,29 @@ func TestPrintEvent(t *testing.T) {
// Columns: Last Seen, Type, Reason, Object, Subobject, Message, First Seen, Count, Name
expected: []metav1.TableRow{{Cells: []interface{}{"3d", "Warning", "Event Reason", "deployment/Deployment Name", "spec.containers{foo}", "kubelet, Node1", "Message Data", "3d", int64(1), "event3"}}},
},
// Basic event, w/o FirstTimestamp and LastTimestamp set
{
event: api.Event{
Source: api.EventSource{
Component: "kubelet",
Host: "Node1",
},
InvolvedObject: api.ObjectReference{
Kind: "Deployment",
Name: "Deployment Name",
FieldPath: "spec.containers{foo}",
},
Reason: "Event Reason",
Message: "Message Data",
EventTime: metav1.MicroTime{Time: time.Now().UTC().AddDate(0, 0, -3)},
Count: 1,
Type: api.EventTypeWarning,
ObjectMeta: metav1.ObjectMeta{Name: "event3"},
},
options: printers.GenerateOptions{Wide: true},
// Columns: Last Seen, Type, Reason, Object, Subobject, Message, First Seen, Count, Name
expected: []metav1.TableRow{{Cells: []interface{}{"3d", "Warning", "Event Reason", "deployment/Deployment Name", "spec.containers{foo}", "kubelet, Node1", "Message Data", "3d", int64(1), "event3"}}},
},
}

for i, test := range tests {
Expand Down