From 4a60d3ce60ea68925d615171e14b65cfa5d5e59a Mon Sep 17 00:00:00 2001 From: Dmitry Shulyak Date: Wed, 18 May 2016 12:04:29 +0300 Subject: [PATCH] Sort revisions in rollout history as integers Previously keys were sorted as strings, thus it was possible to see such order as 1, 10, 2, 3, 4, 5. Ints64 helper implemented in util/slice module to sort []int64 --- pkg/kubectl/history.go | 24 ++++++++---------------- pkg/util/slice/slice.go | 11 +++++++++++ pkg/util/slice/slice_test.go | 9 +++++++++ 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/pkg/kubectl/history.go b/pkg/kubectl/history.go index 529da21fafb..dece14fda8c 100644 --- a/pkg/kubectl/history.go +++ b/pkg/kubectl/history.go @@ -19,8 +19,6 @@ package kubectl import ( "fmt" "io" - "sort" - "strconv" "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api/meta" @@ -29,7 +27,7 @@ import ( clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset" "k8s.io/kubernetes/pkg/runtime" deploymentutil "k8s.io/kubernetes/pkg/util/deployment" - "k8s.io/kubernetes/pkg/util/errors" + sliceutil "k8s.io/kubernetes/pkg/util/slice" ) const ( @@ -103,30 +101,24 @@ func PrintRolloutHistory(historyInfo HistoryInfo, resource, name string) (string return fmt.Sprintf("No rollout history found in %s %q", resource, name), nil } // Sort the revisionToChangeCause map by revision - var revisions []string - for k := range historyInfo.RevisionToTemplate { - revisions = append(revisions, strconv.FormatInt(k, 10)) + var revisions []int64 + for r := range historyInfo.RevisionToTemplate { + revisions = append(revisions, r) } - sort.Strings(revisions) + sliceutil.SortInts64(revisions) return tabbedString(func(out io.Writer) error { fmt.Fprintf(out, "%s %q:\n", resource, name) fmt.Fprintf(out, "REVISION\tCHANGE-CAUSE\n") - errs := []error{} for _, r := range revisions { // Find the change-cause of revision r - r64, err := strconv.ParseInt(r, 10, 64) - if err != nil { - errs = append(errs, err) - continue - } - changeCause := historyInfo.RevisionToTemplate[r64].Annotations[ChangeCauseAnnotation] + changeCause := historyInfo.RevisionToTemplate[r].Annotations[ChangeCauseAnnotation] if len(changeCause) == 0 { changeCause = "" } - fmt.Fprintf(out, "%s\t%s\n", r, changeCause) + fmt.Fprintf(out, "%d\t%s\n", r, changeCause) } - return errors.NewAggregate(errs) + return nil }) } diff --git a/pkg/util/slice/slice.go b/pkg/util/slice/slice.go index 61657ca44ad..f32dbabcf41 100644 --- a/pkg/util/slice/slice.go +++ b/pkg/util/slice/slice.go @@ -48,3 +48,14 @@ func ShuffleStrings(s []string) []string { } return shuffled } + +// Int64Slice attaches the methods of Interface to []int64, +// sorting in increasing order. +type Int64Slice []int64 + +func (p Int64Slice) Len() int { return len(p) } +func (p Int64Slice) Less(i, j int) bool { return p[i] < p[j] } +func (p Int64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } + +// Sorts []int64 in increasing order +func SortInts64(a []int64) { sort.Sort(Int64Slice(a)) } diff --git a/pkg/util/slice/slice_test.go b/pkg/util/slice/slice_test.go index 64f50b44bec..bdfee47cb86 100644 --- a/pkg/util/slice/slice_test.go +++ b/pkg/util/slice/slice_test.go @@ -68,3 +68,12 @@ func TestShuffleStrings(t *testing.T) { } } } + +func TestSortInts64(t *testing.T) { + src := []int64{10, 1, 2, 3, 4, 5, 6} + expected := []int64{1, 2, 3, 4, 5, 6, 10} + SortInts64(src) + if !reflect.DeepEqual(src, expected) { + t.Errorf("func Ints64 didnt sort correctly, %v !- %v", src, expected) + } +}