|
| 1 | +//go:build linux |
| 2 | +// +build linux |
| 3 | + |
| 4 | +/* |
| 5 | +Copyright 2024 The Kubernetes Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +*/ |
| 19 | + |
| 20 | +package e2enode |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "fmt" |
| 25 | + "time" |
| 26 | + |
| 27 | + podv1util "k8s.io/kubernetes/pkg/api/v1/pod" |
| 28 | + imageutils "k8s.io/kubernetes/test/utils/image" |
| 29 | + |
| 30 | + "github.com/onsi/ginkgo/v2" |
| 31 | + "github.com/onsi/gomega" |
| 32 | + "github.com/pkg/errors" |
| 33 | + kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config" |
| 34 | + |
| 35 | + v1 "k8s.io/api/core/v1" |
| 36 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 37 | + "k8s.io/apimachinery/pkg/util/uuid" |
| 38 | + "k8s.io/kubernetes/test/e2e/feature" |
| 39 | + "k8s.io/kubernetes/test/e2e/framework" |
| 40 | + e2epod "k8s.io/kubernetes/test/e2e/framework/pod" |
| 41 | + admissionapi "k8s.io/pod-security-admission/api" |
| 42 | +) |
| 43 | + |
| 44 | +const containerName = "restarts" |
| 45 | + |
| 46 | +var _ = SIGDescribe("Container Restart", feature.CriProxy, framework.WithSerial(), func() { |
| 47 | + f := framework.NewDefaultFramework("container-restart") |
| 48 | + f.NamespacePodSecurityLevel = admissionapi.LevelPrivileged |
| 49 | + |
| 50 | + ginkgo.Context("Container restart backs off", func() { |
| 51 | + |
| 52 | + ginkgo.BeforeEach(func() { |
| 53 | + if err := resetCRIProxyInjector(e2eCriProxy); err != nil { |
| 54 | + ginkgo.Skip("Skip the test since the CRI Proxy is undefined.") |
| 55 | + } |
| 56 | + }) |
| 57 | + |
| 58 | + ginkgo.AfterEach(func() { |
| 59 | + err := resetCRIProxyInjector(e2eCriProxy) |
| 60 | + framework.ExpectNoError(err) |
| 61 | + }) |
| 62 | + |
| 63 | + ginkgo.It("Container restart backs off.", func(ctx context.Context) { |
| 64 | + // 0s, 0s, 10s, 30s, 70s, 150s, 310s |
| 65 | + doTest(ctx, f, 3, containerName, 7) |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + ginkgo.Context("Alternate container restart backs off as expected", func() { |
| 70 | + |
| 71 | + tempSetCurrentKubeletConfig(f, func(ctx context.Context, initialConfig *kubeletconfig.KubeletConfiguration) { |
| 72 | + initialConfig.CrashLoopBackOff.MaxContainerRestartPeriod = &metav1.Duration{Duration: time.Duration(30 * time.Second)} |
| 73 | + initialConfig.FeatureGates = map[string]bool{"KubeletCrashLoopBackOffMax": true} |
| 74 | + }) |
| 75 | + |
| 76 | + ginkgo.BeforeEach(func() { |
| 77 | + if err := resetCRIProxyInjector(e2eCriProxy); err != nil { |
| 78 | + ginkgo.Skip("Skip the test since the CRI Proxy is undefined.") |
| 79 | + } |
| 80 | + }) |
| 81 | + |
| 82 | + ginkgo.AfterEach(func() { |
| 83 | + err := resetCRIProxyInjector(e2eCriProxy) |
| 84 | + framework.ExpectNoError(err) |
| 85 | + }) |
| 86 | + |
| 87 | + ginkgo.It("Alternate restart backs off.", func(ctx context.Context) { |
| 88 | + // 0s, 0s, 10s, 30s, 60s, 90s, 120s, 150s, 180s, 210s, 240s, 270s, 300s |
| 89 | + doTest(ctx, f, 3, containerName, 13) |
| 90 | + }) |
| 91 | + }) |
| 92 | +}) |
| 93 | + |
| 94 | +func doTest(ctx context.Context, f *framework.Framework, targetRestarts int, containerName string, maxRestarts int) { |
| 95 | + |
| 96 | + pod := e2epod.NewPodClient(f).Create(ctx, newFailAlwaysPod()) |
| 97 | + podErr := e2epod.WaitForPodContainerToFail(ctx, f.ClientSet, f.Namespace.Name, pod.Name, 0, "CrashLoopBackOff", 1*time.Minute) |
| 98 | + gomega.Expect(podErr).To(gomega.HaveOccurred()) |
| 99 | + |
| 100 | + // Hard wait 30 seconds for targetRestarts in the best case; longer timeout later will handle if infra was slow. |
| 101 | + time.Sleep(30 * time.Second) |
| 102 | + podErr = waitForContainerRestartedNTimes(ctx, f, f.Namespace.Name, pod.Name, containerName, 5*time.Minute, targetRestarts) |
| 103 | + gomega.Expect(podErr).ShouldNot(gomega.HaveOccurred(), "Expected container to repeatedly back off container failures") |
| 104 | + |
| 105 | + r, err := extractObservedBackoff(ctx, f, pod.Name, containerName) |
| 106 | + framework.ExpectNoError(err) |
| 107 | + |
| 108 | + gomega.Expect(r).Should(gomega.BeNumerically("<=", maxRestarts)) |
| 109 | +} |
| 110 | + |
| 111 | +func extractObservedBackoff(ctx context.Context, f *framework.Framework, podName string, containerName string) (int32, error) { |
| 112 | + var r int32 |
| 113 | + pod, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(ctx, podName, metav1.GetOptions{}) |
| 114 | + if err != nil { |
| 115 | + return r, err |
| 116 | + } |
| 117 | + for _, statuses := range [][]v1.ContainerStatus{pod.Status.ContainerStatuses, pod.Status.InitContainerStatuses, pod.Status.EphemeralContainerStatuses} { |
| 118 | + for _, cs := range statuses { |
| 119 | + if cs.Name == containerName { |
| 120 | + return cs.RestartCount, nil |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + return r, errors.Errorf("Could not find container status for container %s in pod %s", containerName, podName) |
| 125 | +} |
| 126 | + |
| 127 | +func newFailAlwaysPod() *v1.Pod { |
| 128 | + podName := "container-restart" + string(uuid.NewUUID()) |
| 129 | + pod := &v1.Pod{ |
| 130 | + ObjectMeta: metav1.ObjectMeta{ |
| 131 | + Name: podName, |
| 132 | + }, |
| 133 | + Spec: v1.PodSpec{ |
| 134 | + Containers: []v1.Container{ |
| 135 | + { |
| 136 | + Name: containerName, |
| 137 | + Image: imageutils.GetE2EImage(imageutils.BusyBox), |
| 138 | + ImagePullPolicy: v1.PullIfNotPresent, |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + } |
| 143 | + return pod |
| 144 | +} |
| 145 | + |
| 146 | +func waitForContainerRestartedNTimes(ctx context.Context, f *framework.Framework, namespace string, podName string, containerName string, timeout time.Duration, target int) error { |
| 147 | + conditionDesc := fmt.Sprintf("A container in pod %s restarted at least %d times", podName, target) |
| 148 | + return e2epod.WaitForPodCondition(ctx, f.ClientSet, namespace, podName, conditionDesc, timeout, func(pod *v1.Pod) (bool, error) { |
| 149 | + cs, found := podv1util.GetContainerStatus(pod.Status.ContainerStatuses, containerName) |
| 150 | + if !found { |
| 151 | + return false, fmt.Errorf("could not find container %s in pod %s", containerName, podName) |
| 152 | + } |
| 153 | + return cs.RestartCount >= int32(target), nil |
| 154 | + }) |
| 155 | +} |
0 commit comments