Skip to content

Commit 924b324

Browse files
authored
Merge pull request kubernetes#133243 from yuanwang04/restart-rules
ContainerRestartRules feature gate should work with probes
2 parents a2bf45b + fd206a0 commit 924b324

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

pkg/kubelet/kuberuntime/kuberuntime_container.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,12 @@ func (m *kubeGenericRuntimeManager) computeInitContainerActions(ctx context.Cont
12221222

12231223
restartOnFailure := restartOnFailure
12241224
if utilfeature.DefaultFeatureGate.Enabled(features.ContainerRestartRules) {
1225-
restartOnFailure = kubecontainer.ShouldContainerBeRestarted(container, pod, podStatus)
1225+
// Only container-level restart policy is used. The container-level restart
1226+
// rules are not evaluated because the container might not have exited, so
1227+
// there is no exit code on which the rules can be used.
1228+
if container.RestartPolicy != nil {
1229+
restartOnFailure = *container.RestartPolicy != v1.ContainerRestartPolicyNever
1230+
}
12261231
}
12271232
if !restartOnFailure {
12281233
changes.KillPod = true

pkg/kubelet/kuberuntime/kuberuntime_manager.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -589,16 +589,6 @@ func containerChanged(container *v1.Container, containerStatus *kubecontainer.St
589589
}
590590

591591
func shouldRestartOnFailure(pod *v1.Pod) bool {
592-
// With feature ContainerRestartRules enabled, the pod should be restarted
593-
// on failure if any of its containers have container-level restart policy
594-
// that is restartable.
595-
if utilfeature.DefaultFeatureGate.Enabled(features.ContainerRestartRules) {
596-
for _, c := range pod.Spec.Containers {
597-
if podutil.IsContainerRestartable(pod.Spec, c) {
598-
return true
599-
}
600-
}
601-
}
602592
return pod.Spec.RestartPolicy != v1.RestartPolicyNever
603593
}
604594

@@ -1147,7 +1137,11 @@ func (m *kubeGenericRuntimeManager) computePodActions(ctx context.Context, pod *
11471137
var reason containerKillReason
11481138
restart := shouldRestartOnFailure(pod)
11491139
if utilfeature.DefaultFeatureGate.Enabled(features.ContainerRestartRules) {
1150-
restart = kubecontainer.ShouldContainerBeRestarted(&container, pod, podStatus)
1140+
// For probe failures, use container-level restart policy only. Container-level restart
1141+
// rules are not evaluated because the container is still running.
1142+
if container.RestartPolicy != nil {
1143+
restart = *container.RestartPolicy != v1.ContainerRestartPolicyNever
1144+
}
11511145
}
11521146
if _, _, changed := containerChanged(&container, containerStatus); changed {
11531147
message = fmt.Sprintf("Container %s definition changed", container.Name)

pkg/kubelet/kuberuntime/kuberuntime_manager_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2110,6 +2110,11 @@ func TestComputePodActionsWithInitAndEphemeralContainers(t *testing.T) {
21102110
}
21112111

21122112
func TestComputePodActionsWithContainerRestartRules(t *testing.T) {
2113+
// Make sure existing test cases pass with feature enabled
2114+
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ContainerRestartRules, true)
2115+
TestComputePodActions(t)
2116+
TestComputePodActionsWithInitContainers(t)
2117+
21132118
var (
21142119
containerRestartPolicyAlways = v1.ContainerRestartPolicyAlways
21152120
containerRestartPolicyOnFailure = v1.ContainerRestartPolicyOnFailure
@@ -2231,7 +2236,6 @@ func TestComputePodActionsWithContainerRestartRules(t *testing.T) {
22312236
},
22322237
},
22332238
} {
2234-
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ContainerRestartRules, true)
22352239
pod, status := makeBasePodAndStatus()
22362240
if test.mutatePodFn != nil {
22372241
test.mutatePodFn(pod)

test/e2e/node/pods.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ var _ = SIGDescribe("Pods Extended (pod generation)", feature.PodObservedGenerat
718718
})
719719
})
720720

721-
var _ = SIGDescribe("Pod Extended (container restart policy)", feature.ContainerRestartRules, framework.WithFeatureGate(features.ContainerRestartRules), func() {
721+
var _ = SIGDescribe("Pod Extended (container restart policy)", framework.WithFeatureGate(features.ContainerRestartRules), func() {
722722
f := framework.NewDefaultFramework("pods")
723723
f.NamespacePodSecurityLevel = admissionapi.LevelBaseline
724724

0 commit comments

Comments
 (0)