如何根据创建时间对 pods 进行排序

How to sort pods based on creation time

我想根据创建时间对 Kubernetes pod 进行排序。我尝试添加这样的逻辑。这里 'result' 是 Pod 数组(类型为 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) 
})

根据以上逻辑,我得到错误
不能使用 t2(类型“k8s.io/apimachinery/pkg/apis/meta/v1”.Time)作为类型 *“k8s.io/apimachinery/pkg/apis/meta/v1”.Time 在 t1.Before

的参数中

我尝试打印类型为 v1.Time 的“result[j].CreationTimestamp”。我不确定我还缺少什么。谁能指导一下。

解决方法是创建另一个对象并将其字段设置为 Pod 的名称和创建时间并对其进行排序,但这会影响性能。

根据 docs Before 需要一个 *Time 因此我认为你必须像这样引用第二个时间戳:

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