Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion internal/render/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,13 @@ func (p *Pod) Phase(dt *metav1.Time, spec *v1.PodSpec, st *v1.PodStatus) string

status, ok = p.containerPhase(st, status)
if ok && status == Completed {
status = Running
// Some containers completed while others still run: like kubectl,
// only call the pod Running when it is actually ready.
if hasPodReadyCondition(st.Conditions) {
status = Running
} else {
status = PhaseNotReady
}
}
if dt == nil {
return status
Expand Down
53 changes: 53 additions & 0 deletions internal/render/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,59 @@ func TestCheckPhase(t *testing.T) {
},
e: render.PhaseUnknown,
},
"completed-and-running-ready": {
pod: v1.Pod{
Status: v1.PodStatus{
Phase: v1.PodRunning,
Conditions: []v1.PodCondition{
{Type: v1.PodReady, Status: v1.ConditionTrue},
},
ContainerStatuses: []v1.ContainerStatus{
{
Name: "c1",
Ready: true,
State: v1.ContainerState{
Running: &v1.ContainerStateRunning{},
},
},
{
Name: "c2",
State: v1.ContainerState{
Terminated: &v1.ContainerStateTerminated{
Reason: "Completed",
},
},
},
},
},
},
e: render.PhaseRunning,
},
"completed-and-running-not-ready": {
pod: v1.Pod{
Status: v1.PodStatus{
Phase: v1.PodRunning,
ContainerStatuses: []v1.ContainerStatus{
{
Name: "c1",
Ready: true,
State: v1.ContainerState{
Running: &v1.ContainerStateRunning{},
},
},
{
Name: "c2",
State: v1.ContainerState{
Terminated: &v1.ContainerStateTerminated{
Reason: "Completed",
},
},
},
},
},
},
e: render.PhaseNotReady,
},
"restartable": {
pod: v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Expand Down
14 changes: 13 additions & 1 deletion internal/view/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,20 @@ func podIsRunning(f dao.Factory, fqn string) bool {
return false
}

return isPodRunning(po)
}

// isPodRunning reports whether a pod can be shelled into or attached to. A
// pod with some completed containers that is not ready yet renders as
// NoReady, but its remaining containers are still running.
func isPodRunning(po *v1.Pod) bool {
var re render.Pod
return re.Phase(po.DeletionTimestamp, &po.Spec, &po.Status) == render.Running
switch re.Phase(po.DeletionTimestamp, &po.Spec, &po.Status) {
case render.Running, render.PhaseNotReady:
return true
default:
return false
}
}

func getPodOS(f dao.Factory, fqn string) (string, error) {
Expand Down
61 changes: 61 additions & 0 deletions internal/view/pod_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

Expand Down Expand Up @@ -80,3 +81,63 @@ func TestComputeShellArgs(t *testing.T) {
})
}
}

func TestIsPodRunning(t *testing.T) {
running := v1.ContainerStatus{
Name: "c1",
Ready: true,
State: v1.ContainerState{Running: &v1.ContainerStateRunning{}},
}
completed := v1.ContainerStatus{
Name: "c2",
State: v1.ContainerState{
Terminated: &v1.ContainerStateTerminated{Reason: "Completed"},
},
}
readyCond := v1.PodCondition{Type: v1.PodReady, Status: v1.ConditionTrue}

uu := map[string]struct {
st v1.PodStatus
e bool
}{
"running": {
st: v1.PodStatus{
Phase: v1.PodRunning,
Conditions: []v1.PodCondition{readyCond},
ContainerStatuses: []v1.ContainerStatus{running},
},
e: true,
},
"completed-and-running-ready": {
st: v1.PodStatus{
Phase: v1.PodRunning,
Conditions: []v1.PodCondition{readyCond},
ContainerStatuses: []v1.ContainerStatus{running, completed},
},
e: true,
},
"completed-and-running-not-ready": {
st: v1.PodStatus{
Phase: v1.PodRunning,
ContainerStatuses: []v1.ContainerStatus{running, completed},
},
e: true,
},
"pending": {
st: v1.PodStatus{Phase: v1.PodPending},
},
"succeeded": {
st: v1.PodStatus{
Phase: v1.PodSucceeded,
ContainerStatuses: []v1.ContainerStatus{completed},
},
},
}

for k := range uu {
u := uu[k]
t.Run(k, func(t *testing.T) {
assert.Equal(t, u.e, isPodRunning(&v1.Pod{Status: u.st}))
})
}
}
Loading