Merge pull request #89999 from soltysh/fix_events2

If firstTimestamp is not set use eventTime when printing event
This commit is contained in:
Kubernetes Prow Robot 2020-04-20 08:45:40 -07:00 committed by GitHub
commit 5655350b2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 0 deletions

View File

@ -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 {
@ -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

View File

@ -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{
@ -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,
@ -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 {