首页 > 解决方案 > 如何根据创建时间对 pod 进行排序

问题描述

我想在创建时间对 Kubernetes pod 进行排序。我尝试添加这样的逻辑。这里的“结果”是 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) 
})

使用上述逻辑,我得到错误
cannot use t2 (type "k8s.io/apimachinery/pkg/apis/meta/v1".Time) as type *"k8s.io/apimachinery/pkg/apis/meta/v1".Time在 t1.Before 的参数中

我尝试了 v1.Time 的打印类型“result[j].CreationTimestamp”。我不确定我还缺少什么。任何人都可以请指导我。

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

标签: gokubernetes-pod

解决方案


根据文档 Before*Time因此我认为您必须像这样引用第二个时间戳:

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

推荐阅读