Skip to content

Commit 4363a84

Browse files
committed
Restart the init container to not be stuck in created state
The main sync loop should have created and started the container in one step. If the init container is in the 'created' state, it's likely that the container runtime failed to start it. To prevent the container from getting stuck in the 'created' state, restart it.
1 parent cb7b4ea commit 4363a84

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
@@ -1170,6 +1170,17 @@ func TestComputePodActions(t *testing.T) {
11701170
ContainersToStart: []int{1},
11711171
},
11721172
},
1173+
"Restart the container if the container is in created state": {
1174+
mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyNever },
1175+
mutateStatusFn: func(status *kubecontainer.PodStatus) {
1176+
status.ContainerStatuses[1].State = kubecontainer.ContainerStateCreated
1177+
},
1178+
actions: podActions{
1179+
SandboxID: baseStatus.SandboxStatuses[0].Id,
1180+
ContainersToKill: map[kubecontainer.ContainerID]containerToKillInfo{},
1181+
ContainersToStart: []int{1},
1182+
},
1183+
},
11731184
} {
11741185
pod, status := makeBasePodAndStatus()
11751186
if test.mutatePodFn != nil {
@@ -1527,12 +1538,17 @@ func TestComputePodActionsWithRestartableInitContainers(t *testing.T) {
15271538
ContainersToKill: getKillMapWithInitContainers(basePod, baseStatus, []int{}),
15281539
},
15291540
},
1530-
"initialization in progress; do nothing": {
1541+
"an init container is stuck in the created state; restart it": {
15311542
mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyAlways },
15321543
mutateStatusFn: func(pod *v1.Pod, status *kubecontainer.PodStatus) {
15331544
status.ContainerStatuses[2].State = kubecontainer.ContainerStateCreated
15341545
},
1535-
actions: noAction,
1546+
actions: podActions{
1547+
SandboxID: baseStatus.SandboxStatuses[0].Id,
1548+
InitContainersToStart: []int{2},
1549+
ContainersToStart: []int{},
1550+
ContainersToKill: getKillMapWithInitContainers(basePod, baseStatus, []int{}),
1551+
},
15361552
},
15371553
"restartable init container has started; start the next": {
15381554
mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyAlways },

0 commit comments

Comments
 (0)