mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-11 14:11:14 +00:00
add pod util to verify pod is terminal
pods on phase succeeded or failed are guaranteed to have all containers stopped and to not ever regress
This commit is contained in:
@@ -301,6 +301,16 @@ func IsPodReady(pod *v1.Pod) bool {
|
||||
return IsPodReadyConditionTrue(pod.Status)
|
||||
}
|
||||
|
||||
// IsPodTerminal returns true if a pod is terminal, all containers are stopped and cannot ever regress.
|
||||
func IsPodTerminal(pod *v1.Pod) bool {
|
||||
return IsPodPhaseTerminal(pod.Status.Phase)
|
||||
}
|
||||
|
||||
// IsPhaseTerminal returns true if the pod's phase is terminal.
|
||||
func IsPodPhaseTerminal(phase v1.PodPhase) bool {
|
||||
return phase == v1.PodFailed || phase == v1.PodSucceeded
|
||||
}
|
||||
|
||||
// IsPodReadyConditionTrue returns true if a pod is ready; false otherwise.
|
||||
func IsPodReadyConditionTrue(status v1.PodStatus) bool {
|
||||
condition := GetPodReadyCondition(status)
|
||||
|
@@ -749,6 +749,48 @@ func TestIsPodAvailable(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsPodTerminal(t *testing.T) {
|
||||
now := metav1.Now()
|
||||
|
||||
tests := []struct {
|
||||
podPhase v1.PodPhase
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
podPhase: v1.PodFailed,
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
podPhase: v1.PodSucceeded,
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
podPhase: v1.PodUnknown,
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
podPhase: v1.PodPending,
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
podPhase: v1.PodRunning,
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
pod := newPod(now, true, 0)
|
||||
pod.Status.Phase = test.podPhase
|
||||
isTerminal := IsPodTerminal(pod)
|
||||
if isTerminal != test.expected {
|
||||
t.Errorf("[tc #%d] expected terminal pod: %t, got: %t", i, test.expected, isTerminal)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetContainerStatus(t *testing.T) {
|
||||
type ExpectedStruct struct {
|
||||
status v1.ContainerStatus
|
||||
|
Reference in New Issue
Block a user