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 v1beta1.Event printing in kubectl #92542

Merged
merged 1 commit into from
Jul 9, 2020
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
16 changes: 15 additions & 1 deletion pkg/printers/internalversion/printers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1673,11 +1673,25 @@ func printEvent(obj *api.Event, options printers.GenerateOptions) ([]metav1.Tabl
if obj.FirstTimestamp.IsZero() {
firstTimestamp = translateMicroTimestampSince(obj.EventTime)
}

lastTimestamp := translateTimestampSince(obj.LastTimestamp)
if obj.LastTimestamp.IsZero() {
lastTimestamp = firstTimestamp
}

count := obj.Count
if obj.Series != nil {
lastTimestamp = translateMicroTimestampSince(obj.Series.LastObservedTime)
// When a series is created for the first time, its count is set to 1.
// However, for a series to be created, there needs to be an isomorphic
// singleton event created beforehand. This singleton event is not
// counted in the series count which is why one is added here.
count = obj.Series.Count + 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm reading the proposal and code correctly, there's no need to add one here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a series is created for the first time, its count is set to 1. However, for a series to be created, there needs to be an isomorphic singleton event created beforehand. This singleton event is not counted in the series count which is why I added one here.

} else if count == 0 {
// Singleton events don't have a count set in the new API.
count = 1
}

var target string
if len(obj.InvolvedObject.Name) > 0 {
target = fmt.Sprintf("%s/%s", strings.ToLower(obj.InvolvedObject.Kind), obj.InvolvedObject.Name)
Expand All @@ -1694,7 +1708,7 @@ func printEvent(obj *api.Event, options printers.GenerateOptions) ([]metav1.Tabl
formatEventSource(obj.Source),
strings.TrimSpace(obj.Message),
firstTimestamp,
int64(obj.Count),
int64(count),
obj.Name,
)
} else {
Expand Down
56 changes: 52 additions & 4 deletions pkg/printers/internalversion/printers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@ func TestPrintEvent(t *testing.T) {
FirstTimestamp: metav1.Time{Time: time.Now().UTC().AddDate(0, 0, -3)},
Count: 1,
Type: api.EventTypeWarning,
ObjectMeta: metav1.ObjectMeta{Name: "event3"},
ObjectMeta: metav1.ObjectMeta{Name: "event4"},
},
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"}}},
expected: []metav1.TableRow{{Cells: []interface{}{"3d", "Warning", "Event Reason", "deployment/Deployment Name", "spec.containers{foo}", "kubelet, Node1", "Message Data", "3d", int64(1), "event4"}}},
},
// Basic event, w/o FirstTimestamp and LastTimestamp set
{
Expand All @@ -201,11 +201,59 @@ func TestPrintEvent(t *testing.T) {
EventTime: metav1.MicroTime{Time: time.Now().UTC().AddDate(0, 0, -3)},
Count: 1,
Type: api.EventTypeWarning,
ObjectMeta: metav1.ObjectMeta{Name: "event3"},
ObjectMeta: metav1.ObjectMeta{Name: "event5"},
},
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"}}},
expected: []metav1.TableRow{{Cells: []interface{}{"3d", "Warning", "Event Reason", "deployment/Deployment Name", "spec.containers{foo}", "kubelet, Node1", "Message Data", "3d", int64(1), "event5"}}},
},
// Basic event serie, w/o FirstTimestamp, LastTimestamp and Count set
{
event: api.Event{
Source: api.EventSource{
Component: "kubelet",
Host: "Node1",
},
InvolvedObject: api.ObjectReference{
Kind: "Deployment",
Name: "Deployment Name",
FieldPath: "spec.containers{foo}",
},
Series: &api.EventSeries{
Count: 1,
LastObservedTime: metav1.MicroTime{Time: time.Now().UTC().AddDate(0, 0, -2)},
},
Reason: "Event Reason",
Message: "Message Data",
EventTime: metav1.MicroTime{Time: time.Now().UTC().AddDate(0, 0, -3)},
Type: api.EventTypeWarning,
ObjectMeta: metav1.ObjectMeta{Name: "event6"},
},
options: printers.GenerateOptions{Wide: true},
// 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(2), "event6"}}},
},
// Singleton event, w/o FirstTimestamp, LastTimestamp and Count 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)},
Type: api.EventTypeWarning,
ObjectMeta: metav1.ObjectMeta{Name: "event7"},
},
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), "event7"}}},
},
}

Expand Down