Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort revisions in rollout history as integers #25802

Merged
merged 1 commit into from
May 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 8 additions & 16 deletions pkg/kubectl/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ package kubectl
import (
"fmt"
"io"
"sort"
"strconv"

"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/meta"
Expand All @@ -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 (
Expand Down Expand Up @@ -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
})
}

Expand Down
11 changes: 11 additions & 0 deletions pkg/util/slice/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)) }
9 changes: 9 additions & 0 deletions pkg/util/slice/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}