blob: 41ed364efda33a58e225acb6e34be447e735276d [file] [log] [blame]
Tim Windelschmidtddbb9682025-10-06 21:58:26 +02001From 5fa4cde761ff1b7cbd0e47966992d07927e06882 Mon Sep 17 00:00:00 2001
Lorenz Brund13c1c62022-03-30 19:58:58 +02002From: Lorenz Brun <lorenz@monogon.tech>
3Date: Wed, 16 Mar 2022 18:10:09 +0100
Lorenz Brun6211e4d2023-11-14 19:09:40 +01004Subject: [PATCH] Drop legacy log path
Lorenz Brun7b822272021-02-03 17:03:41 +01005
6---
Lorenz Brunf2b7ab62022-05-04 19:06:00 +02007 pkg/kubelet/kubelet.go | 7 ---
8 .../kuberuntime/kuberuntime_container.go | 36 ---------------
9 pkg/kubelet/kuberuntime/kuberuntime_gc.go | 46 -------------------
Lorenz Brunf2b7ab62022-05-04 19:06:00 +020010 test/e2e_node/log_path_test.go | 18 --------
Lorenz Brun837cb8e2024-12-23 13:52:56 +010011 4 files changed, 107 deletions(-)
Lorenz Brun7b822272021-02-03 17:03:41 +010012
13diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go
Tim Windelschmidtddbb9682025-10-06 21:58:26 +020014index 600a006f054..93d1e2735bd 100644
Lorenz Brun7b822272021-02-03 17:03:41 +010015--- a/pkg/kubelet/kubelet.go
16+++ b/pkg/kubelet/kubelet.go
Tim Windelschmidtddbb9682025-10-06 21:58:26 +020017@@ -1643,13 +1643,6 @@ func (kl *Kubelet) initializeModules(ctx context.Context) error {
Lorenz Brun7b822272021-02-03 17:03:41 +010018 return err
19 }
Tim Windelschmidtddbb9682025-10-06 21:58:26 +020020
Lorenz Brun7b822272021-02-03 17:03:41 +010021- // If the container logs directory does not exist, create it.
22- if _, err := os.Stat(ContainerLogsDir); err != nil {
23- if err := kl.os.MkdirAll(ContainerLogsDir, 0755); err != nil {
24- return fmt.Errorf("failed to create directory %q: %v", ContainerLogsDir, err)
25- }
26- }
27-
Lorenz Brun837cb8e2024-12-23 13:52:56 +010028 if goos == "windows" {
Lorenz Brun732a8842024-08-26 23:25:37 +020029 // On Windows we should not allow other users to read the logs directory
30 // to avoid allowing non-root containers from reading the logs of other containers.
Lorenz Brun7b822272021-02-03 17:03:41 +010031diff --git a/pkg/kubelet/kuberuntime/kuberuntime_container.go b/pkg/kubelet/kuberuntime/kuberuntime_container.go
Tim Windelschmidtddbb9682025-10-06 21:58:26 +020032index a1d9453f891..b07d3698b9f 100644
Lorenz Brun7b822272021-02-03 17:03:41 +010033--- a/pkg/kubelet/kuberuntime/kuberuntime_container.go
34+++ b/pkg/kubelet/kuberuntime/kuberuntime_container.go
Tim Windelschmidtddbb9682025-10-06 21:58:26 +020035@@ -296,25 +296,6 @@ func (m *kubeGenericRuntimeManager) startContainer(ctx context.Context, podSandb
Lorenz Brun7b822272021-02-03 17:03:41 +010036 }
Tim Windelschmidtddbb9682025-10-06 21:58:26 +020037 m.recordContainerEvent(ctx, pod, container, containerID, v1.EventTypeNormal, events.StartedContainer, "Started container %v", container.Name)
38
Lorenz Brun7b822272021-02-03 17:03:41 +010039- // Symlink container logs to the legacy container log location for cluster logging
40- // support.
41- // TODO(random-liu): Remove this after cluster logging supports CRI container log path.
42- containerMeta := containerConfig.GetMetadata()
43- sandboxMeta := podSandboxConfig.GetMetadata()
44- legacySymlink := legacyLogSymlink(containerID, containerMeta.Name, sandboxMeta.Name,
45- sandboxMeta.Namespace)
46- containerLog := filepath.Join(podSandboxConfig.LogDirectory, containerConfig.LogPath)
47- // only create legacy symlink if containerLog path exists (or the error is not IsNotExist).
48- // Because if containerLog path does not exist, only dangling legacySymlink is created.
49- // This dangling legacySymlink is later removed by container gc, so it does not make sense
50- // to create it in the first place. it happens when journald logging driver is used with docker.
51- if _, err := m.osInterface.Stat(containerLog); !os.IsNotExist(err) {
52- if err := m.osInterface.Symlink(containerLog, legacySymlink); err != nil {
Tim Windelschmidtddbb9682025-10-06 21:58:26 +020053- logger.Error(err, "Failed to create legacy symbolic link", "path", legacySymlink,
Lorenz Brund13c1c62022-03-30 19:58:58 +020054- "containerID", containerID, "containerLogPath", containerLog)
Lorenz Brun7b822272021-02-03 17:03:41 +010055- }
56- }
57-
58 // Step 4: execute the post start hook.
59 if container.Lifecycle != nil && container.Lifecycle.PostStart != nil {
60 kubeContainerID := kubecontainer.ContainerID{
Tim Windelschmidtddbb9682025-10-06 21:58:26 +020061@@ -1360,23 +1341,6 @@ func (m *kubeGenericRuntimeManager) removeContainerLog(ctx context.Context, cont
Lorenz Brun7b822272021-02-03 17:03:41 +010062 return err
63 }
Tim Windelschmidtddbb9682025-10-06 21:58:26 +020064
Lorenz Brun6211e4d2023-11-14 19:09:40 +010065- resp, err := m.runtimeService.ContainerStatus(ctx, containerID, false)
Lorenz Brun7b822272021-02-03 17:03:41 +010066- if err != nil {
67- return fmt.Errorf("failed to get container status %q: %v", containerID, err)
68- }
Lorenz Brunf2b7ab62022-05-04 19:06:00 +020069- status := resp.GetStatus()
70- if status == nil {
71- return remote.ErrContainerStatusNil
72- }
Lorenz Brun7b822272021-02-03 17:03:41 +010073- // Remove the legacy container log symlink.
74- // TODO(random-liu): Remove this after cluster logging supports CRI container log path.
Tim Windelschmidtddbb9682025-10-06 21:58:26 +020075- labeledInfo := getContainerInfoFromLabels(ctx, status.Labels)
Lorenz Brun7b822272021-02-03 17:03:41 +010076- legacySymlink := legacyLogSymlink(containerID, labeledInfo.ContainerName, labeledInfo.PodName,
77- labeledInfo.PodNamespace)
78- if err := m.osInterface.Remove(legacySymlink); err != nil && !os.IsNotExist(err) {
79- return fmt.Errorf("failed to remove container %q log legacy symbolic link %q: %v",
80- containerID, legacySymlink, err)
81- }
82 return nil
83 }
Tim Windelschmidtddbb9682025-10-06 21:58:26 +020084
Lorenz Brun7b822272021-02-03 17:03:41 +010085diff --git a/pkg/kubelet/kuberuntime/kuberuntime_gc.go b/pkg/kubelet/kuberuntime/kuberuntime_gc.go
Tim Windelschmidtddbb9682025-10-06 21:58:26 +020086index 2691f64d5c3..d766c618db3 100644
Lorenz Brun7b822272021-02-03 17:03:41 +010087--- a/pkg/kubelet/kuberuntime/kuberuntime_gc.go
88+++ b/pkg/kubelet/kuberuntime/kuberuntime_gc.go
Lorenz Brun6211e4d2023-11-14 19:09:40 +010089@@ -19,7 +19,6 @@ package kuberuntime
Lorenz Brun7b822272021-02-03 17:03:41 +010090 import (
Lorenz Brun6211e4d2023-11-14 19:09:40 +010091 "context"
Lorenz Brun7b822272021-02-03 17:03:41 +010092 "fmt"
93- "os"
94 "path/filepath"
95 "sort"
96 "time"
Tim Windelschmidtddbb9682025-10-06 21:58:26 +020097@@ -350,51 +349,6 @@ func (cgc *containerGC) evictPodLogsDirectories(ctx context.Context, allSourcesR
Lorenz Brun7b822272021-02-03 17:03:41 +010098 }
99 }
100 }
101-
102- // Remove dead container log symlinks.
103- // TODO(random-liu): Remove this after cluster logging supports CRI container log path.
104- logSymlinks, _ := osInterface.Glob(filepath.Join(legacyContainerLogsDir, fmt.Sprintf("*.%s", legacyLogSuffix)))
105- for _, logSymlink := range logSymlinks {
106- if _, err := osInterface.Stat(logSymlink); os.IsNotExist(err) {
107- if containerID, err := getContainerIDFromLegacyLogSymlink(logSymlink); err == nil {
Lorenz Brun6211e4d2023-11-14 19:09:40 +0100108- resp, err := cgc.manager.runtimeService.ContainerStatus(ctx, containerID, false)
Lorenz Brun7b822272021-02-03 17:03:41 +0100109- if err != nil {
110- // TODO: we should handle container not found (i.e. container was deleted) case differently
111- // once https://github.com/kubernetes/kubernetes/issues/63336 is resolved
Tim Windelschmidtddbb9682025-10-06 21:58:26 +0200112- logger.Info("Error getting ContainerStatus for containerID", "containerID", containerID, "err", err)
Lorenz Brunf2b7ab62022-05-04 19:06:00 +0200113- } else {
114- status := resp.GetStatus()
115- if status == nil {
Tim Windelschmidtddbb9682025-10-06 21:58:26 +0200116- logger.V(4).Info("Container status is nil")
Lorenz Brunf2b7ab62022-05-04 19:06:00 +0200117- continue
118- }
119- if status.State != runtimeapi.ContainerState_CONTAINER_EXITED {
120- // Here is how container log rotation works (see containerLogManager#rotateLatestLog):
121- //
122- // 1. rename current log to rotated log file whose filename contains current timestamp (fmt.Sprintf("%s.%s", log, timestamp))
123- // 2. reopen the container log
124- // 3. if #2 fails, rename rotated log file back to container log
125- //
126- // There is small but indeterministic amount of time during which log file doesn't exist (between steps #1 and #2, between #1 and #3).
127- // Hence the symlink may be deemed unhealthy during that period.
128- // See https://github.com/kubernetes/kubernetes/issues/52172
129- //
130- // We only remove unhealthy symlink for dead containers
Tim Windelschmidtddbb9682025-10-06 21:58:26 +0200131- logger.V(5).Info("Container is still running, not removing symlink", "containerID", containerID, "path", logSymlink)
Lorenz Brunf2b7ab62022-05-04 19:06:00 +0200132- continue
133- }
Lorenz Brun7b822272021-02-03 17:03:41 +0100134- }
135- } else {
Tim Windelschmidtddbb9682025-10-06 21:58:26 +0200136- logger.V(4).Info("Unable to obtain container ID", "err", err)
Lorenz Brun7b822272021-02-03 17:03:41 +0100137- }
138- err := osInterface.Remove(logSymlink)
139- if err != nil {
Tim Windelschmidtddbb9682025-10-06 21:58:26 +0200140- logger.Error(err, "Failed to remove container log dead symlink", "path", logSymlink)
Lorenz Brun7b822272021-02-03 17:03:41 +0100141- } else {
Tim Windelschmidtddbb9682025-10-06 21:58:26 +0200142- logger.V(4).Info("Removed symlink", "path", logSymlink)
Lorenz Brun7b822272021-02-03 17:03:41 +0100143- }
144- }
145- }
146 return nil
147 }
Tim Windelschmidtddbb9682025-10-06 21:58:26 +0200148
Lorenz Brun7b822272021-02-03 17:03:41 +0100149diff --git a/test/e2e_node/log_path_test.go b/test/e2e_node/log_path_test.go
Lorenz Brun732a8842024-08-26 23:25:37 +0200150index a1589e6d91e..179206eb1dd 100644
Lorenz Brun7b822272021-02-03 17:03:41 +0100151--- a/test/e2e_node/log_path_test.go
152+++ b/test/e2e_node/log_path_test.go
Lorenz Brund13c1c62022-03-30 19:58:58 +0200153@@ -22,8 +22,6 @@ import (
Lorenz Brun7b822272021-02-03 17:03:41 +0100154 v1 "k8s.io/api/core/v1"
155 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
156 "k8s.io/apimachinery/pkg/util/uuid"
157- "k8s.io/kubernetes/pkg/kubelet"
158- kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
159 "k8s.io/kubernetes/test/e2e/framework"
160 e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
Lorenz Brunf2b7ab62022-05-04 19:06:00 +0200161 admissionapi "k8s.io/pod-security-admission/api"
Lorenz Brun732a8842024-08-26 23:25:37 +0200162@@ -122,22 +120,6 @@ var _ = SIGDescribe("ContainerLogPath", framework.WithNodeConformance(), func()
Lorenz Brun6211e4d2023-11-14 19:09:40 +0100163 err := createAndWaitPod(ctx, makeLogPod(logPodName, logString))
Lorenz Brun7b822272021-02-03 17:03:41 +0100164 framework.ExpectNoError(err, "Failed waiting for pod: %s to enter success state", logPodName)
165 })
Lorenz Brun6211e4d2023-11-14 19:09:40 +0100166- ginkgo.It("should print log to correct log path", func(ctx context.Context) {
Lorenz Brun7b822272021-02-03 17:03:41 +0100167-
168- logDir := kubelet.ContainerLogsDir
169-
170- // get containerID from created Pod
Lorenz Brun6211e4d2023-11-14 19:09:40 +0100171- createdLogPod, err := podClient.Get(ctx, logPodName, metav1.GetOptions{})
Lorenz Brun7b822272021-02-03 17:03:41 +0100172- logContainerID := kubecontainer.ParseContainerID(createdLogPod.Status.ContainerStatuses[0].ContainerID)
173- framework.ExpectNoError(err, "Failed to get pod: %s", logPodName)
174-
175- // build log file path
176- expectedlogFile := logDir + "/" + logPodName + "_" + f.Namespace.Name + "_" + logContainerName + "-" + logContainerID.ID + ".log"
177-
178- logCheckPodName := "log-check-" + string(uuid.NewUUID())
Lorenz Brun6211e4d2023-11-14 19:09:40 +0100179- err = createAndWaitPod(ctx, makeLogCheckPod(logCheckPodName, logString, expectedlogFile))
Lorenz Brun7b822272021-02-03 17:03:41 +0100180- framework.ExpectNoError(err, "Failed waiting for pod: %s to enter success state", logCheckPodName)
181- })
Tim Windelschmidtddbb9682025-10-06 21:58:26 +0200182
Lorenz Brun6211e4d2023-11-14 19:09:40 +0100183 ginkgo.It("should print log to correct cri log path", func(ctx context.Context) {
Tim Windelschmidtddbb9682025-10-06 21:58:26 +0200184
185--
1862.50.1
Lorenz Brun7b822272021-02-03 17:03:41 +0100187