From c198062da48294eb9d20860ebc8feea46fd3dc4e Mon Sep 17 00:00:00 2001 From: Danielle Lancashire Date: Wed, 2 Feb 2022 18:59:25 +0100 Subject: [PATCH] cm: Remove legacy docker references Dockershim and built-in Docker support are gone. Cleans up dead code references to them. --- pkg/kubelet/cm/container_manager_linux.go | 122 ---------------------- pkg/kubelet/cm/helpers_linux.go | 9 +- 2 files changed, 1 insertion(+), 130 deletions(-) diff --git a/pkg/kubelet/cm/container_manager_linux.go b/pkg/kubelet/cm/container_manager_linux.go index dce1229e7ac..82de5edb0e8 100644 --- a/pkg/kubelet/cm/container_manager_linux.go +++ b/pkg/kubelet/cm/container_manager_linux.go @@ -25,7 +25,6 @@ import ( "io/ioutil" "os" "path" - "strconv" "strings" "sync" "time" @@ -36,7 +35,6 @@ import ( "github.com/opencontainers/runc/libcontainer/configs" "k8s.io/klog/v2" "k8s.io/mount-utils" - utilio "k8s.io/utils/io" utilpath "k8s.io/utils/path" libcontaineruserns "github.com/opencontainers/runc/libcontainer/userns" @@ -44,7 +42,6 @@ import ( "k8s.io/apimachinery/pkg/api/resource" utilerrors "k8s.io/apimachinery/pkg/util/errors" "k8s.io/apimachinery/pkg/util/sets" - utilversion "k8s.io/apimachinery/pkg/util/version" "k8s.io/apimachinery/pkg/util/wait" utilfeature "k8s.io/apiserver/pkg/util/feature" "k8s.io/client-go/tools/record" @@ -70,20 +67,6 @@ import ( "k8s.io/kubernetes/pkg/kubelet/status" schedulerframework "k8s.io/kubernetes/pkg/scheduler/framework" "k8s.io/kubernetes/pkg/util/oom" - "k8s.io/kubernetes/pkg/util/procfs" -) - -const ( - dockerProcessName = "dockerd" - // dockerd option --pidfile can specify path to use for daemon PID file, pid file path is default "/var/run/docker.pid" - dockerPidFile = "/var/run/docker.pid" - containerdProcessName = "containerd" - maxPidFileLength = 1 << 10 // 1KB -) - -var ( - // The docker version in which containerd was introduced. - containerdAPIVersion = utilversion.MustParseGeneric("1.23") ) // A non-user container tracked by the Kubelet. @@ -496,24 +479,6 @@ func (cm *containerManagerImpl) setupNode(activePods ActivePodsFunc) error { } systemContainers := []*systemContainer{} - if cm.ContainerRuntime == "docker" { - // With the docker-CRI integration, dockershim manages the cgroups - // and oom score for the docker processes. - // Check the cgroup for docker periodically, so kubelet can serve stats for the docker runtime. - // TODO(KEP#866): remove special processing for CRI "docker" enablement - cm.periodicTasks = append(cm.periodicTasks, func() { - klog.V(4).InfoS("Adding periodic tasks for docker CRI integration") - cont, err := getContainerNameForProcess(dockerProcessName, dockerPidFile) - if err != nil { - klog.ErrorS(err, "Failed to get container name for process") - return - } - klog.V(2).InfoS("Discovered runtime cgroup name", "cgroupName", cont) - cm.Lock() - defer cm.Unlock() - cm.RuntimeCgroupsName = cont - }) - } if cm.SystemCgroupsName != "" { if cm.SystemCgroupsName == "/" { @@ -561,21 +526,6 @@ func (cm *containerManagerImpl) setupNode(activePods ActivePodsFunc) error { return nil } -func getContainerNameForProcess(name, pidFile string) (string, error) { - pids, err := getPidsForProcess(name, pidFile) - if err != nil { - return "", fmt.Errorf("failed to detect process id for %q - %v", name, err) - } - if len(pids) == 0 { - return "", nil - } - cont, err := getContainer(pids[0]) - if err != nil { - return "", err - } - return cont, nil -} - func (cm *containerManagerImpl) GetNodeConfig() NodeConfig { cm.RLock() defer cm.RUnlock() @@ -820,78 +770,6 @@ func isProcessRunningInHost(pid int) (bool, error) { return initPidNs == processPidNs, nil } -func getPidFromPidFile(pidFile string) (int, error) { - file, err := os.Open(pidFile) - if err != nil { - return 0, fmt.Errorf("error opening pid file %s: %v", pidFile, err) - } - defer file.Close() - - data, err := utilio.ReadAtMost(file, maxPidFileLength) - if err != nil { - return 0, fmt.Errorf("error reading pid file %s: %v", pidFile, err) - } - - pid, err := strconv.Atoi(string(data)) - if err != nil { - return 0, fmt.Errorf("error parsing %s as a number: %v", string(data), err) - } - - return pid, nil -} - -func getPidsForProcess(name, pidFile string) ([]int, error) { - if len(pidFile) == 0 { - return procfs.PidOf(name) - } - - pid, err := getPidFromPidFile(pidFile) - if err == nil { - return []int{pid}, nil - } - - // Try to lookup pid by process name - pids, err2 := procfs.PidOf(name) - if err2 == nil { - return pids, nil - } - - // Return error from getPidFromPidFile since that should have worked - // and is the real source of the problem. - klog.V(4).InfoS("Unable to get pid from file", "path", pidFile, "err", err) - return []int{}, err -} - -// Ensures that the Docker daemon is in the desired container. -// Temporarily export the function to be used by dockershim. -// TODO(yujuhong): Move this function to dockershim once kubelet migrates to -// dockershim as the default. -func EnsureDockerInContainer(dockerAPIVersion *utilversion.Version, oomScoreAdj int, manager cgroups.Manager) error { - type process struct{ name, file string } - dockerProcs := []process{{dockerProcessName, dockerPidFile}} - if dockerAPIVersion.AtLeast(containerdAPIVersion) { - // By default containerd is started separately, so there is no pid file. - containerdPidFile := "" - dockerProcs = append(dockerProcs, process{containerdProcessName, containerdPidFile}) - } - var errs []error - for _, proc := range dockerProcs { - pids, err := getPidsForProcess(proc.name, proc.file) - if err != nil { - errs = append(errs, fmt.Errorf("failed to get pids for %q: %v", proc.name, err)) - continue - } - - // Move if the pid is not already in the desired container. - for _, pid := range pids { - if err := ensureProcessInContainerWithOOMScore(pid, oomScoreAdj, manager); err != nil { - errs = append(errs, fmt.Errorf("errors moving %q pid: %v", proc.name, err)) - } - } - } - return utilerrors.NewAggregate(errs) -} - func ensureProcessInContainerWithOOMScore(pid int, oomScoreAdj int, manager cgroups.Manager) error { if runningInHost, err := isProcessRunningInHost(pid); err != nil { // Err on the side of caution. Avoid moving the docker daemon unless we are able to identify its context. diff --git a/pkg/kubelet/cm/helpers_linux.go b/pkg/kubelet/cm/helpers_linux.go index 83c80501f5d..160f35490ba 100644 --- a/pkg/kubelet/cm/helpers_linux.go +++ b/pkg/kubelet/cm/helpers_linux.go @@ -25,7 +25,7 @@ import ( libcontainercgroups "github.com/opencontainers/runc/libcontainer/cgroups" - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" utilfeature "k8s.io/apiserver/pkg/util/feature" "k8s.io/kubernetes/pkg/api/v1/resource" @@ -340,12 +340,5 @@ func GetKubeletContainer(kubeletCgroups string) (string, error) { // GetRuntimeContainer returns the cgroup used by the container runtime func GetRuntimeContainer(containerRuntime, runtimeCgroups string) (string, error) { - if containerRuntime == "docker" { - cont, err := getContainerNameForProcess(dockerProcessName, dockerPidFile) - if err != nil { - return "", fmt.Errorf("failed to get container name for docker process: %v", err) - } - return cont, nil - } return runtimeCgroups, nil }