Skip to content

Commit

Permalink
Handle nil elements when sorting, instead of panicking
Browse files Browse the repository at this point in the history
Kubernetes-commit: abe057710b3db2b9b13aaf315d53db04439ce389
  • Loading branch information
soltysh authored and k8s-publishing-bot committed Sep 9, 2020
1 parent 66913e7 commit c649655
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/cmd/get/sorter.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,13 @@ func isLess(i, j reflect.Value) (bool, error) {
return true, nil

case reflect.Interface:
if i.IsNil() && j.IsNil() {
return false, nil
} else if i.IsNil() {
return true, nil
} else if j.IsNil() {
return false, nil
}
switch itype := i.Interface().(type) {
case uint8:
if jtype, ok := j.Interface().(uint8); ok {
Expand Down
26 changes: 26 additions & 0 deletions pkg/cmd/get/sorter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,32 @@ func TestSortingPrinter(t *testing.T) {
field: "{.invalid}",
expectedErr: "couldn't find any field with path \"{.invalid}\" in the list of objects",
},
{
name: "empty fields",
obj: &corev1.EventList{
Items: []corev1.Event{
{
ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.Unix(300, 0)},
LastTimestamp: metav1.Unix(300, 0),
},
{
ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.Unix(200, 0)},
},
},
},
sort: &corev1.EventList{
Items: []corev1.Event{
{
ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.Unix(200, 0)},
},
{
ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.Unix(300, 0)},
LastTimestamp: metav1.Unix(300, 0),
},
},
},
field: "{.lastTimestamp}",
},
}
for _, tt := range tests {
t.Run(tt.name+" table", func(t *testing.T) {
Expand Down

0 comments on commit c649655

Please sign in to comment.