我如何以编程方式确定 pod 是否处于 crashloopbackoff

How do i programatically determine whether a pod is in crashloopbackoff

有没有办法以编程方式确定 pod 是否处于 crashloopbackoff 状态? 我尝试了以下

pods,err :=  client.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil { 
    return err
}
for _, item := range pods.Items {
   log.Printf("found pod %v with state %v  reason %v and phase %v that started at %v",
                item.Name, item.Status.Message, item.Status.Reason, item.Status.Phase, item.CreationTimestamp.Time)
}

然而,这只是出于状态和原因打印空白,虽然它打印了阶段。

为了澄清,我发布了一个社区维基答案。

It's hiding in ContainerStateWaiting.Reason:

kubectl get po -o jsonpath='{.items[*].status.containerStatuses[*].state.waiting.reason}'

although be aware that it only intermittently shows up there, since it is an intermittent state of the container; perhaps a more programmatic approach is to examine the restartCount and the Error state

另见 this repository