Skip to content

Commit be89a61

Browse files
committed
Avoid SidecarContainers code path for non-sidecar pods
This fixes a regression in the SidecarContainers feature by minimizing the impact of the new code path. Use the old code path for pods without restartable init containers, and apply the new code path only to pods with restartable init containers.
1 parent 3b86da0 commit be89a61

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed

pkg/kubelet/kuberuntime/kuberuntime_container.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ func (m *kubeGenericRuntimeManager) killContainersWithSyncResult(ctx context.Con
832832
wg.Add(len(runningPod.Containers))
833833
var termOrdering *terminationOrdering
834834
// we only care about container termination ordering if the sidecars feature is enabled
835-
if utilfeature.DefaultFeatureGate.Enabled(features.SidecarContainers) {
835+
if utilfeature.DefaultFeatureGate.Enabled(features.SidecarContainers) && types.HasRestartableInitContainer(pod) {
836836
var runningContainerNames []string
837837
for _, container := range runningPod.Containers {
838838
runningContainerNames = append(runningContainerNames, container.Name)

pkg/kubelet/kuberuntime/kuberuntime_manager.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,8 @@ func (m *kubeGenericRuntimeManager) computePodActions(ctx context.Context, pod *
833833
ContainersToKill: make(map[kubecontainer.ContainerID]containerToKillInfo),
834834
}
835835

836+
handleRestartableInitContainers := utilfeature.DefaultFeatureGate.Enabled(features.SidecarContainers) && types.HasRestartableInitContainer(pod)
837+
836838
// If we need to (re-)create the pod sandbox, everything will need to be
837839
// killed and recreated, and init containers should be purged.
838840
if createPodSandbox {
@@ -862,7 +864,7 @@ func (m *kubeGenericRuntimeManager) computePodActions(ctx context.Context, pod *
862864
// is done and there is no container to start.
863865
if len(containersToStart) == 0 {
864866
hasInitialized := false
865-
if !utilfeature.DefaultFeatureGate.Enabled(features.SidecarContainers) {
867+
if !handleRestartableInitContainers {
866868
_, _, hasInitialized = findNextInitContainerToRun(pod, podStatus)
867869
} else {
868870
// If there is any regular container, it means all init containers have
@@ -880,7 +882,7 @@ func (m *kubeGenericRuntimeManager) computePodActions(ctx context.Context, pod *
880882
// state.
881883
if len(pod.Spec.InitContainers) != 0 {
882884
// Pod has init containers, return the first one.
883-
if !utilfeature.DefaultFeatureGate.Enabled(features.SidecarContainers) {
885+
if !handleRestartableInitContainers {
884886
changes.NextInitContainerToStart = &pod.Spec.InitContainers[0]
885887
} else {
886888
changes.InitContainersToStart = []int{0}
@@ -903,7 +905,7 @@ func (m *kubeGenericRuntimeManager) computePodActions(ctx context.Context, pod *
903905
}
904906

905907
// Check initialization progress.
906-
if !utilfeature.DefaultFeatureGate.Enabled(features.SidecarContainers) {
908+
if !handleRestartableInitContainers {
907909
initLastStatus, next, done := findNextInitContainerToRun(pod, podStatus)
908910
if !done {
909911
if next != nil {
@@ -1027,7 +1029,7 @@ func (m *kubeGenericRuntimeManager) computePodActions(ctx context.Context, pod *
10271029

10281030
if keepCount == 0 && len(changes.ContainersToStart) == 0 {
10291031
changes.KillPod = true
1030-
if utilfeature.DefaultFeatureGate.Enabled(features.SidecarContainers) {
1032+
if handleRestartableInitContainers {
10311033
// To prevent the restartable init containers to keep pod alive, we should
10321034
// not restart them.
10331035
changes.InitContainersToStart = nil
@@ -1285,7 +1287,7 @@ func (m *kubeGenericRuntimeManager) SyncPod(ctx context.Context, pod *v1.Pod, po
12851287
start(ctx, "ephemeral container", metrics.EphemeralContainer, ephemeralContainerStartSpec(&pod.Spec.EphemeralContainers[idx]))
12861288
}
12871289

1288-
if !utilfeature.DefaultFeatureGate.Enabled(features.SidecarContainers) {
1290+
if !types.HasRestartableInitContainer(pod) {
12891291
// Step 6: start the init container.
12901292
if container := podContainerChanges.NextInitContainerToStart; container != nil {
12911293
// Start the next init container.

pkg/kubelet/kuberuntime/kuberuntime_manager_test.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import (
5151
containertest "k8s.io/kubernetes/pkg/kubelet/container/testing"
5252
imagetypes "k8s.io/kubernetes/pkg/kubelet/images"
5353
proberesults "k8s.io/kubernetes/pkg/kubelet/prober/results"
54+
kubelettypes "k8s.io/kubernetes/pkg/kubelet/types"
5455
)
5556

5657
var (
@@ -1428,6 +1429,20 @@ func testComputePodActionsWithInitContainers(t *testing.T, sidecarContainersEnab
14281429
ContainersToKill: getKillMapWithInitContainers(basePod, baseStatus, []int{}),
14291430
},
14301431
},
1432+
"an init container is in the created state due to an unknown error when starting container; restart it": {
1433+
mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyAlways },
1434+
mutateStatusFn: func(status *kubecontainer.PodStatus) {
1435+
status.ContainerStatuses[2].State = kubecontainer.ContainerStateCreated
1436+
},
1437+
actions: podActions{
1438+
KillPod: false,
1439+
SandboxID: baseStatus.SandboxStatuses[0].Id,
1440+
NextInitContainerToStart: &basePod.Spec.InitContainers[2],
1441+
InitContainersToStart: []int{2},
1442+
ContainersToStart: []int{},
1443+
ContainersToKill: getKillMapWithInitContainers(basePod, baseStatus, []int{}),
1444+
},
1445+
},
14311446
} {
14321447
pod, status := makeBasePodAndStatusWithInitContainers()
14331448
if test.mutatePodFn != nil {
@@ -1438,12 +1453,15 @@ func testComputePodActionsWithInitContainers(t *testing.T, sidecarContainersEnab
14381453
}
14391454
ctx := context.Background()
14401455
actions := m.computePodActions(ctx, pod, status)
1441-
if !sidecarContainersEnabled {
1442-
// If sidecar containers are disabled, we should not see any
1456+
handleRestartableInitContainers := sidecarContainersEnabled && kubelettypes.HasRestartableInitContainer(pod)
1457+
if !handleRestartableInitContainers {
1458+
// If sidecar containers are disabled or the pod does not have any
1459+
// restartable init container, we should not see any
14431460
// InitContainersToStart in the actions.
14441461
test.actions.InitContainersToStart = nil
14451462
} else {
1446-
// If sidecar containers are enabled, we should not see any
1463+
// If sidecar containers are enabled and the pod has any
1464+
// restartable init container, we should not see any
14471465
// NextInitContainerToStart in the actions.
14481466
test.actions.NextInitContainerToStart = nil
14491467
}
@@ -2041,12 +2059,15 @@ func testComputePodActionsWithInitAndEphemeralContainers(t *testing.T, sidecarCo
20412059
}
20422060
ctx := context.Background()
20432061
actions := m.computePodActions(ctx, pod, status)
2044-
if !sidecarContainersEnabled {
2045-
// If sidecar containers are disabled, we should not see any
2062+
handleRestartableInitContainers := sidecarContainersEnabled && kubelettypes.HasRestartableInitContainer(pod)
2063+
if !handleRestartableInitContainers {
2064+
// If sidecar containers are disabled or the pod does not have any
2065+
// restartable init container, we should not see any
20462066
// InitContainersToStart in the actions.
20472067
test.actions.InitContainersToStart = nil
20482068
} else {
2049-
// If sidecar containers are enabled, we should not see any
2069+
// If sidecar containers are enabled and the pod has any
2070+
// restartable init container, we should not see any
20502071
// NextInitContainerToStart in the actions.
20512072
test.actions.NextInitContainerToStart = nil
20522073
}

pkg/kubelet/types/pod_update.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,14 @@ func IsRestartableInitContainer(initContainer *v1.Container) bool {
202202

203203
return *initContainer.RestartPolicy == v1.ContainerRestartPolicyAlways
204204
}
205+
206+
// HasRestartableInitContainer returns true if the pod has any restartable init
207+
// container
208+
func HasRestartableInitContainer(pod *v1.Pod) bool {
209+
for _, container := range pod.Spec.InitContainers {
210+
if IsRestartableInitContainer(&container) {
211+
return true
212+
}
213+
}
214+
return false
215+
}

0 commit comments

Comments
 (0)