k8s.io/kubernetes@v1.29.3/test/e2e_node/container_log_rotation_test.go (about) 1 /* 2 Copyright 2018 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package e2enode 18 19 import ( 20 "context" 21 "time" 22 23 v1 "k8s.io/api/core/v1" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config" 26 kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" 27 kubelogs "k8s.io/kubernetes/pkg/kubelet/logs" 28 "k8s.io/kubernetes/test/e2e/framework" 29 e2epod "k8s.io/kubernetes/test/e2e/framework/pod" 30 admissionapi "k8s.io/pod-security-admission/api" 31 32 "github.com/onsi/ginkgo/v2" 33 "github.com/onsi/gomega" 34 ) 35 36 const ( 37 testContainerLogMaxFiles = 3 38 testContainerLogMaxSize = "40Ki" 39 rotationPollInterval = 5 * time.Second 40 rotationEventuallyTimeout = 3 * time.Minute 41 rotationConsistentlyTimeout = 2 * time.Minute 42 ) 43 44 var _ = SIGDescribe("ContainerLogRotation", framework.WithSlow(), framework.WithSerial(), framework.WithDisruptive(), func() { 45 f := framework.NewDefaultFramework("container-log-rotation-test") 46 f.NamespacePodSecurityLevel = admissionapi.LevelPrivileged 47 ginkgo.Context("when a container generates a lot of log", func() { 48 tempSetCurrentKubeletConfig(f, func(ctx context.Context, initialConfig *kubeletconfig.KubeletConfiguration) { 49 initialConfig.ContainerLogMaxFiles = testContainerLogMaxFiles 50 initialConfig.ContainerLogMaxSize = testContainerLogMaxSize 51 }) 52 53 var logRotationPod *v1.Pod 54 ginkgo.BeforeEach(func(ctx context.Context) { 55 ginkgo.By("create log container") 56 pod := &v1.Pod{ 57 ObjectMeta: metav1.ObjectMeta{ 58 Name: "test-container-log-rotation", 59 }, 60 Spec: v1.PodSpec{ 61 RestartPolicy: v1.RestartPolicyNever, 62 Containers: []v1.Container{ 63 { 64 Name: "log-container", 65 Image: busyboxImage, 66 Command: []string{ 67 "sh", 68 "-c", 69 // ~12Kb/s. Exceeding 40Kb in 4 seconds. Log rotation period is 10 seconds. 70 "while true; do echo hello world; sleep 0.001; done;", 71 }, 72 }, 73 }, 74 }, 75 } 76 logRotationPod = e2epod.NewPodClient(f).CreateSync(ctx, pod) 77 ginkgo.DeferCleanup(e2epod.NewPodClient(f).DeleteSync, logRotationPod.Name, metav1.DeleteOptions{}, time.Minute) 78 }) 79 80 ginkgo.It("should be rotated and limited to a fixed amount of files", func(ctx context.Context) { 81 82 ginkgo.By("get container log path") 83 gomega.Expect(logRotationPod.Status.ContainerStatuses).To(gomega.HaveLen(1), "log rotation pod should have one container") 84 id := kubecontainer.ParseContainerID(logRotationPod.Status.ContainerStatuses[0].ContainerID).ID 85 r, _, err := getCRIClient() 86 framework.ExpectNoError(err, "should connect to CRI and obtain runtime service clients and image service client") 87 resp, err := r.ContainerStatus(context.Background(), id, false) 88 framework.ExpectNoError(err) 89 logPath := resp.GetStatus().GetLogPath() 90 ginkgo.By("wait for container log being rotated to max file limit") 91 gomega.Eventually(ctx, func() (int, error) { 92 logs, err := kubelogs.GetAllLogs(logPath) 93 if err != nil { 94 return 0, err 95 } 96 return len(logs), nil 97 }, rotationEventuallyTimeout, rotationPollInterval).Should(gomega.Equal(testContainerLogMaxFiles), "should eventually rotate to max file limit") 98 ginkgo.By("make sure container log number won't exceed max file limit") 99 gomega.Consistently(ctx, func() (int, error) { 100 logs, err := kubelogs.GetAllLogs(logPath) 101 if err != nil { 102 return 0, err 103 } 104 return len(logs), nil 105 }, rotationConsistentlyTimeout, rotationPollInterval).Should(gomega.BeNumerically("<=", testContainerLogMaxFiles), "should never exceed max file limit") 106 }) 107 }) 108 })