mirror of https://github.com/k3s-io/k3s
Merge pull request #25600 from janetkuo/sort-by-timestamp
Automatic merge from submit-queue Support sort-by timestamp in kubectl get ## Pull Request Guidelines 1. Please read our [contributor guidelines](https://github.com/kubernetes/kubernetes/blob/master/CONTRIBUTING.md). 1. See our [developer guide](https://github.com/kubernetes/kubernetes/blob/master/docs/devel/development.md). 1. Follow the instructions for [labeling and writing a release note for this PR](https://github.com/kubernetes/kubernetes/blob/master/docs/devel/pull-requests.md#release-notes) in the block below. ```release-note ``` **Before:** ```console $ kubectl get svc --sort-by='{.metadata.creationTimestamp}' proto: no encoder for TypeMeta unversioned.TypeMeta [GetProperties] proto: tag has too few fields: "-" proto: no coders for struct *reflect.rtype proto: no encoder for sec int64 [GetProperties] proto: no encoder for nsec int32 [GetProperties] proto: no encoder for loc *time.Location [GetProperties] proto: no encoder for Time time.Time [GetProperties] proto: no coders for intstr.Type proto: no encoder for Type intstr.Type [GetProperties] F0513 16:46:49.499894 29562 sorting_printer.go:182] Field {.metadata.creationTimestamp} in TypeMeta:<kind:"Service" apiVersion:"v1" > metadata:<name:"kubernetes" generateName:"" namespace:"default" selfLink:"/api/v1/namespaces/default/services/kubernetes" uid:"b88b4739-1964-11e6-9ac3-64510658e388" resourceVersion:"8" generation:0 creationTimestamp:<2016-05-13T16:45:06-07:00> labels:<key:"component" value:"apiserver" > labels:<key:"provider" value:"kubernetes" > > spec:<ports:<name:"https" protocol:"TCP" port:443 targetPort:<type:0 intVal:443 strVal:"" > nodePort:0 > clusterIP:"10.0.0.1" type:"ClusterIP" sessionAffinity:"ClientIP" loadBalancerIP:"" > status:<loadBalancer:<> > is an unsortable type: struct, err: unsortable type: struct ``` **After:** ```console $ kubectl get svc --sort-by='{.metadata.creationTimestamp}' NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes 10.0.0.1 <none> 443/TCP 48s frontend 10.0.0.108 <none> 80/TCP 10s ``` @kubernetes/kubectl [![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/.github/PULL_REQUEST_TEMPLATE.md?pixel)]()pull/6/head
commit
88766e8a68
|
@ -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++ {
|
||||
|
@ -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]
|
||||
|
|
Loading…
Reference in New Issue