mirror of https://github.com/k3s-io/k3s
restrict log sym link filename to 255 characters
Signed-off-by: Vishnu Kannan <vishnuk@google.com>pull/6/head
parent
4fdde68f78
commit
acc74fbaa9
|
@ -44,6 +44,7 @@ const (
|
|||
PodInfraContainerName = leaky.PodInfraContainerName
|
||||
DockerPrefix = "docker://"
|
||||
LogSuffix = "log"
|
||||
ext4MaxFileNameLen = 255
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -299,7 +300,13 @@ func ParseDockerName(name string) (dockerName *KubeletContainerName, hash uint64
|
|||
}
|
||||
|
||||
func LogSymlink(containerLogsDir, podFullName, containerName, dockerId string) string {
|
||||
return path.Join(containerLogsDir, fmt.Sprintf("%s_%s-%s.%s", podFullName, containerName, dockerId, LogSuffix))
|
||||
suffix := fmt.Sprintf(".%s", LogSuffix)
|
||||
logPath := fmt.Sprintf("%s_%s-%s", podFullName, containerName, dockerId)
|
||||
// Length of a filename cannot exceed 255 characters in ext4 on Linux.
|
||||
if len(logPath) > ext4MaxFileNameLen-len(suffix) {
|
||||
logPath = logPath[:ext4MaxFileNameLen-len(suffix)]
|
||||
}
|
||||
return path.Join(containerLogsDir, logPath+suffix)
|
||||
}
|
||||
|
||||
// Get a *dockerapi.Client, either using the endpoint passed in, or using
|
||||
|
|
|
@ -20,6 +20,8 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"hash/adler32"
|
||||
"math/rand"
|
||||
"path"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
@ -800,3 +802,24 @@ func TestMilliCPUToQuota(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
|
||||
func randStringBytes(n int) string {
|
||||
b := make([]byte, n)
|
||||
for i := range b {
|
||||
b[i] = letterBytes[rand.Intn(len(letterBytes))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func TestLogSymLink(t *testing.T) {
|
||||
as := assert.New(t)
|
||||
containerLogsDir := "/foo/bar"
|
||||
podFullName := randStringBytes(128)
|
||||
containerName := randStringBytes(70)
|
||||
dockerId := randStringBytes(80)
|
||||
// The file name cannot exceed 255 characters. Since .log suffix is required, the prefix cannot exceed 251 characters.
|
||||
expectedPath := path.Join(containerLogsDir, fmt.Sprintf("%s_%s-%s", podFullName, containerName, dockerId)[:251]+".log")
|
||||
as.Equal(expectedPath, LogSymlink(containerLogsDir, podFullName, containerName, dockerId))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue