Rajjy
Rajjy

Reputation: 186

How to sort pods based on creation time

I want to sort Kubernetes pod's on creation time. I tried adding logic like this. Here 'result' is array of Pod ( of type k8s.io/api/core/v1/Pod)

 sort.Slice(result, func(i, j int) bool { 
    fmt.Printf("%T \n", result[j].CreationTimestamp)
    fmt.Printf("time  %t" , result[i].CreationTimestamp.Before(result[j].CreationTimestamp))
    return result[i].CreationTimestamp.Before(result[j].CreationTimestamp) 
})

With above logic , i get error
cannot use t2 (type "k8s.io/apimachinery/pkg/apis/meta/v1".Time) as type *"k8s.io/apimachinery/pkg/apis/meta/v1".Time in argument to t1.Before

I tried printing type of "result[j].CreationTimestamp" which comes to v1.Time. I am not sure what else I am missing. Can anyone please guide me.

Workaround would be to create a another object and set its fields to name and creationTime of Pod and sort it but that would hit performance.

Upvotes: 0

Views: 1110

Answers (1)

Bäz Kurfer
Bäz Kurfer

Reputation: 36

According to the docs Before takes a *Time therefore I think you have to reference the second timestamp like so:

return result[i].CreationTimestamp.Before(&result[j].CreationTimestamp)

Upvotes: 2

Related Questions