diff --git a/pkg/util/event/sorted_event_list.go b/pkg/util/event/sorted_event_list.go new file mode 100644 index 000000000..9967f953e --- /dev/null +++ b/pkg/util/event/sorted_event_list.go @@ -0,0 +1,36 @@ +/* +Copyright 2014 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package event + +import ( + corev1 "k8s.io/api/core/v1" +) + +// SortableEvents implements sort.Interface for []api.Event based on the Timestamp field +type SortableEvents []corev1.Event + +func (list SortableEvents) Len() int { + return len(list) +} + +func (list SortableEvents) Swap(i, j int) { + list[i], list[j] = list[j], list[i] +} + +func (list SortableEvents) Less(i, j int) bool { + return list[i].LastTimestamp.Time.Before(list[j].LastTimestamp.Time) +} diff --git a/pkg/util/event/sorted_event_list_test.go b/pkg/util/event/sorted_event_list_test.go new file mode 100644 index 000000000..5c1d79257 --- /dev/null +++ b/pkg/util/event/sorted_event_list_test.go @@ -0,0 +1,66 @@ +/* +Copyright 2014 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package event + +import ( + "sort" + "testing" + "time" + + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func TestSortableEvents(t *testing.T) { + // Arrange + list := SortableEvents([]corev1.Event{ + { + Source: corev1.EventSource{Component: "kubelet"}, + Message: "Item 1", + FirstTimestamp: metav1.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)), + LastTimestamp: metav1.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)), + Count: 1, + Type: corev1.EventTypeNormal, + }, + { + Source: corev1.EventSource{Component: "scheduler"}, + Message: "Item 2", + FirstTimestamp: metav1.NewTime(time.Date(1987, time.June, 17, 0, 0, 0, 0, time.UTC)), + LastTimestamp: metav1.NewTime(time.Date(1987, time.June, 17, 0, 0, 0, 0, time.UTC)), + Count: 1, + Type: corev1.EventTypeNormal, + }, + { + Source: corev1.EventSource{Component: "kubelet"}, + Message: "Item 3", + FirstTimestamp: metav1.NewTime(time.Date(2002, time.December, 25, 0, 0, 0, 0, time.UTC)), + LastTimestamp: metav1.NewTime(time.Date(2002, time.December, 25, 0, 0, 0, 0, time.UTC)), + Count: 1, + Type: corev1.EventTypeNormal, + }, + }) + + // Act + sort.Sort(list) + + // Assert + if list[0].Message != "Item 2" || + list[1].Message != "Item 3" || + list[2].Message != "Item 1" { + t.Fatal("List is not sorted by time. List: ", list) + } +}