Sort events by their first timestamp when dumping them

This commit is contained in:
Janet Kuo 2016-02-23 16:39:29 -08:00
parent b19102ba23
commit ebcce18d96

View File

@ -28,6 +28,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"sort"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -1828,8 +1829,13 @@ func dumpAllNamespaceInfo(c *client.Client, namespace string) {
events, err := c.Events(namespace).List(api.ListOptions{}) events, err := c.Events(namespace).List(api.ListOptions{})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
for _, e := range events.Items { // Sort events by their first timestamp
Logf("event for %v: %v %v: %v", e.InvolvedObject.Name, e.Source, e.Reason, e.Message) sortedEvents := events.Items
if len(sortedEvents) > 1 {
sort.Sort(byFirstTimestamp(sortedEvents))
}
for _, e := range sortedEvents {
Logf("At %v - event for %v: %v %v: %v", e.FirstTimestamp, e.InvolvedObject.Name, e.Source, e.Reason, e.Message)
} }
// Note that we don't wait for any cleanup to propagate, which means // Note that we don't wait for any cleanup to propagate, which means
// that if you delete a bunch of pods right before ending your test, // that if you delete a bunch of pods right before ending your test,
@ -1840,6 +1846,19 @@ func dumpAllNamespaceInfo(c *client.Client, namespace string) {
dumpAllNodeInfo(c) dumpAllNodeInfo(c)
} }
// byFirstTimestamp sorts a slice of events by first timestamp, using their involvedObject's name as a tie breaker.
type byFirstTimestamp []api.Event
func (o byFirstTimestamp) Len() int { return len(o) }
func (o byFirstTimestamp) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
func (o byFirstTimestamp) Less(i, j int) bool {
if o[i].FirstTimestamp.Equal(o[j].FirstTimestamp) {
return o[i].InvolvedObject.Name < o[j].InvolvedObject.Name
}
return o[i].FirstTimestamp.Before(o[j].FirstTimestamp)
}
func dumpAllPodInfo(c *client.Client) { func dumpAllPodInfo(c *client.Client) {
pods, err := c.Pods("").List(api.ListOptions{}) pods, err := c.Pods("").List(api.ListOptions{})
if err != nil { if err != nil {