go - 如何根据创建时间对 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 并对其进行排序,但这会影响性能。
解决方案
根据文档 Before
,*Time
因此我认为您必须像这样引用第二个时间戳:
return result[i].CreationTimestamp.Before(&result[j].CreationTimestamp)
推荐阅读
- javascript - Javascript img onclick 总是点击所有的imgs,无法捕捉到点击的正确img
- python - 评估使用 Keras 创建的已保存 TensorFlow 图的结果不一致
- javascript - 如何从具有数组值的表单中获取值 ajax
- azure-devops - 在 VSTS 发布管道期间替换存储在不同工件中的多个 appsettings 文件中的值
- javascript - 如何动态地将数组值作为对象道具传递?
- android - 如何在区域设置更改期间在 Android Oreo 中进行后台 API 调用?
- xamarin.forms - 是否有 Xamarin Forms 3.6 Datepicker 的占位符与材料视觉?
- c - 带有无源蜂鸣器的 Arduino 超声波距离传感器可实现不同的音调
- python - cv2.error:(-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
- kubernetes - Kubernetes DNS 间歇性失败,kube-dns 服务和 CoreDNS pod 似乎正常