Skip to content

Commit 2d8fbd6

Browse files
authored
Merge pull request kubernetes#94666 from soltysh/sort_nil
Handle nil elements when sorting, instead of panicking
2 parents 9983a52 + a2d96fc commit 2d8fbd6

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/get/sorter.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,13 @@ func isLess(i, j reflect.Value) (bool, error) {
206206
return true, nil
207207

208208
case reflect.Interface:
209+
if i.IsNil() && j.IsNil() {
210+
return false, nil
211+
} else if i.IsNil() {
212+
return true, nil
213+
} else if j.IsNil() {
214+
return false, nil
215+
}
209216
switch itype := i.Interface().(type) {
210217
case uint8:
211218
if jtype, ok := j.Interface().(uint8); ok {

staging/src/k8s.io/kubectl/pkg/cmd/get/sorter_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,32 @@ func TestSortingPrinter(t *testing.T) {
409409
field: "{.invalid}",
410410
expectedErr: "couldn't find any field with path \"{.invalid}\" in the list of objects",
411411
},
412+
{
413+
name: "empty fields",
414+
obj: &corev1.EventList{
415+
Items: []corev1.Event{
416+
{
417+
ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.Unix(300, 0)},
418+
LastTimestamp: metav1.Unix(300, 0),
419+
},
420+
{
421+
ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.Unix(200, 0)},
422+
},
423+
},
424+
},
425+
sort: &corev1.EventList{
426+
Items: []corev1.Event{
427+
{
428+
ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.Unix(200, 0)},
429+
},
430+
{
431+
ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.Unix(300, 0)},
432+
LastTimestamp: metav1.Unix(300, 0),
433+
},
434+
},
435+
},
436+
field: "{.lastTimestamp}",
437+
},
412438
}
413439
for _, tt := range tests {
414440
t.Run(tt.name+" table", func(t *testing.T) {

0 commit comments

Comments
 (0)