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

Support sort-by timestamp in kubectl get #25600

Merged
merged 1 commit into from
May 23, 2016
Merged
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
30 changes: 4 additions & 26 deletions pkg/kubectl/sorting_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ func isLess(i, j reflect.Value) (bool, error) {
case reflect.Ptr:
return isLess(i.Elem(), j.Elem())
case reflect.Struct:
// special case handling
lessFuncList := []structLessFunc{timeLess}
if ok, less := structLess(i, j, lessFuncList); ok {
return less, nil
// sort unversioned.Time
in := i.Interface()
if t, ok := in.(unversioned.Time); ok {
return t.Before(j.Interface().(unversioned.Time)), nil
}
// fallback to the fields comparision
for idx := 0; idx < i.NumField(); idx++ {
Expand All @@ -183,28 +183,6 @@ func isLess(i, j reflect.Value) (bool, error) {
}
}

// structLessFunc checks whether i and j could be compared(the first return value),
// and if it could, return whether i is less than j(the second return value)
type structLessFunc func(i, j reflect.Value) (bool, bool)

// structLess returns whether i and j could be compared with the given function list
func structLess(i, j reflect.Value, lessFuncList []structLessFunc) (bool, bool) {
for _, lessFunc := range lessFuncList {
if ok, less := lessFunc(i, j); ok {
return ok, less
}
}
return false, false
}

// compare two unversioned.Time values.
func timeLess(i, j reflect.Value) (bool, bool) {
if i.Type() != reflect.TypeOf(unversioned.Unix(0, 0)) {
return false, false
}
return true, i.MethodByName("Before").Call([]reflect.Value{j})[0].Bool()
}

func (r *RuntimeSort) Less(i, j int) bool {
iObj := r.objs[i]
jObj := r.objs[j]
Expand Down