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
pull/6/head
Dmitry Shulyak 2016-05-18 12:04:29 +03:00
parent e43ec4c445
commit 4a60d3ce60
3 changed files with 28 additions and 16 deletions

View File

@ -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 = "<none>"
}
fmt.Fprintf(out, "%s\t%s\n", r, changeCause)
fmt.Fprintf(out, "%d\t%s\n", r, changeCause)
}
return errors.NewAggregate(errs)
return nil
})
}

View File

@ -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)) }

View File

@ -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)
}
}