如何通过编程确定一个 Pod 是否处于 CrashLoopBackOff 状态?

huangapple go评论84阅读模式
英文:

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)
}

然而,这只会打印出状态和原因为空,但会打印出阶段信息。

英文:

Is there a way to determine programatically if a pod is in crashloopbackoff?
I tried the following

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)
}

However this just prints blank for state and reason, tough it prints the phase.

答案1

得分: 2

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

>它隐藏在ContainerStateWaiting.Reason中:

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

>需要注意的是,它只会间歇性地显示在那里,因为它是容器的间歇性状态;也许更加程序化的方法是检查restartCountError状态。

另请参阅此存储库

英文:

To clarify I am posting a community wiki answer.

>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

See also this repository.

huangapple
  • 本文由 发表于 2022年3月21日 01:37:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/71549110.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定