From 501594f8c736b011d5fe7f1e3d862c9de34e282d Mon Sep 17 00:00:00 2001 From: hurf Date: Thu, 13 Aug 2015 16:23:10 +0800 Subject: [PATCH 1/2] fix AGE error when resource has no creationTimeStamp If the resource doesn't contain creationTimeStamp, report AGE as unknown. This usually happens when the resouces is created locally or is not accually created at server side. --- pkg/kubectl/resource_printer.go | 3 +++ pkg/kubectl/resource_printer_test.go | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/pkg/kubectl/resource_printer.go b/pkg/kubectl/resource_printer.go index 0db84a8c339..76e28aebcee 100644 --- a/pkg/kubectl/resource_printer.go +++ b/pkg/kubectl/resource_printer.go @@ -376,6 +376,9 @@ func shortHumanDuration(d time.Duration) string { // translateTimestamp returns the elapsed time since timestamp in // human-readable approximation. func translateTimestamp(timestamp util.Time) string { + if timestamp.IsZero() { + return "" + } return shortHumanDuration(time.Now().Sub(timestamp.Time)) } diff --git a/pkg/kubectl/resource_printer_test.go b/pkg/kubectl/resource_printer_test.go index a4f79e4bf5f..ebc7ab7a437 100644 --- a/pkg/kubectl/resource_printer_test.go +++ b/pkg/kubectl/resource_printer_test.go @@ -1097,3 +1097,26 @@ func TestPrintPodWithLabels(t *testing.T) { buf.Reset() } } + +type stringTestList []struct { + name, got, exp string +} + +func TestTranslateTimestamp(t *testing.T) { + tl := stringTestList{ + {"now", translateTimestamp(util.Time{Time: time.Now()}), "0s"}, + {"unknown", translateTimestamp(util.Time{}), ""}, + {"30 seconds ago", translateTimestamp(util.Time{Time: time.Now().Add(-3e10)}), "30s"}, + {"5 minutes ago", translateTimestamp(util.Time{Time: time.Now().Add(-3e11)}), "5m"}, + {"an hour ago", translateTimestamp(util.Time{Time: time.Now().Add(-6e12)}), "1h"}, + {"2 days ago", translateTimestamp(util.Time{Time: time.Now().AddDate(0, 0, -2)}), "2d"}, + {"months ago", translateTimestamp(util.Time{Time: time.Now().AddDate(0, -3, 0)}), "92d"}, + {"10 years ago", translateTimestamp(util.Time{Time: time.Now().AddDate(-10, 0, 0)}), "10y"}, + } + for _, test := range tl { + if test.got != test.exp { + t.Errorf("On %v, expected '%v', but got '%v'", + test.name, test.exp, test.got) + } + } +} From c20ed0344a785cf3a672399c98e1c5e8cc56fd0d Mon Sep 17 00:00:00 2001 From: hurf Date: Fri, 14 Aug 2015 17:10:22 +0800 Subject: [PATCH 2/2] Enhance shortHumanDuration to handle more situation Allow 2 seconds deviation of current time. For time more than 2 seconds from current time, output as ''. --- pkg/kubectl/resource_printer.go | 8 +++++++- pkg/kubectl/resource_printer_test.go | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/kubectl/resource_printer.go b/pkg/kubectl/resource_printer.go index 76e28aebcee..309e5c99c15 100644 --- a/pkg/kubectl/resource_printer.go +++ b/pkg/kubectl/resource_printer.go @@ -361,7 +361,13 @@ func podHostString(host, ip string) string { } func shortHumanDuration(d time.Duration) string { - if seconds := int(d.Seconds()); seconds < 60 { + // Allow deviation no more than 2 seconds(excluded) to tolerate machine time + // inconsistence, it can be considered as almost now. + if seconds := int(d.Seconds()); seconds < -1 { + return fmt.Sprintf("") + } else if seconds < 0 { + return fmt.Sprintf("0s") + } else if seconds < 60 { return fmt.Sprintf("%ds", seconds) } else if minutes := int(d.Minutes()); minutes < 60 { return fmt.Sprintf("%dm", minutes) diff --git a/pkg/kubectl/resource_printer_test.go b/pkg/kubectl/resource_printer_test.go index ebc7ab7a437..1a15740e531 100644 --- a/pkg/kubectl/resource_printer_test.go +++ b/pkg/kubectl/resource_printer_test.go @@ -1104,6 +1104,8 @@ type stringTestList []struct { func TestTranslateTimestamp(t *testing.T) { tl := stringTestList{ + {"a while from now", translateTimestamp(util.Time{Time: time.Now().Add(2.1e9)}), ""}, + {"almost now", translateTimestamp(util.Time{Time: time.Now().Add(1.9e9)}), "0s"}, {"now", translateTimestamp(util.Time{Time: time.Now()}), "0s"}, {"unknown", translateTimestamp(util.Time{}), ""}, {"30 seconds ago", translateTimestamp(util.Time{Time: time.Now().Add(-3e10)}), "30s"},