Move legacy log symlink to kuberuntime

Also remove the dockertools.DockerType constant.
This commit is contained in:
Yu-Ju Hong 2017-05-03 13:46:14 -07:00
parent 389c140eaf
commit 8cc4b3a81e
5 changed files with 70 additions and 48 deletions

View File

@ -19,7 +19,6 @@ package dockertools
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"path"
"strings" "strings"
"github.com/docker/docker/pkg/jsonmessage" "github.com/docker/docker/pkg/jsonmessage"
@ -32,13 +31,6 @@ import (
"k8s.io/kubernetes/pkg/kubelet/images" "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 an abstract interface for testability. It abstracts image pull operations.
// DockerPuller is *not* in use anywhere in the codebase. // DockerPuller is *not* in use anywhere in the codebase.
// TODO: Examine whether we can migrate the unit tests and remove the code. // 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 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)
}

View File

@ -18,14 +18,10 @@ package dockertools
import ( import (
"encoding/json" "encoding/json"
"fmt"
"math/rand"
"path"
"strings" "strings"
"testing" "testing"
"github.com/docker/docker/pkg/jsonmessage" "github.com/docker/docker/pkg/jsonmessage"
"github.com/stretchr/testify/assert"
"k8s.io/kubernetes/pkg/api/v1" "k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/credentialprovider" "k8s.io/kubernetes/pkg/credentialprovider"
"k8s.io/kubernetes/pkg/kubelet/dockershim/libdocker" "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))
}

View File

@ -17,8 +17,10 @@ limitations under the License.
package kuberuntime package kuberuntime
import ( import (
"fmt"
"path"
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
"k8s.io/kubernetes/pkg/kubelet/dockertools"
) )
// This file implements the functions that are needed for backward // This file implements the functions that are needed for backward
@ -30,12 +32,24 @@ const (
// kubelet.containerLogsDir. // kubelet.containerLogsDir.
legacyContainerLogsDir = "/var/log/containers" legacyContainerLogsDir = "/var/log/containers"
// legacyLogSuffix is the legacy log suffix. // 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 // legacyLogSymlink composes the legacy container log path. It is only used for legacy cluster
// logging support. // logging support.
func legacyLogSymlink(containerID string, containerName, podName, podNamespace string) string { 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) 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)
}

View File

@ -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))
}

View File

@ -21,7 +21,6 @@ import (
v1helper "k8s.io/kubernetes/pkg/api/v1/helper" v1helper "k8s.io/kubernetes/pkg/api/v1/helper"
"k8s.io/kubernetes/pkg/kubelet/container" "k8s.io/kubernetes/pkg/kubelet/container"
"k8s.io/kubernetes/pkg/kubelet/dockertools"
"k8s.io/kubernetes/pkg/kubelet/lifecycle" "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 // (e.g., 1.24). Append the version with a ".0" so that it works
// with both the CRI and dockertools comparison logic. // with both the CRI and dockertools comparison logic.
dockerMinimumAPIVersion = "1.24.0" 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 { type runtimeAdmitHandler struct {
result lifecycle.PodAdmitResult result lifecycle.PodAdmitResult
} }
@ -42,7 +46,7 @@ var _ lifecycle.PodAdmitHandler = &runtimeAdmitHandler{}
// NewRuntimeAdmitHandler returns a sysctlRuntimeAdmitHandler which checks whether // NewRuntimeAdmitHandler returns a sysctlRuntimeAdmitHandler which checks whether
// the given runtime support sysctls. // the given runtime support sysctls.
func NewRuntimeAdmitHandler(runtime container.Runtime) (*runtimeAdmitHandler, error) { func NewRuntimeAdmitHandler(runtime container.Runtime) (*runtimeAdmitHandler, error) {
if runtime.Type() == dockertools.DockerType { if runtime.Type() == dockerTypeName {
v, err := runtime.APIVersion() v, err := runtime.APIVersion()
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to get runtime version: %v", err) return nil, fmt.Errorf("failed to get runtime version: %v", err)