Skip to content

Commit

Permalink
Merge pull request #70740 from mfpierre/fix-kubectl-get-sort-out-of-r…
Browse files Browse the repository at this point in the history
…ange

Fix index out of range error when sorting kubectl get
  • Loading branch information
k8s-ci-robot committed Nov 24, 2018
2 parents 610f48f + bb9ea69 commit 1e50c57
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pkg/kubectl/cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,14 @@ type RuntimeSorter struct {
}

func (r *RuntimeSorter) Sort() error {
if len(r.objects) <= 1 {
// a list is only considered "sorted" if there are 0 or 1 items in it
// AND (if 1 item) the item is not a Table object
// a list is only considered "sorted" if there are 0 or 1 items in it
// AND (if 1 item) the item is not a Table object
if len(r.objects) == 0 {
return nil
}
if len(r.objects) == 1 {
_, isTable := r.objects[0].(*metav1beta1.Table)
if len(r.objects) == 0 || !isTable {
if !isTable {
return nil
}
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/kubectl/cmd/get/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,15 @@ func TestRuntimeSorter(t *testing.T) {
expect string
expectError string
}{
{
name: "ensure sorter works with an empty object list",
field: "metadata.name",
objs: []runtime.Object{},
op: func(sorter *RuntimeSorter, objs []runtime.Object, out io.Writer) error {
return nil
},
expect: "",
},
{
name: "ensure sorter returns original position",
field: "metadata.name",
Expand Down

0 comments on commit 1e50c57

Please sign in to comment.