container_log_rotation_test.go 3.6 KB

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