kuberuntime_manager: fix container success check.

When evaluating whether a container ran to completion,
we only check whether the CRI container status `ExitCode` is 0.

But, the ExitCode is only meaningful if the container has actually
run and exited.

There are other states, eg: `Created` where the container runtime never
set an ExitCode - we shouldn't read it in that case.
This commit is contained in:
Abhijit Hoskeri 2024-07-28 17:18:41 -07:00
parent a2106b5f73
commit ae5268431a
2 changed files with 19 additions and 2 deletions

View File

@ -534,10 +534,11 @@ func shouldRestartOnFailure(pod *v1.Pod) bool {
func containerSucceeded(c *v1.Container, podStatus *kubecontainer.PodStatus) bool {
cStatus := podStatus.FindContainerStatusByName(c.Name)
if cStatus == nil || cStatus.State == kubecontainer.ContainerStateRunning {
if cStatus == nil {
return false
}
return cStatus.ExitCode == 0
// Container has exited, with an exit code of 0.
return cStatus.State == kubecontainer.ContainerStateExited && cStatus.ExitCode == 0
}
func isInPlacePodVerticalScalingAllowed(pod *v1.Pod) bool {

View File

@ -994,6 +994,22 @@ func TestComputePodActions(t *testing.T) {
ContainersToKill: getKillMap(basePod, baseStatus, []int{}),
},
},
"restart created but not started containers if RestartPolicy == OnFailure": {
mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyOnFailure },
mutateStatusFn: func(status *kubecontainer.PodStatus) {
// The first container completed, don't restart it.
status.ContainerStatuses[0].State = kubecontainer.ContainerStateExited
status.ContainerStatuses[0].ExitCode = 0
// The second container was created, but never started.
status.ContainerStatuses[1].State = kubecontainer.ContainerStateCreated
},
actions: podActions{
SandboxID: baseStatus.SandboxStatuses[0].Id,
ContainersToStart: []int{1},
ContainersToKill: getKillMap(basePod, baseStatus, []int{}),
},
},
"don't restart containers if RestartPolicy == Never": {
mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyNever },
mutateStatusFn: func(status *kubecontainer.PodStatus) {