Handle nil elements when sorting, instead of panicking

Kubernetes-commit: abe057710b3db2b9b13aaf315d53db04439ce389
This commit is contained in:
Maciej Szulik 2020-09-09 22:39:46 +02:00 committed by Kubernetes Publisher
parent 66913e7730
commit c649655dbe
2 changed files with 33 additions and 0 deletions

View File

@ -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 {

View File

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