k8s.io/kubernetes@v1.29.3/pkg/kubelet/kuberuntime/legacy.go (about) 1 /* 2 Copyright 2016 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 kuberuntime 18 19 import ( 20 "fmt" 21 "path/filepath" 22 "strings" 23 24 kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" 25 ) 26 27 // This file implements the functions that are needed for backward 28 // compatibility. Therefore, it imports various kubernetes packages 29 // directly. 30 31 const ( 32 // legacyContainerLogsDir is the legacy location of container logs. It is the same with 33 // kubelet.containerLogsDir. 34 legacyContainerLogsDir = "/var/log/containers" 35 // legacyLogSuffix is the legacy log suffix. 36 legacyLogSuffix = "log" 37 38 ext4MaxFileNameLen = 255 39 ) 40 41 // legacyLogSymlink composes the legacy container log path. It is only used for legacy cluster 42 // logging support. 43 func legacyLogSymlink(containerID string, containerName, podName, podNamespace string) string { 44 return logSymlink(legacyContainerLogsDir, kubecontainer.BuildPodFullName(podName, podNamespace), 45 containerName, containerID) 46 } 47 48 // getContainerIDFromLegacyLogSymlink returns error if container Id cannot be parsed 49 func getContainerIDFromLegacyLogSymlink(logSymlink string) (string, error) { 50 parts := strings.Split(logSymlink, "-") 51 if len(parts) == 0 { 52 return "", fmt.Errorf("unable to find separator in %q", logSymlink) 53 } 54 containerIDWithSuffix := parts[len(parts)-1] 55 suffix := fmt.Sprintf(".%s", legacyLogSuffix) 56 if !strings.HasSuffix(containerIDWithSuffix, suffix) { 57 return "", fmt.Errorf("%q doesn't end with %q", logSymlink, suffix) 58 } 59 containerIDWithoutSuffix := strings.TrimSuffix(containerIDWithSuffix, suffix) 60 // container can be retrieved with container Id as short as 6 characters 61 if len(containerIDWithoutSuffix) < 6 { 62 return "", fmt.Errorf("container Id %q is too short", containerIDWithoutSuffix) 63 } 64 return containerIDWithoutSuffix, nil 65 } 66 67 func logSymlink(containerLogsDir, podFullName, containerName, containerID string) string { 68 suffix := fmt.Sprintf(".%s", legacyLogSuffix) 69 logPath := fmt.Sprintf("%s_%s-%s", podFullName, containerName, containerID) 70 // Length of a filename cannot exceed 255 characters in ext4 on Linux. 71 if len(logPath) > ext4MaxFileNameLen-len(suffix) { 72 logPath = logPath[:ext4MaxFileNameLen-len(suffix)] 73 } 74 return filepath.Join(containerLogsDir, logPath+suffix) 75 }