...
1
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
28
29
30
31 const (
32
33
34 legacyContainerLogsDir = "/var/log/containers"
35
36 legacyLogSuffix = "log"
37
38 ext4MaxFileNameLen = 255
39 )
40
41
42
43 func legacyLogSymlink(containerID string, containerName, podName, podNamespace string) string {
44 return logSymlink(legacyContainerLogsDir, kubecontainer.BuildPodFullName(podName, podNamespace),
45 containerName, containerID)
46 }
47
48
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
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
71 if len(logPath) > ext4MaxFileNameLen-len(suffix) {
72 logPath = logPath[:ext4MaxFileNameLen-len(suffix)]
73 }
74 return filepath.Join(containerLogsDir, logPath+suffix)
75 }
76
View as plain text