diff --git a/pkg/kubelet/kuberuntime/kuberuntime_container.go b/pkg/kubelet/kuberuntime/kuberuntime_container.go index 3075566105f..621f06934ab 100644 --- a/pkg/kubelet/kuberuntime/kuberuntime_container.go +++ b/pkg/kubelet/kuberuntime/kuberuntime_container.go @@ -1222,7 +1222,9 @@ func (m *kubeGenericRuntimeManager) computeInitContainerActions(ctx context.Cont restartOnFailure := restartOnFailure if utilfeature.DefaultFeatureGate.Enabled(features.ContainerRestartRules) { - restartOnFailure = kubecontainer.ShouldContainerBeRestarted(container, pod, podStatus) + if container.RestartPolicy != nil { + restartOnFailure = *container.RestartPolicy != v1.ContainerRestartPolicyNever + } } if !restartOnFailure { changes.KillPod = true diff --git a/pkg/kubelet/kuberuntime/kuberuntime_manager.go b/pkg/kubelet/kuberuntime/kuberuntime_manager.go index 26ba815fee2..37762fc0eac 100644 --- a/pkg/kubelet/kuberuntime/kuberuntime_manager.go +++ b/pkg/kubelet/kuberuntime/kuberuntime_manager.go @@ -1147,7 +1147,9 @@ func (m *kubeGenericRuntimeManager) computePodActions(ctx context.Context, pod * var reason containerKillReason restart := shouldRestartOnFailure(pod) if utilfeature.DefaultFeatureGate.Enabled(features.ContainerRestartRules) { - restart = kubecontainer.ShouldContainerBeRestarted(&container, pod, podStatus) + if container.RestartPolicy != nil { + restart = *container.RestartPolicy != v1.ContainerRestartPolicyNever + } } if _, _, changed := containerChanged(&container, containerStatus); changed { message = fmt.Sprintf("Container %s definition changed", container.Name) diff --git a/pkg/kubelet/kuberuntime/kuberuntime_manager_test.go b/pkg/kubelet/kuberuntime/kuberuntime_manager_test.go index 118590b7f9d..4dc328bb499 100644 --- a/pkg/kubelet/kuberuntime/kuberuntime_manager_test.go +++ b/pkg/kubelet/kuberuntime/kuberuntime_manager_test.go @@ -2110,6 +2110,11 @@ func TestComputePodActionsWithInitAndEphemeralContainers(t *testing.T) { } func TestComputePodActionsWithContainerRestartRules(t *testing.T) { + // Make sure existing test cases pass with feature enabled + featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ContainerRestartRules, true) + TestComputePodActions(t) + TestComputePodActionsWithInitContainers(t) + var ( containerRestartPolicyAlways = v1.ContainerRestartPolicyAlways containerRestartPolicyOnFailure = v1.ContainerRestartPolicyOnFailure @@ -2231,7 +2236,6 @@ func TestComputePodActionsWithContainerRestartRules(t *testing.T) { }, }, } { - featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ContainerRestartRules, true) pod, status := makeBasePodAndStatus() if test.mutatePodFn != nil { test.mutatePodFn(pod)