From 6b94e872c63eeea2ed4fdc510c008e4ff9713953 Mon Sep 17 00:00:00 2001 From: ceshihao Date: Mon, 16 Apr 2018 19:28:00 +0800 Subject: [PATCH] Make pod status to "Running" if there is at least one container still reporting as "Running" status --- pkg/printers/internalversion/printers.go | 7 +++++++ pkg/printers/internalversion/printers_test.go | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/pkg/printers/internalversion/printers.go b/pkg/printers/internalversion/printers.go index c2834a8cd69..78341d8b1c3 100644 --- a/pkg/printers/internalversion/printers.go +++ b/pkg/printers/internalversion/printers.go @@ -574,6 +574,7 @@ func printPod(pod *api.Pod, options printers.PrintOptions) ([]metav1beta1.TableR } if !initializing { restarts = 0 + hasRunning := false for i := len(pod.Status.ContainerStatuses) - 1; i >= 0; i-- { container := pod.Status.ContainerStatuses[i] @@ -589,9 +590,15 @@ func printPod(pod *api.Pod, options printers.PrintOptions) ([]metav1beta1.TableR reason = fmt.Sprintf("ExitCode:%d", container.State.Terminated.ExitCode) } } else if container.Ready && container.State.Running != nil { + hasRunning = true readyContainers++ } } + + // change pod status back to "Running" if there is at least one container still reporting as "Running" status + if reason == "Completed" && hasRunning { + reason = "Running" + } } if pod.DeletionTimestamp != nil && pod.Status.Reason == node.NodeUnreachablePodReason { diff --git a/pkg/printers/internalversion/printers_test.go b/pkg/printers/internalversion/printers_test.go index 9fda8a66cb9..1e5b26f3654 100644 --- a/pkg/printers/internalversion/printers_test.go +++ b/pkg/printers/internalversion/printers_test.go @@ -1700,6 +1700,22 @@ func TestPrintPod(t *testing.T) { }, []metav1beta1.TableRow{{Cells: []interface{}{"test5", "1/2", "podReason", 6, ""}}}, }, + { + // Test pod has 2 containers, one is running and the other is completed. + api.Pod{ + ObjectMeta: metav1.ObjectMeta{Name: "test6"}, + Spec: api.PodSpec{Containers: make([]api.Container, 2)}, + Status: api.PodStatus{ + Phase: "Running", + Reason: "", + ContainerStatuses: []api.ContainerStatus{ + {Ready: true, RestartCount: 3, State: api.ContainerState{Terminated: &api.ContainerStateTerminated{Reason: "Completed", ExitCode: 0}}}, + {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}}, + }, + }, + }, + []metav1beta1.TableRow{{Cells: []interface{}{"test6", "1/2", "Running", 6, ""}}}, + }, } for i, test := range tests {