From 8cc4b3a81e24451321302307742ab3a8dd14b6e0 Mon Sep 17 00:00:00 2001 From: Yu-Ju Hong Date: Wed, 3 May 2017 13:46:14 -0700 Subject: [PATCH] Move legacy log symlink to kuberuntime Also remove the dockertools.DockerType constant. --- pkg/kubelet/dockertools/docker.go | 18 ---------- pkg/kubelet/dockertools/docker_test.go | 25 -------------- pkg/kubelet/kuberuntime/legacy.go | 20 +++++++++-- pkg/kubelet/kuberuntime/legacy_test.go | 47 ++++++++++++++++++++++++++ pkg/kubelet/sysctl/runtime.go | 8 +++-- 5 files changed, 70 insertions(+), 48 deletions(-) create mode 100644 pkg/kubelet/kuberuntime/legacy_test.go diff --git a/pkg/kubelet/dockertools/docker.go b/pkg/kubelet/dockertools/docker.go index 01ba78ae26a..384403cab8e 100644 --- a/pkg/kubelet/dockertools/docker.go +++ b/pkg/kubelet/dockertools/docker.go @@ -19,7 +19,6 @@ package dockertools import ( "fmt" "net/http" - "path" "strings" "github.com/docker/docker/pkg/jsonmessage" @@ -32,13 +31,6 @@ import ( "k8s.io/kubernetes/pkg/kubelet/images" ) -const ( - LogSuffix = "log" - ext4MaxFileNameLen = 255 - - DockerType = "docker" -) - // DockerPuller is an abstract interface for testability. It abstracts image pull operations. // DockerPuller is *not* in use anywhere in the codebase. // TODO: Examine whether we can migrate the unit tests and remove the code. @@ -147,13 +139,3 @@ func (p dockerPuller) GetImageRef(image string) (string, error) { } return "", err } - -func LogSymlink(containerLogsDir, podFullName, containerName, dockerId string) string { - 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) -} diff --git a/pkg/kubelet/dockertools/docker_test.go b/pkg/kubelet/dockertools/docker_test.go index ed5a9e24dc3..070388034c3 100644 --- a/pkg/kubelet/dockertools/docker_test.go +++ b/pkg/kubelet/dockertools/docker_test.go @@ -18,14 +18,10 @@ package dockertools import ( "encoding/json" - "fmt" - "math/rand" - "path" "strings" "testing" "github.com/docker/docker/pkg/jsonmessage" - "github.com/stretchr/testify/assert" "k8s.io/kubernetes/pkg/api/v1" "k8s.io/kubernetes/pkg/credentialprovider" "k8s.io/kubernetes/pkg/kubelet/dockershim/libdocker" @@ -181,24 +177,3 @@ func TestPullWithSecrets(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)) -} diff --git a/pkg/kubelet/kuberuntime/legacy.go b/pkg/kubelet/kuberuntime/legacy.go index 3805165634b..2a00ad012f1 100644 --- a/pkg/kubelet/kuberuntime/legacy.go +++ b/pkg/kubelet/kuberuntime/legacy.go @@ -17,8 +17,10 @@ limitations under the License. package kuberuntime import ( + "fmt" + "path" + kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" - "k8s.io/kubernetes/pkg/kubelet/dockertools" ) // This file implements the functions that are needed for backward @@ -30,12 +32,24 @@ const ( // kubelet.containerLogsDir. legacyContainerLogsDir = "/var/log/containers" // legacyLogSuffix is the legacy log suffix. - legacyLogSuffix = dockertools.LogSuffix + legacyLogSuffix = "log" + + ext4MaxFileNameLen = 255 ) // legacyLogSymlink composes the legacy container log path. It is only used for legacy cluster // logging support. func legacyLogSymlink(containerID string, containerName, podName, podNamespace string) string { - return dockertools.LogSymlink(legacyContainerLogsDir, kubecontainer.BuildPodFullName(podName, podNamespace), + return logSymlink(legacyContainerLogsDir, kubecontainer.BuildPodFullName(podName, podNamespace), containerName, containerID) } + +func logSymlink(containerLogsDir, podFullName, containerName, dockerId string) string { + suffix := fmt.Sprintf(".%s", legacyLogSuffix) + 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) +} diff --git a/pkg/kubelet/kuberuntime/legacy_test.go b/pkg/kubelet/kuberuntime/legacy_test.go new file mode 100644 index 00000000000..a3508bdc0c8 --- /dev/null +++ b/pkg/kubelet/kuberuntime/legacy_test.go @@ -0,0 +1,47 @@ +/* +Copyright 2017 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package kuberuntime + +import ( + "fmt" + "math/rand" + "path" + "testing" + + "github.com/stretchr/testify/assert" +) + +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)) +} diff --git a/pkg/kubelet/sysctl/runtime.go b/pkg/kubelet/sysctl/runtime.go index e01b92e5fbd..6c11600ae8f 100644 --- a/pkg/kubelet/sysctl/runtime.go +++ b/pkg/kubelet/sysctl/runtime.go @@ -21,7 +21,6 @@ import ( v1helper "k8s.io/kubernetes/pkg/api/v1/helper" "k8s.io/kubernetes/pkg/kubelet/container" - "k8s.io/kubernetes/pkg/kubelet/dockertools" "k8s.io/kubernetes/pkg/kubelet/lifecycle" ) @@ -31,8 +30,13 @@ const ( // (e.g., 1.24). Append the version with a ".0" so that it works // with both the CRI and dockertools comparison logic. dockerMinimumAPIVersion = "1.24.0" + + dockerTypeName = "docker" ) +// TODO: The admission logic in this file is runtime-dependent. It should be +// changed to be generic and CRI-compatible. + type runtimeAdmitHandler struct { result lifecycle.PodAdmitResult } @@ -42,7 +46,7 @@ var _ lifecycle.PodAdmitHandler = &runtimeAdmitHandler{} // NewRuntimeAdmitHandler returns a sysctlRuntimeAdmitHandler which checks whether // the given runtime support sysctls. func NewRuntimeAdmitHandler(runtime container.Runtime) (*runtimeAdmitHandler, error) { - if runtime.Type() == dockertools.DockerType { + if runtime.Type() == dockerTypeName { v, err := runtime.APIVersion() if err != nil { return nil, fmt.Errorf("failed to get runtime version: %v", err)