Skip to content

Commit b73f84c

Browse files
authored
Merge pull request kubernetes#126543 from gjkim42/restart-failed-to-start-container
Restart the init container to not be stuck in created state
2 parents a8d4eb6 + 4363a84 commit b73f84c

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

pkg/kubelet/kuberuntime/kuberuntime_container.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,12 @@ func (m *kubeGenericRuntimeManager) computeInitContainerActions(pod *v1.Pod, pod
10841084

10851085
switch status.State {
10861086
case kubecontainer.ContainerStateCreated:
1087-
// nothing to do but wait for it to start
1087+
// The main sync loop should have created and started the container
1088+
// in one step. If the init container is in the 'created' state,
1089+
// it is likely that the container runtime failed to start it. To
1090+
// prevent the container from getting stuck in the 'created' state,
1091+
// restart it.
1092+
changes.InitContainersToStart = append(changes.InitContainersToStart, i)
10881093

10891094
case kubecontainer.ContainerStateRunning:
10901095
if !types.IsRestartableInitContainer(container) {

pkg/kubelet/kuberuntime/kuberuntime_manager_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,17 @@ func TestComputePodActions(t *testing.T) {
11711171
ContainersToStart: []int{1},
11721172
},
11731173
},
1174+
"Restart the container if the container is in created state": {
1175+
mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyNever },
1176+
mutateStatusFn: func(status *kubecontainer.PodStatus) {
1177+
status.ContainerStatuses[1].State = kubecontainer.ContainerStateCreated
1178+
},
1179+
actions: podActions{
1180+
SandboxID: baseStatus.SandboxStatuses[0].Id,
1181+
ContainersToKill: map[kubecontainer.ContainerID]containerToKillInfo{},
1182+
ContainersToStart: []int{1},
1183+
},
1184+
},
11741185
} {
11751186
pod, status := makeBasePodAndStatus()
11761187
if test.mutatePodFn != nil {
@@ -1545,12 +1556,17 @@ func TestComputePodActionsWithRestartableInitContainers(t *testing.T) {
15451556
ContainersToKill: getKillMapWithInitContainers(basePod, baseStatus, []int{}),
15461557
},
15471558
},
1548-
"initialization in progress; do nothing": {
1559+
"an init container is stuck in the created state; restart it": {
15491560
mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyAlways },
15501561
mutateStatusFn: func(pod *v1.Pod, status *kubecontainer.PodStatus) {
15511562
status.ContainerStatuses[2].State = kubecontainer.ContainerStateCreated
15521563
},
1553-
actions: noAction,
1564+
actions: podActions{
1565+
SandboxID: baseStatus.SandboxStatuses[0].Id,
1566+
InitContainersToStart: []int{2},
1567+
ContainersToStart: []int{},
1568+
ContainersToKill: getKillMapWithInitContainers(basePod, baseStatus, []int{}),
1569+
},
15541570
},
15551571
"restartable init container has started; start the next": {
15561572
mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyAlways },

0 commit comments

Comments
 (0)